-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cpp
271 lines (228 loc) · 8.16 KB
/
test.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
#include "Matrix.h"
#include "LDPC.h"
#include "WeightDist.h"
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <math.h>
using namespace std;
WeightDist dist;
//////////////////////////////////////////////////////////////////////////
// Estimate the mean of errors at some EB/N0 point such that the standard
// deviation on the estimated error rate is no more than 10% of the
// estimate.
//
// Returns true if successful. Returns false if the condition is not
// met when the number of tests reaches the maximum desired.
//////////////////////////////////////////////////////////////////////////
bool testPoint(float EBOverN0, LDPC& ldpc, int maxTests,
int maxItersPerTest, double& errorRate, double& symErrorRate)
{
int numErrors = 0;
unsigned int bitTests = 0;
unsigned int messageBitErrors = 0;
// Set the energy ratio
ldpc.setEBOverN0(EBOverN0);
for (int testI = 1; testI < maxTests; testI++)
{
// Create some random message. Note that internally LDPC transposes
// the G matrix, so the message length is the width of G
vector<unsigned char> message;
for (int i = 0; i < ldpc.g.width; i++)
{
message.push_back(rand()%2);
}
// Encode the message
vector<unsigned char> codeBits = ldpc.encode(message);
// Store the weight information of the message
dist.addCodeword(codeBits);
// Transform to soft floating bits
vector<float> code = ldpc.floatTransform(codeBits);
// Add some noise
ldpc.addAWGN(code);
// Attempt to decode
vector<unsigned char> decoded;
if (ldpc.decode(code, decoded, maxItersPerTest))
{
// Check if the decoded codeword is the one sent on the channel
// and count the wrong bits in the message payload
bool matches = true;
for (int i = 0; i < message.size(); i++)
{
if (decoded[i] != message[i])
{
matches = false;
break;
}
}
if (!matches)
{
numErrors++;
// The distance vector is most likely a low weight codeword
vector<unsigned char> diff;
diff.resize(decoded.size());
int weight = 0;
for (int i = 0; i < decoded.size(); i++)
{
if (codeBits[i] != decoded[i])
{
weight++;
diff[i] = 1;
}
else
diff[i] = 0;
}
if (weight <= dist.currMinDist)
dist.addMinCodeword(diff, weight);
}
}
else
{
// Count a decoding failure the same as undetected error
// codeword
numErrors++;
}
// Count up the wrong bits in case the decoder decides to use the
// codeword regardless
bitTests += ldpc.g.width;
for (int i = 0; i < ldpc.g.width; i++)
{
if (decoded[i] != message[i])
messageBitErrors++;
}
// Make the estimates on the error rate and see if the termination
// condition succeeded
errorRate = ((double) numErrors) / testI;
symErrorRate = ((double) messageBitErrors) / bitTests;
if (testI % 10000 == 0)
cout << testI << " " << numErrors << " "
<< errorRate << " " << symErrorRate << " " << endl;
if (numErrors >= 100)
{
cout << "Ended in: " << testI << endl;
cerr << "DMin: " << dist.currMinDist
<< " K: " << dist.minCodes.size() << endl;
return true;
}
}
return false;
}
/////////////////////////////////////////////////////////////////////////////
// Output some plot points linearly on the EB/N0 scale to some output stream
/////////////////////////////////////////////////////////////////////////////
void testLinear(LDPC& ldpc, ostream& out, float min, float max, float step,
int maxTests, int maxIters)
{
double curr = min;
dist.init(ldpc.g.height);
while (curr < max)
{
double errorRate;
double symErrorRate;
bool success = testPoint(curr, ldpc, maxTests, maxIters,
errorRate, symErrorRate);
cout << curr << " " << errorRate << " " << symErrorRate << " ";
out << curr << " " << errorRate << " " << symErrorRate << " ";
if (!success)
{
cout << " FAILURE\n";
out << " FAILURE\n";
}
else
{
cout << endl;
out << endl;
}
curr += step;
// Dump some stats on the weight distribution
dist.dumpStats();
}
}
/////////////////////////////////////////////////////////////////////////////
// Output some plot points multiplicatively on the EB/N0 scale (such that in
// a log-log plot, the points are linearly sampled) to some output stream
//////////////////////////////////////////////////////////////////////////////
void testLogLinear(LDPC& ldpc, ostream& out, float min, float max, float step,
int maxTests, int maxIters)
{
double curr = min;
dist.init(ldpc.g.height);
while (curr < max)
{
double errorRate;
double symErrorRate;
bool success = testPoint(curr, ldpc, maxTests, maxIters,
errorRate, symErrorRate);
cout << curr << " " << errorRate << " " << symErrorRate << " ";
out << curr << " " << errorRate << " " << symErrorRate << " ";
if (!success)
{
cout << " FAILURE\n";
out << " FAILURE\n";
}
else
{
cout << endl;
out << endl;
}
curr *= step;
// Dump some stats on the weight distribution
dist.dumpStats();
}
}
int main(int argc, char ** argv)
{
if (argc != 10)
{
cout << "Usage: test gFilename hFilename out dist ...\n"
<< " min max mult maxIters maxIterDecode\n\n"
<< "\tGenerates a series of plot points of errors\n\n";
cout << "\thFilename, gFilename - the files to read the parity\n"
<< "\t check matrix and generator matrix\n"
<< "\t to respectively\n\n";
cout << "\tout - the file to write results to. The lines are:\n"
"\t EB/NO wordErrorRate symbolErrorRate\n";
cout << "\tdist - the file to write codeword distribution results to.\n";
cout << "\tmin, max - The minimum/maximum values of EB/NO to try\n";
cout << "\tmult - the amount to multiplicatively step. This makes\n"
"\t uniform step in a log scale plot\n\n";
cout << "\tmaxIters - the max number of iterations to do per\n"
"\t plotpoint\n";
cout << "\tmaxIterDecode - the max number of iterations to do per\n"
"\t decoding\n";
exit(0) ;
}
int seed = time(0);
srand(seed);
string gFilename = argv[1];
string hFilename = argv[2];
string outFilename = argv[3];
string distFilename = argv[4];
double min = atof(argv[5]);
double max = atof(argv[6]);
double mult = atof(argv[7]);
int maxIters = atoi(argv[8]);
int maxIterDecode = atoi(argv[9]);
cout << "Seed: " << seed << endl;
cout << "Loading G\n";
cout.flush();
ifstream gin(gFilename.c_str());
Matrix g;
g.load(gin);
gin.close();
cout << "Loading H\n";
cout.flush();
ifstream hin(hFilename.c_str());
Matrix h;
h.load(hin);
hin.close();
LDPC ldpc;
ldpc.setMatrices(g, h);
cout << "Starting\n";
ofstream out(outFilename.c_str());
ofstream statDump(distFilename.c_str());
dist.setDumpStream(statDump);
testLogLinear(ldpc, out, min, max, mult, maxIters, maxIterDecode);
statDump.close();
out.close();
}