Fix compiler warnings across the tree. Adds --enable-warnings-as-errors configure...
[debian/gnuradio] / gnuradio-core / src / lib / filter / gr_fft_filter_ccc.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 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 /*
24  * WARNING: This file is automatically generated by generate_gr_fft_filter_XXX.py
25  * Any changes made to this file will be overwritten.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <gr_fft_filter_ccc.h>
33 #include <gr_io_signature.h>
34 #include <gri_fft.h>
35 #include <math.h>
36 #include <assert.h>
37 #include <stdexcept>
38 #include <gr_firdes.h>
39
40 #include <iostream>
41 #include <string.h>
42
43 gr_fft_filter_ccc_sptr gr_make_fft_filter_ccc (int decimation, const std::vector<gr_complex> &taps)
44 {
45   return gr_fft_filter_ccc_sptr (new gr_fft_filter_ccc (decimation, taps));
46 }
47
48
49 gr_fft_filter_ccc::gr_fft_filter_ccc (int decimation, const std::vector<gr_complex> &taps)
50   : gr_sync_decimator ("fft_filter_ccc",
51                        gr_make_io_signature (1, 1, sizeof (gr_complex)),
52                        gr_make_io_signature (1, 1, sizeof (gr_complex)),
53                        decimation),
54     d_fftsize(-1), d_fwdfft(0), d_invfft(0), d_updated(false)
55 {
56   // if (decimation != 1)
57   //    throw std::invalid_argument("gr_fft_filter_ccc: decimation must be 1");
58
59   set_history(1);
60   actual_set_taps(taps);
61 }
62
63 gr_fft_filter_ccc::~gr_fft_filter_ccc ()
64 {
65   delete d_fwdfft;
66   delete d_invfft;
67 }
68
69 #if 0
70 static void 
71 print_vector_complex(const std::string label, const std::vector<gr_complex> &x)
72 {
73   std::cout << label;
74   for (unsigned i = 0; i < x.size(); i++)
75     std::cout << x[i] << " ";
76   std::cout << "\n";
77 }
78 #endif
79
80 void
81 gr_fft_filter_ccc::set_taps (const std::vector<gr_complex> &taps)
82 {
83   d_new_taps = taps;
84   d_updated = true;
85 }
86
87 /*
88  * determines d_ntaps, d_nsamples, d_fftsize, d_xformed_taps
89  */
90 void
91 gr_fft_filter_ccc::actual_set_taps (const std::vector<gr_complex> &taps)
92 {
93   int i = 0;
94   compute_sizes(taps.size());
95
96   d_tail.resize(tailsize());
97   for (i = 0; i < tailsize(); i++)
98     d_tail[i] = 0;
99
100   gr_complex *in = d_fwdfft->get_inbuf();
101   gr_complex *out = d_fwdfft->get_outbuf();
102
103   float scale = 1.0 / d_fftsize;
104   
105   // Compute forward xform of taps.
106   // Copy taps into first ntaps slots, then pad with zeros
107   for (i = 0; i < d_ntaps; i++)
108     in[i] = taps[i] * scale;
109
110   for (; i < d_fftsize; i++)
111     in[i] = 0;
112
113   d_fwdfft->execute();          // do the xform
114
115   // now copy output to d_xformed_taps
116   for (i = 0; i < d_fftsize; i++)
117     d_xformed_taps[i] = out[i];
118
119   //print_vector_complex("transformed taps:", d_xformed_taps);
120 }
121
122 // determine and set d_ntaps, d_nsamples, d_fftsize
123
124 void
125 gr_fft_filter_ccc::compute_sizes(int ntaps)
126 {
127   int old_fftsize = d_fftsize;
128   d_ntaps = ntaps;
129   d_fftsize = (int) (2 * pow(2.0, ceil(log(ntaps) / log(2))));
130   d_nsamples = d_fftsize - d_ntaps + 1;
131
132   if (0)
133     fprintf(stderr, "gr_fft_filter: ntaps = %d, fftsize = %d, nsamples = %d\n",
134             d_ntaps, d_fftsize, d_nsamples);
135
136   assert(d_fftsize == d_ntaps + d_nsamples -1 );
137
138   if (d_fftsize != old_fftsize){        // compute new plans
139     delete d_fwdfft;
140     delete d_invfft;
141     d_fwdfft = new gri_fft_complex(d_fftsize, true);
142     d_invfft = new gri_fft_complex(d_fftsize, false);
143     d_xformed_taps.resize(d_fftsize);
144   }
145
146   set_output_multiple(d_nsamples);
147 }
148
149 int
150 gr_fft_filter_ccc::work (int noutput_items,
151                          gr_vector_const_void_star &input_items,
152                          gr_vector_void_star &output_items)
153 {
154   gr_complex *in = (gr_complex *) input_items[0];
155   gr_complex *out = (gr_complex *) output_items[0];
156
157   if (d_updated){
158     actual_set_taps(d_new_taps);
159     d_updated = false;
160     return 0;                           // output multiple may have changed
161   }
162
163   assert(noutput_items % d_nsamples == 0);
164
165   int dec_ctr = 0;
166   int j = 0;
167   int ninput_items = noutput_items * decimation();
168
169   for (int i = 0; i < ninput_items; i += d_nsamples){
170     
171     memcpy(d_fwdfft->get_inbuf(), &in[i], d_nsamples * sizeof(gr_complex));
172
173     for (j = d_nsamples; j < d_fftsize; j++)
174       d_fwdfft->get_inbuf()[j] = 0;
175
176     d_fwdfft->execute();        // compute fwd xform
177
178     gr_complex *a = d_fwdfft->get_outbuf();
179     gr_complex *b = &d_xformed_taps[0];
180     gr_complex *c = d_invfft->get_inbuf();
181
182     for (j = 0; j < d_fftsize; j++)     // filter in the freq domain
183       c[j] = a[j] * b[j];
184     
185     d_invfft->execute();        // compute inv xform
186
187     // add in the overlapping tail
188
189     for (j = 0; j < tailsize(); j++)
190       d_invfft->get_outbuf()[j] += d_tail[j];
191
192     // copy nsamples to output
193
194     //memcpy(out, d_invfft->get_outbuf(), d_nsamples * sizeof(gr_complex));
195     //out += d_nsamples;
196
197     j = dec_ctr;
198     while (j < d_nsamples) {
199       *out++ = d_invfft->get_outbuf()[j];
200       j += decimation();
201     }
202     dec_ctr = (j - d_nsamples);
203
204     // stash the tail
205     memcpy(&d_tail[0], d_invfft->get_outbuf() + d_nsamples,
206            tailsize() * sizeof(gr_complex));
207   }
208
209   assert((out - (gr_complex *) output_items[0]) == noutput_items);
210   assert(dec_ctr == 0);
211
212   return noutput_items;
213 }