-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerfTest.cpp
More file actions
609 lines (550 loc) · 16.3 KB
/
Copy pathPerfTest.cpp
File metadata and controls
609 lines (550 loc) · 16.3 KB
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <bitset>
#include <windows.h>
#include "rvgs.h"
#include "rngs.h"
#include "ArrayLDPCMacro.h"
#include "ArrayLDPC.h"
#include "PerfTest.h"
using std::endl;
using std::cout;
using std::cin;
using std::ofstream;
int ArrayLDPC_Debug_Wifi()
{
//2209 - 231 = 1978
//1978 / 8 = 247, 1978 % 8 = 2
//char InfoStream[248] = "OMG how long should this string be to make it 248, just imagine that. \
I guess it's still not long enough. Let's see. This is a testing string \
for a lot of characters so that we have some random bit stream that's\
correct";
//--- unused debugging varibles
//char InfoStream[243] = "";// all zero info, exactly 243 bytes for wifi code
char InfoStream[122] = "OMG how long dd should this string be to make it 243";
//int InfoBit[INFO_LENGTH];
int Codewordtemp[CWD_LENGTH];
//double Receive[CWD_LENGTH];
//--- unused debugging varibles
int i, j;
int LLR_fp[CWD_LENGTH];
//int pckerror = 0;
double biterror = 0;
long Counter = 0;
double LLR[CWD_LENGTH];
double db = 1.0;
double Rate = 0;
double EbN0_dB = 4;
double snr = 0;
double sigma;
double blkerror = 0;
double pckerror = 0;
int info_indx[INFO_LENGTH];
class FP_Decoder Decoder;
class FP_Encoder Encoder("H_802.11_IndZerog.txt", 0);
EbN0_dB = 3;
//Rate = Decoder.getRate();
cout << "EbNo in dB? ";
cin >> EbN0_dB;
snr = 2*pow(10.0, EbN0_dB/10)*0.5; // Eb/N0 to snr
sigma = sqrt(1/snr);
cout << "SNR is " << 10*log10(snr) << " dB" << endl;
biterror = 0;
//---- debugging
//char out[277] = "";
//out = new char[553];
// working now
//Encoder.encode(InfoStream, out, 248);
//----
// set the true information bits in the decoder to calculate BER
Decoder.setInfoBit(InfoStream, 122);
//for(i = 0; i < INFO_LENGTH; i++)
//{
// //info_indx[i] = Encoder.getInfoIndex(i);
// // for wifi code it's systematic due to staircase structure
// info_indx[i] = i;
//}
// NEED IMPROVEMENT! SHOULD BE ABLE TO HAVE SYSTEMATIC BITS
//ofstream file("wifi_infoindx.txt");
for(i = 0; i < INFO_LENGTH; i++)
{
info_indx[i] = Encoder.getInfoIndex(i);
//file << info_indx[i] << " ";
}
//file.close();
Encoder.encode(InfoStream, 122);
Decoder.setInfoIndex(info_indx);
//for(i = 0; i < CWD_LENGTH; i++)
// Codewordtemp[i] = Encoder.getCodeword(i);
//Decoder.setCodeword(Codewordtemp);
//cout << "true codeword checksum:" << Decoder.check() <<endl;
Decoder.ReadH();
while(pckerror < 100)
{
blkerror = 0;
//for(i = 247; i >= 0; i--)
// cout << std::bitset<8>(InfoStream[i]) << ", ";
//cout <<endl;
//WIFI code
//cout << "codeword checksum:" << Decoder.check_fp(Codewordtemp) <<endl;
//cout << "codeword checksum: (0 is pass, 1 is not pass):" << Decoder.check_fp(Codeword)<<endl;
for(i = 0; i < CWD_LENGTH; i++)
{
//-- BPSK modulation => 0 -> 1, 1 -> -1
// Using Box-Muller method to generate Gaussian noise
LLR[i] = 2*snr*(1 - 2*Encoder.getCodeword(i) + Normal(0, sigma));
// -- all zero
//LLR[i] = 2*snr*(1 + Normal(0, sigma));
//-- noiseless case
//LLR[i] = 2*snr*(1 - 2*Encoder.getCodeword(i));
// Using Wallace method to generate Gaussian noise
//LLR[i] = 2*EbN0*(1 + Wallace(0, sigma));
LLR_fp[i] = int(LLR[i]*(1<<FRAC_WIDTH));
}
Decoder.setState(PCV);
//cout << "checking hard output before decoding:" << Decoder.hardDecision(LLR_fp) << endl;
//blkerror = Decoder.decode(LLR);
//cout << Decoder.decode_fixpoint(LLR_fp) << " total iterations" <<endl;
//Decoder.decode(LLR);
Decoder.decode_general_fp(LLR_fp);
Decoder.resetBER();
blkerror = Decoder.calculateBER();
if(blkerror > 0)
pckerror++;
biterror += blkerror;
Counter++;
}
cout << biterror << " " << pckerror << " " << Counter << endl
<< " FER: " << pckerror/Counter << " BER: " << biterror/Counter/CWD_LENGTH << endl;
//Decoder.getInfoBit();
return 0;
}
//---------------------------- Non in-class code
void noMoreMemory()
{
cerr << "Unable to satisfy request for memory\n";
abort();
}
int DecodeTrial(double EbN0_dB, int MaxPacket)
{
double LLR[CWD_LENGTH];
int LLR_fp[100][CWD_LENGTH];
int i, j;
double snr, sigma, Rate = 0;;
class FP_Decoder Decoder;
__int64 ctr1 = 0, ctr2 = 0, freq = 0;
//EbN0_dB = 4.5;
Rate = Decoder.getRate();
snr = 2*pow(10.0, EbN0_dB/10)*Rate; // Eb/N0 to snr
sigma = sqrt(1/snr);
cout << "equivalent SNR is: " << 10*log10(snr) << endl;
for(j = 0; j < 100; j++)
{
for(i = 0; i < CWD_LENGTH; i++)
{
//-- BPSK modulation => 0 -> 1, 1 -> -1 assume all zero codeword
// Using Box-Muller method to generate Gaussian noise
LLR[i] = 2*snr*(1 + Normal(0, sigma));
LLR_fp[j][i] = int(LLR[i]*(1<<FRAC_WIDTH));
}
}
// Start timing the code.for(i = 0; i < CWD_LENGTH; i++)
if(QueryPerformanceCounter((LARGE_INTEGER *) &ctr1) != 0)
{
// Do what ever you do, what ever you need to time...
// Finish timing the code.
for(i = 0; i < MaxPacket; i++)
{
Decoder.setState(PCV);
Decoder.decode_fixpoint(LLR_fp[i%100]);
}
QueryPerformanceCounter((LARGE_INTEGER *) &ctr2);
QueryPerformanceFrequency((LARGE_INTEGER *) &freq);
// Print the time spent in microseconds to the console.
std::cout << ((ctr2 - ctr1) * 1.0 / freq) << " seconds" << std::endl;
std::cout << 2209*MaxPacket/((ctr2 - ctr1) * 1.0 / freq) << " bits per second for decoder" << std::endl;
}
return 0;
}
int EncodeTrial(char *info, int MaxPacket)
{
class FP_Encoder Encoder("G_array_forward.txt", 0);
int i;
__int64 ctr1 = 0, ctr2 = 0, freq = 0;
// Start timing the code.
if(QueryPerformanceCounter((LARGE_INTEGER *) &ctr1) != 0)
{
for(i = 0; i < MaxPacket; i++)
{
Encoder.encode(info, 248);
}
QueryPerformanceCounter((LARGE_INTEGER *) &ctr2);
QueryPerformanceFrequency((LARGE_INTEGER *) &freq);
// Print the time spent in microseconds to the console.
cout << ((ctr2 - ctr1) * 1.0 / freq) << " seconds" << endl;
cout << 2209*MaxPacket/((ctr2 - ctr1) * 1.0 / freq) << " bits per second for encoder" << endl;
}
return 0;
}
int ArrayLDPC_Debug()
{
//2209 - 231 = 1978
//1978 / 8 = 247, 1978 % 8 = 2
char InfoStream[248] = "OMG how long should this string be to make it 248, just imagine that. \
I guess it's still not long enough. Let's see. This is a testing string \
for a lot of characters so that we have some random bit stream that's\
correct";
//--- unused debugging varibles
//char InfoStream[248] = "";
//int InfoBit[INFO_LENGTH];
//int Codewordtemp[CWD_LENGTH];
//double Receive[CWD_LENGTH];
//--- unused debugging varibles
int i, j;
int LLR_fp[CWD_LENGTH];
//int pckerror = 0;
double biterror = 0;
long Counter = 0;
long Clk = 0;
double LLR[CWD_LENGTH];
double db = 1.0;
double Rate = 0;
double EbN0_dB = 4;
double snr = 0;
double sigma;
double blkerror = 0;
double pckerror = 0;
int info_indx[INFO_LENGTH];
class FP_Decoder Decoder;
class FP_Encoder Encoder("G_array_forward.txt", 0);
EbN0_dB = 4.5;
Rate = Decoder.getRate();
snr = 2*pow(10.0, EbN0_dB/10)*Rate; // Eb/N0 to snr
sigma = sqrt(1/snr);
cout << "SNR is " << 10*log10(snr) << " dB" << endl;
biterror = 0;
Clk = 0;
//---- debugging
char out[277] = "";
//out = new char[553];
// working now
Encoder.encode(InfoStream, out, 248);
//----
Decoder.setInfoBit(InfoStream, 248);
for(i = 0; i < INFO_LENGTH; i++)
{
info_indx[i] = Encoder.getInfoIndex(i);
}
Decoder.setInfoIndex(info_indx);
//for(i = 0; i < CWD_LENGTH; i++)
// Codewordtemp[i] = Encoder.getCodeword(i);
//Decoder.setCodeword(Codewordtemp);
//cout << "true codeword checksum:" << Decoder.check() <<endl;
while(pckerror < 100)
{
blkerror = 0;
//for(i = 247; i >= 0; i--)
// cout << std::bitset<8>(InfoStream[i]) << ", ";
//cout <<endl;
Encoder.encode(InfoStream, 248);
//cout << "codeword checksum:" << Decoder.check_fp(Codewordtemp) <<endl;
//cout << "codeword checksum: (0 is pass, 1 is not pass):" << Decoder.check_fp(Codeword)<<endl;
for(i = 0; i < CWD_LENGTH; i++)
{
//-- BPSK modulation => 0 -> 1, 1 -> -1
// Using Box-Muller method to generate Gaussian noise
LLR[i] = 2*snr*(1 - 2*Encoder.getCodeword(i) + Normal(0, sigma));
//-- noiseless case
//LLR[i] = 2*snr*(1 - 2*Encoder.getCodeword(i));
// Using Wallace method to generate Gaussian noise
//LLR[i] = 2*EbN0*(1 + Wallace(0, sigma));
LLR_fp[i] = int(LLR[i]*(1<<FRAC_WIDTH));
}
Decoder.setState(PCV);
//cout << "checking hard output before decoding:" << Decoder.hardDecision(LLR_fp) << endl;
//blkerror = Decoder.decode(LLR);
//cout << Decoder.decode_fixpoint(LLR_fp) << " total iterations" <<endl;
Decoder.decode_fixpoint(LLR_fp);
Decoder.resetBER();
blkerror = Decoder.calculateBER();
if(blkerror > 0)
pckerror++;
biterror += blkerror;
Counter++;
}
cout << biterror << " " << pckerror << " " << Counter << endl
<< " FER: " << pckerror/Counter << " BER: " << biterror/Counter/CWD_LENGTH << endl;
//Decoder.getInfoBit();
return 0;
}
int ArrayLDPC_Debug_Shorten(int short_len)
{
//2209 - 231 = 1978
//1978 / 8 = 247, 1978 % 8 = 2
char InfoStream[248] = "OMG how long should this string be to make it 248, just imagine that. \
I guess it's still not long enough. Let's see. This is a testing string \
for a lot of characters so that we have some random bit stream that's\
correct";
//--- unused debugging varibles
//char InfoStream[248] = "";
//int InfoBit[INFO_LENGTH];
//int Codewordtemp[CWD_LENGTH];
//double Receive[CWD_LENGTH];
//--- unused debugging varibles
int i, j;
int LLR_fp[CWD_LENGTH];
//int pckerror = 0;
double biterror = 0;
long Counter = 0;
long Clk = 0;
double LLR[CWD_LENGTH];
double db = 1.0;
double Rate = 0;
double EbN0_dB = 4;
double snr = 0;
double sigma;
double blkerror = 0;
double pckerror = 0;
int info_indx[INFO_LENGTH];
class FP_Decoder Decoder;
class FP_Encoder Encoder("G_array_forward.txt", 0);
EbN0_dB = 4.5;
Rate = Decoder.getRate();
snr = 2*pow(10.0, EbN0_dB/10)*Rate; // Eb/N0 to snr
snr = 2*pow(10.0, EbN0_dB/10)*(1978.0 - 976.0)/2209.0;
sigma = sqrt(1/snr);
cout << "SNR is " << 10*log10(snr) << " dB" << endl;
biterror = 0;
Clk = 0;
//---- shortening, try 976 out of 1978 since it corresondes to 122 char
for(i = 0; i < short_len; i ++)
{
//--- first short_len number of string becomes
InfoStream[i] = 0;
}
//---- debugging
//char out[277] = "";
//out = new char[553];
// working now
//Encoder.encode(InfoStream, out, 248);
//----
//Decoder.setShorten(short_len);
Decoder.setInfoBit(InfoStream, 248);
for(i = 0; i < INFO_LENGTH; i++)
{
info_indx[i] = Encoder.getInfoIndex(i);
}
Decoder.setInfoIndex(info_indx);
//for(i = 0; i < CWD_LENGTH; i++)
// Codewordtemp[i] = Encoder.getCodeword(i);
//Decoder.setCodeword(Codewordtemp);
//cout << "true codeword checksum:" << Decoder.check() <<endl;
while(pckerror < 100)
{
blkerror = 0;
//for(i = 247; i >= 0; i--)
// cout << std::bitset<8>(InfoStream[i]) << ", ";
//cout <<endl;
Encoder.encode(InfoStream, 248);
//cout << "codeword checksum:" << Decoder.check_fp(Codewordtemp) <<endl;
//cout << "codeword checksum: (0 is pass, 1 is not pass):" << Decoder.check_fp(Codeword)<<endl;
for(i = 0; i < CWD_LENGTH; i++)
{
//-- BPSK modulation => 0 -> 1, 1 -> -1
// Using Box-Muller method to generate Gaussian noise
LLR[i] = 2*snr*(1 - 2*Encoder.getCodeword(i) + Normal(0, sigma));
//-- noiseless case
//LLR[i] = 2*snr*(1 - 2*Encoder.getCodeword(i));
// Using Wallace method to generate Gaussian noise
//LLR[i] = 2*EbN0*(1 + Wallace(0, sigma));
LLR_fp[i] = int(LLR[i]*(1<<FRAC_WIDTH));
}
Decoder.setState(PCV);
//----- shortening part
for(i = 0; i < short_len; i ++)
{
// set to max integer value
LLR_fp[info_indx[i]] = 7*(1<<FRAC_WIDTH );
}
//cout << "checking hard output before decoding:" << Decoder.hardDecision(LLR_fp) << endl;
//blkerror = Decoder.decode(LLR);
//cout << Decoder.decode_fixpoint(LLR_fp) << " total iterations" <<endl;
cout << Decoder.decode_fixpoint(LLR_fp) << ", ";
Decoder.resetBER();
blkerror = Decoder.calculateBER();
if(blkerror > 0)
pckerror++;
biterror += blkerror;
Counter++;
}
cout << biterror << " " << pckerror << " " << Counter << endl
<< " FER: " << pckerror/Counter << " BER: " << biterror/Counter/CWD_LENGTH << endl;
//Decoder.getInfoBit();
return 0;
}
int ArrayLDPC_PerfTest(double db_start, double db_end, double db_step, char* Filename)
{
char Filename2[40]="";
int InfoBit[INFO_LENGTH];
int i, j;
int CurrentInd = 0;
int LLR_fp[CWD_LENGTH];
//int pckerror = 0;
double BitError = 0;
long Counter = 0;
long Clk = 0;
double Receive[CWD_LENGTH];
double LLR[CWD_LENGTH];
double snr;
double db = 1.0;
double Rate = 0;
double EbN0_dB = 4;
double EbN0 = 0;
double sigma;
double blkerror = 0;
double pckerror = 0;
double **MemBank; //Memory bank
bool per_flag;
class FP_Decoder Decoder;
class ControlFSM FSM;
ofstream FilePtr(Filename);
if(!FilePtr)
{
cerr << "failed to open " << Filename << endl;
system("pause");
exit(0);
}
FilePtr.close();
//strcat(Filename2, "log_");
//strcat(Filename2, Filename);
//strcat(Filename2, ".txt");
sprintf(Filename2, "%s_log.txt", Filename);
ofstream LogPtr(Filename2);
if(!LogPtr)
{
cerr << "failed to open " << Filename2 << endl;
system("pause");
exit(0);
}
LogPtr.close();
//cout << "SNR? ";
//cin >> EbN0_dB;
EbN0_dB = db_start;
Rate = Decoder.getRate();
EbN0 = 2*pow(10.0, EbN0_dB/10)*Rate; // Eb/N0
sigma = sqrt(1/EbN0);
BitError = 0;
Clk = 0;
while(pckerror < 100)
{
blkerror = 0;
for(i = 0; i < CWD_LENGTH; i++)
{
//-- All zero codeword
// Using Box-Muller method to generate Gaussian noise
LLR[i] = 2*EbN0*(1 + Normal(0, sigma));
// Using Wallace method to generate Gaussian noise
//LLR[i] = 2*EbN0*(1 + Wallace(0, sigma));
LLR_fp[i] = int(LLR[i]*(1<<FRAC_WIDTH));
//Debug
//LLR_fp[i] = i % 47;
}
Decoder.setState(PCV);
//blkerror = Decoder.decode(LLR);
blkerror = Decoder.decode_fixpoint(LLR_fp);
if(blkerror > 0)
pckerror++;
BitError += blkerror;
Counter++;
}
cout << BitError << " " << pckerror << " " << Counter << endl
<< " FER: " << pckerror/Counter << " BER: " << BitError/Counter/CWD_LENGTH << endl;
//Decoder.getInfoBit();
return 0;
}
int ArrayLDPC_TimeTrial(double db, int MaxPckNum, char* Filename)
{
char Filename2[40]="";
char InfoStream[248] = "OMG how long should this string be to make it 248, just imagine that. \
I guess it's still not long enough. Let's see. This is a testing string \
for a lot of characters so that we have some random bit stream that's\
correct";
int i, j;
int CurrentInd = 0;
int LLR_fp[CWD_LENGTH];
double BitError = 0;
long Counter = 0;
long Clk = 0;
//double Receive[CWD_LENGTH];
double LLR[CWD_LENGTH];
double snr;
//double db = 1.0;
double Rate = 0;
double EbN0_dB = 4;
double EbN0 = 0;
double sigma;
double blkerror = 0;
double pckerror = 0;
//double **MemBank; //Memory bank
bool per_flag;
class FP_Decoder Decoder;
class ControlFSM FSM;
ofstream FilePtr(Filename);
if(!FilePtr)
{
cerr << "failed to open " << Filename << endl;
system("pause");
exit(0);
}
FilePtr.close();
//strcat(Filename2, "log_");
//strcat(Filename2, Filename);
//strcat(Filename2, ".txt");
sprintf(Filename2, "%s_log.txt", Filename);
ofstream LogPtr(Filename2);
if(!LogPtr)
{
cerr << "failed to open " << Filename2 << endl;
system("pause");
exit(0);
}
LogPtr.close();
//cout << "SNR? ";
//cin >> EbN0_dB;
EbN0_dB = db;
Rate = Decoder.getRate();
EbN0 = 2*pow(10.0, EbN0_dB/10)*Rate; // Eb/N0
sigma = sqrt(1/EbN0);
BitError = 0;
Clk = 0;
while(Counter < MaxPckNum)
{
blkerror = 0;
for(i = 0; i < CWD_LENGTH; i++)
{
//-- All zero codeword
// Using Box-Muller method to generate Gaussian noise
LLR[i] = 2*EbN0*(1 + Normal(0, sigma));
// Using Wallace method to generate Gaussian noise
//LLR[i] = 2*EbN0*(1 + Wallace(0, sigma));
LLR_fp[i] = int(LLR[i]*(1<<FRAC_WIDTH));
//Debug
//LLR_fp[i] = i % 47;
}
Decoder.setState(PCV);
//blkerror = Decoder.decode(LLR);
blkerror = Decoder.decode_fixpoint(LLR_fp);
if(blkerror > 0)
pckerror++;
BitError += blkerror;
Counter++;
}
cout << BitError << " " << pckerror << " " << Counter << endl
<< " FER: " << pckerror/Counter << " BER: " << BitError/Counter/CWD_LENGTH << endl;
//Decoder.getInfoBit();
return 0;
}