3 * Copyright 2002 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
7 * GNU Radio is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3, or (at your option)
12 * GNU Radio is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Radio; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street,
20 * Boston, MA 02110-1301, USA.
27 #include <cppunit/TestAssert.h>
30 #include <atsci_single_viterbi.h>
31 #include <qa_atsci_single_viterbi.h>
35 static const int NTRIALS = 50;
36 static const int MAXERRORS = 10;
37 static const int NN = 200;
39 static const int MAXDIBIT = 3;
42 qa_atsci_single_viterbi::encode_block (unsigned char *out, unsigned char *in,
45 for (unsigned int i = 0; i < n; i++) {
46 out[i] = encoder.encode(in[i]);
52 qa_atsci_single_viterbi::decode_block (unsigned char *out, unsigned char *in,
53 unsigned int n, float noise_factor)
55 for (unsigned int i = 0; i < n; i++) {
56 out[i] = decoder.decode((2*in[i]-7) + noise () * noise_factor);
61 qa_atsci_single_viterbi::noise ()
63 return 2.0 * ((float) random () / RANDOM_MAX - 0.5); // uniformly (-1, 1)
67 qa_atsci_single_viterbi::t0 ()
70 unsigned char in[blocklen];
71 unsigned char enc[blocklen];
72 unsigned char out[blocklen];
73 int decoder_errors = 0;
74 int delay = decoder.delay ();
77 // printf (" Delay is %d.\n", delay);
79 srandom (27); // reproducable sequence of "random" values
81 for (int nt = 0; nt < NTRIALS; nt++){
83 // load block with random data and encode
85 for (i = 0; i < (blocklen-delay); i++)
86 in[i] = random () & MAXDIBIT;
87 for ( ; i < blocklen; i++)
88 in[i] = 0; /* To empty the delay buffers */
91 encode_block (enc, in, blocklen);
96 decode_block (out, enc, blocklen, 1.0);
98 // int offset = delay/4;
100 bool differs = (memcmp (in+offset,
101 out+delay+offset, blocklen-(delay+offset)));
103 // initial values after reset are 0
104 for (i = 0; i < delay; i++){
106 printf (" initial output at %i is %X, not 0\n",
111 printf (" incorrect data\n");
113 printf ("\n Erroneous result dibits:");
114 for (int erri = 0; erri < (NN-delay); erri++) {
115 if (in[erri] != out[erri+delay])
116 printf (" %d", erri);
119 for (int erri = 0; erri < (NN-delay); erri++) {
120 printf (" %d", in[erri]);
123 for (int erri = 0; erri < (NN-delay); erri++) {
124 printf (" %d", out[erri+delay]);
127 for (int erri = 0; erri < (NN-delay); erri++) {
128 printf (" %c", (in[erri] != out[erri+delay])? '*': ' ');
130 printf ("\n THIS IS A REAL PROBLEM.\n");
135 printf (" Summary: %d decoder errors out of %d trials.\n",
136 decoder_errors, NTRIALS);
138 CPPUNIT_ASSERT (decoder_errors == 0);
142 qa_atsci_single_viterbi::t1 ()
145 unsigned char in[blocklen];
146 unsigned char enc[blocklen];
147 unsigned char out[blocklen];
151 int decoder_errors = 0;
152 int delay = decoder.delay ();
155 // printf (" Delay is %d.\n", delay);
157 srandom (1); // reproducable sequence of "random" values
159 for (int nt = 0; nt < NTRIALS; nt++){
161 // test up to the error correction capacity of the code
162 for (int errors = 0; errors <= MAXERRORS; errors++){
164 // load block with random data and encode
166 for (i = 0; i < (blocklen-delay); i++)
167 in[i] = random () & MAXDIBIT;
168 for ( ; i < blocklen; i++)
169 in[i] = 0; /* To empty the delay buffers */
172 encode_block (enc, in, blocklen);
174 // Now generate 0 to N errors in the encoded symbols.
176 // If we restrict ourselves to damaging the low-order bit,
177 // our decoder finds and fixes the vast majority of errors.
179 // If we munge any or all of the three bits of the symbol,
180 // our decoder frequently gets the wrong data even with a single
183 // Let's see what it can do with just the two low-order bits.
185 // ALSO: Don't let any error be within 12 spots of another
186 // error. This simulates the muxed behavior.
188 memset (errlocs, 0, sizeof (errlocs));
190 for (int j = 0; j < errors; j++){
193 // errval = random () & 3; // FIXME: 1; // FIXME: MAXSYM;
194 errval = random () & 1; // FIXME: 1; // FIXME: MAXSYM;
195 } while (errval == 0); // error value must be non-zero
197 // Don't insert errors in the first delay slot, since we
198 // don't have valid history to correct them. Also, don't
199 // insert burst errors (adjacent errors), or errors within 2,
200 // since we can't reliably correct them. Also we must not choose
201 // the same location twice when inserting an error.
204 errloc = random () % NN;
205 } while (errloc < delay || errlocs[errloc] != 0
206 || (errloc > 0 && errlocs[errloc-1] != 0)
207 || (errloc > 1 && errlocs[errloc-2] != 0)
208 || (errloc < (NN-1) && errlocs[errloc+1] != 0)
209 || (errloc < (NN-2) && errlocs[errloc+2] != 0));
213 enc[errloc] ^= errval; // cause the error
216 // decode the errored block
218 decode_block (out, enc, blocklen, 0.5);
220 // int offset = delay/4;
222 bool differs = (memcmp (in+offset,
223 out+delay+offset, blocklen-(delay+offset)));
225 // initial values after reset are 0
226 for (i = 0; i < delay; i++){
228 printf (" initial output at %i is %X, not 0\n",
233 printf (" %2d errors introduced, %scorrect data\n",
234 errors, differs? "in": "");
236 // FIXME, should we be able to tell how many errs too?
238 const int ERRTOL = 12; /* Or relate to delay? */
242 printf ( " Inserted errors: ");
243 for (int erri = 0; erri < NN; erri++) {
245 printf (" %d", erri);
246 // if (erri < lasti+ERRTOL)
251 printf ("\n Erroneous result dibits:");
252 for (int erri = 0; erri < (NN-delay); erri++) {
253 if (in[erri] != out[erri+delay])
254 printf (" %d", erri);
257 for (int erri = 0; erri < (NN-delay); erri++) {
258 printf (" %d", in[erri]);
261 for (int erri = 0; erri < (NN-delay); erri++) {
262 printf (" %d", out[erri+delay]);
265 for (int erri = 0; erri < (NN-delay); erri++) {
266 printf (" %c", (in[erri] != out[erri+delay])? '*': ' ');
269 printf ("\n THIS IS A REAL PROBLEM.\n");
271 printf ("\n BUT THAT'S OK since errors are too close.\n");
279 printf (" Summary: %d decoder errors out of %d trials.\n",
280 decoder_errors, (MAXERRORS*NTRIALS));
282 CPPUNIT_ASSERT (decoder_errors <= (MAXERRORS*NTRIALS) * .1);