]> git.gag.com Git - debian/gnuradio/blob - gnuradio-core/src/lib/filter/gr_pfb_clock_sync_ccf.cc
3af53fed5b7f408451a933f9419728993312adc2
[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 (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 int ios[] = {sizeof(gr_complex), sizeof(float), sizeof(float), sizeof(float)};
49 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_filtnum = (int)floor(d_k);
72
73   d_filters = std::vector<gr_fir_ccf*>(d_nfilters);
74   d_diff_filters = std::vector<gr_fir_ccf*>(d_nfilters);
75
76   // Create an FIR filter for each channel and zero out the taps
77   std::vector<float> vtaps(0, d_nfilters);
78   for(unsigned int i = 0; i < d_nfilters; i++) {
79     d_filters[i] = gr_fir_util::create_gr_fir_ccf(vtaps);
80     d_diff_filters[i] = gr_fir_util::create_gr_fir_ccf(vtaps);
81   }
82
83   // Now, actually set the filters' taps
84   std::vector<float> dtaps;
85   create_diff_taps(taps, dtaps);
86   set_taps(taps, d_taps, d_filters);
87   set_taps(dtaps, d_dtaps, d_diff_filters);
88 }
89
90 gr_pfb_clock_sync_ccf::~gr_pfb_clock_sync_ccf ()
91 {
92   for(unsigned int i = 0; i < d_nfilters; i++) {
93     delete d_filters[i];
94   }
95 }
96
97 void
98 gr_pfb_clock_sync_ccf::set_taps (const std::vector<float> &newtaps,
99                                  std::vector< std::vector<float> > &ourtaps,
100                                  std::vector<gr_fir_ccf*> &ourfilter)
101 {
102   int i,j;
103
104   unsigned int ntaps = newtaps.size();
105   d_taps_per_filter = (unsigned int)ceil((double)ntaps/(double)d_nfilters);
106
107   // Create d_numchan vectors to store each channel's taps
108   ourtaps.resize(d_nfilters);
109   
110   // Make a vector of the taps plus fill it out with 0's to fill
111   // each polyphase filter with exactly d_taps_per_filter
112   std::vector<float> tmp_taps;
113   tmp_taps = newtaps;
114   while((float)(tmp_taps.size()) < d_nfilters*d_taps_per_filter) {
115     tmp_taps.push_back(0.0);
116   }
117   
118   // Partition the filter
119   for(i = 0; i < d_nfilters; i++) {
120     // Each channel uses all d_taps_per_filter with 0's if not enough taps to fill out
121     //ourtaps[i] = std::vector<float>(d_taps_per_filter, 0);
122     ourtaps[d_nfilters-1-i] = std::vector<float>(d_taps_per_filter, 0);
123     for(j = 0; j < d_taps_per_filter; j++) {
124       ourtaps[d_nfilters - 1 - i][j] = tmp_taps[i + j*d_nfilters];
125     }
126     
127     // Build a filter for each channel and add it's taps to it
128     //ourfilter[i]->set_taps(ourtaps[i]);
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_ccf::create_diff_taps(const std::vector<float> &newtaps,
140                                         std::vector<float> &difftaps)
141 {
142   float maxtap = -1e12;
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     if(tap > maxtap) {
148      maxtap = tap;
149     }
150     //maxtap += tap;
151     difftaps.push_back(tap);
152   }
153   difftaps.push_back(0);//-newtaps[newtaps.size()-1]);
154
155   for(unsigned int i = 0; i < difftaps.size(); i++) {
156     difftaps[i] /= 1;//maxtap;
157   }
158 }
159
160 void
161 gr_pfb_clock_sync_ccf::print_taps()
162 {
163   unsigned int i, j;
164   printf("[ ");
165   for(i = 0; i < d_nfilters; i++) {
166     printf("[%.4e, ", d_taps[i][0]);
167     for(j = 1; j < d_taps_per_filter-1; j++) {
168       printf("%.4e,", d_taps[i][j]);
169     }
170     printf("%.4e],", d_taps[i][j]);
171   }
172   printf(" ]\n");
173 }
174
175 void
176 gr_pfb_clock_sync_ccf::print_diff_taps()
177 {
178   unsigned int i, j;
179   printf("[ ");
180   for(i = 0; i < d_nfilters; i++) {
181     printf("[%.4e, ", d_dtaps[i][0]);
182     for(j = 1; j < d_taps_per_filter-1; j++) {
183       printf("%.4e,", d_dtaps[i][j]);
184     }
185     printf("%.4e],", d_dtaps[i][j]);
186   }
187   printf(" ]\n");
188 }
189
190
191 std::vector<float>
192 gr_pfb_clock_sync_ccf::channel_taps(int channel)
193 {
194   std::vector<float> taps;
195   unsigned int i;
196   for(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_ccf::diff_channel_taps(int channel)
204 {
205   std::vector<float> taps;
206   unsigned int i;
207   for(i = 0; i < d_taps_per_filter; i++) {
208     taps.push_back(d_dtaps[channel][i]);
209   }
210   return taps;
211 }
212
213
214 int
215 gr_pfb_clock_sync_ccf::general_work (int noutput_items,
216                                      gr_vector_int &ninput_items,
217                                      gr_vector_const_void_star &input_items,
218                                      gr_vector_void_star &output_items)
219 {
220   gr_complex *in = (gr_complex *) input_items[0];
221   gr_complex *out = (gr_complex *) output_items[0];
222
223   float *err, *outrate, *outk;
224   if(output_items.size() > 2) {
225     err = (float *) output_items[1];
226     outrate = (float*)output_items[2];
227     outk = (float*)output_items[3];
228   }
229   
230   if (d_updated) {
231     d_updated = false;
232     return 0;                // history requirements may have changed.
233   }
234
235   // We need this many to process one output
236   int nrequired = ninput_items[0] - d_taps_per_filter;
237
238   int i = 0, count = 0;
239   float error, error_r, error_i;
240
241   // produce output as long as we can and there are enough input samples
242   while((i < noutput_items) && (count < nrequired)) {
243     d_filtnum = (int)floor(d_k);
244
245     // Keep the current filter number in [0, d_nfilters]
246     // If we've run beyond the last filter, wrap around and go to next sample
247     // If we've go below 0, wrap around and go to previous sample
248     while(d_filtnum >= d_nfilters) {
249       d_k -= d_nfilters;
250       d_filtnum -= d_nfilters;
251       count += 1;
252     }
253     while(d_filtnum < 0) {
254       d_k += d_nfilters;
255       d_filtnum += d_nfilters;
256       count -= 1;
257     }
258
259     out[i] = d_filters[d_filtnum]->filter(&in[count]);
260     gr_complex diff = d_diff_filters[d_filtnum]->filter(&in[count]);
261     error_r  = out[i].real() * diff.real();
262     error_i  = out[i].imag() * diff.imag();
263     error = error_i + error_r;
264
265     d_k = d_k + d_alpha*error + d_rate;
266     d_rate = d_rate + d_beta*error;
267     
268     // Keep our rate within a good range
269     d_rate = gr_branchless_clip(d_rate, d_max_dev);
270
271     i++;
272     count += (int)floor(d_sps);
273
274     if(output_items.size() > 2) {
275       err[i] = error;
276       outrate[i] = d_rate;
277       outk[i] = d_k;
278     }
279   }
280   consume_each(count);
281
282   return i;
283 }