-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayLDPC_Encoder.cpp
More file actions
621 lines (603 loc) · 15.2 KB
/
Copy pathArrayLDPC_Encoder.cpp
File metadata and controls
621 lines (603 loc) · 15.2 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
608
609
610
611
612
613
614
615
616
617
618
#include <iostream>
#include <fstream>
#include <bitset>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "rngs.h"
#include "rvgs.h"
#include "MemoryMacro.h"
#include "Memory.h"
//#include "ArrayLDPC.h"
#include "ArrayLDPCMacro.h"
using namespace std;
//--------- notes -----------
// 1. don't really need the posterior stored. just the hard decision would be enough
//
//---------------------------
//-------------- Encoder part starts
FP_Encoder::~FP_Encoder()
{
;
//int i;
//delete [] ParityIndex;
//delete [] InfoIndex;
//for(i = 0; i < Gdim_col; i++ )
// delete [] GeneratorMat[i];
//
//delete [] InfoBuffer;
//delete [] GeneratorMat;
}
FP_Encoder::FP_Encoder(char* Filename, int flag)
{
//flag == 1 : verbose, flag == 0 supress message
if(flag)
cout << "reading file " << Filename << endl;
try{
//ifstream FileStr("G_array_zindx.txt");
ifstream FileStr(Filename);
FileStr.exceptions ( ifstream::failbit | ifstream::badbit );
if(1)// alist file format, current test mode
{
int vnum, cnum, vdeg_max, cdeg_max;
int par_count = 0, info_count = 0;
FileStr >> vnum;
FileStr >> cnum;
Gdim_col = cnum;
Gdim_row = vnum - cnum;
FileStr >> vdeg_max;
FileStr >> cdeg_max;
int i, j;
// input column flag
for(i = 0; i < vnum; i++)
{
FileStr >> ColumnFlag[i];
if(ColumnFlag[i] == 1)
{
ParityIndex[par_count] = i;
par_count++;
}
else
{
InfoIndex[info_count] = i;
info_count++;
}
}
for(i = 0; i < cnum;i++)
{
FileStr >> ChkDeg[i];
}
//for(i = 0; i < vnum; i++)
//{
// for(j = 0; j < VarDeg[i]; j++)
// FileStr >> dummy;
//}
for(i = 0; i < cnum; i++)
{
for(j = 0; j < ChkDeg[i]; j++)
FileStr >> G_mlist[i][j];
}
//FileStr.close();
//ofstream OutStr("G_array_zindx.txt");
//OutStr << vnum << " ";
//OutStr << cnum << " " << endl;
//OutStr << vdeg_max << " ";
//OutStr << cdeg_max << " " << endl;
//for(i = 0; i < vnum;i++)
//{
// OutStr << ColumnFlag[i] << " ";
//}
//OutStr << endl;
//for(i = 0; i < cnum;i++)
//{
// OutStr << ChkDeg[i] << " ";
//}
//OutStr << endl;
//for(i = 0; i < cnum; i++)
//{
// for(j = 0; j < ChkDeg[i]; j++)
// {
// OutStr << G_mlist[i][j]-1 << " ";
// }
// OutStr << endl;
//}
//OutStr.close();
}
else // binary file format (the generator matrix in binary)
{
//int par_num, info_num;
int i, j;
FileStr >> Gdim_row; // row dimension
//info_num; //
FileStr >> Gdim_col;
//Gdim_row = info_num;
//Gdim_col = par_num;
//InfoIndex = new unsigned int[Gdim_row];
//ParityIndex = new unsigned int[Gdim_col];
//GeneratorMat = new unsigned int*[Gdim_col];
//InfoBuffer = new unsigned int [InfoLen];
//for(i = 0; i < Gdim_col; i++)
//{
// GeneratorMat[i] = new unsigned int [Gdim_row];
//}
for(i = 0; i < Gdim_row; i++)
FileStr >> InfoIndex[i];
for(i = 0; i < Gdim_col; i++)
FileStr >> ParityIndex[i];
for(i = 0; i < Gdim_row; i++)
{
for(j = 0; j < Gdim_col; j++)
{
// this is the transposed version, i.e., the true parity part of a systematic generator matrix
//FileStr >> GeneratorMat[i][j];
;
}
}
}
FileStr.close();
}
catch (ifstream::failure e) {
cout << "Exception opening/reading file " << e.what() << endl;
system("pause");
exit(0);
}
catch (std::bad_alloc& ba)
{
std::cerr << "bad_alloc caught: " << ba.what() << '\n';
exit(0);
}
if(flag)
cout << "encoder initialized" << endl;
}
// an overloaded function that outputs simple integer array for debugging
int FP_Encoder::encode(char *in, int in_len)
{
int i, j, k, counter = 0;
int out_len = 2209; // hard coded for now
int num_blks;
int char_size = sizeof(char)*8;
unsigned int temp;
// ideally in_len is matching Gdim_col/8
// if not matching just pad zero
// output codeword bitwise stored in output
// calculate the number of codeword blocks to output
num_blks = in_len*char_size/INFO_LENGTH + (in_len*char_size % INFO_LENGTH > 0? 1:0);
// already allocated in the top block. unsafe to allocate the memory in the function
//out = new int[out_len*num_blks];
//for(i = 0; i<num_blks; i++)
//{
// unload the information bits from the char array till the
// second last element
for(i = 0; i < in_len-1; i++)
{
for(j = 0; j < char_size; j++)// per byte
{
InfoBuffer[counter] = (in[i] >> j) & 1;
counter ++;
}
}
// unload the remaining bits from the last element
for(j = 0; j < Gdim_row % char_size; j++)
{
InfoBuffer[counter] = (in[in_len-1] >> j) & 1;
counter ++;
}
//}
// NOTE for improvement: can ignore InfoBuffer and just use systematic codewords
for(i = 0; i < Gdim_row; i++)
{
Codeword[InfoIndex[i]] = InfoBuffer[i];
}
//c = uG : each column is a parity bit.
/*for(i = 0; i < Gdim_col; i++)
{
temp = 0;
for(j = 0; j < Gdim_row; j++)
{
temp ^= (GeneratorMat[j][i] & InfoBuffer[j]);
}
Codeword[ParityIndex[i]] = temp;
}*/
for(i = 0; i < Gdim_col; i++)
{
temp = 0;
for(j = 0; j < ChkDeg[i]; j++)
{
if(ColumnFlag[G_mlist[i][j]] == 0)
{
temp ^= Codeword[G_mlist[i][j]];
//cout << G_mlist[i][j] << " ";
}
}
Codeword[ParityIndex[i]] = temp;
}
return out_len;
}
// not done yet
int FP_Encoder::encode(char *in, char *out, int in_len)
{
int i, j, k, counter = 0;
int num_blks;
int out_len;
int char_size = sizeof(char)*8;
unsigned int temp;
// ideally in_len is matching Gdim_col/8
// if not matching just pad zero
// output codeword bitwise stored in output
// calculate the number of codeword blocks to output
num_blks = in_len*char_size/INFO_LENGTH + (in_len*char_size % INFO_LENGTH > 0? 1:0);
for(i = 0; i < in_len-1; i++)
{
for(j = 0; j < char_size; j++)// per byte
{
InfoBuffer[counter] = (in[i] >> j) & 1;
counter ++;
}
}
// unload the remaining bits from the last element
for(j = 0; j < Gdim_row % char_size; j++)
{
InfoBuffer[counter] = (in[in_len-1] >> j) & 1;
counter ++;
}
//}
// NOTE for improvement: can ignore InfoBuffer and just use systematic codewords
for(i = 0; i < Gdim_row; i++)
{
Codeword[InfoIndex[i]] = InfoBuffer[i];
}
//c = uG : each column is a parity bit.
/*for(i = 0; i < Gdim_col; i++)
{
temp = 0;
for(j = 0; j < Gdim_row; j++)
{
temp ^= (GeneratorMat[j][i] & InfoBuffer[j]);
}
Codeword[ParityIndex[i]] = temp;
}*/
for(i = 0; i < Gdim_col; i++)
{
temp = 0;
for(j = 0; j < ChkDeg[i]; j++)
{
if(ColumnFlag[G_mlist[i][j]] == 0)
{
temp ^= Codeword[G_mlist[i][j]];
//cout << G_mlist[i][j] << " ";
}
}
Codeword[ParityIndex[i]] = temp;
}
out_len = num_blks*NUM_VAR/8 + (num_blks*NUM_VAR % 8 > 0? 1:0);
//out = new char[out_len];
// stuff the codeword into an array of char
cout << out;
counter = 0;
for(i = 0; i < 276; i++)
{
//cout << "out[i] = " << std::bitset<8>(out[i]) << endl;
// increase the index by one every 8 bits (one byte)
for(j = 0 ;j < 8; j++)
{
//cout << 0xff;
out[i] = out[i] ^ (Codeword[i*8 + j] << j);
//cout << "codeword " << j << "=" << Codeword[j] << endl;
//cout << std::bitset<8>(out[i]) << endl;
// shift the result to the left one (stuffing in)
//out[i] = (out[i] & 0xff) >> 1;
//cout << std::bitset<8>(out[i]) << endl;
}
cout << std::bitset<8>(out[i]) << endl;
for(j = 7; j >= 0; j--)
cout << Codeword[i*8 + j];
cout << endl;
}
for(j = 0 ;j < 2209 % 8; j++)
{
out[i] = out[i] ^ (Codeword[i*8 + j] << j);
}
//for(i)
//for(i = 0; i < 277; i++)
//{
// cout << std::bitset<8>(out[i]) << ", ";
// for(j = 0; j < 8; j++)
// cout << Codeword[i*8 + j];
// cout << endl;
//}
cout <<endl;
// returning the padding length (or should I return the codeword length?)
return out_len/num_blks;
}
//---------------- Ecoder part ends
//FP_Encoder::~FP_Encoder()
//{
// ;
// //int i;
// //delete [] ParityIndex;
// //delete [] InfoIndex;
// //for(i = 0; i < Gdim_col; i++ )
// // delete [] GeneratorMat[i];
// //
// //delete [] InfoBuffer;
// //delete [] GeneratorMat;
//}
//FP_Encoder::FP_Encoder(char* Filename, int flag)
//{
// cout << "reading file " << Filename << endl;
// int dummy;
// try{
// //ifstream FileStr("G_array_zindx.txt");
// ifstream FileStr(Filename);
// FileStr.exceptions ( ifstream::failbit | ifstream::badbit );
// //if(!FileStr)
// //{
// // cout << "fail to read the file" <<endl;
// //}
// if(1)// alist file format, current test mode
// {
// int vnum, cnum, vdeg_max, cdeg_max;
// int par_count = 0, info_count = 0;
// FileStr >> vnum;
//
// FileStr >> cnum;
// Gdim_col = cnum;
// Gdim_row = vnum - cnum;
// FileStr >> vdeg_max;
// FileStr >> cdeg_max;
// int i, j;
// for(i = 0; i < vnum; i++)
// {
// FileStr >> ColumnFlag[i];
// if(ColumnFlag[i] == 1)
// {
// ParityIndex[par_count] = i;
// par_count++;
// }
// else
// {
// InfoIndex[info_count] = i;
// info_count++;
// }
// }
// for(i = 0; i < cnum;i++)
// {
// FileStr >> ChkDeg[i];
// }
// //for(i = 0; i < vnum; i++)
// //{
// // for(j = 0; j < VarDeg[i]; j++)
// // FileStr >> dummy;
// //}
// for(i = 0; i < cnum; i++)
// {
// for(j = 0; j < ChkDeg[i]; j++)
// FileStr >> G_mlist[i][j];
// }
// //FileStr.close();
// //ofstream OutStr("G_array_zindx.txt");
// //OutStr << vnum << " ";
// //OutStr << cnum << " " << endl;
// //OutStr << vdeg_max << " ";
// //OutStr << cdeg_max << " " << endl;
// //for(i = 0; i < vnum;i++)
// //{
// // OutStr << ColumnFlag[i] << " ";
// //}
// //OutStr << endl;
// //for(i = 0; i < cnum;i++)
// //{
// // OutStr << ChkDeg[i] << " ";
// //}
// //OutStr << endl;
// //for(i = 0; i < cnum; i++)
// //{
// // for(j = 0; j < ChkDeg[i]; j++)
// // {
// // OutStr << G_mlist[i][j]-1 << " ";
// // }
// // OutStr << endl;
// //}
// //OutStr.close();
// }
// else if(flag == 0) // binary file format (the generator matrix in binary)
// {
// //int par_num, info_num;
// int i, j;
// FileStr >> Gdim_row; // row dimension
// //info_num; //
// FileStr >> Gdim_col;
// //Gdim_row = info_num;
// //Gdim_col = par_num;
// InfoLen = Gdim_row;
//
// //InfoIndex = new unsigned int[Gdim_row];
// //ParityIndex = new unsigned int[Gdim_col];
// //GeneratorMat = new unsigned int*[Gdim_col];
// //InfoBuffer = new unsigned int [InfoLen];
// //for(i = 0; i < Gdim_col; i++)
// //{
// // GeneratorMat[i] = new unsigned int [Gdim_row];
// //}
// for(i = 0; i < Gdim_row; i++)
// FileStr >> InfoIndex[i];
// for(i = 0; i < Gdim_col; i++)
// FileStr >> ParityIndex[i];
// for(i = 0; i < Gdim_row; i++)
// {
// for(j = 0; j < Gdim_col; j++)
// {
// // this is the transposed version, i.e., the true parity part of a systematic generator matrix
// //FileStr >> GeneratorMat[i][j];
// ;
// }
// }
// }
// FileStr.close();
// }
//
// catch (ifstream::failure e) {
// cout << "Exception opening/reading file " << e.what() << endl;
// system("pause");
// exit(0);
// }
// catch (std::bad_alloc& ba)
// {
// std::cerr << "bad_alloc caught: " << ba.what() << '\n';
// exit(0);
// }
//
// cout << "encoder initialized" << endl;
//}
//
//// an overloaded function that outputs simple integer array for debugging
//int FP_Encoder::encode(char *in, int in_len)
//{
// int i, j, k, counter = 0;
// int out_len = 2209; // hard coded for now
// int num_blks;
// int char_size = sizeof(char)*8;
// //unsigned int *info_temp;
// //unsigned int *codeword;
// unsigned int temp;
// // ideally in_len is matching Gdim_col/8
// // if not matching just pad zero
// // output codeword bitwise stored in output
//
// // calculate the number of codeword blocks to output
// num_blks = in_len*char_size/InfoLen + (in_len*char_size % InfoLen > 0? 1:0);
//
// // already allocated in the top block. unsafe to allocate the memory in the function
// //out = new int[out_len*num_blks];
//
// //for(i = 0; i<num_blks; i++)
// //{
// // unload the information bits from the char array till the
// // second last element
// for(i = 0; i < in_len-1; i++)
// {
// for(j = 0; j < char_size; j++)// per byte
// {
// InfoBuffer[counter] = (in[i] >> j) & 1;
// counter ++;
// }
// }
// // unload the remaining bits from the last element
// for(j = 0; j < Gdim_row % char_size; j++)
// {
// InfoBuffer[counter] = (in[in_len-1] >> j) & 1;
// counter ++;
// }
// //}
// // NOTE for improvement: can ignore InfoBuffer and just use systematic codewords
// for(i = 0; i < Gdim_row; i++)
// {
// Codeword[InfoIndex[i]] = InfoBuffer[i];
// }
// //c = uG : each column is a parity bit.
// /*for(i = 0; i < Gdim_col; i++)
// {
// temp = 0;
// for(j = 0; j < Gdim_row; j++)
// {
// temp ^= (GeneratorMat[j][i] & InfoBuffer[j]);
// }
// Codeword[ParityIndex[i]] = temp;
// }*/
// for(i = 0; i < Gdim_col; i++)
// {
// temp = 0;
// for(j = 0; j < ChkDeg[i]; j++)
// {
// if(ColumnFlag[G_mlist[i][j]] == 0)
// {
// temp ^= Codeword[G_mlist[i][j]];
// //cout << G_mlist[i][j] << " ";
// }
// }
// Codeword[ParityIndex[i]] = temp;
// // self check on generator mat
// //temp = 0;
// //for(j = 0; j < ChkDeg[i]; j++)
// //{
// // temp ^= Codeword[G_mlist[i][j]];
// //}
// //cout << temp;
// }
// return out_len;
//}
//
//// not done yet
//int FP_Encoder::encode(char *in, char *out, int in_len)
//{
// int i, j, k, counter = 0;
// int num_blks;
// unsigned int *info_temp;
// //unsigned int *codeword;
// unsigned int temp;
// int out_len;
// // ideally in_len is matching Gdim_col/8
// // if not matching just pad zero
// // output codeword bitwise stored in output
// info_temp = new unsigned int [InfoLen];
//
//
// // calculate the number of codeword blocks to output
// num_blks = in_len*sizeof(char)/InfoLen + (in_len*sizeof(char) % InfoLen > 0? 1:0);
//
// // unload the information bits from the char array till the
// // second last element
// for(i = 0; i < in_len-1; i ++)
// {
// for(j = 0; j < 8; j++)// per byte
// {
// info_temp[counter] = (in[i] >> j) & 1;
// counter ++;
// }
// }
// // unload the remaining bits from the last element
// for(j = 0; j < InfoLen % 8; j++)
// {
// info_temp[counter] = (in[in_len-1] >> j) & 1;
// counter ++;
// }
//
//
// // temp test: 2209 /8 = 151
// // temp test residual: 1
// // zero padding = 7
// int pad_len = 8 - NUM_VAR %8;
// // should be improved to do xoring 32 bits at a time
// for(i = 0; i < Gdim_col; i++)
// {
// Codeword[InfoIndex[i]] = info_temp[i];
// }
// for(i = 0; i < Gdim_row; i++)
// {
// temp = 0;
// for(j = 0; j < Gdim_col; j++)
// {
// temp ^= (GeneratorMat[j][i]&info_temp[j]);
// }
// Codeword[ParityIndex[i]] = temp;
// }
// // allocate the memory in the function or not?
// // hard coded output length. Potentiall we should take more than one block
// // of input streams and coded into multiple codeword blocks
//
// // calculate how many char is needed to output the codeword
// out_len = num_blks*NUM_VAR/8 + (num_blks*NUM_VAR % 8 > 0? 1:0);
// out = new char[out_len];
// // stuff the codeword into an array of char
// for(i = 0; i < NUM_VAR; i++)
// {
// // increase the index by one every 8 bits (one byte)
// out[i/8] = out[i/8] + Codeword[i];
// // shift the result to the left one (stuffing in)
// out[i/8] = out[i/8]<< 1;
// }
// // returning the padding length (or should I return the codeword length?)
// return out_len;
//}
//---------------- Encoder part ends