Fixing signed/unsigned warnings.
[debian/gnuradio] / gnuradio-core / src / lib / filter / gr_pfb_interpolator_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_interpolator_ccf.h>
28 #include <gr_fir_ccf.h>
29 #include <gr_fir_util.h>
30 #include <gr_io_signature.h>
31 #include <cstdio>
32
33 gr_pfb_interpolator_ccf_sptr gr_make_pfb_interpolator_ccf (unsigned int interp, 
34                                                            const std::vector<float> &taps)
35 {
36   return gnuradio::get_initial_sptr(new gr_pfb_interpolator_ccf (interp, taps));
37 }
38
39
40 gr_pfb_interpolator_ccf::gr_pfb_interpolator_ccf (unsigned int interp, 
41                                                   const std::vector<float> &taps)
42   : gr_sync_interpolator ("pfb_interpolator_ccf",
43                           gr_make_io_signature (1, 1, sizeof(gr_complex)),
44                           gr_make_io_signature (1, 1, sizeof(gr_complex)),
45                           interp),
46     d_updated (false)
47 {
48   d_rate = interp;
49   d_filters = std::vector<gr_fir_ccf*>(d_rate);
50
51   // Create an FIR filter for each channel and zero out the taps
52   std::vector<float> vtaps(0, d_rate);
53   for(unsigned int i = 0; i < d_rate; i++) {
54     d_filters[i] = gr_fir_util::create_gr_fir_ccf(vtaps);
55   }
56
57   // Now, actually set the filters' taps
58   set_taps(taps);
59 }
60
61 gr_pfb_interpolator_ccf::~gr_pfb_interpolator_ccf ()
62 {
63   for(unsigned int i = 0; i < d_rate; i++) {
64     delete d_filters[i];
65   }
66 }
67
68 void
69 gr_pfb_interpolator_ccf::set_taps (const std::vector<float> &taps)
70 {
71   unsigned int i,j;
72
73   unsigned int ntaps = taps.size();
74   d_taps_per_filter = (unsigned int)ceil((double)ntaps/(double)d_rate);
75
76   // Create d_numchan vectors to store each channel's taps
77   //std::vector< std::vector<float> > vtaps(d_rate);
78   d_taps.resize(d_rate);
79
80   // Make a vector of the taps plus fill it out with 0's to fill
81   // each polyphase filter with exactly d_taps_per_filter
82   std::vector<float> tmp_taps;
83   tmp_taps = taps;
84   while((float)(tmp_taps.size()) < d_rate*d_taps_per_filter) {
85     tmp_taps.push_back(0.0);
86   }
87   
88   // Partition the filter
89   for(i = 0; i < d_rate; i++) {
90     // Each channel uses all d_taps_per_filter with 0's if not enough taps to fill out
91     d_taps[i] = std::vector<float>(d_taps_per_filter, 0);
92     for(j = 0; j < d_taps_per_filter; j++) {
93       d_taps[i][j] = tmp_taps[i + j*d_rate];  // add taps to channels in reverse order
94     }
95     
96     // Build a filter for each channel and add it's taps to it
97     d_filters[i]->set_taps(d_taps[i]);
98   }
99
100   // Set the history to ensure enough input items for each filter
101   set_history (d_taps_per_filter);
102
103   d_updated = true;
104 }
105
106 void
107 gr_pfb_interpolator_ccf::print_taps()
108 {
109   unsigned int i, j;
110   for(i = 0; i < d_rate; i++) {
111     printf("filter[%d]: [", i);
112     for(j = 0; j < d_taps_per_filter; j++) {
113       printf(" %.4e", d_taps[i][j]);
114     }
115     printf("]\n\n");
116   }
117 }
118
119 int
120 gr_pfb_interpolator_ccf::work (int noutput_items,
121                                gr_vector_const_void_star &input_items,
122                                gr_vector_void_star &output_items)
123 {
124   gr_complex *in = (gr_complex *) input_items[0];
125   gr_complex *out = (gr_complex *) output_items[0];
126
127   if (d_updated) {
128     d_updated = false;
129     return 0;                // history requirements may have changed.
130   }
131
132   int i = 0, count = 0;
133
134   while(i < noutput_items) {
135     for(unsigned int j = 0; j < d_rate; j++) {
136       out[i] = d_filters[j]->filter(&in[count]);
137       i++;
138     }
139     count++;
140   }
141   
142   return i;
143 }