e05e18ff2e0f04c38bd3ce6c152402e6876806d5
[debian/gnuradio] / gnuradio-core / src / lib / filter / gr_pfb_decimator_ccf.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2009 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 <gr_pfb_decimator_ccf.h>
28 #include <gr_fir_ccf.h>
29 #include <gr_fir_util.h>
30 #include <gri_fft.h>
31 #include <gr_io_signature.h>
32 #include <gr_expj.h>
33 #include <cstdio>
34
35 gr_pfb_decimator_ccf_sptr gr_make_pfb_decimator_ccf (unsigned int decim, 
36                                                      const std::vector<float> &taps,
37                                                      unsigned int channel)
38 {
39   return gr_pfb_decimator_ccf_sptr (new gr_pfb_decimator_ccf (decim, taps, channel));
40 }
41
42
43 gr_pfb_decimator_ccf::gr_pfb_decimator_ccf (unsigned int decim, 
44                                             const std::vector<float> &taps,
45                                             unsigned int channel)
46   : gr_sync_block ("pfb_decimator_ccf",
47                    gr_make_io_signature (decim, decim, sizeof(gr_complex)),
48                    gr_make_io_signature (1, 1, sizeof(gr_complex))),
49     d_updated (false)
50 {
51   d_rate = decim;
52   d_filters = std::vector<gr_fir_ccf*>(d_rate);
53   d_chan = channel;
54   d_rotator = new gr_complex[d_rate];
55
56   // Create an FIR filter for each channel and zero out the taps
57   std::vector<float> vtaps(0, d_rate);
58   for(unsigned int i = 0; i < d_rate; i++) {
59     d_filters[i] = gr_fir_util::create_gr_fir_ccf(vtaps);
60     d_rotator[i] = gr_expj(i*2*M_PI*d_chan/d_rate);
61   }
62
63   // Now, actually set the filters' taps
64   set_taps(taps);
65
66   // Create the FFT to handle the output de-spinning of the channels
67   d_fft = new gri_fft_complex (d_rate, false);
68 }
69
70 gr_pfb_decimator_ccf::~gr_pfb_decimator_ccf ()
71 {
72   for(unsigned int i = 0; i < d_rate; i++) {
73     delete d_filters[i];
74   }
75 }
76
77 void
78 gr_pfb_decimator_ccf::set_taps (const std::vector<float> &taps)
79 {
80   unsigned int i,j;
81
82   unsigned int ntaps = taps.size();
83   d_taps_per_filter = (unsigned int)ceil((double)ntaps/(double)d_rate);
84
85   // Create d_numchan vectors to store each channel's taps
86   d_taps.resize(d_rate);
87
88   // Make a vector of the taps plus fill it out with 0's to fill
89   // each polyphase filter with exactly d_taps_per_filter
90   std::vector<float> tmp_taps;
91   tmp_taps = taps;
92   while((float)(tmp_taps.size()) < d_rate*d_taps_per_filter) {
93     tmp_taps.push_back(0.0);
94   }
95   
96   // Partition the filter
97   for(i = 0; i < d_rate; i++) {
98     // Each channel uses all d_taps_per_filter with 0's if not enough taps to fill out
99     d_taps[i] = std::vector<float>(d_taps_per_filter, 0);
100     for(j = 0; j < d_taps_per_filter; j++) {
101       d_taps[i][j] = tmp_taps[i + j*d_rate];  // add taps to channels in reverse order
102     }
103     
104     // Build a filter for each channel and add it's taps to it
105     d_filters[i]->set_taps(d_taps[i]);
106   }
107
108   // Set the history to ensure enough input items for each filter
109   set_history (d_taps_per_filter);
110
111   d_updated = true;
112 }
113
114 void
115 gr_pfb_decimator_ccf::print_taps()
116 {
117   unsigned int i, j;
118   for(i = 0; i < d_rate; i++) {
119     printf("filter[%d]: [", i);
120     for(j = 0; j < d_taps_per_filter; j++) {
121       printf(" %.4e", d_taps[i][j]);
122     }
123     printf("]\n\n");
124   }
125 }
126
127 #define ROTATEFFT
128
129 int
130 gr_pfb_decimator_ccf::work (int noutput_items,
131                             gr_vector_const_void_star &input_items,
132                             gr_vector_void_star &output_items)
133 {
134   gr_complex *in;
135   gr_complex *out = (gr_complex *) output_items[0];
136
137   if (d_updated) {
138     d_updated = false;
139     return 0;                // history requirements may have changed.
140   }
141
142   int i;
143   for(i = 0; i < noutput_items; i++) {
144     // Move through filters from bottom to top
145     out[i] = 0;
146     for(int j = d_rate-1; j >= 0; j--) {
147       // Take in the items from the first input stream to d_rate
148       in = (gr_complex*)input_items[d_rate - 1 - j];
149
150       // Filter current input stream from bottom filter to top
151       // The rotate them by expj(j*k*2pi/M) where M is the number of filters
152       // (the decimation rate) and k is the channel number to extract
153       
154       // This is the real math that goes on; we abuse the FFT to do this quickly
155       // for decimation rates > N where N is a small number (~5):
156       //       out[i] += d_filters[j]->filter(&in[i])*gr_expj(j*d_chan*2*M_PI/d_rate);
157 #ifdef ROTATEFFT
158       d_fft->get_inbuf()[j] = d_filters[j]->filter(&in[i]);
159 #else
160       out[i] += d_filters[j]->filter(&in[i])*d_rotator[i];
161 #endif
162     }
163
164 #ifdef ROTATEFFT
165     // Perform the FFT to do the complex multiply despinning for all channels
166     d_fft->execute();
167
168     // Select only the desired channel out
169     out[i] = d_fft->get_outbuf()[d_chan];
170 #endif
171     
172   }
173   
174   return noutput_items;
175 }