Fixed missing set_relative_rate in these two blocks. The others don't actually do...
[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 gnuradio::get_initial_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   // The over sampling rate must be rationally related to the number of channels
53   // in that it must be N/i for i in [1,N], which gives an outputsample rate 
54   // of [fs/N, fs] where fs is the input sample rate.
55   // This tests the specified input sample rate to see if it conforms to this
56   // requirement within a few significant figures.
57   double intp = 0;
58   double x = (10000.0*rint(numchans / oversample_rate)) / 10000.0;
59   double fltp = modf(numchans / oversample_rate, &intp);
60   if(fltp != 0.0)
61     throw std::invalid_argument("gr_pfb_channelizer: oversample rate must be N/i for i in [1, N]"); 
62
63   set_relative_rate(1.0/intp);
64
65   d_filters = std::vector<gr_fir_ccf*>(d_numchans);
66
67   // Create an FIR filter for each channel and zero out the taps
68   std::vector<float> vtaps(0, d_numchans);
69   for(unsigned int i = 0; i < d_numchans; i++) {
70     d_filters[i] = gr_fir_util::create_gr_fir_ccf(vtaps);
71   }
72
73   // Now, actually set the filters' taps
74   set_taps(taps);
75
76   // Create the FFT to handle the output de-spinning of the channels
77   d_fft = new gri_fft_complex (d_numchans, false);
78
79   // Although the filters change, we use this look up table
80   // to set the index of the FFT input buffer, which equivalently
81   // performs the FFT shift operation on every other turn.
82   d_rate_ratio = (int)rintf(d_numchans / d_oversample_rate);
83   d_idxlut = new int[d_numchans];
84   for(unsigned int i = 0; i < d_numchans; i++) {
85     d_idxlut[i] = d_numchans - ((i + d_rate_ratio) % d_numchans) - 1;
86   }
87
88   // Calculate the number of filtering rounds to do to evenly
89   // align the input vectors with the output channels
90   d_output_multiple = 1;
91   while((d_output_multiple * d_rate_ratio) % d_numchans != 0)
92     d_output_multiple++;
93   set_output_multiple(d_output_multiple);
94 }
95
96 gr_pfb_channelizer_ccf::~gr_pfb_channelizer_ccf ()
97 {
98   delete [] d_idxlut; 
99   
100   for(unsigned int i = 0; i < d_numchans; i++) {
101     delete d_filters[i];
102   }
103 }
104
105 void
106 gr_pfb_channelizer_ccf::set_taps (const std::vector<float> &taps)
107 {
108   unsigned int i,j;
109
110   unsigned int ntaps = taps.size();
111   d_taps_per_filter = (unsigned int)ceil((double)ntaps/(double)d_numchans);
112
113   // Create d_numchan vectors to store each channel's taps
114   d_taps.resize(d_numchans);
115
116   // Make a vector of the taps plus fill it out with 0's to fill
117   // each polyphase filter with exactly d_taps_per_filter
118   std::vector<float> tmp_taps;
119   tmp_taps = taps;
120   while((float)(tmp_taps.size()) < d_numchans*d_taps_per_filter) {
121     tmp_taps.push_back(0.0);
122   }
123  
124   // Partition the filter
125   for(i = 0; i < d_numchans; i++) {
126     // Each channel uses all d_taps_per_filter with 0's if not enough taps to fill out
127     d_taps[i] = std::vector<float>(d_taps_per_filter, 0);
128     for(j = 0; j < d_taps_per_filter; j++) {
129       d_taps[i][j] = tmp_taps[i + j*d_numchans];  // add taps to channels in reverse order
130     }
131     
132     // Build a filter for each channel and add it's taps to it
133     d_filters[i]->set_taps(d_taps[i]);
134   }
135
136   // Set the history to ensure enough input items for each filter
137   set_history (d_taps_per_filter+1);
138
139   d_updated = true;
140 }
141
142 void
143 gr_pfb_channelizer_ccf::print_taps()
144 {
145   unsigned int i, j;
146   for(i = 0; i < d_numchans; i++) {
147     printf("filter[%d]: [", i);
148     for(j = 0; j < d_taps_per_filter; j++) {
149       printf(" %.4e", d_taps[i][j]);
150     }
151     printf("]\n\n");
152   }
153 }
154
155
156 int
157 gr_pfb_channelizer_ccf::general_work (int noutput_items,
158                                       gr_vector_int &ninput_items,
159                                       gr_vector_const_void_star &input_items,
160                                       gr_vector_void_star &output_items)
161 {
162   gr_complex *in = (gr_complex *) input_items[0];
163   gr_complex *out = (gr_complex *) output_items[0];
164
165   if (d_updated) {
166     d_updated = false;
167     return 0;                // history requirements may have changed.
168   }
169
170   int n=1, i=-1, j=0, last;
171   int toconsume = (int)rintf(noutput_items/d_oversample_rate);
172   while(n <= toconsume) {
173     j = 0;
174     i = (i + d_rate_ratio) % d_numchans;
175     last = i;
176     while(i >= 0) {
177       in = (gr_complex*)input_items[j];
178       d_fft->get_inbuf()[d_idxlut[j]] = d_filters[i]->filter(&in[n]);
179       j++;
180       i--;
181     }
182
183     i = d_numchans-1;
184     while(i > last) {
185       in = (gr_complex*)input_items[j];
186       d_fft->get_inbuf()[d_idxlut[j]] = d_filters[i]->filter(&in[n-1]);
187       j++;
188       i--;
189     }
190
191     n += (i+d_rate_ratio) >= (int)d_numchans;
192
193     // despin through FFT
194     d_fft->execute();
195     memcpy(out, d_fft->get_outbuf(), d_numchans*sizeof(gr_complex));
196     out += d_numchans;
197   }
198
199   consume_each(toconsume);
200   return noutput_items;
201 }