summaryrefslogtreecommitdiff
path: root/cesar/maximus/channel/src/ChannelComputerTest.cpp
blob: fead65238235f49e22dbf7d181a1eb430e1d04ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include "ChannelComputerTest.h"

#include "ChannelComputer.h"
#include "ChannelPoint.h"
#include "CoreEngine.h"
#include "PhySciMsgMpdu.h"
#include "PhySciMsgNoise.h"
#include "INetworkClock.h"

#include "Logger.h"
#include "Error.h"

#include <math.h> // for 'pow()' and 'sqrt()'
#include <cstring>
using namespace std;

CPPUNIT_TEST_SUITE_REGISTRATION (ChannelComputerTest);

class ProtectedTest : public ChannelComputer
{
public:
  ProtectedTest(IPhy * p_phy, ISystem * p_system) : ChannelComputer(p_phy, p_system) {}
  ~ProtectedTest() {}
  double getSigmaTest ( const Channel_Mod modulation, const float snr_in_db ) const
  {
    return getSigma(modulation, snr_in_db);
  }
  float getPowerScaleTest ( const Channel_Mod modulation ) const
  {
    return getPowerScale(modulation);
  }
  double getLinearSnrTest ( const float snr_in_db ) const
  {
    return getLinearSnr(snr_in_db);
  }
  ChannelPoint addNoiseTest ( const ChannelPoint & point, const double sigma )
  {
    return addNoise(point, sigma);
  }
  unsigned short int computeBerTest ( const unsigned short int codeA, const unsigned short int codeB ) const
  {
    return computeBer(codeA, codeB);
  }
  unsigned int computeNoiseTest ( const ChannelPoint & pointA, const ChannelPoint & pointB ) const
  {
    return computeNoise(pointA, pointB);
  }
  float computeFerTest ( const unsigned short int ber, const unsigned int n, const Channel_Mod modulation ) const
  {
    return computeFer(ber, n, modulation);
  }
  bool computeCrcErrorTest ( const float fer )
  {
    return computeCrcError(fer);
  }
};


void ChannelComputerTest::setUp (void)
{
  logTest();

  mpCoreEngine = new CoreEngine();
  CPPUNIT_ASSERT_MESSAGE ( "Core Engine pointer is NULL", NULL != mpCoreEngine );
  mpChannelComputer = new ChannelComputer(mpCoreEngine->getPhy(), mpCoreEngine->getSystem());
  CPPUNIT_ASSERT_MESSAGE ( "Channel computer pointer is NULL", NULL != mpChannelComputer );
}


void ChannelComputerTest::tearDown (void) 
{
  logTest();
  
  if (NULL != mpChannelComputer)
  {
    delete (mpChannelComputer);
    mpChannelComputer = NULL;
  }
  if (NULL != mpCoreEngine)
  {
    delete (mpCoreEngine);
    mpCoreEngine = NULL;
  }
}


void ChannelComputerTest::duplicateMpduPayloadTest (void)
{
  logTest();

  PhySciMsgMpdu mpdu(mpCoreEngine->getPhy());
  unsigned long length = 512;
  unsigned char payload[length];
  mpdu.setMpdu(length, payload);
  mpdu.setPbSize(512);
  mpdu.setNbOfPbs(2);
  mpdu.setFecrate(PHY_FEC_RATE_1_2);
  mpdu.setMod(PHY_MOD_ROBO);
  CPPUNIT_ASSERT_MESSAGE ( "duplicateMpduPayload failed",
                           mpChannelComputer->duplicateMpduPayload(mpdu)
                           && (1040*2*4 == mpdu.getPayloadLength()) );
}


void ChannelComputerTest::addPerturbationTest (void)
{
  logTest();

  PhySciMsgMpdu mpdu(mpCoreEngine->getPhy());
  PhySciMsgNoise noise(mpCoreEngine->getPhy());
  uint32_t pbMeasurement[MAC_MAX_PB_PER_MPDU];
  uint32_t pbMeasurementResult[MAC_MAX_PB_PER_MPDU];
  memset(pbMeasurement, '\0', MAC_MAX_PB_PER_MPDU);
  memset(pbMeasurementResult, '\0', MAC_MAX_PB_PER_MPDU);
  uint32_t pbHdr[MAC_MAX_PB_PER_MPDU];
  memset(pbHdr, 0x12, MAC_MAX_PB_PER_MPDU);
  CPPUNIT_ASSERT_MESSAGE ( "addPerturbation failed",
                           (mpChannelComputer->addPerturbation(pbMeasurementResult,
                                                               noise,
                                                               pbHdr,
                                                               mpdu,
                                                               0,
                                                               0,
                                                               mpCoreEngine->getNetworkClock()->getCurrentTickValue()))
                           && (NULL != pbMeasurementResult)
                           && (0 != memcmp(pbMeasurement, pbMeasurementResult, MAC_MAX_PB_PER_MPDU))
                           && (NULL != noise.getSpecializedSciMsgData())
                           && ((PHY_CARRIER_NB*sizeof(uint32_t) + MAC_MAX_SYMB_PER_MPDU*sizeof(uint32_t)) == noise.getSpecializedSciMsgDataLength()) );
}


void ChannelComputerTest::protectedTest (void)
{
  logTest();

  if (NULL != mpChannelComputer)
  {
    delete (mpChannelComputer);
    mpChannelComputer = NULL;
  }

  ProtectedTest channelComputer(mpCoreEngine->getPhy(), mpCoreEngine->getSystem());

  // Test PowerScale
  Channel_Mod modulation = MAXIMUS_CHANNEL_MOD_BPSK;
  float powerScale = channelComputer.getPowerScaleTest(modulation);
  CPPUNIT_ASSERT_MESSAGE ( "getPowerScale failed", almost_eqf (1, powerScale, 5));
  modulation = MAXIMUS_CHANNEL_MOD_QPSK;
  powerScale = channelComputer.getPowerScaleTest(modulation);
  CPPUNIT_ASSERT_MESSAGE ( "getPowerScale failed", almost_eqf (2, powerScale, 5));
  modulation = MAXIMUS_CHANNEL_MOD_QAM8;
  powerScale = channelComputer.getPowerScaleTest(modulation);
  CPPUNIT_ASSERT_MESSAGE ( "getPowerScale failed", almost_eqf (5 + pow (1.29, 2), powerScale, 5));
  modulation = MAXIMUS_CHANNEL_MOD_QAM16;
  powerScale = channelComputer.getPowerScaleTest(modulation);
  CPPUNIT_ASSERT_MESSAGE ( "getPowerScale failed", almost_eqf (10, powerScale, 5));
  modulation = MAXIMUS_CHANNEL_MOD_QAM64;
  powerScale = channelComputer.getPowerScaleTest(modulation);
  CPPUNIT_ASSERT_MESSAGE ( "getPowerScale failed", almost_eqf (42, powerScale, 5));
  modulation = MAXIMUS_CHANNEL_MOD_QAM256;
  powerScale = channelComputer.getPowerScaleTest(modulation);
  CPPUNIT_ASSERT_MESSAGE ( "getPowerScale failed", almost_eqf (170, powerScale, 5));
  modulation = MAXIMUS_CHANNEL_MOD_QAM1024;
  powerScale = channelComputer.getPowerScaleTest(modulation);
  CPPUNIT_ASSERT_MESSAGE ( "getPowerScale failed", almost_eqf (682, powerScale, 5));

  // Test linear SNR
  float snr = 10; // in dB
  double linearSnr = channelComputer.getLinearSnrTest(snr);
  CPPUNIT_ASSERT_MESSAGE ( "getLinearSnr failed", almost_eqf (10, linearSnr, 5));
  snr = 16; // in dB
  linearSnr = channelComputer.getLinearSnrTest(snr);
  CPPUNIT_ASSERT_MESSAGE ( "getLinearSnr failed", almost_eqf (pow (10, 1.6), linearSnr, 5));
  snr = 15.55555; // in dB
  linearSnr = channelComputer.getLinearSnrTest(snr);
  CPPUNIT_ASSERT_MESSAGE ( "getLinearSnr failed", almost_eqf (pow (10, 1.555555), linearSnr, 5));

  // Test sigma factor
  modulation = MAXIMUS_CHANNEL_MOD_BPSK;
  snr = 10; // in dB
  double sigma = channelComputer.getSigmaTest(modulation, snr);
  CPPUNIT_ASSERT_MESSAGE ( "getSigma failed", almost_eqf (sqrt(1/(2*channelComputer.getLinearSnrTest(snr))), sigma, 5));
  modulation = MAXIMUS_CHANNEL_MOD_QPSK;
  snr = 11.1; // in dB
  sigma = channelComputer.getSigmaTest(modulation, snr);
  CPPUNIT_ASSERT_MESSAGE ( "getSigma failed", almost_eqf (sqrt(2/(2*channelComputer.getLinearSnrTest(snr))), sigma, 5));
  modulation = MAXIMUS_CHANNEL_MOD_QAM8;
  snr = 12.2; // in dB
  sigma = channelComputer.getSigmaTest(modulation, snr);
  CPPUNIT_ASSERT_MESSAGE ( "getSigma failed", almost_eqf (sqrt((5+pow(1.29, 2))/(2*channelComputer.getLinearSnrTest(snr))), sigma, 5));
  modulation = MAXIMUS_CHANNEL_MOD_QAM16;
  snr = 13.3; // in dB
  sigma = channelComputer.getSigmaTest(modulation, snr);
  CPPUNIT_ASSERT_MESSAGE ( "getSigma failed", almost_eqf (sqrt(10/(2*channelComputer.getLinearSnrTest(snr))), sigma, 5));
  modulation = MAXIMUS_CHANNEL_MOD_QAM64;
  snr = 14.4; // in dB
  sigma = channelComputer.getSigmaTest(modulation, snr);
  CPPUNIT_ASSERT_MESSAGE ( "getSigma failed", almost_eqf (sqrt(42/(2*channelComputer.getLinearSnrTest(snr))), sigma, 5));
  modulation = MAXIMUS_CHANNEL_MOD_QAM256;
  snr = 15.5; // in dB
  sigma = channelComputer.getSigmaTest(modulation, snr);
  CPPUNIT_ASSERT_MESSAGE ( "getSigma failed", almost_eqf (sqrt(170/(2*channelComputer.getLinearSnrTest(snr))), sigma, 5));
  modulation = MAXIMUS_CHANNEL_MOD_QAM1024;
  snr = 16.6; // in dB
  sigma = channelComputer.getSigmaTest(modulation, snr);
  CPPUNIT_ASSERT_MESSAGE ( "getSigma failed", almost_eqf (sqrt(682/(2*channelComputer.getLinearSnrTest(snr))), sigma, 5));
  snr = 0; // in dB
  sigma = channelComputer.getSigmaTest(modulation, snr);
  CPPUNIT_ASSERT_MESSAGE ( "getSigma failed", almost_eqf (sqrt(682/(2*channelComputer.getLinearSnrTest(snr))), sigma, 5));

  // Test noise point
  ChannelPoint point(1, 0);
  ChannelPoint noisePoint = channelComputer.addNoiseTest(point, sigma);
  CPPUNIT_ASSERT_MESSAGE ( "addNoise failed",
                           (point.I != noisePoint.I)
                           && (point.Q != noisePoint.Q));

  // Test BER
  unsigned short int codeA = 0x3;
  unsigned short int codeB = 0x2;
  unsigned short int ber = channelComputer.computeBerTest(codeA, codeB);
  CPPUNIT_ASSERT_MESSAGE ( "computeBer failed", 1 == ber);
  codeA = 0x3FF;
  codeB = 0x0;
  ber = channelComputer.computeBerTest(codeA, codeB);
  CPPUNIT_ASSERT_MESSAGE ( "computeBer failed", 10 == ber);
  codeA = 0xFF;
  codeB = 0xF;
  ber = channelComputer.computeBerTest(codeA, codeB);
  CPPUNIT_ASSERT_MESSAGE ( "computeBer failed", 4 == ber);
  codeA = 0x44;
  codeB = 0x44;
  ber = channelComputer.computeBerTest(codeA, codeB);
  CPPUNIT_ASSERT_MESSAGE ( "computeBer failed", 0 == ber);
  bool b = false;
  try
  {
    codeA = 0xFFF;
    ber = channelComputer.computeBerTest(codeA, codeB);
  }
  catch (Error & e)
  {
    b = true;
  }
  CPPUNIT_ASSERT_MESSAGE ( "computeBer failed", b);

  // Test noise
  ChannelPoint pointA(0, 0);
  ChannelPoint pointB(0, 0);
  unsigned int noise = channelComputer.computeNoiseTest(pointA, pointB);
  CPPUNIT_ASSERT_MESSAGE ( "computeNoise failed", 0 == noise);
  pointB.I = 1;
  pointB.Q = 1;
  noise = channelComputer.computeNoiseTest(pointA, pointB);
  CPPUNIT_ASSERT_MESSAGE ( "computeNoise failed", 2 == noise);
  pointA.I = -3;
  pointA.Q = -1;
  noise = channelComputer.computeNoiseTest(pointA, pointB);
  CPPUNIT_ASSERT_MESSAGE ( "computeNoise failed", 20 == noise);
  pointB.I = -0.12345;
  pointB.Q = 6.789;
  noise = channelComputer.computeNoiseTest(pointA, pointB);
  CPPUNIT_ASSERT_MESSAGE ( "computeNoise failed", (unsigned int)pow(7.789, 2) + (unsigned int)pow(2.87655, 2) == noise);

  // Test FER
  unsigned int n = 10;
  modulation = MAXIMUS_CHANNEL_MOD_BPSK;
  float fer = channelComputer.computeFerTest(ber, n, modulation);
  float expectedFer = pow(10, 1-(unsigned short int)100*exp(-pow((unsigned short int)50*ber, 2)))/10;
  CPPUNIT_ASSERT_MESSAGE ( "computeFer 1 failed", almost_eqf (expectedFer, fer, 5));
  modulation = MAXIMUS_CHANNEL_MOD_QPSK;
  fer = channelComputer.computeFerTest(ber, n, modulation);
  expectedFer = pow(10, 1-(unsigned short int)100*exp(-pow((unsigned short int)50*(ber-0.01*(float)4.6), 2)))/10;
  float result = (fer > expectedFer) ? fer - expectedFer : expectedFer - fer;
  CPPUNIT_ASSERT_MESSAGE ( "computeFer 2 failed", result < 0.001);
  n = 5;
  fer = channelComputer.computeFerTest(ber, n, modulation);
  expectedFer = pow(10, 1-(unsigned short int)100*exp(-pow((unsigned short int)50*(ber-0.01*(float)2.25), 2)))/10;
  CPPUNIT_ASSERT_MESSAGE ( "computeFer 3 failed", result < 0.001);
  n = 0;
  fer = channelComputer.computeFerTest(ber, n, modulation);
  expectedFer = pow(10, 1-(unsigned short int)100*exp(-pow((unsigned short int)50*(ber-0.01*(float)0.3), 2)))/10;
  CPPUNIT_ASSERT_MESSAGE ( "computeFer 4 failed", result < 0.001);

  // Test CRC
  int count = 0;
  fer = 0;
  for (int i=0; i<11; i++)
  {
    if (channelComputer.computeCrcErrorTest(fer))
    {
      count++;
    }
    fer += 0.1;
  }
  CPPUNIT_ASSERT_MESSAGE ( "computeCrcError failed", (count > 0) && (count < 11));
}