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