e1ab1ffee7edbcf4c40b3e7db969272f8bff42e0
[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_interpolator ("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                           oversample_rate),
51     d_updated (false), d_oversample_rate(oversample_rate)
52 {
53   d_numchans = numchans;
54   d_filters = std::vector<gr_fir_ccf*>(d_numchans);
55
56   // Create an FIR filter for each channel and zero out the taps
57   std::vector<float> vtaps(0, d_numchans);
58   for(unsigned int i = 0; i < d_numchans; i++) {
59     d_filters[i] = gr_fir_util::create_gr_fir_ccf(vtaps);
60   }
61
62   // Now, actually set the filters' taps
63   set_taps(taps);
64
65   // Create the FFT to handle the output de-spinning of the channels
66   d_fft = new gri_fft_complex (d_numchans, false);
67 }
68
69 gr_pfb_channelizer_ccf::~gr_pfb_channelizer_ccf ()
70 {
71   for(unsigned int i = 0; i < d_numchans; i++) {
72     delete d_filters[i];
73   }
74 }
75
76 void
77 gr_pfb_channelizer_ccf::set_taps (const std::vector<float> &taps)
78 {
79   unsigned int i,j;
80
81   unsigned int ntaps = taps.size();
82   d_taps_per_filter = (unsigned int)ceil((double)ntaps/(double)d_numchans);
83
84   // Create d_numchan vectors to store each channel's taps
85   d_taps.resize(d_numchans);
86
87   // Make a vector of the taps plus fill it out with 0's to fill
88   // each polyphase filter with exactly d_taps_per_filter
89   std::vector<float> tmp_taps;
90   tmp_taps = taps;
91   while((float)(tmp_taps.size()) < d_numchans*d_taps_per_filter) {
92     tmp_taps.push_back(0.0);
93   }
94  
95   // Partition the filter
96   for(i = 0; i < d_numchans; i++) {
97     // Each channel uses all d_taps_per_filter with 0's if not enough taps to fill out
98     d_taps[i] = std::vector<float>(d_taps_per_filter, 0);
99     for(j = 0; j < d_taps_per_filter; j++) {
100       d_taps[i][j] = tmp_taps[i + j*d_numchans];  // add taps to channels in reverse order
101     }
102     
103     // Build a filter for each channel and add it's taps to it
104     d_filters[i]->set_taps(d_taps[i]);
105   }
106
107   // Set the history to ensure enough input items for each filter
108   set_history (d_taps_per_filter);
109
110   d_updated = true;
111 }
112
113 void
114 gr_pfb_channelizer_ccf::print_taps()
115 {
116   unsigned int i, j;
117   for(i = 0; i < d_numchans; i++) {
118     printf("filter[%d]: [", i);
119     for(j = 0; j < d_taps_per_filter; j++) {
120       printf(" %.4e", d_taps[i][j]);
121     }
122     printf("]\n\n");
123   }
124 }
125
126
127 int
128 gr_pfb_channelizer_ccf::work (int noutput_items,
129                               gr_vector_const_void_star &input_items,
130                               gr_vector_void_star &output_items)
131 {
132   gr_complex *in = (gr_complex *) input_items[0];
133   gr_complex *out = (gr_complex *) output_items[0];
134
135   if (d_updated) {
136     d_updated = false;
137     return 0;                // history requirements may have changed.
138   }
139
140 #if 0
141   int M = d_numchans;
142   int N = d_oversample_rate;
143   int lastidx = 0, i = 1, k = 0, m = 0, n = 0;
144
145   int *idx = new int[M];
146   for(k = 0; k < M; k++)
147     idx[k] = 0;
148
149   while(i <= noutput_items/N) {
150     unsigned int x = 0;
151     unsigned int y = 0;
152     for(n = N-1; n >= 0; n--) {
153       for(k = 0; k < M/N; k++)
154         idx[(lastidx + k) % M]++;
155       lastidx = (lastidx + M/N) % M;
156
157       x += M/N;
158       y  = M/N;
159       for(m = 0; m < M; m++) {
160         in = (gr_complex*)input_items[m];
161
162         x = (M + x - 1) % M;
163         y = (M + y - 1) % M;
164
165         d_fft->get_inbuf()[y] = d_filters[x]->filter(&in[idx[m]]);
166       }
167       
168       d_fft->execute();
169       memcpy(out, d_fft->get_outbuf(), d_numchans*sizeof(gr_complex));
170       out += d_numchans;
171     }
172     i++;
173   }
174       
175 #else
176
177   int M = d_oversample_rate;
178   int N = d_numchans;
179   int r = N / M;
180
181   int n=1, i=-1, j=0, last;
182   //int state = 0;
183
184   // Although the filters change, we use this look up table
185   // to set the index of the FFT input buffer, which equivalently
186   // performs the FFT shift operation on every other turn.
187   int *idxlut = new int[N];
188   for(int ii = 0; ii < N; ii++) {
189     idxlut[ii] = N - ((ii + r) % N) - 1;
190   }
191
192   while(n <= noutput_items/M) {
193     j = 0;
194     i = (i + r) % N;
195     last = i;
196     while(i >= 0) {
197       in = (gr_complex*)input_items[j];
198       //d_fft->get_inbuf()[(i + state*r) % N] = d_filters[i]->filter(&in[n]);
199       d_fft->get_inbuf()[idxlut[j]] = d_filters[i]->filter(&in[n]);
200       j++;
201       i--;
202     }
203
204     i = N-1;
205     while(i > last) {
206       in = (gr_complex*)input_items[j];
207       //d_fft->get_inbuf()[(i + state*r) % N] = d_filters[i]->filter(&in[n-1]);
208       d_fft->get_inbuf()[idxlut[j]] = d_filters[i]->filter(&in[n-1]);
209       j++;
210       i--;
211     }
212
213     n += (i+r) >= N;
214     //state ^= 1;
215
216     // despin through FFT
217     d_fft->execute();
218     memcpy(out, d_fft->get_outbuf(), d_numchans*sizeof(gr_complex));
219     out += d_numchans;
220   }
221   
222   delete [] idxlut; 
223
224 #endif
225   
226   return noutput_items;
227 }