GNU Radio 3.6.5.1 C++ API
digital_pfb_clock_sync_fff.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2009,2010,2012 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 
24 #ifndef INCLUDED_DIGITAL_PFB_CLOCK_SYNC_FFF_H
25 #define INCLUDED_DIGITAL_PFB_CLOCK_SYNC_FFF_H
26 
27 #include <digital_api.h>
28 #include <gr_block.h>
29 
33 digital_make_pfb_clock_sync_fff(double sps, float gain,
34  const std::vector<float> &taps,
35  unsigned int filter_size=32,
36  float init_phase=0,
37  float max_rate_deviation=1.5,
38  int osps=1);
39 
40 class gr_fir_fff;
41 
42 /*!
43  * \brief Timing synchronizer using polyphase filterbanks
44  * \ingroup synchronizers_blk
45  *
46  * \details
47  * This block performs timing synchronization for PAM signals by
48  * minimizing the derivative of the filtered signal, which in turn
49  * maximizes the SNR and minimizes ISI.
50  *
51  * This approach works by setting up two filterbanks; one filterbank
52  * contains the signal's pulse shaping matched filter (such as a root
53  * raised cosine filter), where each branch of the filterbank contains
54  * a different phase of the filter. The second filterbank contains
55  * the derivatives of the filters in the first filterbank. Thinking of
56  * this in the time domain, the first filterbank contains filters that
57  * have a sinc shape to them. We want to align the output signal to be
58  * sampled at exactly the peak of the sinc shape. The derivative of
59  * the sinc contains a zero at the maximum point of the sinc (sinc(0)
60  * = 1, sinc(0)' = 0). Furthermore, the region around the zero point
61  * is relatively linear. We make use of this fact to generate the
62  * error signal.
63  *
64  * If the signal out of the derivative filters is d_i[n] for the ith
65  * filter, and the output of the matched filter is x_i[n], we
66  * calculate the error as: e[n] = (Re{x_i[n]} * Re{d_i[n]} +
67  * Im{x_i[n]} * Im{d_i[n]}) / 2.0 This equation averages the error in
68  * the real and imaginary parts. There are two reasons we multiply by
69  * the signal itself. First, if the symbol could be positive or
70  * negative going, but we want the error term to always tell us to go
71  * in the same direction depending on which side of the zero point we
72  * are on. The sign of x_i[n] adjusts the error term to do
73  * this. Second, the magnitude of x_i[n] scales the error term
74  * depending on the symbol's amplitude, so larger signals give us a
75  * stronger error term because we have more confidence in that
76  * symbol's value. Using the magnitude of x_i[n] instead of just the
77  * sign is especially good for signals with low SNR.
78  *
79  * The error signal, e[n], gives us a value proportional to how far
80  * away from the zero point we are in the derivative signal. We want
81  * to drive this value to zero, so we set up a second order loop. We
82  * have two variables for this loop; d_k is the filter number in the
83  * filterbank we are on and d_rate is the rate which we travel through
84  * the filters in the steady state. That is, due to the natural clock
85  * differences between the transmitter and receiver, d_rate represents
86  * that difference and would traverse the filter phase paths to keep
87  * the receiver locked. Thinking of this as a second-order PLL, the
88  * d_rate is the frequency and d_k is the phase. So we update d_rate
89  * and d_k using the standard loop equations based on two error
90  * signals, d_alpha and d_beta. We have these two values set based on
91  * each other for a critically damped system, so in the block
92  * constructor, we just ask for "gain," which is d_alpha while d_beta
93  * is equal to (gain^2)/4.
94  *
95  * The block's parameters are:
96  *
97  * \li \p sps: The clock sync block needs to know the number of samples per
98  * symbol, because it defaults to return a single point representing
99  * the symbol. The sps can be any positive real number and does not
100  * need to be an integer.
101  *
102  * \li \p loop_bw: The loop bandwidth is used to set the gain of the
103  * inner control loop (see:
104  * http://gnuradio.squarespace.com/blog/2011/8/13/control-loop-gain-values.html).
105  * This should be set small (a value of around 2pi/100 is suggested in
106  * that blog post as the step size for the number of radians around
107  * the unit circle to move relative to the error).
108  *
109  * \li \p taps: One of the most important parameters for this block is
110  * the taps of the filter. One of the benefits of this algorithm is
111  * that you can put the matched filter in here as the taps, so you get
112  * both the matched filter and sample timing correction in one go. So
113  * create your normal matched filter. For a typical digital
114  * modulation, this is a root raised cosine filter. The number of taps
115  * of this filter is based on how long you expect the channel to be;
116  * that is, how many symbols do you want to combine to get the current
117  * symbols energy back (there's probably a better way of stating
118  * that). It's usually 5 to 10 or so. That gives you your filter, but
119  * now we need to think about it as a filter with different phase
120  * profiles in each filter. So take this number of taps and multiply
121  * it by the number of filters. This is the number you would use to
122  * create your prototype filter. When you use this in the PFB
123  * filerbank, it segments these taps into the filterbanks in such a
124  * way that each bank now represents the filter at different phases,
125  * equally spaced at 2pi/N, where N is the number of filters.
126  *
127  * \li \p filter_size (default=32): The number of filters can also be
128  * set and defaults to 32. With 32 filters, you get a good enough
129  * resolution in the phase to produce very small, almost unnoticeable,
130  * ISI. Going to 64 filters can reduce this more, but after that
131  * there is very little gained for the extra complexity.
132  *
133  * \li \p init_phase (default=0): The initial phase is another
134  * settable parameter and refers to the filter path the algorithm
135  * initially looks at (i.e., d_k starts at init_phase). This value
136  * defaults to zero, but it might be useful to start at a different
137  * phase offset, such as the mid-point of the filters.
138  *
139  * \li \p max_rate_deviation (default=1.5): The next parameter is the
140  * max_rate_devitation, which defaults to 1.5. This is how far we
141  * allow d_rate to swing, positive or negative, from 0. Constraining
142  * the rate can help keep the algorithm from walking too far away to
143  * lock during times when there is no signal.
144  *
145  * \li \p osps (default=1): The osps is the number of output samples
146  * per symbol. By default, the algorithm produces 1 sample per symbol,
147  * sampled at the exact sample value. This osps value was added to
148  * better work with equalizers, which do a better job of modeling the
149  * channel if they have 2 samps/sym.
150  */
151 
153 {
154  private:
155  /*!
156  * Build the polyphase filterbank timing synchronizer.
157  * \param sps (double) The number of samples per second in the incoming signal
158  * \param gain (float) The alpha gain of the control loop; beta = (gain^2)/4 by default.
159  * \param taps (vector<int>) The filter taps.
160  * \param filter_size (uint) The number of filters in the filterbank (default = 32).
161  * \param init_phase (float) The initial phase to look at, or which filter to start
162  * with (default = 0).
163  * \param max_rate_deviation (float) Distance from 0 d_rate can get (default = 1.5).
164  * \param osps (int) The number of output samples per symbol (default=1).
165  *
166  */
168  digital_make_pfb_clock_sync_fff(double sps, float gain,
169  const std::vector<float> &taps,
170  unsigned int filter_size,
171  float init_phase,
172  float max_rate_deviation,
173  int osps);
174 
175  bool d_updated;
176  double d_sps;
177  double d_sample_num;
178  float d_loop_bw;
179  float d_damping;
180  float d_alpha;
181  float d_beta;
182 
183  int d_nfilters;
184  int d_taps_per_filter;
185  std::vector<gr_fir_fff*> d_filters;
186  std::vector<gr_fir_fff*> d_diff_filters;
187  std::vector< std::vector<float> > d_taps;
188  std::vector< std::vector<float> > d_dtaps;
189 
190  float d_k;
191  float d_rate;
192  float d_rate_i;
193  float d_rate_f;
194  float d_max_dev;
195  int d_filtnum;
196  int d_osps;
197  float d_error;
198  int d_out_idx;
199 
200  /*!
201  * Build the polyphase filterbank timing synchronizer.
202  */
203  digital_pfb_clock_sync_fff(double sps, float gain,
204  const std::vector<float> &taps,
205  unsigned int filter_size,
206  float init_phase,
207  float max_rate_deviation,
208  int osps);
209 
210  void create_diff_taps(const std::vector<float> &newtaps,
211  std::vector<float> &difftaps);
212 
213 public:
215 
216  /*! \brief update the system gains from omega and eta
217  *
218  * This function updates the system gains based on the loop
219  * bandwidth and damping factor of the system.
220  * These two factors can be set separately through their own
221  * set functions.
222  */
223  void update_gains();
224 
225  /*!
226  * Resets the filterbank's filter taps with the new prototype filter
227  */
228  void set_taps(const std::vector<float> &taps,
229  std::vector< std::vector<float> > &ourtaps,
230  std::vector<gr_fir_fff*> &ourfilter);
231 
232  /*!
233  * Returns all of the taps of the matched filter
234  */
235  std::vector< std::vector<float> > get_taps();
236 
237  /*!
238  * Returns all of the taps of the derivative filter
239  */
240  std::vector< std::vector<float> > get_diff_taps();
241 
242  /*!
243  * Returns the taps of the matched filter for a particular channel
244  */
245  std::vector<float> get_channel_taps(int channel);
246 
247  /*!
248  * Returns the taps in the derivative filter for a particular channel
249  */
250  std::vector<float> get_diff_channel_taps(int channel);
251 
252  /*!
253  * Return the taps as a formatted string for printing
254  */
255  std::string get_taps_as_string();
256 
257  /*!
258  * Return the derivative filter taps as a formatted string for printing
259  */
260  std::string get_diff_taps_as_string();
261 
262 
263  /*******************************************************************
264  SET FUNCTIONS
265  *******************************************************************/
266 
267 
268  /*!
269  * \brief Set the loop bandwidth
270  *
271  * Set the loop filter's bandwidth to \p bw. This should be between
272  * 2*pi/200 and 2*pi/100 (in rads/samp). It must also be a positive
273  * number.
274  *
275  * When a new damping factor is set, the gains, alpha and beta, of the loop
276  * are recalculated by a call to update_gains().
277  *
278  * \param bw (float) new bandwidth
279  *
280  */
281  void set_loop_bandwidth(float bw);
282 
283  /*!
284  * \brief Set the loop damping factor
285  *
286  * Set the loop filter's damping factor to \p df. The damping factor
287  * should be sqrt(2)/2.0 for critically damped systems.
288  * Set it to anything else only if you know what you are doing. It must
289  * be a number between 0 and 1.
290  *
291  * When a new damping factor is set, the gains, alpha and beta, of the loop
292  * are recalculated by a call to update_gains().
293  *
294  * \param df (float) new damping factor
295  *
296  */
297  void set_damping_factor(float df);
298 
299  /*!
300  * \brief Set the loop gain alpha
301  *
302  * Set's the loop filter's alpha gain parameter.
303  *
304  * This value should really only be set by adjusting the loop bandwidth
305  * and damping factor.
306  *
307  * \param alpha (float) new alpha gain
308  *
309  */
310  void set_alpha(float alpha);
311 
312  /*!
313  * \brief Set the loop gain beta
314  *
315  * Set's the loop filter's beta gain parameter.
316  *
317  * This value should really only be set by adjusting the loop bandwidth
318  * and damping factor.
319  *
320  * \param beta (float) new beta gain
321  *
322  */
323  void set_beta(float beta);
324 
325  /*!
326  * Set the maximum deviation from 0 d_rate can have
327  */
328  void set_max_rate_deviation(float m)
329  {
330  d_max_dev = m;
331  }
332 
333  /*******************************************************************
334  GET FUNCTIONS
335  *******************************************************************/
336 
337  /*!
338  * \brief Returns the loop bandwidth
339  */
340  float get_loop_bandwidth() const;
341 
342  /*!
343  * \brief Returns the loop damping factor
344  */
345  float get_damping_factor() const;
346 
347  /*!
348  * \brief Returns the loop gain alpha
349  */
350  float get_alpha() const;
351 
352  /*!
353  * \brief Returns the loop gain beta
354  */
355  float get_beta() const;
356 
357  /*!
358  * \brief Returns the current clock rate
359  */
360  float get_clock_rate() const;
361 
362  /*******************************************************************
363  *******************************************************************/
364 
365  bool check_topology(int ninputs, int noutputs);
366 
367  int general_work(int noutput_items,
368  gr_vector_int &ninput_items,
369  gr_vector_const_void_star &input_items,
370  gr_vector_void_star &output_items);
371 };
372 
373 #endif