Merge branch 'maint'
[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 <gri_fft.h>
29 #include <gr_io_signature.h>
30 #include <cstdio>
31 #include <cstring>
32
33 gr_pfb_synthesis_filterbank_ccf_sptr gr_make_pfb_synthesis_filterbank_ccf 
34     (unsigned int numchans, const std::vector<float> &taps)
35 {
36   return gr_pfb_synthesis_filterbank_ccf_sptr 
37     (new gr_pfb_synthesis_filterbank_ccf (numchans, taps));
38 }
39
40
41 gr_pfb_synthesis_filterbank_ccf::gr_pfb_synthesis_filterbank_ccf
42     (unsigned int numchans, const std::vector<float> &taps)
43   : gr_sync_interpolator ("pfb_synthesis_filterbank_ccf",
44                           gr_make_io_signature (1, numchans, sizeof(gr_complex)),
45                           gr_make_io_signature (1, 1, sizeof(gr_complex)),
46                           numchans),
47     d_updated (false), d_numchans(numchans)
48 {
49   d_filters = std::vector<gri_fir_filter_with_buffer_ccf*>(d_numchans);
50
51   // Create an FIR filter for each channel and zero out the taps
52   std::vector<float> vtaps(0, d_numchans);
53   for(unsigned int i = 0; i < d_numchans; i++) {
54     d_filters[i] = new gri_fir_filter_with_buffer_ccf(vtaps);
55   }
56
57   // Now, actually set the filters' taps
58   set_taps(taps);
59
60   // Create the IFFT to handle the input channel rotations
61   d_fft = new gri_fft_complex (d_numchans, true);
62 }
63
64 gr_pfb_synthesis_filterbank_ccf::~gr_pfb_synthesis_filterbank_ccf ()
65 {
66   for(unsigned int i = 0; i < d_numchans; i++) {
67     delete d_filters[i];
68   }
69 }
70
71 void
72 gr_pfb_synthesis_filterbank_ccf::set_taps (const std::vector<float> &taps)
73 {
74   unsigned int i,j;
75
76   unsigned int ntaps = taps.size();
77   d_taps_per_filter = (unsigned int)ceil((double)ntaps/(double)d_numchans);
78
79   // Create d_numchan vectors to store each channel's taps
80   d_taps.resize(d_numchans);
81
82   // Make a vector of the taps plus fill it out with 0's to fill
83   // each polyphase filter with exactly d_taps_per_filter
84   std::vector<float> tmp_taps;
85   tmp_taps = taps;
86   while((float)(tmp_taps.size()) < d_numchans*d_taps_per_filter) {
87     tmp_taps.push_back(0.0);
88   }
89  
90   // Partition the filter
91   for(i = 0; i < d_numchans; i++) {
92     // Each channel uses all d_taps_per_filter with 0's if not enough taps to fill out
93     d_taps[i] = std::vector<float>(d_taps_per_filter, 0);
94     for(j = 0; j < d_taps_per_filter; j++) {
95       d_taps[i][j] = tmp_taps[i + j*d_numchans];  // add taps to channels in reverse order
96     }
97     
98     // Build a filter for each channel and add it's taps to it
99     d_filters[i]->set_taps(d_taps[i]);
100   }
101
102   // Set the history to ensure enough input items for each filter
103   set_history (d_taps_per_filter+1);
104
105   d_updated = true;
106 }
107
108 void
109 gr_pfb_synthesis_filterbank_ccf::print_taps()
110 {
111   unsigned int i, j;
112   for(i = 0; i < d_numchans; i++) {
113     printf("filter[%d]: [", i);
114     for(j = 0; j < d_taps_per_filter; j++) {
115       printf(" %.4e", d_taps[i][j]);
116     }
117     printf("]\n\n");
118   }
119 }
120
121
122 int
123 gr_pfb_synthesis_filterbank_ccf::work (int noutput_items,
124                                        gr_vector_const_void_star &input_items,
125                                        gr_vector_void_star &output_items)
126 {
127   gr_complex *in = (gr_complex*) input_items[0];
128   gr_complex *out = (gr_complex *) output_items[0];
129   int numsigs = input_items.size();
130   int ndiff   = d_numchans - numsigs;
131   unsigned int nhalf = (unsigned int)ceil((float)numsigs/2.0f);
132
133   if (d_updated) {
134     d_updated = false;
135     return 0;                // history requirements may have changed.
136   }
137
138   unsigned int n, i;
139   for(n = 0; n < noutput_items/d_numchans; n++) {
140     // fill up the populated channels based on the 
141     // number of real input streams
142     for(i = 0; i < nhalf; i++) {
143       in = (gr_complex*)input_items[i];
144       d_fft->get_inbuf()[i] = (in+i)[n];
145     }
146
147     // Make the ndiff channels around N/2 0
148     for(; i < nhalf+ndiff; i++) {
149       d_fft->get_inbuf()[i] = gr_complex(0,0);
150     }
151
152     // Finish off channels with data
153     for(; i < d_numchans; i++) {
154       in = (gr_complex*)input_items[i-ndiff];
155       d_fft->get_inbuf()[i] = (in+i)[n];
156     }
157
158     // spin through IFFT
159     d_fft->execute();
160
161     for(i = 0; i < d_numchans; i++) {
162       out[d_numchans-i-1] = d_filters[d_numchans-i-1]->filter(d_fft->get_outbuf()[i]);
163     }
164     
165     out += d_numchans;
166   }
167
168   return noutput_items;
169 }