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