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
|
/* -*- c++ -*- */
/*
* Copyright 2002 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
* GNU Radio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* GNU Radio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Radio; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <cppunit/TestAssert.h>
#include <stdlib.h>
#include <stdio.h>
#include <atsci_single_viterbi.h>
#include <qa_atsci_single_viterbi.h>
#include <random.h>
#include <string.h>
static const int NTRIALS = 50;
static const int MAXERRORS = 10;
static const int NN = 200;
static const int MAXDIBIT = 3;
void
qa_atsci_single_viterbi::encode_block (unsigned char *out, unsigned char *in,
unsigned int n)
{
for (unsigned int i = 0; i < n; i++) {
out[i] = encoder.encode(in[i]);
}
}
void
qa_atsci_single_viterbi::decode_block (unsigned char *out, unsigned char *in,
unsigned int n, float noise_factor)
{
for (unsigned int i = 0; i < n; i++) {
out[i] = decoder.decode((2*in[i]-7) + noise () * noise_factor);
}
}
float
qa_atsci_single_viterbi::noise ()
{
return 2.0 * ((float) random () / RANDOM_MAX - 0.5); // uniformly (-1, 1)
}
void
qa_atsci_single_viterbi::t0 ()
{
static const int blocklen = NN;
unsigned char in[blocklen];
unsigned char enc[blocklen];
unsigned char out[blocklen];
int decoder_errors = 0;
int delay = decoder.delay ();
int i;
// printf (" Delay is %d.\n", delay);
srandom (27); // reproducable sequence of "random" values
for (int nt = 0; nt < NTRIALS; nt++){
// load block with random data and encode
for (i = 0; i < (blocklen-delay); i++)
in[i] = random () & MAXDIBIT;
for ( ; i < blocklen; i++)
in[i] = 0; /* To empty the delay buffers */
encoder.reset ();
encode_block (enc, in, blocklen);
decoder.reset ();
// decode the block
decode_block (out, enc, blocklen, 1.0);
// int offset = delay/4;
int offset = 2;
bool differs = (memcmp (in+offset,
out+delay+offset, blocklen-(delay+offset)));
// initial values after reset are 0
for (i = 0; i < delay; i++){
if (out[i] != 0)
printf (" initial output at %i is %X, not 0\n",
i, out[i]);
}
if (differs){
printf (" incorrect data\n");
printf ("\n Erroneous result dibits:");
for (int erri = 0; erri < (NN-delay); erri++) {
if (in[erri] != out[erri+delay])
printf (" %d", erri);
}
printf ("\n In: ");
for (int erri = 0; erri < (NN-delay); erri++) {
printf (" %d", in[erri]);
}
printf ("\n Out: ");
for (int erri = 0; erri < (NN-delay); erri++) {
printf (" %d", out[erri+delay]);
}
printf ("\n Errs:");
for (int erri = 0; erri < (NN-delay); erri++) {
printf (" %c", (in[erri] != out[erri+delay])? '*': ' ');
}
printf ("\n THIS IS A REAL PROBLEM.\n");
decoder_errors++;
}
}
printf (" Summary: %d decoder errors out of %d trials.\n",
decoder_errors, NTRIALS);
CPPUNIT_ASSERT (decoder_errors == 0);
}
void
qa_atsci_single_viterbi::t1 ()
{
static const int blocklen = NN;
unsigned char in[blocklen];
unsigned char enc[blocklen];
unsigned char out[blocklen];
int errlocs[NN];
int errval;
int errloc;
int decoder_errors = 0;
int delay = decoder.delay ();
int i;
// printf (" Delay is %d.\n", delay);
srandom (1); // reproducable sequence of "random" values
for (int nt = 0; nt < NTRIALS; nt++){
// test up to the error correction capacity of the code
for (int errors = 0; errors <= MAXERRORS; errors++){
// load block with random data and encode
for (i = 0; i < (blocklen-delay); i++)
in[i] = random () & MAXDIBIT;
for ( ; i < blocklen; i++)
in[i] = 0; /* To empty the delay buffers */
encoder.reset ();
encode_block (enc, in, blocklen);
// Now generate 0 to N errors in the encoded symbols.
//
// If we restrict ourselves to damaging the low-order bit,
// our decoder finds and fixes the vast majority of errors.
//
// If we munge any or all of the three bits of the symbol,
// our decoder frequently gets the wrong data even with a single
// error.
//
// Let's see what it can do with just the two low-order bits.
//
// ALSO: Don't let any error be within 12 spots of another
// error. This simulates the muxed behavior.
memset (errlocs, 0, sizeof (errlocs));
for (int j = 0; j < errors; j++){
do {
// errval = random () & 3; // FIXME: 1; // FIXME: MAXSYM;
errval = random () & 1; // FIXME: 1; // FIXME: MAXSYM;
} while (errval == 0); // error value must be non-zero
// Don't insert errors in the first delay slot, since we
// don't have valid history to correct them. Also, don't
// insert burst errors (adjacent errors), or errors within 2,
// since we can't reliably correct them. Also we must not choose
// the same location twice when inserting an error.
do {
errloc = random () % NN;
} while (errloc < delay || errlocs[errloc] != 0
|| (errloc > 0 && errlocs[errloc-1] != 0)
|| (errloc > 1 && errlocs[errloc-2] != 0)
|| (errloc < (NN-1) && errlocs[errloc+1] != 0)
|| (errloc < (NN-2) && errlocs[errloc+2] != 0));
errlocs[errloc] = 1;
enc[errloc] ^= errval; // cause the error
}
// decode the errored block
decoder.reset ();
decode_block (out, enc, blocklen, 0.5);
// int offset = delay/4;
int offset = 2;
bool differs = (memcmp (in+offset,
out+delay+offset, blocklen-(delay+offset)));
// initial values after reset are 0
for (i = 0; i < delay; i++){
if (out[i] != 0)
printf (" initial output at %i is %X, not 0\n",
i, out[i]);
}
if (differs){
printf (" %2d errors introduced, %scorrect data\n",
errors, differs? "in": "");
// FIXME, should we be able to tell how many errs too?
if (differs) {
//const int ERRTOL = 12; /* Or relate to delay? */
int shouldfix = 1;
//int lasti = -ERRTOL;
printf ( " Inserted errors: ");
for (int erri = 0; erri < NN; erri++) {
if (errlocs[erri]) {
printf (" %d", erri);
// if (erri < lasti+ERRTOL)
// shouldfix = 0;
//lasti = erri;
}
}
printf ("\n Erroneous result dibits:");
for (int erri = 0; erri < (NN-delay); erri++) {
if (in[erri] != out[erri+delay])
printf (" %d", erri);
}
printf ("\n In: ");
for (int erri = 0; erri < (NN-delay); erri++) {
printf (" %d", in[erri]);
}
printf ("\n Out: ");
for (int erri = 0; erri < (NN-delay); erri++) {
printf (" %d", out[erri+delay]);
}
printf ("\n Errs:");
for (int erri = 0; erri < (NN-delay); erri++) {
printf (" %c", (in[erri] != out[erri+delay])? '*': ' ');
}
if (shouldfix)
printf ("\n THIS IS A REAL PROBLEM.\n");
else
printf ("\n BUT THAT'S OK since errors are too close.\n");
if (shouldfix)
decoder_errors++;
}
}
}
}
printf (" Summary: %d decoder errors out of %d trials.\n",
decoder_errors, (MAXERRORS*NTRIALS));
CPPUNIT_ASSERT (decoder_errors <= (MAXERRORS*NTRIALS) * .1);
}
|