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>
36 static const int NTRIALS = 50;
37 static const int MAXERRORS = 10;
38 static const int NN = 200;
40 static const int MAXDIBIT = 3;
43 qa_atsci_single_viterbi::encode_block (unsigned char *out, unsigned char *in,
46 for (unsigned int i = 0; i < n; i++) {
47 out[i] = encoder.encode(in[i]);
53 qa_atsci_single_viterbi::decode_block (unsigned char *out, unsigned char *in,
54 unsigned int n, float noise_factor)
56 for (unsigned int i = 0; i < n; i++) {
57 out[i] = decoder.decode((2*in[i]-7) + noise () * noise_factor);
62 qa_atsci_single_viterbi::noise ()
64 return 2.0 * ((float) random () / RANDOM_MAX - 0.5); // uniformly (-1, 1)
68 qa_atsci_single_viterbi::t0 ()
71 unsigned char in[blocklen];
72 unsigned char enc[blocklen];
73 unsigned char out[blocklen];
74 int decoder_errors = 0;
75 int delay = decoder.delay ();
78 // printf (" Delay is %d.\n", delay);
80 srandom (27); // reproducable sequence of "random" values
82 for (int nt = 0; nt < NTRIALS; nt++){
84 // load block with random data and encode
86 for (i = 0; i < (blocklen-delay); i++)
87 in[i] = random () & MAXDIBIT;
88 for ( ; i < blocklen; i++)
89 in[i] = 0; /* To empty the delay buffers */
92 encode_block (enc, in, blocklen);
97 decode_block (out, enc, blocklen, 1.0);
99 // int offset = delay/4;
101 bool differs = (memcmp (in+offset,
102 out+delay+offset, blocklen-(delay+offset)));
104 // initial values after reset are 0
105 for (i = 0; i < delay; i++){
107 printf (" initial output at %i is %X, not 0\n",
112 printf (" incorrect data\n");
114 printf ("\n Erroneous result dibits:");
115 for (int erri = 0; erri < (NN-delay); erri++) {
116 if (in[erri] != out[erri+delay])
117 printf (" %d", erri);
120 for (int erri = 0; erri < (NN-delay); erri++) {
121 printf (" %d", in[erri]);
124 for (int erri = 0; erri < (NN-delay); erri++) {
125 printf (" %d", out[erri+delay]);
128 for (int erri = 0; erri < (NN-delay); erri++) {
129 printf (" %c", (in[erri] != out[erri+delay])? '*': ' ');
131 printf ("\n THIS IS A REAL PROBLEM.\n");
136 printf (" Summary: %d decoder errors out of %d trials.\n",
137 decoder_errors, NTRIALS);
139 CPPUNIT_ASSERT (decoder_errors == 0);
143 qa_atsci_single_viterbi::t1 ()
146 unsigned char in[blocklen];
147 unsigned char enc[blocklen];
148 unsigned char out[blocklen];
152 int decoder_errors = 0;
153 int delay = decoder.delay ();
156 // printf (" Delay is %d.\n", delay);
158 srandom (1); // reproducable sequence of "random" values
160 for (int nt = 0; nt < NTRIALS; nt++){
162 // test up to the error correction capacity of the code
163 for (int errors = 0; errors <= MAXERRORS; errors++){
165 // load block with random data and encode
167 for (i = 0; i < (blocklen-delay); i++)
168 in[i] = random () & MAXDIBIT;
169 for ( ; i < blocklen; i++)
170 in[i] = 0; /* To empty the delay buffers */
173 encode_block (enc, in, blocklen);
175 // Now generate 0 to N errors in the encoded symbols.
177 // If we restrict ourselves to damaging the low-order bit,
178 // our decoder finds and fixes the vast majority of errors.
180 // If we munge any or all of the three bits of the symbol,
181 // our decoder frequently gets the wrong data even with a single
184 // Let's see what it can do with just the two low-order bits.
186 // ALSO: Don't let any error be within 12 spots of another
187 // error. This simulates the muxed behavior.
189 memset (errlocs, 0, sizeof (errlocs));
191 for (int j = 0; j < errors; j++){
194 // errval = random () & 3; // FIXME: 1; // FIXME: MAXSYM;
195 errval = random () & 1; // FIXME: 1; // FIXME: MAXSYM;
196 } while (errval == 0); // error value must be non-zero
198 // Don't insert errors in the first delay slot, since we
199 // don't have valid history to correct them. Also, don't
200 // insert burst errors (adjacent errors), or errors within 2,
201 // since we can't reliably correct them. Also we must not choose
202 // the same location twice when inserting an error.
205 errloc = random () % NN;
206 } while (errloc < delay || errlocs[errloc] != 0
207 || (errloc > 0 && errlocs[errloc-1] != 0)
208 || (errloc > 1 && errlocs[errloc-2] != 0)
209 || (errloc < (NN-1) && errlocs[errloc+1] != 0)
210 || (errloc < (NN-2) && errlocs[errloc+2] != 0));
214 enc[errloc] ^= errval; // cause the error
217 // decode the errored block
219 decode_block (out, enc, blocklen, 0.5);
221 // int offset = delay/4;
223 bool differs = (memcmp (in+offset,
224 out+delay+offset, blocklen-(delay+offset)));
226 // initial values after reset are 0
227 for (i = 0; i < delay; i++){
229 printf (" initial output at %i is %X, not 0\n",
234 printf (" %2d errors introduced, %scorrect data\n",
235 errors, differs? "in": "");
237 // FIXME, should we be able to tell how many errs too?
239 const int ERRTOL = 12; /* Or relate to delay? */
243 printf ( " Inserted errors: ");
244 for (int erri = 0; erri < NN; erri++) {
246 printf (" %d", erri);
247 // if (erri < lasti+ERRTOL)
252 printf ("\n Erroneous result dibits:");
253 for (int erri = 0; erri < (NN-delay); erri++) {
254 if (in[erri] != out[erri+delay])
255 printf (" %d", erri);
258 for (int erri = 0; erri < (NN-delay); erri++) {
259 printf (" %d", in[erri]);
262 for (int erri = 0; erri < (NN-delay); erri++) {
263 printf (" %d", out[erri+delay]);
266 for (int erri = 0; erri < (NN-delay); erri++) {
267 printf (" %c", (in[erri] != out[erri+delay])? '*': ' ');
270 printf ("\n THIS IS A REAL PROBLEM.\n");
272 printf ("\n BUT THAT'S OK since errors are too close.\n");
280 printf (" Summary: %d decoder errors out of %d trials.\n",
281 decoder_errors, (MAXERRORS*NTRIALS));
283 CPPUNIT_ASSERT (decoder_errors <= (MAXERRORS*NTRIALS) * .1);