3 * Copyright 2005 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 2, 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 <gr_fft_filter_fff.h>
28 #include <gr_io_signature.h>
33 #include <gr_firdes.h>
37 gr_fft_filter_fff_sptr gr_make_fft_filter_fff (int decimation, const std::vector<float> &taps)
39 return gr_fft_filter_fff_sptr (new gr_fft_filter_fff (decimation, taps));
43 gr_fft_filter_fff::gr_fft_filter_fff (int decimation, const std::vector<float> &taps)
44 : gr_sync_decimator ("fft_filter_fff",
45 gr_make_io_signature (1, 1, sizeof (float)),
46 gr_make_io_signature (1, 1, sizeof (float)),
48 d_fftsize(-1), d_fwdfft(0), d_invfft(0), d_updated(false)
51 actual_set_taps(taps);
54 gr_fft_filter_fff::~gr_fft_filter_fff ()
61 print_vector_complex(const std::string label, const std::vector<gr_complex> &x)
64 for (unsigned i = 0; i < x.size(); i++)
65 std::cout << x[i] << " ";
70 print_vector_float(const std::string label, const std::vector<float> &x)
73 for (unsigned i = 0; i < x.size(); i++)
74 std::cout << x[i] << " ";
79 gr_fft_filter_fff::set_taps (const std::vector<float> &taps)
86 * determines d_ntaps, d_nsamples, d_fftsize, d_xformed_taps
89 gr_fft_filter_fff::actual_set_taps (const std::vector<float> &taps)
92 compute_sizes(taps.size());
94 d_tail.resize(tailsize());
95 for (i = 0; i < tailsize(); i++)
98 float *in = d_fwdfft->get_inbuf();
99 gr_complex *out = d_fwdfft->get_outbuf();
101 float scale = 1.0 / d_fftsize;
103 // Compute forward xform of taps.
104 // Copy taps into first ntaps slots, then pad with zeros
105 for (i = 0; i < d_ntaps; i++)
106 in[i] = taps[i] * scale;
108 for (; i < d_fftsize; i++)
111 d_fwdfft->execute(); // do the xform
113 // now copy output to d_xformed_taps
114 for (i = 0; i < d_fftsize/2+1; i++)
115 d_xformed_taps[i] = out[i];
117 //print_vector_complex("transformed taps:", d_xformed_taps);
120 // determine and set d_ntaps, d_nsamples, d_fftsize
123 gr_fft_filter_fff::compute_sizes(int ntaps)
125 int old_fftsize = d_fftsize;
127 d_fftsize = (int) (2 * pow(2.0, ceil(log(ntaps) / log(2))));
128 d_nsamples = d_fftsize - d_ntaps + 1;
131 fprintf(stderr, "gr_fft_filter: ntaps = %d, fftsize = %d, nsamples = %d\n",
132 d_ntaps, d_fftsize, d_nsamples);
134 assert(d_fftsize == d_ntaps + d_nsamples -1 );
136 if (d_fftsize != old_fftsize){ // compute new plans
139 d_fwdfft = new gri_fft_real_fwd(d_fftsize);
140 d_invfft = new gri_fft_real_rev(d_fftsize);
141 d_xformed_taps.resize(d_fftsize/2+1);
144 set_output_multiple(d_nsamples);
148 gr_fft_filter_fff::work (int noutput_items,
149 gr_vector_const_void_star &input_items,
150 gr_vector_void_star &output_items)
152 const float *in = (const float *) input_items[0];
153 float *out = (float *) output_items[0];
156 actual_set_taps(d_new_taps);
158 return 0; // output multiple may have changed
161 assert(noutput_items % d_nsamples == 0);
165 int ninput_items = noutput_items * decimation();
167 for (int i = 0; i < ninput_items; i += d_nsamples){
169 memcpy(d_fwdfft->get_inbuf(), &in[i], d_nsamples * sizeof(float));
171 for (j = d_nsamples; j < d_fftsize; j++)
172 d_fwdfft->get_inbuf()[j] = 0;
174 d_fwdfft->execute(); // compute fwd xform
176 gr_complex *a = d_fwdfft->get_outbuf();
177 gr_complex *b = &d_xformed_taps[0];
178 gr_complex *c = d_invfft->get_inbuf();
180 for (j = 0; j < d_fftsize/2+1; j++) // filter in the freq domain
183 d_invfft->execute(); // compute inv xform
185 // add in the overlapping tail
187 for (j = 0; j < tailsize(); j++)
188 d_invfft->get_outbuf()[j] += d_tail[j];
190 // copy nsamples to output
192 //memcpy(out, d_invfft->get_outbuf(), d_nsamples * sizeof(float));
196 while (j < d_nsamples) {
197 *out++ = d_invfft->get_outbuf()[j];
200 dec_ctr = (j - d_nsamples);
203 memcpy(&d_tail[0], d_invfft->get_outbuf() + d_nsamples,
204 tailsize() * sizeof(float));
207 assert((out - (float *) output_items[0]) == noutput_items);
208 assert(dec_ctr == 0);
210 return noutput_items;