1 /* -*- c++ -*- */
2 /*
3  * Copyright 2012,2014 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 <gnuradio/filter/polyphase_filterbank.h>
28 #include <cstdio>
29 
30 namespace gr {
31 namespace filter {
32 namespace kernel {
33 
polyphase_filterbank(unsigned int nfilts,const std::vector<float> & taps,bool fft_forward)34 polyphase_filterbank::polyphase_filterbank(unsigned int nfilts,
35                                            const std::vector<float>& taps,
36                                            bool fft_forward)
37     : d_nfilts(nfilts)
38 {
39     d_fir_filters = std::vector<kernel::fir_filter_ccf*>(d_nfilts);
40     d_fft_filters = std::vector<kernel::fft_filter_ccf*>(d_nfilts);
41 
42     // Create an FIR filter for each channel and zero out the taps
43     std::vector<float> vtaps(1, 0.0f);
44     for (unsigned int i = 0; i < d_nfilts; i++) {
45         d_fir_filters[i] = new kernel::fir_filter_ccf(1, vtaps);
46         d_fft_filters[i] = new kernel::fft_filter_ccf(1, vtaps);
47     }
48 
49     // Now, actually set the filters' taps
50     set_taps(taps);
51 
52     // Create the FFT to handle the output de-spinning of the channels
53     d_fft = new fft::fft_complex(d_nfilts, fft_forward);
54 }
55 
~polyphase_filterbank()56 polyphase_filterbank::~polyphase_filterbank()
57 {
58     delete d_fft;
59     for (unsigned int i = 0; i < d_nfilts; i++) {
60         delete d_fir_filters[i];
61         delete d_fft_filters[i];
62     }
63 }
64 
set_taps(const std::vector<float> & taps)65 void polyphase_filterbank::set_taps(const std::vector<float>& taps)
66 {
67     unsigned int i, j;
68     unsigned int ntaps = taps.size();
69     d_taps_per_filter = (unsigned int)ceil((double)ntaps / (double)d_nfilts);
70 
71     // Create d_numchan vectors to store each channel's taps
72     d_taps.resize(d_nfilts);
73 
74     // Make a vector of the taps plus fill it out with 0's to fill
75     // each polyphase filter with exactly d_taps_per_filter
76     std::vector<float> tmp_taps = taps;
77     while ((float)(tmp_taps.size()) < d_nfilts * d_taps_per_filter) {
78         tmp_taps.push_back(0.0);
79     }
80 
81     // Partition the filter
82     for (i = 0; i < d_nfilts; i++) {
83         // Each channel uses all d_taps_per_filter with 0's if not enough taps to fill out
84         d_taps[i] = std::vector<float>(d_taps_per_filter, 0);
85         for (j = 0; j < d_taps_per_filter; j++) {
86             d_taps[i][j] = tmp_taps[i + j * d_nfilts];
87         }
88 
89         // Set the filter taps for each channel
90         d_fir_filters[i]->set_taps(d_taps[i]);
91         d_fft_filters[i]->set_taps(d_taps[i]);
92     }
93 }
94 
print_taps()95 void polyphase_filterbank::print_taps()
96 {
97     unsigned int i, j;
98     for (i = 0; i < d_nfilts; i++) {
99         printf("filter[%d]: [", i);
100         for (j = 0; j < d_taps_per_filter; j++) {
101             printf(" %.4e", d_taps[i][j]);
102         }
103         printf("]\n\n");
104     }
105 }
106 
taps() const107 std::vector<std::vector<float>> polyphase_filterbank::taps() const { return d_taps; }
108 
109 } /* namespace kernel */
110 } /* namespace filter */
111 } /* namespace gr */
112