Opening up channelizer to have different sampling rates out. This first pass produces...
[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_sync_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_oversample_rate(oversample_rate)
51 {
52   d_numchans = numchans;
53   d_filters = std::vector<gr_fir_ccf*>(d_numchans);
54
55   // Create an FIR filter for each channel and zero out the taps
56   std::vector<float> vtaps(0, d_numchans);
57   for(unsigned int i = 0; i < d_numchans; i++) {
58     d_filters[i] = gr_fir_util::create_gr_fir_ccf(vtaps);
59   }
60
61   // Now, actually set the filters' taps
62   set_taps(taps);
63
64   // Create the FFT to handle the output de-spinning of the channels
65   d_fft = new gri_fft_complex (d_numchans, false);
66 }
67
68 gr_pfb_channelizer_ccf::~gr_pfb_channelizer_ccf ()
69 {
70   for(unsigned int i = 0; i < d_numchans; i++) {
71     delete d_filters[i];
72   }
73 }
74
75 void
76 gr_pfb_channelizer_ccf::set_taps (const std::vector<float> &taps)
77 {
78   unsigned int i,j;
79
80   unsigned int ntaps = taps.size();
81   d_taps_per_filter = (unsigned int)ceil((double)ntaps/(double)d_numchans);
82
83   // Create d_numchan vectors to store each channel's taps
84   d_taps.resize(d_numchans);
85
86   // Make a vector of the taps plus fill it out with 0's to fill
87   // each polyphase filter with exactly d_taps_per_filter
88   std::vector<float> tmp_taps;
89   tmp_taps = taps;
90   while((float)(tmp_taps.size()) < d_numchans*d_taps_per_filter) {
91     tmp_taps.push_back(0.0);
92   }
93  
94   // Partition the filter
95   for(i = 0; i < d_numchans; i++) {
96     // Each channel uses all d_taps_per_filter with 0's if not enough taps to fill out
97     d_taps[i] = std::vector<float>(d_taps_per_filter, 0);
98     for(j = 0; j < d_taps_per_filter; j++) {
99       d_taps[i][j] = tmp_taps[i + j*d_numchans];  // add taps to channels in reverse order
100     }
101     
102     // Build a filter for each channel and add it's taps to it
103     d_filters[i]->set_taps(d_taps[i]);
104   }
105
106   // Set the history to ensure enough input items for each filter
107   set_history (d_taps_per_filter);
108
109   d_updated = true;
110 }
111
112 void
113 gr_pfb_channelizer_ccf::print_taps()
114 {
115   unsigned int i, j;
116   for(i = 0; i < d_numchans; i++) {
117     printf("filter[%d]: [", i);
118     for(j = 0; j < d_taps_per_filter; j++) {
119       printf(" %.4e", d_taps[i][j]);
120     }
121     printf("]\n\n");
122   }
123 }
124
125
126 int
127 gr_pfb_channelizer_ccf::work (int noutput_items,
128                               gr_vector_const_void_star &input_items,
129                               gr_vector_void_star &output_items)
130 {
131   gr_complex *in = (gr_complex *) input_items[0];
132   gr_complex *out = (gr_complex *) output_items[0];
133
134   if (d_updated) {
135     d_updated = false;
136     return 0;                // history requirements may have changed.
137   }
138
139   int M = d_oversample_rate;
140   int N = d_numchans;
141   int r = N / M;
142
143   int n=0, i=0, j=0;
144   
145   printf("\nnoutput_items = %d\n", noutput_items);
146   printf("N = %d   M  = %d   r = %d\n", N, M, r);
147
148   //for(int n = 1; n < noutput_items; n++) {
149   while(n < noutput_items) {
150     j = 0;
151     i = (i + r - 1) % N;
152     //printf("i = %d   i >= 0   n = %d\n", i, n);
153     while(i >= 0) {
154       in = (gr_complex*)input_items[j];
155       d_fft->get_inbuf()[i] = d_filters[i]->filter(&in[n]);
156       j++;
157       i--;
158     }
159
160     i = N;
161     //printf("i = %d   r = %d   i >= r\n", i, r);
162     while(i > r) {
163       i--;
164       in = (gr_complex*)input_items[j];
165       d_fft->get_inbuf()[i] = d_filters[i]->filter(&in[n-1]);
166       j++;
167     }
168
169     n += (i+r) >= N;
170
171     /*
172     // Move through filters from bottom to top
173     for(int j = d_numchans-1; j >= 0; j--) {
174       // Take in the items from the first input stream to d_numchans
175       in = (gr_complex*)input_items[d_numchans - 1 - j];
176
177       // Filter current input stream from bottom filter to top
178       d_fft->get_inbuf()[j] = d_filters[j]->filter(&in[i]);
179     }
180     */
181
182     // despin through FFT
183     d_fft->execute();
184     memcpy(&out[d_numchans*n], d_fft->get_outbuf(), d_numchans*sizeof(gr_complex));
185     //memcpy(&out[d_numchans*i], d_fft->get_outbuf(), d_numchans*sizeof(gr_complex));
186   }
187   
188   return noutput_items;
189 }