core: added gr.pfb_clock_sync_fff based on _ccf version, updated example
[debian/gnuradio] / gnuradio-core / src / lib / filter / gr_pfb_clock_sync_fff.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 <cstdio>
28 #include <cmath>
29
30 #include <gr_pfb_clock_sync_fff.h>
31 #include <gr_fir_fff.h>
32 #include <gr_fir_util.h>
33 #include <gr_io_signature.h>
34 #include <gr_math.h>
35
36 gr_pfb_clock_sync_fff_sptr gr_make_pfb_clock_sync_fff (double sps, float gain,
37                                                        const std::vector<float> &taps,
38                                                        unsigned int filter_size,
39                                                        float init_phase,
40                                                        float max_rate_deviation)
41 {
42   return gr_pfb_clock_sync_fff_sptr (new gr_pfb_clock_sync_fff (sps, gain, taps,
43                                                                 filter_size,
44                                                                 init_phase,
45                                                                 max_rate_deviation));
46 }
47
48 static int ios[] = {sizeof(float), sizeof(float), sizeof(float), sizeof(float)};
49 static std::vector<int> iosig(ios, ios+sizeof(ios)/sizeof(int));
50 gr_pfb_clock_sync_fff::gr_pfb_clock_sync_fff (double sps, float gain,
51                                               const std::vector<float> &taps,
52                                               unsigned int filter_size,
53                                               float init_phase,
54                                               float max_rate_deviation)
55   : gr_block ("pfb_clock_sync_fff",
56               gr_make_io_signature (1, 1, sizeof(float)),
57               gr_make_io_signaturev (1, 4, iosig)),
58     d_updated (false), d_nfilters(filter_size),
59     d_max_dev(max_rate_deviation)
60 {
61   d_nfilters = filter_size;
62   d_sps = floor(sps);
63
64   // Store the last filter between calls to work
65   // The accumulator keeps track of overflow to increment the stride correctly.
66   // set it here to the fractional difference based on the initial phaes
67   set_alpha(gain);
68   set_beta(0.25*gain*gain);
69   d_k = init_phase;
70   d_rate = (sps-floor(sps))*(double)d_nfilters;
71   d_rate_i = (int)floor(d_rate);
72   d_rate_f = d_rate - (float)d_rate_i;
73   d_filtnum = (int)floor(d_k);
74
75   d_filters = std::vector<gr_fir_fff*>(d_nfilters);
76   d_diff_filters = std::vector<gr_fir_fff*>(d_nfilters);
77
78   // Create an FIR filter for each channel and zero out the taps
79   std::vector<float> vtaps(0, d_nfilters);
80   for(int i = 0; i < d_nfilters; i++) {
81     d_filters[i] = gr_fir_util::create_gr_fir_fff(vtaps);
82     d_diff_filters[i] = gr_fir_util::create_gr_fir_fff(vtaps);
83   }
84
85   // Now, actually set the filters' taps
86   std::vector<float> dtaps;
87   create_diff_taps(taps, dtaps);
88   set_taps(taps, d_taps, d_filters);
89   set_taps(dtaps, d_dtaps, d_diff_filters);
90 }
91
92 gr_pfb_clock_sync_fff::~gr_pfb_clock_sync_fff ()
93 {
94   for(int i = 0; i < d_nfilters; i++) {
95     delete d_filters[i];
96   }
97 }
98
99 void
100 gr_pfb_clock_sync_fff::set_taps (const std::vector<float> &newtaps,
101                                  std::vector< std::vector<float> > &ourtaps,
102                                  std::vector<gr_fir_fff*> &ourfilter)
103 {
104   int i,j;
105
106   unsigned int ntaps = newtaps.size();
107   d_taps_per_filter = (unsigned int)ceil((double)ntaps/(double)d_nfilters);
108
109   // Create d_numchan vectors to store each channel's taps
110   ourtaps.resize(d_nfilters);
111   
112   // Make a vector of the taps plus fill it out with 0's to fill
113   // each polyphase filter with exactly d_taps_per_filter
114   std::vector<float> tmp_taps;
115   tmp_taps = newtaps;
116   while((float)(tmp_taps.size()) < d_nfilters*d_taps_per_filter) {
117     tmp_taps.push_back(0.0);
118   }
119   
120   // Partition the filter
121   for(i = 0; i < d_nfilters; i++) {
122     // Each channel uses all d_taps_per_filter with 0's if not enough taps to fill out
123     ourtaps[d_nfilters-1-i] = std::vector<float>(d_taps_per_filter, 0);
124     for(j = 0; j < d_taps_per_filter; j++) {
125       ourtaps[d_nfilters - 1 - i][j] = tmp_taps[i + j*d_nfilters];
126     }
127     
128     // Build a filter for each channel and add it's taps to it
129     ourfilter[i]->set_taps(ourtaps[d_nfilters-1-i]);
130   }
131
132   // Set the history to ensure enough input items for each filter
133   set_history (d_taps_per_filter + d_sps);
134
135   d_updated = true;
136 }
137
138 void
139 gr_pfb_clock_sync_fff::create_diff_taps(const std::vector<float> &newtaps,
140                                         std::vector<float> &difftaps)
141 {
142   float maxtap = 1e-20;
143   difftaps.clear();
144   difftaps.push_back(0); //newtaps[0]);
145   for(unsigned int i = 1; i < newtaps.size()-1; i++) {
146     float tap = newtaps[i+1] - newtaps[i-1];
147     difftaps.push_back(tap);
148     if(tap > maxtap) {
149       maxtap = tap;
150     }
151   }
152   difftaps.push_back(0);//-newtaps[newtaps.size()-1]);
153
154   // Scale the differential taps; helps scale error term to better update state
155   // FIXME: should this be scaled this way or use the same gain as the taps?
156   for(unsigned int i = 0; i < difftaps.size(); i++) {
157     difftaps[i] /= maxtap;
158   }
159 }
160
161 void
162 gr_pfb_clock_sync_fff::print_taps()
163 {
164   int i, j;
165   printf("[ ");
166   for(i = 0; i < d_nfilters; i++) {
167     printf("[%.4e, ", d_taps[i][0]);
168     for(j = 1; j < d_taps_per_filter-1; j++) {
169       printf("%.4e,", d_taps[i][j]);
170     }
171     printf("%.4e],", d_taps[i][j]);
172   }
173   printf(" ]\n");
174 }
175
176 void
177 gr_pfb_clock_sync_fff::print_diff_taps()
178 {
179   int i, j;
180   printf("[ ");
181   for(i = 0; i < d_nfilters; i++) {
182     printf("[%.4e, ", d_dtaps[i][0]);
183     for(j = 1; j < d_taps_per_filter-1; j++) {
184       printf("%.4e,", d_dtaps[i][j]);
185     }
186     printf("%.4e],", d_dtaps[i][j]);
187   }
188   printf(" ]\n");
189 }
190
191
192 std::vector<float>
193 gr_pfb_clock_sync_fff::channel_taps(int channel)
194 {
195   std::vector<float> taps;
196   for(int i = 0; i < d_taps_per_filter; i++) {
197     taps.push_back(d_taps[channel][i]);
198   }
199   return taps;
200 }
201
202 std::vector<float>
203 gr_pfb_clock_sync_fff::diff_channel_taps(int channel)
204 {
205   std::vector<float> taps;
206   for(int i = 0; i < d_taps_per_filter; i++) {
207     taps.push_back(d_dtaps[channel][i]);
208   }
209   return taps;
210 }
211
212
213 int
214 gr_pfb_clock_sync_fff::general_work (int noutput_items,
215                                      gr_vector_int &ninput_items,
216                                      gr_vector_const_void_star &input_items,
217                                      gr_vector_void_star &output_items)
218 {
219   float *in = (float *) input_items[0];
220   float *out = (float *) output_items[0];
221
222   float *err, *outrate, *outk;
223   if(output_items.size() > 2) {
224     err = (float *) output_items[1];
225     outrate = (float*)output_items[2];
226     outk = (float*)output_items[3];
227   }
228   
229   if (d_updated) {
230     d_updated = false;
231     return 0;                // history requirements may have changed.
232   }
233
234   // We need this many to process one output
235   int nrequired = ninput_items[0] - d_taps_per_filter;
236
237   int i = 0, count = 0;
238   float error;
239
240   // produce output as long as we can and there are enough input samples
241   while((i < noutput_items) && (count < nrequired)) {
242     d_filtnum = (int)floor(d_k);
243
244     // Keep the current filter number in [0, d_nfilters]
245     // If we've run beyond the last filter, wrap around and go to next sample
246     // If we've go below 0, wrap around and go to previous sample
247     while(d_filtnum >= d_nfilters) {
248       d_k -= d_nfilters;
249       d_filtnum -= d_nfilters;
250       count += 1;
251     }
252     while(d_filtnum < 0) {
253       d_k += d_nfilters;
254       d_filtnum += d_nfilters;
255       count -= 1;
256     }
257
258     out[i] = d_filters[d_filtnum]->filter(&in[count]);
259     float diff = d_diff_filters[d_filtnum]->filter(&in[count]);
260     error  = out[i] * diff;
261
262     // Run the control loop to update the current phase (k) and tracking rate
263     d_k = d_k + d_alpha*error + d_rate_i + d_rate_f;
264     d_rate_f = d_rate_f + d_beta*error;
265     
266     // Keep our rate within a good range
267     d_rate_f = gr_branchless_clip(d_rate_f, d_max_dev);
268
269     i++;
270     count += (int)floor(d_sps);
271
272     if(output_items.size() > 2) {
273       err[i] = error;
274       outrate[i] = d_rate_f;
275       outk[i] = d_k;
276     }
277   }
278   consume_each(count);
279
280   return i;
281 }