Merge commit 'v3.3.0' into upstream
[debian/gnuradio] / gnuradio-core / src / lib / filter / gri_fft_filter_ccc_generic.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 <gri_fft_filter_ccc_generic.h>
28 #include <gri_fft.h>
29 #include <assert.h>
30 #include <stdexcept>
31 #include <cstdio>
32 #include <cstring>
33 #include <fftw3.h>
34
35 gri_fft_filter_ccc_generic::gri_fft_filter_ccc_generic (int decimation, 
36                                                         const std::vector<gr_complex> &taps)
37   : d_fftsize(-1), d_decimation(decimation), d_fwdfft(0), d_invfft(0)
38 {
39   set_taps(taps);
40 }
41
42 gri_fft_filter_ccc_generic::~gri_fft_filter_ccc_generic ()
43 {
44   delete d_fwdfft;
45   delete d_invfft;
46 }
47
48 #if 0
49 static void 
50 print_vector_complex(const std::string label, const std::vector<gr_complex> &x)
51 {
52   std::cout << label;
53   for (unsigned i = 0; i < x.size(); i++)
54     std::cout << x[i] << " ";
55   std::cout << "\n";
56 }
57 #endif
58
59
60 /*
61  * determines d_ntaps, d_nsamples, d_fftsize, d_xformed_taps
62  */
63 int
64 gri_fft_filter_ccc_generic::set_taps (const std::vector<gr_complex> &taps)
65 {
66   int i = 0;
67   compute_sizes(taps.size());
68
69   d_tail.resize(tailsize());
70   for (i = 0; i < tailsize(); i++)
71     d_tail[i] = 0;
72
73   gr_complex *in = d_fwdfft->get_inbuf();
74   gr_complex *out = d_fwdfft->get_outbuf();
75
76   float scale = 1.0 / d_fftsize;
77   
78   // Compute forward xform of taps.
79   // Copy taps into first ntaps slots, then pad with zeros
80   for (i = 0; i < d_ntaps; i++)
81     in[i] = taps[i] * scale;
82
83   for (; i < d_fftsize; i++)
84     in[i] = 0;
85
86   d_fwdfft->execute();          // do the xform
87
88   // now copy output to d_xformed_taps
89   for (i = 0; i < d_fftsize; i++)
90     d_xformed_taps[i] = out[i];
91   
92   return d_nsamples;
93 }
94
95 // determine and set d_ntaps, d_nsamples, d_fftsize
96
97 void
98 gri_fft_filter_ccc_generic::compute_sizes(int ntaps)
99 {
100   int old_fftsize = d_fftsize;
101   d_ntaps = ntaps;
102   d_fftsize = (int) (2 * pow(2.0, ceil(log(ntaps) / log(2))));
103   d_nsamples = d_fftsize - d_ntaps + 1;
104
105   if (0)
106     fprintf(stderr, "gri_fft_filter_ccc_generic: ntaps = %d, fftsize = %d, nsamples = %d\n",
107             d_ntaps, d_fftsize, d_nsamples);
108
109   assert(d_fftsize == d_ntaps + d_nsamples -1 );
110
111   if (d_fftsize != old_fftsize){        // compute new plans
112     delete d_fwdfft;
113     delete d_invfft;
114     d_fwdfft = new gri_fft_complex(d_fftsize, true);
115     d_invfft = new gri_fft_complex(d_fftsize, false);
116     d_xformed_taps.resize(d_fftsize);
117   }
118 }
119
120 int
121 gri_fft_filter_ccc_generic::filter (int nitems, const gr_complex *input, gr_complex *output)
122 {
123   int dec_ctr = 0;
124   int j = 0;
125   int ninput_items = nitems * d_decimation;
126
127   for (int i = 0; i < ninput_items; i += d_nsamples){
128     
129     memcpy(d_fwdfft->get_inbuf(), &input[i], d_nsamples * sizeof(gr_complex));
130
131     for (j = d_nsamples; j < d_fftsize; j++)
132       d_fwdfft->get_inbuf()[j] = 0;
133
134     d_fwdfft->execute();        // compute fwd xform
135     
136     gr_complex *a = d_fwdfft->get_outbuf();
137     gr_complex *b = &d_xformed_taps[0];
138     gr_complex *c = d_invfft->get_inbuf();
139
140     for (j = 0; j < d_fftsize; j+=1) {  // filter in the freq domain
141       c[j] = a[j] * b[j];
142     } 
143     
144     d_invfft->execute();        // compute inv xform
145
146     // add in the overlapping tail
147
148     for (j = 0; j < tailsize(); j++)
149       d_invfft->get_outbuf()[j] += d_tail[j];
150
151     // copy nsamples to output
152     j = dec_ctr;
153     while (j < d_nsamples) {
154       *output++ = d_invfft->get_outbuf()[j];
155       j += d_decimation;
156     }
157     dec_ctr = (j - d_nsamples);
158
159     // stash the tail
160     memcpy(&d_tail[0], d_invfft->get_outbuf() + d_nsamples,
161            tailsize() * sizeof(gr_complex));
162   }
163
164   assert(dec_ctr == 0);
165
166   return nitems;
167 }