GNU Radio 3.6.5 C++ API

gr_pfb_clock_sync_fff.h

Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2009,2010 Free Software Foundation, Inc.
00004  *
00005  * This file is part of GNU Radio
00006  *
00007  * GNU Radio is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 3, or (at your option)
00010  * any later version.
00011  *
00012  * GNU Radio is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with GNU Radio; see the file COPYING.  If not, write to
00019  * the Free Software Foundation, Inc., 51 Franklin Street,
00020  * Boston, MA 02110-1301, USA.
00021  */
00022 
00023 
00024 #ifndef INCLUDED_GR_PFB_CLOCK_SYNC_FFF_H
00025 #define INCLUDED_GR_PFB_CLOCK_SYNC_FFF_H
00026 
00027 #include <gr_core_api.h>
00028 #include <gr_block.h>
00029 
00030 class gr_pfb_clock_sync_fff;
00031 typedef boost::shared_ptr<gr_pfb_clock_sync_fff> gr_pfb_clock_sync_fff_sptr;
00032 GR_CORE_API gr_pfb_clock_sync_fff_sptr gr_make_pfb_clock_sync_fff (double sps, float gain,
00033                                                        const std::vector<float> &taps,
00034                                                        unsigned int filter_size=32,
00035                                                        float init_phase=0,
00036                                                        float max_rate_deviation=1.5);
00037 
00038 class gr_fir_fff;
00039 
00040 /*!
00041  * \brief Timing synchronizer using polyphase filterbanks
00042  *
00043  * This block performs timing synchronization for PAM signals by
00044  * minimizing the derivative of the filtered signal, which in turn
00045  * maximizes the SNR and minimizes ISI.
00046  *
00047  * This approach works by setting up two filterbanks; one filterbank
00048  * contains the signal's pulse shaping matched filter (such as a root
00049  * raised cosine filter), where each branch of the filterbank contains
00050  * a different phase of the filter.  The second filterbank contains
00051  * the derivatives of the filters in the first filterbank. Thinking of
00052  * this in the time domain, the first filterbank contains filters that
00053  * have a sinc shape to them. We want to align the output signal to be
00054  * sampled at exactly the peak of the sinc shape. The derivative of
00055  * the sinc contains a zero at the maximum point of the sinc (sinc(0)
00056  * = 1, sinc(0)' = 0).  Furthermore, the region around the zero point
00057  * is relatively linear. We make use of this fact to generate the
00058  * error signal.
00059  *
00060  * If the signal out of the derivative filters is d_i[n] for the ith
00061  * filter, and the output of the matched filter is x_i[n], we
00062  * calculate the error as: e[n] = (Re{x_i[n]} * Re{d_i[n]} +
00063  * Im{x_i[n]} * Im{d_i[n]}) / 2.0 This equation averages the error in
00064  * the real and imaginary parts. There are two reasons we multiply by
00065  * the signal itself. First, if the symbol could be positive or
00066  * negative going, but we want the error term to always tell us to go
00067  * in the same direction depending on which side of the zero point we
00068  * are on. The sign of x_i[n] adjusts the error term to do
00069  * this. Second, the magnitude of x_i[n] scales the error term
00070  * depending on the symbol's amplitude, so larger signals give us a
00071  * stronger error term because we have more confidence in that
00072  * symbol's value.  Using the magnitude of x_i[n] instead of just the
00073  * sign is especially good for signals with low SNR.
00074  *
00075  * The error signal, e[n], gives us a value proportional to how far
00076  * away from the zero point we are in the derivative signal. We want
00077  * to drive this value to zero, so we set up a second order loop. We
00078  * have two variables for this loop; d_k is the filter number in the
00079  * filterbank we are on and d_rate is the rate which we travel through
00080  * the filters in the steady state. That is, due to the natural clock
00081  * differences between the transmitter and receiver, d_rate represents
00082  * that difference and would traverse the filter phase paths to keep
00083  * the receiver locked. Thinking of this as a second-order PLL, the
00084  * d_rate is the frequency and d_k is the phase. So we update d_rate
00085  * and d_k using the standard loop equations based on two error
00086  * signals, d_alpha and d_beta.  We have these two values set based on
00087  * each other for a critically damped system, so in the block
00088  * constructor, we just ask for "gain," which is d_alpha while d_beta
00089  * is equal to (gain^2)/4.
00090  *
00091  * The block's parameters are:
00092  *
00093  * \li \p sps: The clock sync block needs to know the number of samples per
00094  * symbol, because it defaults to return a single point representing
00095  * the symbol. The sps can be any positive real number and does not
00096  * need to be an integer.
00097  *
00098  * \li \p loop_bw: The loop bandwidth is used to set the gain of the
00099  * inner control loop (see:
00100  * http://gnuradio.squarespace.com/blog/2011/8/13/control-loop-gain-values.html).
00101  * This should be set small (a value of around 2pi/100 is suggested in
00102  * that blog post as the step size for the number of radians around
00103  * the unit circle to move relative to the error).
00104  *
00105  * \li \p taps: One of the most important parameters for this block is
00106  * the taps of the filter. One of the benefits of this algorithm is
00107  * that you can put the matched filter in here as the taps, so you get
00108  * both the matched filter and sample timing correction in one go. So
00109  * create your normal matched filter. For a typical digital
00110  * modulation, this is a root raised cosine filter. The number of taps
00111  * of this filter is based on how long you expect the channel to be;
00112  * that is, how many symbols do you want to combine to get the current
00113  * symbols energy back (there's probably a better way of stating
00114  * that). It's usually 5 to 10 or so. That gives you your filter, but
00115  * now we need to think about it as a filter with different phase
00116  * profiles in each filter. So take this number of taps and multiply
00117  * it by the number of filters. This is the number you would use to
00118  * create your prototype filter. When you use this in the PFB
00119  * filerbank, it segments these taps into the filterbanks in such a
00120  * way that each bank now represents the filter at different phases,
00121  * equally spaced at 2pi/N, where N is the number of filters.
00122  *
00123  * \li \p filter_size (default=32): The number of filters can also be
00124  * set and defaults to 32. With 32 filters, you get a good enough
00125  * resolution in the phase to produce very small, almost unnoticeable,
00126  * ISI.  Going to 64 filters can reduce this more, but after that
00127  * there is very little gained for the extra complexity.
00128  *
00129  * \li \p init_phase (default=0): The initial phase is another
00130  * settable parameter and refers to the filter path the algorithm
00131  * initially looks at (i.e., d_k starts at init_phase). This value
00132  * defaults to zero, but it might be useful to start at a different
00133  * phase offset, such as the mid-point of the filters.
00134  *
00135  * \li \p max_rate_deviation (default=1.5): The next parameter is the
00136  * max_rate_devitation, which defaults to 1.5. This is how far we
00137  * allow d_rate to swing, positive or negative, from 0. Constraining
00138  * the rate can help keep the algorithm from walking too far away to
00139  * lock during times when there is no signal.
00140  *
00141  * \li \p osps: note that unlike the ccf version of this algorithm,
00142  * this block does \a not have a setting for the number of output
00143  * samples per symbol. This is mostly because it should not be
00144  * necessary as the reason for having multiple output sps is to
00145  * perform equalization and the equalizers will take in complex
00146  * numbers in order to do magnitude and phase correction.
00147  */
00148 
00149 class GR_CORE_API gr_pfb_clock_sync_fff : public gr_block
00150 {
00151  private:
00152   /*!
00153    * Build the polyphase filterbank timing synchronizer.
00154    * \param sps (double) The number of samples per second in the incoming signal
00155    * \param gain (float) The alpha gain of the control loop; beta = (gain^2)/4 by default.
00156    * \param taps (vector<int>) The filter taps.
00157    * \param filter_size (uint) The number of filters in the filterbank (default = 32).
00158    * \param init_phase (float) The initial phase to look at, or which filter to start
00159    *                           with (default = 0).
00160    * \param max_rate_deviation (float) Distance from 0 d_rate can get (default = 1.5).
00161    *
00162    */
00163   friend GR_CORE_API gr_pfb_clock_sync_fff_sptr gr_make_pfb_clock_sync_fff (double sps, float gain,
00164                                                                 const std::vector<float> &taps,
00165                                                                 unsigned int filter_size,
00166                                                                 float init_phase,
00167                                                                 float max_rate_deviation);
00168 
00169   bool                     d_updated;
00170   double                   d_sps;
00171   double                   d_sample_num;
00172   float                    d_alpha;
00173   float                    d_beta;
00174   int                      d_nfilters;
00175   std::vector<gr_fir_fff*> d_filters;
00176   std::vector<gr_fir_fff*> d_diff_filters;
00177   std::vector< std::vector<float> > d_taps;
00178   std::vector< std::vector<float> > d_dtaps;
00179   float                    d_k;
00180   float                    d_rate;
00181   float                    d_rate_i;
00182   float                    d_rate_f;
00183   float                    d_max_dev;
00184   int                      d_filtnum;
00185   int                      d_taps_per_filter;
00186 
00187   /*!
00188    * Build the polyphase filterbank timing synchronizer.
00189    */
00190   gr_pfb_clock_sync_fff (double sps, float gain,
00191                          const std::vector<float> &taps,
00192                          unsigned int filter_size,
00193                          float init_phase,
00194                          float max_rate_deviation);
00195 
00196   void create_diff_taps(const std::vector<float> &newtaps,
00197                         std::vector<float> &difftaps);
00198 
00199 public:
00200   ~gr_pfb_clock_sync_fff ();
00201 
00202   /*!
00203    * Resets the filterbank's filter taps with the new prototype filter
00204    */
00205   void set_taps (const std::vector<float> &taps,
00206                  std::vector< std::vector<float> > &ourtaps,
00207                  std::vector<gr_fir_fff*> &ourfilter);
00208 
00209   /*!
00210    * Returns the taps of the matched filter
00211    */
00212   std::vector<float> channel_taps(int channel);
00213 
00214   /*!
00215    * Returns the taps in the derivative filter
00216    */
00217   std::vector<float> diff_channel_taps(int channel);
00218 
00219   /*!
00220    * Print all of the filterbank taps to screen.
00221    */
00222   void print_taps();
00223 
00224   /*!
00225    * Print all of the filterbank taps of the derivative filter to screen.
00226    */
00227   void print_diff_taps();
00228 
00229   /*!
00230    * Set the gain value alpha for the control loop
00231    */
00232   void set_alpha(float alpha)
00233   {
00234     d_alpha = alpha;
00235   }
00236 
00237   /*!
00238    * Set the gain value beta for the control loop
00239    */
00240   void set_beta(float beta)
00241   {
00242     d_beta = beta;
00243   }
00244 
00245   /*!
00246    * Set the maximum deviation from 0 d_rate can have
00247    */
00248   void set_max_rate_deviation(float m)
00249   {
00250     d_max_dev = m;
00251   }
00252 
00253   bool check_topology(int ninputs, int noutputs);
00254 
00255   int general_work (int noutput_items,
00256                     gr_vector_int &ninput_items,
00257                     gr_vector_const_void_star &input_items,
00258                     gr_vector_void_star &output_items);
00259 };
00260 
00261 #endif