Updated FSF address in all files. Fixes ticket:51
[debian/gnuradio] / gr-atsc / src / lib / qa_atsci_fake_single_viterbi.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2002 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
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 2, or (at your option)
10  * any later version.
11  * 
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.
16  * 
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.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <cppunit/TestAssert.h>
28 #include <stdio.h>
29 #include <atsci_fake_single_viterbi.h>
30 #include <qa_atsci_fake_single_viterbi.h>
31 #include <random.h>
32
33
34 static const int NTRIALS     =   50;
35 static const int MAXERRORS   =   10;
36 static const int NN          =  200;
37
38 static const int MAXDIBIT    = 3;
39
40 void
41 qa_atsci_fake_single_viterbi::encode_block (unsigned char *out, unsigned char *in, 
42                                       unsigned int n)
43 {
44   for (unsigned int i = 0; i < n; i++) {
45     out[i] = encoder.encode(in[i]);
46   }
47 }
48
49
50 void
51 qa_atsci_fake_single_viterbi::decode_block (unsigned char *out, unsigned char *in, 
52                                       unsigned int n)
53 {
54   for (unsigned int i = 0; i < n; i++) {
55     out[i] = decoder.decode ((2*in[i]-7) + noise ());
56   }
57 }
58
59 float
60 qa_atsci_fake_single_viterbi::noise ()
61 {
62 #if 1
63   return 2.0 * ((float) random () / RANDOM_MAX - 0.5);;
64 #else
65   return 0;
66 #endif
67 }
68
69 void
70 qa_atsci_fake_single_viterbi::t0 ()
71 {
72   int                     blocklen = NN;
73   unsigned char           in[blocklen];
74   unsigned char           enc[blocklen];
75   unsigned char           out[blocklen];
76   int                     decoder_errors = 0;
77   int                     delay = decoder.delay ();
78   int                     i;
79
80   // printf ("  Delay is %d.\n", delay);
81   
82   srandom (27);         // reproducable sequence of "random" values
83   
84   for (int nt = 0; nt < NTRIALS; nt++){
85
86     // load block with random data and encode
87
88     for (i = 0; i < (blocklen-delay); i++)
89       in[i] = random () & MAXDIBIT;
90     for (     ; i < blocklen; i++)
91       in[i] = 0;                /* To empty the delay buffers */
92
93     encoder.reset ();
94     encode_block (enc, in, blocklen);
95
96     decoder.reset ();
97
98     // decode the block
99     decode_block (out, enc, blocklen);
100
101     // int offset = delay/4;
102     int offset = 2;
103     bool differs = (memcmp (in+offset,
104                             out+delay+offset, blocklen-(delay+offset)));
105
106     // initial values after reset are 0
107     for (i = 0; i < delay; i++){
108       if (out[i] != 0)
109         printf ("  initial output at %i is %X, not 0\n",
110                 i, out[i]);
111     }
112
113     if (differs){
114       printf ("  incorrect data, trial #%d\n", nt);
115
116       printf ("\n  Erroneous result dibits:");
117       for (int erri = 0; erri < (NN-delay); erri++) {
118         if (in[erri] != out[erri+delay])
119           printf (" %d", erri);
120       }
121       printf ("\n  In:  ");
122       for (int erri = 0; erri < (NN-delay); erri++) {
123         printf (" %d", in[erri]);
124       }
125       printf ("\n  Out: ");
126       for (int erri = 0; erri < (NN-delay); erri++) {
127         printf (" %d", out[erri+delay]);
128       }
129       printf ("\n  Errs:");
130       for (int erri = 0; erri < (NN-delay); erri++) {
131         printf (" %c", (in[erri] != out[erri+delay])? '*': ' ');
132       }
133       printf ("\n    THIS IS A REAL PROBLEM.\n");
134       decoder_errors++;
135     }
136   }
137
138   printf ("  Summary: %d decoder errors out of %d trials.\n",
139           decoder_errors, NTRIALS);
140
141   CPPUNIT_ASSERT (decoder_errors == 0);
142 }
143