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.
27 #include <gri_fft_filter_fff_generic.h>
34 gri_fft_filter_fff_generic::gri_fft_filter_fff_generic (int decimation,
35 const std::vector<float> &taps)
36 : d_fftsize(-1), d_decimation(decimation), d_fwdfft(0), d_invfft(0)
41 gri_fft_filter_fff_generic::~gri_fft_filter_fff_generic ()
48 * determines d_ntaps, d_nsamples, d_fftsize, d_xformed_taps
51 gri_fft_filter_fff_generic::set_taps (const std::vector<float> &taps)
54 compute_sizes(taps.size());
56 d_tail.resize(tailsize());
57 for (i = 0; i < tailsize(); i++)
60 float *in = d_fwdfft->get_inbuf();
61 gr_complex *out = d_fwdfft->get_outbuf();
63 float scale = 1.0 / d_fftsize;
65 // Compute forward xform of taps.
66 // Copy taps into first ntaps slots, then pad with zeros
67 for (i = 0; i < d_ntaps; i++)
68 in[i] = taps[i] * scale;
70 for (; i < d_fftsize; i++)
73 d_fwdfft->execute(); // do the xform
75 // now copy output to d_xformed_taps
76 for (i = 0; i < d_fftsize/2+1; i++)
77 d_xformed_taps[i] = out[i];
82 // determine and set d_ntaps, d_nsamples, d_fftsize
85 gri_fft_filter_fff_generic::compute_sizes(int ntaps)
87 int old_fftsize = d_fftsize;
89 d_fftsize = (int) (2 * pow(2.0, ceil(log(ntaps) / log(2))));
90 d_nsamples = d_fftsize - d_ntaps + 1;
93 fprintf(stderr, "gri_fft_filter_fff_generic: ntaps = %d, fftsize = %d, nsamples = %d\n",
94 d_ntaps, d_fftsize, d_nsamples);
96 assert(d_fftsize == d_ntaps + d_nsamples -1 );
98 if (d_fftsize != old_fftsize){ // compute new plans
101 d_fwdfft = new gri_fft_real_fwd(d_fftsize);
102 d_invfft = new gri_fft_real_rev(d_fftsize);
103 d_xformed_taps.resize(d_fftsize/2+1);
108 gri_fft_filter_fff_generic::filter (int nitems, const float *input, float *output)
112 int ninput_items = nitems * d_decimation;
114 for (int i = 0; i < ninput_items; i += d_nsamples){
116 memcpy(d_fwdfft->get_inbuf(), &input[i], d_nsamples * sizeof(float));
118 for (j = d_nsamples; j < d_fftsize; j++)
119 d_fwdfft->get_inbuf()[j] = 0;
121 d_fwdfft->execute(); // compute fwd xform
123 gr_complex *a = d_fwdfft->get_outbuf();
124 gr_complex *b = &d_xformed_taps[0];
125 gr_complex *c = d_invfft->get_inbuf();
127 for (j = 0; j < d_fftsize/2+1; j++) { // filter in the freq domain
131 d_invfft->execute(); // compute inv xform
133 // add in the overlapping tail
135 for (j = 0; j < tailsize(); j++)
136 d_invfft->get_outbuf()[j] += d_tail[j];
138 // copy nsamples to output
140 //memcpy(out, d_invfft->get_outbuf(), d_nsamples * sizeof(float));
144 while (j < d_nsamples) {
145 *output++ = d_invfft->get_outbuf()[j];
148 dec_ctr = (j - d_nsamples);
151 memcpy(&d_tail[0], d_invfft->get_outbuf() + d_nsamples,
152 tailsize() * sizeof(float));
155 assert(dec_ctr == 0);