-
Notifications
You must be signed in to change notification settings - Fork 0
/
LDPC.cpp
486 lines (391 loc) · 12.7 KB
/
LDPC.cpp
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
/*
* LDPC.cpp
*
* Created on: 22 Dec, 2014
* Author: Mehrdad Tahernia
* User: mehrdad
*/
#include <cstdlib>
#include "Report.h"
#include "Channel.h"
#include "Encoding.h"
#include "Utils_2.h" // vector, array
#include "Variable_Node.h"
#include "Check_Node.h"
#include "Edge.h"
#include "LDPC.h"
/**************************************************************
*
* LDPC Constructors
*
**************************************************************/
void LDPC_Code::GetFromFile(std::ifstream &file) {
char dummy_buffer[10000];
int rhos_index, lambdas_index;
rhos_index = lambdas_index = 0;
bool GotMapInUse = false;
//----------------------------------------------
// Go over file
//----------------------------------------------
while (!file.eof()) {
switch (file.peek()) {
case 'r': // Read Rhos from file
if (rhos_index >= MAX_RHOS) {
cout << "LDPC_Code::GetFromFile: MAX_RHOS exceeded\n";
exit(1);
}
file >> rho_degs[rhos_index] >> rho_wts[rhos_index];
file.getline(dummy_buffer, sizeof(dummy_buffer)); // reach EoL
rhos_index++;
break;
case 'l': // Read Lambdas From File
if (lambdas_index >= MAX_LAMBDAS) {
cout << "LDPC_Code::GetFromFile: MAX_LAMBDAS exceeded\n";
exit(1);
}
file >> lambda_degs[lambdas_index] >> lambda_wts[lambdas_index];
lambdas_index++;
file.getline(dummy_buffer, sizeof(dummy_buffer)); // reach EoL
break;
case 'm': // Read mapping from file
GotMapInUse = true;
MapInUse.GetFromFile(file);
// Initialize GF(q)
GFq::Initialize(MapInUse.GetQ());
break;
default:
file.getline(dummy_buffer, sizeof(dummy_buffer)); // Skip line and go to beginning of the next line if nothing matches r l m
}
}
if (!GotMapInUse) {
cout << "mapping not defined\n";
exit(1);
}
rho_degs[rhos_index] = -1;
rho_wts[rhos_index] = -1;
lambda_degs[lambdas_index] = -1;
lambda_wts[lambdas_index] = -1;
}
// Main Constructor of LDPC_Code class
LDPC_Code::LDPC_Code(std::ifstream &File, int p_BlockLength, channel *p_Channel) :
BlockLength(p_BlockLength), Channel(p_Channel) {
GetFromFile(File);
}
/*************************************************************************
*
* Calc rate
*
*************************************************************************/
double LDPC_Code::sigma_lambda() {
double n;
n = 0;
for (int i = 0; lambda_degs[i] != -1; i++)
n += lambda_wts[i] / lambda_degs[i];
return n;
}
double LDPC_Code::sigma_rho() {
double m;
m = 0;
for (int i = 0; rho_degs[i] != -1; i++)
m += rho_wts[i] / rho_degs[i];
return m;
}
double LDPC_Code::Calc_Symbol_Rate() {
double SigmaLambda = sigma_lambda();
double SigmaRho = sigma_rho();
return 1 - SigmaRho / SigmaLambda;
}
/************************************************************************
*
* LDPC code
*
************************************************************************/
/**
* Initialize Variable nodes with channel output
*/
void LDPC_Code::Init_Messages(vector &ChannelOutput) {
if (ChannelOutput.GetSize() != Variables.GetLength()) {
cout << "LDPC_Code::Init_Messages: Incompatible sizes\n";
exit(1);
}
for (int i = 0; i < Variables.GetLength(); i++)
Variables[i].Initialize(*Channel, ChannelOutput[i]);
}
void LDPC_Code::GetZeroCodeword(vector &Codeword) {
Codeword.Allocate(Variables.GetLength());
for (int i = 0; i < Variables.GetLength(); i++)
Codeword[i] = Variables[i].GetZeroSignal();
}
void LDPC_Code::GetCodeword(vector &Codeword) {
Codeword.Allocate(Variables.GetLength());
for (int i = 0; i < Variables.GetLength(); i++)
Codeword[i] = Variables[i].GetSignal();
}
void LDPC_Code::Leftbound_Iteration() {
for (int i = 0; i < Checks.GetLength(); i++)
Checks[i].CalcAllLeftboundMessages();
}
void LDPC_Code::Rightbound_Iteration() {
for (int i = 0; i < Variables.GetLength(); i++)
Variables[i].CalcAllRightboundMessages();
}
void LDPC_Code::FinalIteration() {
for (int i = 0; i < Variables.GetLength(); i++)
Variables[i].CalcFinalMessage();
}
double LDPC_Code::Calc_Symbol_Error_Rate() {
double acc_correct = 0;
for (int i = 0; i < Variables.GetLength(); i++)
// acc_correct += Variables[i].FinalEstimate.Decision().IsZero();
acc_correct += Variables[i].DecSymbol == Variables[i].Symbol;
return 1.0 - acc_correct / (double) Variables.GetLength();
}
double LDPC_Code::Calc_Rightbound_Symbol_Error_Rate() {
double acc_correct = 0;
for (int i = 0; i < Graph.E; i++)
acc_correct += Graph.edges[i].RightBoundMessage.Decision().IsZero();
return 1.0 - acc_correct / Graph.E;
}
double LDPC_Code::Belief_Propagation_Decoder(int Count_Iterations) {
static char buffer[500];
double Func_RC = 0; //=0 is for initialization of the variable so the compiler won't give warnings
double LastMin = INF;
int CountIncreaseIterations = 0;
cout << "Initial symbol error rate = " << Calc_Symbol_Error_Rate() << "\n";
for (int i = 0; i < Count_Iterations; i++) {
Leftbound_Iteration();
Rightbound_Iteration();
// Func_RC = Calc_Rightbound_Symbol_Error_Rate(); //FIXME: Not modified for PNC yet
// sprintf(buffer, "%d: Rightbound SER = %1.10f, %s", i + 1, Func_RC,CharTime());
Func_RC = Calc_Symbol_Error_Rate();
// sprintf(buffer, "%d: SER = %1.10f, %s", i + 1, Func_RC,CharTime());
// cout << buffer;
cout << ".";
// Stop when Func_RC doesn't fall for some consecutive iterations
if (Func_RC < LastMin) {
LastMin = Func_RC;
CountIncreaseIterations = 0;
} else {
CountIncreaseIterations++;
if (CountIncreaseIterations > 50){
sprintf(buffer, "\n%d: SER = %1.10f, %s\n", i + 1, Func_RC,CharTime());
cout << buffer;
break;
}
}
if (Func_RC < 1e-7){
sprintf(buffer, "\n%d: SER = %1.10f, %s\n", i + 1, Func_RC,CharTime());
cout << buffer;
break;
}
}
return Func_RC;
}
/*********************************************************************************
*
* Encoder
*
*********************************************************************************/
void LDPC_Code::GenerateRandomSystematic() {
//------------------------------------------------------
// Randomly select values for systematic digits
//------------------------------------------------------
// for (int i = 0; i < Systematic; i++)
// cout << "Var Nodes: " << Variables.GetLength() << "\n";
for (int i = 0; i < Variables.GetLength(); i++)
Variables[i].Symbol.val = uniform_random(GFq::q);
}
void LDPC_Code::Get_Symbols(GFq *Symbols)
{
for (int i = 0; i < Variables.GetLength(); i++)
*(Symbols+i) = Variables[i].Symbol;
}
void LDPC_Code::Set_Symbols(GFq *Symbols)
{
for (int i = 0; i < Variables.GetLength(); i++)
Variables[i].Symbol = *(Symbols+i);
}
void LDPC_Code::Encode() {
int FirstVarOfTriangle = Systematic + Gap;
//------------------------------------------------------
// Determine gap symbols
//------------------------------------------------------
if (Gap > 0) {
// Multiply systematic vector multiplied by GapMatrix
matrix Vals(Gap); // a column vector for results
for (int i = 0; i < Gap; i++) {
Vals[i] = 0;
for (int j = 0; j < Systematic; j++)
Vals[i] += Variables[j].Symbol * GapMatrix.Element(i, j);
}
matrix GapVars(Gap);
GapVars = MinusPhiInverse * Vals;
// place vals
for (int i = 0; i < Gap; i++)
Variables[Systematic + i].Symbol = GapVars[i];
}
//------------------------------------------------------
// Determine all the rest
//------------------------------------------------------
for (int i = 0; i < Triangle; i++) {
// Calculate value of check node without symbol
Variables[FirstVarOfTriangle + i].Symbol = 0;
GFq label = Checks[i].Element(FirstVarOfTriangle + i);
Variables[FirstVarOfTriangle + i].Symbol = (Checks[i].Value() / label).Minus();
}
}
int LDPC_Code::GenerateEncoder_WithoutGap() {
//------------------------------------------------------
// Approximate lower triangularize
//------------------------------------------------------
// Init
Variables.Init(Graph.variable_nodes, Graph.N);
Checks.Init(Graph.check_nodes, Graph.M);
UrbankeAlgorithmAH(/* columns */Checks, /* rows */Variables);
// Reverse because we have made checks the columns and variables the rows
Variables.Reverse();
Checks.Reverse();
// Determine values
Systematic = Variables.GetLength() - Checks.GetLength();
Gap = Checks.Gap;
Triangle = Variables.GetLength() - Systematic - Gap;
int FirstCheckOfGap = Checks.GetLength() - Gap;
//------------------------------------------------------
// Eliminate gap check nodes
//------------------------------------------------------
if (Gap > MAX_GAP) {
cout << "GenerateEncoder_WithoutGap: Gap = " << Gap << " too big\n";
// exit(1);
return 1;
}
for (int i = FirstCheckOfGap; i < Checks.GetLength(); i++) {
Checks[i].Disconnect();
}
// Update variables
Systematic += Gap;
Gap = 0;
GapMatrix.deAllocate();
MinusPhiInverse.deAllocate();
return 0;
}
void LDPC_Code::GenerateEncoder() {
//------------------------------------------------------
// Approximate lower triangularize
//------------------------------------------------------
// Init
Variables.Init(Graph.variable_nodes, Graph.N);
Checks.Init(Graph.check_nodes, Graph.M);
UrbankeAlgorithmAH(/* columns */Checks, /* rows */Variables);
// Reverse because we have made checks the columns and variables the rows
Variables.Reverse();
Checks.Reverse();
// Determine values
Systematic = Variables.GetLength() - Checks.GetLength();
Gap = Checks.Gap;
Triangle = Variables.GetLength() - Systematic - Gap;
cout << "GenerateEncoder: Gap = " << Gap << "\n";
//ofstream bbbTemp("Temp.txt");
//for (int i = 0; i < Variables.GetLength(); i++)
// for (int j = 0; j < Variables[i].GetDegree(); j++)
// bbbTemp << Variables[i].AdjacentNode(j).GetID() << " " << i << "\n";
//bbbTemp.close();
//exit(0);
int FirstCheckOfGap = Checks.GetLength() - Gap;
int FirstVarOfTriangle = Systematic + Gap;
//------------------------------------------------------
// Handle gap
//------------------------------------------------------
// Init gap matrix
GapMatrix.Init(Gap, Variables.GetLength());
for (int i = 0; i < Gap; i++) {
//cout << FirstCheckOfGap + i << ">" << Checks[FirstCheckOfGap + i].GetID() << " " << Checks[FirstCheckOfGap + i].degree << "\n";
GapMatrix.Set(i, Checks[FirstCheckOfGap + i]);
}
//ofstream bbbTemp2("Temp.txt");
//GapMatrix.SparseOut(bbbTemp2);
//bbbTemp2.close();
//exit(0);
// Gaussian elimination
for (int j = Triangle - 1; j >= 0; j--) // column
{
// Find element appropriate matrix element
int EliminatedCol = FirstVarOfTriangle + j; // Column to be eliminated
check_node &CheckRow = Checks[j]; // j'th check should be nonzero at j'th column
GFq EliminatingRowVal = CheckRow.Element(EliminatedCol); // Value of row at column to be eliminated
for (int i = 0; i < GapMatrix.M; i++) // row of GapMatrix
{
GFq ValToBeEliminated = GapMatrix.Element(i, EliminatedCol);
if (!ValToBeEliminated.IsZero()) {
GFq Mult = (ValToBeEliminated / EliminatingRowVal).Minus();
GapMatrix.Add(i, CheckRow, Mult);
}
}
}
// obtain phi^-1
// While matrix is singular, switch columns
bool Success = false;
for (int Attempts = 0; Attempts < 10000; Attempts++) {
matrix phi = GapMatrix.Extract(Systematic, Gap); // Systematic = first col after systematic
MinusPhiInverse = phi.Inverse();
if (!MinusPhiInverse.IsNull()) // if success
{
MinusPhiInverse.MultiplyByMinusOne();
Success = true;
break;
} else { // switch columns randomly
int i1 = uniform_random(Systematic);
int i2 = uniform_random(Gap) + Systematic;
GapMatrix.SwitchColumns(i1, i2);
Variables.SwitchNodes(i1, i2);
}
}
if (!Success) {
cout
<< "Unable to find an invertible phi, maybe increase number of attempts\n";
exit(1);
}
}
double LDPC_Code::SumLambda() {
double sum = 0;
for (int i = 0; lambda_degs[i] != -1; i++)
sum += lambda_wts[i];
return sum;
}
double LDPC_Code::SumRho() {
double sum = 0;
for (int i = 0; rho_degs[i] != -1; i++)
sum += rho_wts[i];
return sum;
}
double LDPC_Code::Calc_Bit_Rate() {
return Calc_Symbol_Rate() * log((double) GFq::q) / log(2.);
}
void LDPC_Code::MakeLambdasValid() /// Make lambdas sum = 1
{
double sum = SumLambda();
for (int i = 0; lambda_degs[i] != -1; i++)
lambda_wts[i] /= sum;
}
void LDPC_Code::MakeRhosValid() /// Make rhos sum = 1
{
double sum = SumRho();
for (int i = 0; rho_degs[i] != -1; i++)
rho_wts[i] /= sum;
}
int LDPC_Code::CountLambdaDegs() {
int count;
for (count = 0; lambda_degs[count] != -1; count++)
;
return count;
}
int LDPC_Code::CountRhoDegs() {
int count;
for (count = 0; rho_degs[count] != -1; count++)
;
return count;
}
void LDPC_Code::ResetGraph() {
Graph.Reset(BlockLength, lambda_degs, lambda_wts, rho_degs, rho_wts, MapInUse);
Variables.Init(Graph.variable_nodes, Graph.N);
Checks.Init(Graph.check_nodes, Graph.M);
}