Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-core / src / lib / filter / gr_fft_filter_fff.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2005 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 2, 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 <gr_fft_filter_fff.h>
28 #include <gr_io_signature.h>
29 #include <gri_fft.h>
30 #include <math.h>
31 #include <assert.h>
32 #include <stdexcept>
33 #include <gr_firdes.h>
34
35 #include <iostream>
36
37 gr_fft_filter_fff_sptr gr_make_fft_filter_fff (int decimation, const std::vector<float> &taps)
38 {
39   return gr_fft_filter_fff_sptr (new gr_fft_filter_fff (decimation, taps));
40 }
41
42
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)),
47                        decimation),
48     d_fftsize(-1), d_fwdfft(0), d_invfft(0), d_updated(false)
49 {
50   set_history(1);
51   actual_set_taps(taps);
52 }
53
54 gr_fft_filter_fff::~gr_fft_filter_fff ()
55 {
56   delete d_fwdfft;
57   delete d_invfft;
58 }
59
60 static void 
61 print_vector_complex(const std::string label, const std::vector<gr_complex> &x)
62 {
63   std::cout << label;
64   for (unsigned i = 0; i < x.size(); i++)
65     std::cout << x[i] << " ";
66   std::cout << "\n";
67 }
68
69 static void 
70 print_vector_float(const std::string label, const std::vector<float> &x)
71 {
72   std::cout << label;
73   for (unsigned i = 0; i < x.size(); i++)
74     std::cout << x[i] << " ";
75   std::cout << "\n";
76 }
77
78 void
79 gr_fft_filter_fff::set_taps (const std::vector<float> &taps)
80 {
81   d_new_taps = taps;
82   d_updated = true;
83 }
84
85 /*
86  * determines d_ntaps, d_nsamples, d_fftsize, d_xformed_taps
87  */
88 void
89 gr_fft_filter_fff::actual_set_taps (const std::vector<float> &taps)
90 {
91   int i = 0;
92   compute_sizes(taps.size());
93
94   d_tail.resize(tailsize());
95   for (i = 0; i < tailsize(); i++)
96     d_tail[i] = 0;
97
98   float *in = d_fwdfft->get_inbuf();
99   gr_complex *out = d_fwdfft->get_outbuf();
100
101   float scale = 1.0 / d_fftsize;
102   
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;
107
108   for (; i < d_fftsize; i++)
109     in[i] = 0;
110
111   d_fwdfft->execute();          // do the xform
112
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];
116
117   //print_vector_complex("transformed taps:", d_xformed_taps);
118 }
119
120 // determine and set d_ntaps, d_nsamples, d_fftsize
121
122 void
123 gr_fft_filter_fff::compute_sizes(int ntaps)
124 {
125   int old_fftsize = d_fftsize;
126   d_ntaps = ntaps;
127   d_fftsize = (int) (2 * pow(2.0, ceil(log(ntaps) / log(2))));
128   d_nsamples = d_fftsize - d_ntaps + 1;
129
130   if (0)
131     fprintf(stderr, "gr_fft_filter: ntaps = %d, fftsize = %d, nsamples = %d\n",
132             d_ntaps, d_fftsize, d_nsamples);
133
134   assert(d_fftsize == d_ntaps + d_nsamples -1 );
135
136   if (d_fftsize != old_fftsize){        // compute new plans
137     delete d_fwdfft;
138     delete d_invfft;
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);
142   }
143
144   set_output_multiple(d_nsamples);
145 }
146
147 int
148 gr_fft_filter_fff::work (int noutput_items,
149                          gr_vector_const_void_star &input_items,
150                          gr_vector_void_star &output_items)
151 {
152   const float *in = (const float *) input_items[0];
153   float *out = (float *) output_items[0];
154
155   if (d_updated){
156     actual_set_taps(d_new_taps);
157     d_updated = false;
158     return 0;                           // output multiple may have changed
159   }
160
161   assert(noutput_items % d_nsamples == 0);
162
163   int dec_ctr = 0;
164   int j = 0;
165   int ninput_items = noutput_items * decimation();
166
167   for (int i = 0; i < ninput_items; i += d_nsamples){
168     
169     memcpy(d_fwdfft->get_inbuf(), &in[i], d_nsamples * sizeof(float));
170
171     for (j = d_nsamples; j < d_fftsize; j++)
172       d_fwdfft->get_inbuf()[j] = 0;
173
174     d_fwdfft->execute();        // compute fwd xform
175
176     gr_complex *a = d_fwdfft->get_outbuf();
177     gr_complex *b = &d_xformed_taps[0];
178     gr_complex *c = d_invfft->get_inbuf();
179
180     for (j = 0; j < d_fftsize/2+1; j++) // filter in the freq domain
181       c[j] = a[j] * b[j];
182     
183     d_invfft->execute();        // compute inv xform
184
185     // add in the overlapping tail
186
187     for (j = 0; j < tailsize(); j++)
188       d_invfft->get_outbuf()[j] += d_tail[j];
189
190     // copy nsamples to output
191
192     //memcpy(out, d_invfft->get_outbuf(), d_nsamples * sizeof(float));
193     //out += d_nsamples;
194
195     j = dec_ctr;
196     while (j < d_nsamples) {
197       *out++ = d_invfft->get_outbuf()[j];
198       j += decimation();
199     }
200     dec_ctr = (j - d_nsamples);
201
202     // stash the tail
203     memcpy(&d_tail[0], d_invfft->get_outbuf() + d_nsamples,
204            tailsize() * sizeof(float));
205   }
206
207   assert((out - (float *) output_items[0]) == noutput_items);
208   assert(dec_ctr == 0);
209
210   return noutput_items;
211 }