Updated license from GPL version 2 or later to GPL version 3 or later.
[debian/gnuradio] / gr-atsc / src / lib / qa_atsci_equalizer_nop.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 #include <qa_atsci_equalizer_nop.h>
24 #include <atsci_equalizer.h>
25 #include <atsci_equalizer_nop.h>
26 #include <atsci_pnXXX.h>
27 #include <atsc_types.h>
28 #include <cppunit/TestAssert.h>
29 #include <assert.h>
30 #include <iostream>
31
32 using std::cerr;
33 using std::endl;
34
35
36 static const int SYMBOLS_PER_FIELD = (ATSC_DSEGS_PER_FIELD + 1) * ATSC_DATA_SEGMENT_LENGTH;
37 static const int PAD = 500;
38 static const int NFIELDS = 2;
39
40 static const int INPUT_SIZE = PAD + (NFIELDS * SYMBOLS_PER_FIELD);
41
42 static float
43 bin_map (int bit)
44 {
45   return bit ? +5 : -5;
46 }
47
48 static void
49 init_tags (atsc::syminfo *tags, int segnum, int fieldnum)
50 {
51   int   i;
52
53   for (i = 0; i < ATSC_DATA_SEGMENT_LENGTH; i++){
54     tags[i].symbol_num = i;
55     tags[i].segment_num = segnum;
56     tags[i].field_num = fieldnum;
57     tags[i].valid = 1;
58   }
59 }
60
61 static void
62 init_data_seg (float *p, atsc::syminfo *tags, int v, int segnum, int fieldnum)
63 {
64   init_tags (tags, segnum, fieldnum);
65
66   int   i = 0;
67
68   p[i++] = bin_map (1);                 // data segment sync pulse
69   p[i++] = bin_map (0);
70   p[i++] = bin_map (0);
71   p[i++] = bin_map (1);
72
73   for (; i < ATSC_DATA_SEGMENT_LENGTH; i++)
74     p[i] = (float) (i + v);
75 }
76
77 static void
78 init_field_sync_tags (atsc::syminfo *tags, int fieldnum)
79 {
80   init_tags (tags, atsc::SI_FIELD_SYNC_SEGMENT_NUM, fieldnum);
81 }
82
83 static void
84 init_field_sync_common (float *p, int mask)
85                         
86 {
87   int  i = 0;
88
89   p[i++] = bin_map (1);                 // data segment sync pulse
90   p[i++] = bin_map (0);
91   p[i++] = bin_map (0);
92   p[i++] = bin_map (1);
93
94   for (int j = 0; j < 511; j++)         // PN511
95     p[i++] = bin_map (atsc_pn511[j]);
96
97   for (int j = 0; j < 63; j++)          // PN63
98     p[i++] = bin_map (atsc_pn63[j]);
99
100   for (int j = 0; j < 63; j++)          // PN63, toggled on field 2
101     p[i++] = bin_map (atsc_pn63[j] ^ mask);
102   
103   for (int j = 0; j < 63; j++)          // PN63
104     p[i++] = bin_map (atsc_pn63[j]);
105
106   p[i++] = bin_map (0);                 // 24 bits of VSB8 mode identifiera
107   p[i++] = bin_map (0);
108   p[i++] = bin_map (0);
109   p[i++] = bin_map (0);
110
111   p[i++] = bin_map (1);
112   p[i++] = bin_map (0);
113   p[i++] = bin_map (1);
114   p[i++] = bin_map (0);
115
116   p[i++] = bin_map (0);
117   p[i++] = bin_map (1);
118   p[i++] = bin_map (0);
119   p[i++] = bin_map (1);
120
121   p[i++] = bin_map (1);
122   p[i++] = bin_map (1);
123   p[i++] = bin_map (1);
124   p[i++] = bin_map (1);
125
126   p[i++] = bin_map (0);
127   p[i++] = bin_map (1);
128   p[i++] = bin_map (0);
129   p[i++] = bin_map (1);
130
131   p[i++] = bin_map (1);
132   p[i++] = bin_map (0);
133   p[i++] = bin_map (1);
134   p[i++] = bin_map (0);
135
136
137   for (int j = 0; j < 92; j++)          // 92 more bits
138     p[i++] = bin_map (atsc_pn63[j % 63]);
139
140   // now copy the last 12 symbols of the previous segment
141   // bogus fill
142
143   for (int j = 0; j < 12; j++)
144     p[i++] = bin_map (j & 1);
145
146   assert (i == ATSC_DATA_SEGMENT_LENGTH);
147 }
148
149 void
150 qa_atsci_equalizer_nop::setUp ()
151 {
152   eq = new atsci_equalizer_nop ();
153 }
154
155 void
156 qa_atsci_equalizer_nop::tearDown ()
157 {
158   delete eq;
159   eq = 0;
160 }
161
162 static void
163 setup_test_data (float *data, atsc::syminfo *tags)
164 {
165   int   mask = 0;
166   int   i = 0;
167
168   for (i = 0; i < PAD; i++){
169     data[i] = (float) i;
170     tags[i].symbol_num = 13 + i;
171     tags[i].segment_num = 17;
172     tags[i].field_num = 0;
173     tags[i].valid = 1;
174   }
175
176   for (int nfields = 0; nfields < NFIELDS; nfields++){
177     init_field_sync_common (&data[i], mask);
178     init_field_sync_tags (&tags[i], mask);
179     i += ATSC_DATA_SEGMENT_LENGTH;
180
181     for (int segnum = 0; segnum < 312; segnum++){
182       init_data_seg (&data[i], &tags[i], i, segnum, mask);
183       i += ATSC_DATA_SEGMENT_LENGTH;
184     }
185     mask ^= 1;
186   }
187   assert (i == INPUT_SIZE);
188 }
189
190 void
191 qa_atsci_equalizer_nop::t0 ()
192 {
193   // these are dynamically allocated because they are bigger
194   // than the default stack limit.
195
196   float         *input_data = new float[INPUT_SIZE];
197   atsc::syminfo *input_tags = new atsc::syminfo[INPUT_SIZE];
198   float         *output_data = new float[INPUT_SIZE];
199
200   try {
201
202     memset (input_data, 0, sizeof (input_data));
203     memset (input_tags, 0, sizeof (input_tags));
204     memset (output_data, 0, sizeof (output_data));
205
206     setup_test_data (input_data, input_tags);
207
208     eq->filter (input_data, input_tags,
209                 output_data, INPUT_SIZE);
210
211
212     // now check that data values got copied correctly
213
214     for (int i = 0; i < INPUT_SIZE; i++){
215
216       if (input_tags[i].segment_num == atsc::SI_FIELD_SYNC_SEGMENT_NUM){
217         // ignore entire field sync data segment
218       }
219       else if (input_tags[i].symbol_num <= 3){
220         // ignore 4 symbols of data segment sync (+5, -5, -5, +5)
221       }
222       else {
223         if (output_data[i] != (float) i){
224           cerr << "output_data[" << i << "] == " << output_data[i] << endl;
225           CPPUNIT_ASSERT_DOUBLES_EQUAL ((float) i, output_data[i], 1e-6);
226         }
227       }
228     }
229
230     delete [] input_data;
231     delete [] input_tags;
232     delete [] output_data;
233   }
234
235   catch ( ... ){
236     delete [] input_data;
237     delete [] input_tags;
238     delete [] output_data;
239   }
240 }
241
242 void
243 qa_atsci_equalizer_nop::t1 ()
244 {
245   // think of another test...
246 }