3 * Copyright 2010 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.
28 #include <qa_gri_fir_filter_with_buffer_fsf.h>
29 #include <gri_fir_filter_with_buffer_fsf.h>
33 #include <cppunit/TestAssert.h>
40 typedef float tap_type;
41 typedef float acc_type;
45 #define NELEM(x) (sizeof (x) / sizeof (x[0]))
50 return 2.0 * ((float) random () / RANDOM_MAX - 0.5); // uniformly (-1, 1)
54 random_floats (float *buf, unsigned n)
56 for (unsigned i = 0; i < n; i++)
57 buf[i] = (float) rint (uniform () * 128);
61 ref_dotprod (const i_type input[], const tap_type taps[], int ntaps)
64 for (int i = 0; i < ntaps; i++) {
65 sum += input[i] * taps[i];
71 qa_gri_fir_filter_with_buffer_fsf::t1 ()
77 qa_gri_fir_filter_with_buffer_fsf::t2 ()
83 qa_gri_fir_filter_with_buffer_fsf::t3 ()
89 // Test for ntaps in [0,9], and input lengths in [0,17].
90 // This ensures that we are building the shifted taps correctly,
91 // and exercises all corner cases on input alignment and length.
94 qa_gri_fir_filter_with_buffer_fsf::test_decimate (unsigned int decimate)
96 const int MAX_TAPS = 9;
97 const int OUTPUT_LEN = 17;
98 const int INPUT_LEN = MAX_TAPS + OUTPUT_LEN;
100 // Mem aligned buffer not really necessary, but why not?
101 i_type *input = (i_type *)malloc16Align(INPUT_LEN * sizeof(i_type));
102 i_type *dline = (i_type*)malloc16Align(INPUT_LEN * sizeof(i_type));
103 o_type expected_output[OUTPUT_LEN];
104 o_type actual_output[OUTPUT_LEN];
105 tap_type taps[MAX_TAPS];
107 srandom (0); // we want reproducibility
108 memset(dline, 0, INPUT_LEN*sizeof(i_type));
110 for (int n = 0; n <= MAX_TAPS; n++){
111 for (int ol = 0; ol <= OUTPUT_LEN; ol++){
113 // cerr << "@@@ n:ol " << n << ":" << ol << endl;
115 // build random test case
116 random_floats (input, INPUT_LEN);
117 random_floats (taps, MAX_TAPS);
119 // compute expected output values
120 memset(dline, 0, INPUT_LEN*sizeof(i_type));
121 for (int o = 0; o < (int)(ol/decimate); o++){
122 // use an actual delay line for this test
123 for(int dd = 0; dd < (int)decimate; dd++) {
124 for(int oo = INPUT_LEN-1; oo > 0; oo--)
125 dline[oo] = dline[oo-1];
126 dline[0] = input[decimate*o+dd];
128 expected_output[o] = ref_dotprod (dline, taps, n);
132 vector<tap_type> f1_taps(&taps[0], &taps[n]);
133 gri_fir_filter_with_buffer_fsf *f1 = new gri_fir_filter_with_buffer_fsf(f1_taps);
135 // zero the output, then do the filtering
136 memset (actual_output, 0, sizeof (actual_output));
137 f1->filterNdec (actual_output, input, ol/decimate, decimate);
140 for (int o = 0; o < (int)(ol/decimate); o++){
141 CPPUNIT_ASSERT_EQUAL(expected_output[o], actual_output[o]);