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