Merge branch 'upstream' into dfsg-orig
[debian/gnuradio] / gnuradio-core / src / lib / filter / gr_pfb_clock_sync_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 <cstdio>
28 #include <cmath>
29
30 #include <gr_pfb_clock_sync_ccf.h>
31 #include <gr_fir_ccf.h>
32 #include <gr_fir_util.h>
33 #include <gr_io_signature.h>
34 #include <gr_math.h>
35
36 gr_pfb_clock_sync_ccf_sptr gr_make_pfb_clock_sync_ccf (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_ccf_sptr (new gr_pfb_clock_sync_ccf (sps, gain, taps,
43                                                                 filter_size,
44                                                                 init_phase,
45                                                                 max_rate_deviation));
46 }
47
48 static int ios[] = {sizeof(gr_complex), sizeof(float), sizeof(float), sizeof(float)};
49 static std::vector<int> iosig(ios, ios+sizeof(ios)/sizeof(int));
50 gr_pfb_clock_sync_ccf::gr_pfb_clock_sync_ccf (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_ccf",
56               gr_make_io_signature (1, 1, sizeof(gr_complex)),
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_ccf*>(d_nfilters);
76   d_diff_filters = std::vector<gr_fir_ccf*>(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_ccf(vtaps);
82     d_diff_filters[i] = gr_fir_util::create_gr_fir_ccf(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_ccf::~gr_pfb_clock_sync_ccf ()
93 {
94   for(int i = 0; i < d_nfilters; i++) {
95     delete d_filters[i];
96     delete d_diff_filters[i];
97   }
98 }
99
100 bool
101 gr_pfb_clock_sync_ccf::check_topology(int ninputs, int noutputs)
102 {
103   return noutputs == 1 || noutputs == 4;
104 }
105
106 void
107 gr_pfb_clock_sync_ccf::set_taps (const std::vector<float> &newtaps,
108                                  std::vector< std::vector<float> > &ourtaps,
109                                  std::vector<gr_fir_ccf*> &ourfilter)
110 {
111   int i,j;
112
113   unsigned int ntaps = newtaps.size();
114   d_taps_per_filter = (unsigned int)ceil((double)ntaps/(double)d_nfilters);
115
116   // Create d_numchan vectors to store each channel's taps
117   ourtaps.resize(d_nfilters);
118   
119   // Make a vector of the taps plus fill it out with 0's to fill
120   // each polyphase filter with exactly d_taps_per_filter
121   std::vector<float> tmp_taps;
122   tmp_taps = newtaps;
123   while((float)(tmp_taps.size()) < d_nfilters*d_taps_per_filter) {
124     tmp_taps.push_back(0.0);
125   }
126   
127   // Partition the filter
128   for(i = 0; i < d_nfilters; i++) {
129     // Each channel uses all d_taps_per_filter with 0's if not enough taps to fill out
130     ourtaps[d_nfilters-1-i] = std::vector<float>(d_taps_per_filter, 0);
131     for(j = 0; j < d_taps_per_filter; j++) {
132       ourtaps[d_nfilters - 1 - i][j] = tmp_taps[i + j*d_nfilters];
133     }
134     
135     // Build a filter for each channel and add it's taps to it
136     ourfilter[i]->set_taps(ourtaps[d_nfilters-1-i]);
137   }
138
139   // Set the history to ensure enough input items for each filter
140   set_history (d_taps_per_filter + d_sps);
141
142   d_updated = true;
143 }
144
145 void
146 gr_pfb_clock_sync_ccf::create_diff_taps(const std::vector<float> &newtaps,
147                                         std::vector<float> &difftaps)
148 {
149   float maxtap = 1e-20;
150   difftaps.clear();
151   difftaps.push_back(0); //newtaps[0]);
152   for(unsigned int i = 1; i < newtaps.size()-1; i++) {
153     float tap = newtaps[i+1] - newtaps[i-1];
154     difftaps.push_back(tap);
155     if(tap > maxtap) {
156       maxtap = tap;
157     }
158   }
159   difftaps.push_back(0);//-newtaps[newtaps.size()-1]);
160
161   // Scale the differential taps; helps scale error term to better update state
162   // FIXME: should this be scaled this way or use the same gain as the taps?
163   for(unsigned int i = 0; i < difftaps.size(); i++) {
164     difftaps[i] /= maxtap;
165   }
166 }
167
168 void
169 gr_pfb_clock_sync_ccf::print_taps()
170 {
171   int i, j;
172   printf("[ ");
173   for(i = 0; i < d_nfilters; i++) {
174     printf("[%.4e, ", d_taps[i][0]);
175     for(j = 1; j < d_taps_per_filter-1; j++) {
176       printf("%.4e,", d_taps[i][j]);
177     }
178     printf("%.4e],", d_taps[i][j]);
179   }
180   printf(" ]\n");
181 }
182
183 void
184 gr_pfb_clock_sync_ccf::print_diff_taps()
185 {
186   int i, j;
187   printf("[ ");
188   for(i = 0; i < d_nfilters; i++) {
189     printf("[%.4e, ", d_dtaps[i][0]);
190     for(j = 1; j < d_taps_per_filter-1; j++) {
191       printf("%.4e,", d_dtaps[i][j]);
192     }
193     printf("%.4e],", d_dtaps[i][j]);
194   }
195   printf(" ]\n");
196 }
197
198
199 std::vector<float>
200 gr_pfb_clock_sync_ccf::channel_taps(int channel)
201 {
202   std::vector<float> taps;
203   for(int i = 0; i < d_taps_per_filter; i++) {
204     taps.push_back(d_taps[channel][i]);
205   }
206   return taps;
207 }
208
209 std::vector<float>
210 gr_pfb_clock_sync_ccf::diff_channel_taps(int channel)
211 {
212   std::vector<float> taps;
213   for(int i = 0; i < d_taps_per_filter; i++) {
214     taps.push_back(d_dtaps[channel][i]);
215   }
216   return taps;
217 }
218
219
220 int
221 gr_pfb_clock_sync_ccf::general_work (int noutput_items,
222                                      gr_vector_int &ninput_items,
223                                      gr_vector_const_void_star &input_items,
224                                      gr_vector_void_star &output_items)
225 {
226   gr_complex *in = (gr_complex *) input_items[0];
227   gr_complex *out = (gr_complex *) output_items[0];
228
229   float *err = 0, *outrate = 0, *outk = 0;
230   if(output_items.size() == 4) {
231     err = (float *) output_items[1];
232     outrate = (float*)output_items[2];
233     outk = (float*)output_items[3];
234   }
235   
236   if (d_updated) {
237     d_updated = false;
238     return 0;                // history requirements may have changed.
239   }
240
241   // We need this many to process one output
242   int nrequired = ninput_items[0] - d_taps_per_filter;
243
244   int i = 0, count = 0;
245   float error, error_r, error_i;
246
247   // produce output as long as we can and there are enough input samples
248   while((i < noutput_items) && (count < nrequired)) {
249     d_filtnum = (int)floor(d_k);
250
251     // Keep the current filter number in [0, d_nfilters]
252     // If we've run beyond the last filter, wrap around and go to next sample
253     // If we've go below 0, wrap around and go to previous sample
254     while(d_filtnum >= d_nfilters) {
255       d_k -= d_nfilters;
256       d_filtnum -= d_nfilters;
257       count += 1;
258     }
259     while(d_filtnum < 0) {
260       d_k += d_nfilters;
261       d_filtnum += d_nfilters;
262       count -= 1;
263     }
264
265     out[i] = d_filters[d_filtnum]->filter(&in[count]);
266     gr_complex diff = d_diff_filters[d_filtnum]->filter(&in[count]);
267     error_r  = out[i].real() * diff.real();
268     error_i  = out[i].imag() * diff.imag();
269     error = (error_i + error_r) / 2.0;       // average error from I&Q channel
270
271     // Run the control loop to update the current phase (k) and tracking rate
272     d_k = d_k + d_alpha*error + d_rate_i + d_rate_f;
273     d_rate_f = d_rate_f + d_beta*error;
274     
275     // Keep our rate within a good range
276     d_rate_f = gr_branchless_clip(d_rate_f, d_max_dev);
277
278     i++;
279     count += (int)floor(d_sps);
280
281     if(output_items.size() == 4) {
282       err[i] = error;
283       outrate[i] = d_rate_f;
284       outk[i] = d_k;
285     }
286   }
287   consume_each(count);
288
289   return i;
290 }