Merge commit 'v3.3.0' into upstream
[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 3, 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 #include <string.h>
33
34
35 static const int NTRIALS     =   50;
36 static const int MAXERRORS   =   10;
37 static const int NN          =  200;
38
39 static const int MAXDIBIT    = 3;
40
41 void
42 qa_atsci_fake_single_viterbi::encode_block (unsigned char *out, unsigned char *in, 
43                                       unsigned int n)
44 {
45   for (unsigned int i = 0; i < n; i++) {
46     out[i] = encoder.encode(in[i]);
47   }
48 }
49
50
51 void
52 qa_atsci_fake_single_viterbi::decode_block (unsigned char *out, unsigned char *in, 
53                                       unsigned int n)
54 {
55   for (unsigned int i = 0; i < n; i++) {
56     out[i] = decoder.decode ((2*in[i]-7) + noise ());
57   }
58 }
59
60 float
61 qa_atsci_fake_single_viterbi::noise ()
62 {
63 #if 1
64   return 2.0 * ((float) random () / RANDOM_MAX - 0.5);;
65 #else
66   return 0;
67 #endif
68 }
69
70 void
71 qa_atsci_fake_single_viterbi::t0 ()
72 {
73   int                     blocklen = NN;
74   unsigned char           in[blocklen];
75   unsigned char           enc[blocklen];
76   unsigned char           out[blocklen];
77   int                     decoder_errors = 0;
78   int                     delay = decoder.delay ();
79   int                     i;
80
81   // printf ("  Delay is %d.\n", delay);
82   
83   srandom (27);         // reproducable sequence of "random" values
84   
85   for (int nt = 0; nt < NTRIALS; nt++){
86
87     // load block with random data and encode
88
89     for (i = 0; i < (blocklen-delay); i++)
90       in[i] = random () & MAXDIBIT;
91     for (     ; i < blocklen; i++)
92       in[i] = 0;                /* To empty the delay buffers */
93
94     encoder.reset ();
95     encode_block (enc, in, blocklen);
96
97     decoder.reset ();
98
99     // decode the block
100     decode_block (out, enc, blocklen);
101
102     // int offset = delay/4;
103     int offset = 2;
104     bool differs = (memcmp (in+offset,
105                             out+delay+offset, blocklen-(delay+offset)));
106
107     // initial values after reset are 0
108     for (i = 0; i < delay; i++){
109       if (out[i] != 0)
110         printf ("  initial output at %i is %X, not 0\n",
111                 i, out[i]);
112     }
113
114     if (differs){
115       printf ("  incorrect data, trial #%d\n", nt);
116
117       printf ("\n  Erroneous result dibits:");
118       for (int erri = 0; erri < (NN-delay); erri++) {
119         if (in[erri] != out[erri+delay])
120           printf (" %d", erri);
121       }
122       printf ("\n  In:  ");
123       for (int erri = 0; erri < (NN-delay); erri++) {
124         printf (" %d", in[erri]);
125       }
126       printf ("\n  Out: ");
127       for (int erri = 0; erri < (NN-delay); erri++) {
128         printf (" %d", out[erri+delay]);
129       }
130       printf ("\n  Errs:");
131       for (int erri = 0; erri < (NN-delay); erri++) {
132         printf (" %c", (in[erri] != out[erri+delay])? '*': ' ');
133       }
134       printf ("\n    THIS IS A REAL PROBLEM.\n");
135       decoder_errors++;
136     }
137   }
138
139   printf ("  Summary: %d decoder errors out of %d trials.\n",
140           decoder_errors, NTRIALS);
141
142   CPPUNIT_ASSERT (decoder_errors == 0);
143 }
144