Once and for all, here is the properly updated Makefile.am for the apps
[debian/gnuradio] / gnuradio-core / src / lib / filter / gr_pfb_channelizer_ccf.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2009 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 {
38   return gr_pfb_channelizer_ccf_sptr (new gr_pfb_channelizer_ccf (numchans, taps));
39 }
40
41
42 gr_pfb_channelizer_ccf::gr_pfb_channelizer_ccf (unsigned int numchans, 
43                                       const std::vector<float> &taps)
44   : gr_sync_block ("pfb_channelizer_ccf",
45                    gr_make_io_signature (numchans, numchans, sizeof(gr_complex)),
46                    gr_make_io_signature (1, 1, numchans*sizeof(gr_complex))),
47     d_updated (false)
48 {
49   d_numchans = numchans;
50   d_filters = std::vector<gr_fir_ccf*>(d_numchans);
51
52   // Create an FIR filter for each channel and zero out the taps
53   std::vector<float> vtaps(0, d_numchans);
54   for(unsigned int i = 0; i < d_numchans; i++) {
55     d_filters[i] = gr_fir_util::create_gr_fir_ccf(vtaps);
56   }
57
58   // Now, actually set the filters' taps
59   set_taps(taps);
60
61   // Create the FFT to handle the output de-spinning of the channels
62   d_fft = new gri_fft_complex (d_numchans, false);
63 }
64
65 gr_pfb_channelizer_ccf::~gr_pfb_channelizer_ccf ()
66 {
67   for(unsigned int i = 0; i < d_numchans; i++) {
68     delete d_filters[i];
69   }
70 }
71
72 void
73 gr_pfb_channelizer_ccf::set_taps (const std::vector<float> &taps)
74 {
75   unsigned int i,j;
76
77   unsigned int ntaps = taps.size();
78   d_taps_per_filter = (unsigned int)ceil((double)ntaps/(double)d_numchans);
79
80   // Create d_numchan vectors to store each channel's taps
81   d_taps.resize(d_numchans);
82
83   // Make a vector of the taps plus fill it out with 0's to fill
84   // each polyphase filter with exactly d_taps_per_filter
85   std::vector<float> tmp_taps;
86   tmp_taps = taps;
87   while((float)(tmp_taps.size()) < d_numchans*d_taps_per_filter) {
88     tmp_taps.push_back(0.0);
89   }
90  
91   // Partition the filter
92   for(i = 0; i < d_numchans; i++) {
93     // Each channel uses all d_taps_per_filter with 0's if not enough taps to fill out
94     d_taps[i] = std::vector<float>(d_taps_per_filter, 0);
95     for(j = 0; j < d_taps_per_filter; j++) {
96       d_taps[i][j] = tmp_taps[i + j*d_numchans];  // add taps to channels in reverse order
97     }
98     
99     // Build a filter for each channel and add it's taps to it
100     d_filters[i]->set_taps(d_taps[i]);
101   }
102
103   // Set the history to ensure enough input items for each filter
104   set_history (d_taps_per_filter);
105
106   d_updated = true;
107 }
108
109 void
110 gr_pfb_channelizer_ccf::print_taps()
111 {
112   unsigned int i, j;
113   for(i = 0; i < d_numchans; i++) {
114     printf("filter[%d]: [", i);
115     for(j = 0; j < d_taps_per_filter; j++) {
116       printf(" %.4e", d_taps[i][j]);
117     }
118     printf("]\n\n");
119   }
120 }
121
122
123 int
124 gr_pfb_channelizer_ccf::work (int noutput_items,
125                               gr_vector_const_void_star &input_items,
126                               gr_vector_void_star &output_items)
127 {
128   gr_complex *in = (gr_complex *) input_items[0];
129   gr_complex *out = (gr_complex *) output_items[0];
130
131   if (d_updated) {
132     d_updated = false;
133     return 0;                // history requirements may have changed.
134   }
135
136   for(int i = 0; i < noutput_items; i++) {
137     // Move through filters from bottom to top
138     for(int j = d_numchans-1; j >= 0; j--) {
139       // Take in the items from the first input stream to d_numchans
140       in = (gr_complex*)input_items[d_numchans - 1 - j];
141
142       // Filter current input stream from bottom filter to top
143       d_fft->get_inbuf()[j] = d_filters[j]->filter(&in[i]);
144     }
145
146     // despin through FFT
147     d_fft->execute();
148     memcpy(&out[d_numchans*i], d_fft->get_outbuf(), d_numchans*sizeof(gr_complex));
149   }
150   
151   return noutput_items;
152 }