GNU Radio 3.6.5 C++ API

gr_pfb_arb_resampler_fff.h

Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2009-2011 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_ARB_RESAMPLER_FFF_H
00025 #define INCLUDED_GR_PFB_ARB_RESAMPLER_FFF_H
00026 
00027 #include <gr_core_api.h>
00028 #include <gr_block.h>
00029 
00030 class gr_pfb_arb_resampler_fff;
00031 typedef boost::shared_ptr<gr_pfb_arb_resampler_fff> gr_pfb_arb_resampler_fff_sptr;
00032 GR_CORE_API gr_pfb_arb_resampler_fff_sptr gr_make_pfb_arb_resampler_fff (float rate,
00033                                                              const std::vector<float> &taps,
00034                                                              unsigned int filter_size=32);
00035 
00036 class gr_fir_fff;
00037 
00038 /*!
00039  * \brief Polyphase filterbank arbitrary resampler with
00040  *        float input, float output and float taps
00041  *
00042  * This block takes in a signal stream and performs arbitrary
00043  * resampling. The resampling rate can be any real
00044  * number <EM>r</EM>. The resampling is done by constructing
00045  * <EM>N</EM> filters where <EM>N</EM> is the interpolation rate.  We
00046  * then calculate <EM>D</EM> where <EM>D = floor(N/r)</EM>.
00047  *
00048  * Using <EM>N</EM> and <EM>D</EM>, we can perform rational resampling
00049  * where <EM>N/D</EM> is a rational number close to the input rate
00050  * <EM>r</EM> where we have <EM>N</EM> filters and we cycle through
00051  * them as a polyphase filterbank with a stride of <EM>D</EM> so that
00052  * <EM>i+1 = (i + D) % N</EM>.
00053  *
00054  * To get the arbitrary rate, we want to interpolate between two
00055  * points. For each value out, we take an output from the current
00056  * filter, <EM>i</EM>, and the next filter <EM>i+1</EM> and then
00057  * linearly interpolate between the two based on the real resampling
00058  * rate we want.
00059  *
00060  * The linear interpolation only provides us with an approximation to
00061  * the real sampling rate specified. The error is a quantization error
00062  * between the two filters we used as our interpolation points.  To
00063  * this end, the number of filters, <EM>N</EM>, used determines the
00064  * quantization error; the larger <EM>N</EM>, the smaller the
00065  * noise. You can design for a specified noise floor by setting the
00066  * filter size (parameters <EM>filter_size</EM>). The size defaults to
00067  * 32 filters, which is about as good as most implementations need.
00068  *
00069  * The trick with designing this filter is in how to specify the taps
00070  * of the prototype filter. Like the PFB interpolator, the taps are
00071  * specified using the interpolated filter rate. In this case, that
00072  * rate is the input sample rate multiplied by the number of filters
00073  * in the filterbank, which is also the interpolation rate. All other
00074  * values should be relative to this rate.
00075  *
00076  * For example, for a 32-filter arbitrary resampler and using the
00077  * GNU Radio's firdes utility to build the filter, we build a low-pass
00078  * filter with a sampling rate of <EM>fs</EM>, a 3-dB bandwidth of
00079  * <EM>BW</EM> and a transition bandwidth of <EM>TB</EM>. We can also
00080  * specify the out-of-band attenuation to use, <EM>ATT</EM>, and the
00081  * filter window function (a Blackman-harris window in this case). The
00082  * first input is the gain of the filter, which we specify here as the
00083  * interpolation rate (<EM>32</EM>).
00084  *
00085  *      <B><EM>self._taps = gr.firdes.low_pass_2(32, 32*fs, BW, TB,
00086  *           attenuation_dB=ATT, window=gr.firdes.WIN_BLACKMAN_hARRIS)</EM></B>
00087  *
00088  * The theory behind this block can be found in Chapter 7.5 of
00089  * the following book.
00090  *
00091  *    <B><EM>f. harris, "Multirate Signal Processing for Communication
00092  *       Systems", Upper Saddle River, NJ: Prentice Hall, Inc. 2004.</EM></B>
00093  */
00094 
00095 class GR_CORE_API gr_pfb_arb_resampler_fff : public gr_block
00096 {
00097  private:
00098   /*!
00099    * Build the polyphase filterbank arbitray resampler.
00100    * \param rate  (float) Specifies the resampling rate to use
00101    * \param taps  (vector/list of floats) The prototype filter to populate the filterbank. The taps
00102    *                                      should be generated at the filter_size sampling rate.
00103    * \param filter_size (unsigned int) The number of filters in the filter bank. This is directly
00104                                        related to quantization noise introduced during the resampling.
00105                                        Defaults to 32 filters.
00106    */
00107   friend GR_CORE_API gr_pfb_arb_resampler_fff_sptr gr_make_pfb_arb_resampler_fff (float rate,
00108                                                                       const std::vector<float> &taps,
00109                                                                       unsigned int filter_size);
00110 
00111   std::vector<gr_fir_fff*> d_filters;
00112   std::vector<gr_fir_fff*> d_diff_filters;
00113   std::vector< std::vector<float> > d_taps;
00114   std::vector< std::vector<float> > d_dtaps;
00115   unsigned int             d_int_rate;          // the number of filters (interpolation rate)
00116   unsigned int             d_dec_rate;          // the stride through the filters (decimation rate)
00117   float                    d_flt_rate;          // residual rate for the linear interpolation
00118   float                    d_acc;
00119   unsigned int             d_last_filter;
00120   int                      d_start_index;
00121   unsigned int             d_taps_per_filter;
00122   bool                     d_updated;
00123 
00124   /*!
00125    * Build the polyphase filterbank arbitray resampler.
00126    * \param rate  (float) Specifies the resampling rate to use
00127    * \param taps  (vector/list of floats) The prototype filter to populate the filterbank. The taps
00128    *                                      should be generated at the filter_size sampling rate.
00129    * \param filter_size (unsigned int) The number of filters in the filter bank. This is directly
00130                                        related to quantization noise introduced during the resampling.
00131                                        Defaults to 32 filters.
00132    */
00133   gr_pfb_arb_resampler_fff (float rate,
00134                             const std::vector<float> &taps,
00135                             unsigned int filter_size);
00136 
00137   void create_diff_taps(const std::vector<float> &newtaps,
00138                         std::vector<float> &difftaps);
00139 
00140   /*!
00141    * Resets the filterbank's filter taps with the new prototype filter
00142    * \param newtaps    (vector of floats) The prototype filter to populate the filterbank.
00143    *                   The taps should be generated at the interpolated sampling rate.
00144    * \param ourtaps    (vector of floats) Reference to our internal member of holding the taps.
00145    * \param ourfilter  (vector of filters) Reference to our internal filter to set the taps for.
00146    */
00147   void create_taps (const std::vector<float> &newtaps,
00148                     std::vector< std::vector<float> > &ourtaps,
00149                     std::vector<gr_fir_fff*> &ourfilter);
00150 
00151 
00152 public:
00153   ~gr_pfb_arb_resampler_fff ();
00154 
00155   // FIXME: See about a set_taps function during runtime.
00156 
00157   /*!
00158    * Print all of the filterbank taps to screen.
00159    */
00160   void print_taps();
00161   void set_rate (float rate) {
00162     d_dec_rate = (unsigned int)floor(d_int_rate/rate);
00163     d_flt_rate = (d_int_rate/rate) - d_dec_rate;
00164     set_relative_rate(rate);
00165   }
00166 
00167   int general_work (int noutput_items,
00168                     gr_vector_int &ninput_items,
00169                     gr_vector_const_void_star &input_items,
00170                     gr_vector_void_star &output_items);
00171 };
00172 
00173 #endif