Adding a FIR filter implemented with its own internal buffer. This one keeps its...
[debian/gnuradio] / gnuradio-core / src / lib / filter / gr_pfb_synthesis_filterbank_ccf.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 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_synthesis_filterbank_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_synthesis_filterbank_ccf_sptr gr_make_pfb_synthesis_filterbank_ccf 
36     (unsigned int numchans, const std::vector<float> &taps)
37 {
38   return gr_pfb_synthesis_filterbank_ccf_sptr 
39     (new gr_pfb_synthesis_filterbank_ccf (numchans, taps));
40 }
41
42
43 gr_pfb_synthesis_filterbank_ccf::gr_pfb_synthesis_filterbank_ccf
44     (unsigned int numchans, const std::vector<float> &taps)
45   : gr_sync_interpolator ("pfb_synthesis_filterbank_ccf",
46                           gr_make_io_signature (1, numchans, sizeof(gr_complex)),
47                           gr_make_io_signature (1, 1, sizeof(gr_complex)),
48                           numchans),
49     d_updated (false), d_numchans(numchans)
50 {
51   //d_filters = std::vector<gr_fir_ccf*>(d_numchans);
52   d_filters = std::vector<gri_fir_filter_with_buffer_ccf*>(d_numchans);
53
54   //d_buffer = new gr_complex*[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] = new gri_fir_filter_with_buffer_ccf(vtaps);
60     //d_filters[i] = gr_fir_util::create_gr_fir_ccf(vtaps);
61     //d_buffer[i] = new gr_complex[65535];
62     //memset(d_buffer[i], 0, 65535*sizeof(gr_complex));
63   }
64
65   // Now, actually set the filters' taps
66   set_taps(taps);
67
68   // Create the IFFT to handle the input channel rotations
69   d_fft = new gri_fft_complex (d_numchans, true);
70 }
71
72 gr_pfb_synthesis_filterbank_ccf::~gr_pfb_synthesis_filterbank_ccf ()
73 {
74   for(unsigned int i = 0; i < d_numchans; i++) {
75     delete d_filters[i];
76   }
77 }
78
79 void
80 gr_pfb_synthesis_filterbank_ccf::set_taps (const std::vector<float> &taps)
81 {
82   unsigned int i,j;
83
84   unsigned int ntaps = taps.size();
85   d_taps_per_filter = (unsigned int)ceil((double)ntaps/(double)d_numchans);
86
87   // Create d_numchan vectors to store each channel's taps
88   d_taps.resize(d_numchans);
89
90   // Make a vector of the taps plus fill it out with 0's to fill
91   // each polyphase filter with exactly d_taps_per_filter
92   std::vector<float> tmp_taps;
93   tmp_taps = taps;
94   while((float)(tmp_taps.size()) < d_numchans*d_taps_per_filter) {
95     tmp_taps.push_back(0.0);
96   }
97  
98   // Partition the filter
99   for(i = 0; i < d_numchans; i++) {
100     // Each channel uses all d_taps_per_filter with 0's if not enough taps to fill out
101     d_taps[i] = std::vector<float>(d_taps_per_filter, 0);
102     for(j = 0; j < d_taps_per_filter; j++) {
103       d_taps[i][j] = tmp_taps[i + j*d_numchans];  // add taps to channels in reverse order
104     }
105     
106     // Build a filter for each channel and add it's taps to it
107     d_filters[i]->set_taps(d_taps[i]);
108   }
109
110   // Set the history to ensure enough input items for each filter
111   set_history (d_taps_per_filter+1);
112
113   d_updated = true;
114 }
115
116 void
117 gr_pfb_synthesis_filterbank_ccf::print_taps()
118 {
119   unsigned int i, j;
120   for(i = 0; i < d_numchans; i++) {
121     printf("filter[%d]: [", i);
122     for(j = 0; j < d_taps_per_filter; j++) {
123       printf(" %.4e", d_taps[i][j]);
124     }
125     printf("]\n\n");
126   }
127 }
128
129
130 int
131 gr_pfb_synthesis_filterbank_ccf::work (int noutput_items,
132                                        gr_vector_const_void_star &input_items,
133                                        gr_vector_void_star &output_items)
134 {
135   gr_complex *in = (gr_complex*) input_items[0];
136   gr_complex *out = (gr_complex *) output_items[0];
137   int numsigs = input_items.size();
138   int ndiff   = d_numchans - numsigs;
139   unsigned int nhalf = (unsigned int)ceil((float)numsigs/2.0f);
140
141   if (d_updated) {
142     d_updated = false;
143     return 0;                // history requirements may have changed.
144   }
145
146   unsigned int n, i;
147   for(n = 0; n < noutput_items/d_numchans; n++) {
148     // fill up the populated channels based on the 
149     // number of real input streams
150     for(i = 0; i < nhalf; i++) {
151       in = (gr_complex*)input_items[i];
152       d_fft->get_inbuf()[i] = (in+i)[n];
153     }
154
155     // Make the ndiff channels around N/2 0
156     for(; i < nhalf+ndiff; i++) {
157       d_fft->get_inbuf()[i] = gr_complex(0,0);
158     }
159
160     // Finish off channels with data
161     for(; i < d_numchans; i++) {
162       in = (gr_complex*)input_items[i-ndiff];
163       d_fft->get_inbuf()[i] = (in+i)[n];
164     }
165
166     // spin through IFFT
167     d_fft->execute();
168
169     for(i = 0; i < d_numchans; i++) {
170       //d_buffer[i][n+d_taps_per_filter-1] = d_fft->get_outbuf()[i];
171       //out[d_numchans-i-1] = d_filters[d_numchans-i-1]->filter(&d_buffer[i][n]);
172       out[d_numchans-i-1] = d_filters[d_numchans-i-1]->filter(d_fft->get_outbuf()[i]);
173     }
174     out += d_numchans;
175   }
176
177   // Move the last chunk of memory to the front for the next entry
178   // this make sure that the first taps_per_filter values are correct
179
180   /*
181   for(i = 0; i < d_numchans; i++) {
182     memcpy(d_buffer[i], &d_buffer[i][n],
183            (d_taps_per_filter)*sizeof(gr_complex));
184   }
185   */
186
187   return noutput_items;
188 }