Moving towards gr_block implementation to enable non-integer output rates. Also,...
[debian/gnuradio] / gnuradio-core / src / lib / filter / gr_pfb_channelizer_ccf.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2009,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 <gr_pfb_channelizer_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 <cstdio>
33 #include <cstring>
34
35 gr_pfb_channelizer_ccf_sptr gr_make_pfb_channelizer_ccf (unsigned int numchans, 
36                                                          const std::vector<float> &taps,
37                                                          float oversample_rate)
38 {
39   return gr_pfb_channelizer_ccf_sptr (new gr_pfb_channelizer_ccf (numchans, taps,
40                                                                   oversample_rate));
41 }
42
43
44 gr_pfb_channelizer_ccf::gr_pfb_channelizer_ccf (unsigned int numchans, 
45                                                 const std::vector<float> &taps,
46                                                 float oversample_rate)
47   : gr_block ("pfb_channelizer_ccf",
48               gr_make_io_signature (numchans, numchans, sizeof(gr_complex)),
49               gr_make_io_signature (1, 1, numchans*sizeof(gr_complex))),
50     d_updated (false), d_numchans(numchans), d_oversample_rate(oversample_rate)
51 {
52   d_filters = std::vector<gr_fir_ccf*>(d_numchans);
53
54   // Create an FIR filter for each channel and zero out the taps
55   std::vector<float> vtaps(0, d_numchans);
56   for(unsigned int i = 0; i < d_numchans; i++) {
57     d_filters[i] = gr_fir_util::create_gr_fir_ccf(vtaps);
58   }
59
60   // Now, actually set the filters' taps
61   set_taps(taps);
62
63   // Create the FFT to handle the output de-spinning of the channels
64   d_fft = new gri_fft_complex (d_numchans, false);
65
66   // Although the filters change, we use this look up table
67   // to set the index of the FFT input buffer, which equivalently
68   // performs the FFT shift operation on every other turn.
69   int r = d_numchans / d_oversample_rate;
70   d_idxlut = new int[d_numchans];
71   for(int i = 0; i < d_numchans; i++) {
72     d_idxlut[i] = d_numchans - ((i + r) % d_numchans) - 1;
73   }
74
75   set_output_multiple(d_oversample_rate);
76 }
77
78 gr_pfb_channelizer_ccf::~gr_pfb_channelizer_ccf ()
79 {
80   delete [] d_idxlut; 
81   
82   for(unsigned int i = 0; i < d_numchans; i++) {
83     delete d_filters[i];
84   }
85 }
86
87 void
88 gr_pfb_channelizer_ccf::set_taps (const std::vector<float> &taps)
89 {
90   unsigned int i,j;
91
92   unsigned int ntaps = taps.size();
93   d_taps_per_filter = (unsigned int)ceil((double)ntaps/(double)d_numchans);
94
95   // Create d_numchan vectors to store each channel's taps
96   d_taps.resize(d_numchans);
97
98   // Make a vector of the taps plus fill it out with 0's to fill
99   // each polyphase filter with exactly d_taps_per_filter
100   std::vector<float> tmp_taps;
101   tmp_taps = taps;
102   while((float)(tmp_taps.size()) < d_numchans*d_taps_per_filter) {
103     tmp_taps.push_back(0.0);
104   }
105  
106   // Partition the filter
107   for(i = 0; i < d_numchans; i++) {
108     // Each channel uses all d_taps_per_filter with 0's if not enough taps to fill out
109     d_taps[i] = std::vector<float>(d_taps_per_filter, 0);
110     for(j = 0; j < d_taps_per_filter; j++) {
111       d_taps[i][j] = tmp_taps[i + j*d_numchans];  // add taps to channels in reverse order
112     }
113     
114     // Build a filter for each channel and add it's taps to it
115     d_filters[i]->set_taps(d_taps[i]);
116   }
117
118   // Set the history to ensure enough input items for each filter
119   set_history (d_taps_per_filter);
120
121   d_updated = true;
122 }
123
124 void
125 gr_pfb_channelizer_ccf::print_taps()
126 {
127   unsigned int i, j;
128   for(i = 0; i < d_numchans; i++) {
129     printf("filter[%d]: [", i);
130     for(j = 0; j < d_taps_per_filter; j++) {
131       printf(" %.4e", d_taps[i][j]);
132     }
133     printf("]\n\n");
134   }
135 }
136
137
138 int
139 gr_pfb_channelizer_ccf::general_work (int noutput_items,
140                                       gr_vector_int &ninput_items,
141                                       gr_vector_const_void_star &input_items,
142                                       gr_vector_void_star &output_items)
143 {
144   gr_complex *in = (gr_complex *) input_items[0];
145   gr_complex *out = (gr_complex *) output_items[0];
146
147   if (d_updated) {
148     d_updated = false;
149     return 0;                // history requirements may have changed.
150   }
151
152   int M = d_oversample_rate;
153   int N = d_numchans;
154   int r = N / M;
155
156   int n=1, i=-1, j=0, last;
157
158   while(n <= noutput_items/M) {
159     j = 0;
160     i = (i + r) % N;
161     last = i;
162     while(i >= 0) {
163       in = (gr_complex*)input_items[j];
164       d_fft->get_inbuf()[d_idxlut[j]] = d_filters[i]->filter(&in[n]);
165       j++;
166       i--;
167     }
168
169     i = N-1;
170     while(i > last) {
171       in = (gr_complex*)input_items[j];
172       d_fft->get_inbuf()[d_idxlut[j]] = d_filters[i]->filter(&in[n-1]);
173       j++;
174       i--;
175     }
176
177     n += (i+r) >= N;
178
179     // despin through FFT
180     d_fft->execute();
181     memcpy(out, d_fft->get_outbuf(), d_numchans*sizeof(gr_complex));
182     out += d_numchans;
183   }
184   
185   consume_each(noutput_items/M);
186   return noutput_items;
187 }