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