Fixing up filters a bit to pass QA tests for all versions.
[debian/gnuradio] / gnuradio-core / src / lib / filter / qa_gri_fir_filter_with_buffer_fsf.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2010 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 <gr_types.h>
28 #include <qa_gri_fir_filter_with_buffer_fsf.h>
29 #include <gri_fir_filter_with_buffer_fsf.h>
30 #include <string.h>
31 #include <iostream>
32 #include <cmath>
33 #include <cppunit/TestAssert.h>
34 #include <random.h>
35 #include <malloc16.h>
36 #include <string.h>
37
38 typedef float   i_type;
39 typedef short   o_type;
40 typedef float   tap_type;
41 typedef float   acc_type;
42
43 using std::vector;
44
45 #define NELEM(x) (sizeof (x) / sizeof (x[0]))
46
47 static float
48 uniform ()
49 {
50   return 2.0 * ((float) random () / RANDOM_MAX - 0.5);  // uniformly (-1, 1)
51 }
52
53 static void
54 random_floats (float *buf, unsigned n)
55 {
56   for (unsigned i = 0; i < n; i++)
57     buf[i] = (float) rint (uniform () * 128);
58 }
59
60 static o_type
61 ref_dotprod (const i_type input[], const tap_type taps[], int ntaps)
62 {
63   acc_type      sum = 0;
64   for (int i = 0; i < ntaps; i++) {
65     sum += input[i] * taps[i];
66   }
67   return (o_type)sum;
68 }
69
70 //
71 // Test for ntaps in [0,9], and input lengths in [0,17].
72 // This ensures that we are building the shifted taps correctly,
73 // and exercises all corner cases on input alignment and length.
74 //
75
76 void
77 qa_gri_fir_filter_with_buffer_fsf::t1 ()  
78 {
79   const int     MAX_TAPS        = 9;
80   const int     OUTPUT_LEN      = 17;
81   const int     INPUT_LEN       = MAX_TAPS + OUTPUT_LEN;
82
83   // Mem aligned buffer not really necessary, but why not?
84   i_type       *input = (i_type *)malloc16Align(INPUT_LEN * sizeof(i_type));
85   i_type       *dline = (i_type*)malloc16Align(INPUT_LEN * sizeof(i_type));
86   o_type        expected_output[OUTPUT_LEN];
87   o_type        actual_output[OUTPUT_LEN];
88   tap_type      taps[MAX_TAPS];
89
90   srandom (0);  // we want reproducibility
91   memset(dline, 0, INPUT_LEN*sizeof(i_type));
92
93   for (int n = 0; n <= MAX_TAPS; n++){
94     for (int ol = 0; ol <= OUTPUT_LEN; ol++){
95
96       // cerr << "@@@ n:ol " << n << ":" << ol << endl;
97
98       // build random test case
99       random_floats (input, INPUT_LEN);
100       random_floats (taps, MAX_TAPS);
101
102       // compute expected output values
103       memset(dline, 0, INPUT_LEN*sizeof(i_type));
104       for (int o = 0; o < ol; o++){
105         // use an actual delay line for this test
106         for(int oo = INPUT_LEN-1; oo > 0; oo--)
107           dline[oo] = dline[oo-1];
108         dline[0] = input[o];
109         expected_output[o] = ref_dotprod (dline, taps, n);
110       }
111
112       // build filter
113       vector<tap_type> f1_taps(&taps[0], &taps[n]);
114       gri_fir_filter_with_buffer_fsf *f1 = new gri_fir_filter_with_buffer_fsf(f1_taps);
115
116       // zero the output, then do the filtering
117       memset (actual_output, 0, sizeof (actual_output));
118       f1->filterN (actual_output, input, ol);
119
120       // check results
121       for (int o = 0; o < ol; o++){
122         CPPUNIT_ASSERT_EQUAL(expected_output[o], actual_output[o]);
123       }
124       delete f1;
125     }
126   }
127   free16Align(input);
128 }