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