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