GNU Radio 3.6.0 C++ API
gr_pfb_channelizer_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_CHANNELIZER_CCF_H
00025 #define INCLUDED_GR_PFB_CHANNELIZER_CCF_H
00026 
00027 #include <gr_core_api.h>
00028 #include <gr_block.h>
00029 #include <gruel/thread.h>
00030 
00031 class gr_pfb_channelizer_ccf;
00032 typedef boost::shared_ptr<gr_pfb_channelizer_ccf> gr_pfb_channelizer_ccf_sptr;
00033 GR_CORE_API gr_pfb_channelizer_ccf_sptr gr_make_pfb_channelizer_ccf (unsigned int numchans,
00034                                                          const std::vector<float> &taps,
00035                                                          float oversample_rate=1);
00036 
00037 class gr_fir_ccf;
00038 class gri_fft_complex;
00039 
00040 
00041 /*!
00042  * \class gr_pfb_channelizer_ccf
00043  *
00044  * \brief Polyphase filterbank channelizer with
00045  *        gr_complex input, gr_complex output and float taps
00046  *
00047  * \ingroup filter_blk
00048  * \ingroup pfb_blk
00049  *
00050  * This block takes in complex inputs and channelizes it to <EM>M</EM>
00051  * channels of equal bandwidth. Each of the resulting channels is
00052  * decimated to the new rate that is the input sampling rate
00053  * <EM>fs</EM> divided by the number of channels, <EM>M</EM>.
00054  *
00055  * The PFB channelizer code takes the taps generated above and builds
00056  * a set of filters. The set contains <EM>M</EM> number of filters
00057  * and each filter contains ceil(taps.size()/decim) number of taps.
00058  * Each tap from the filter prototype is sequentially inserted into
00059  * the next filter. When all of the input taps are used, the remaining
00060  * filters in the filterbank are filled out with 0's to make sure each
00061  * filter has the same number of taps.
00062  *
00063  * Each filter operates using the gr_fir filter classs of GNU Radio,
00064  * which takes the input stream at <EM>i</EM> and performs the inner
00065  * product calculation to <EM>i+(n-1)</EM> where <EM>n</EM> is the
00066  * number of filter taps. To efficiently handle this in the GNU Radio
00067  * structure, each filter input must come from its own input
00068  * stream. So the channelizer must be provided with <EM>M</EM> streams
00069  * where the input stream has been deinterleaved. This is most easily
00070  * done using the gr_stream_to_streams block.
00071  *
00072  * The output is then produced as a vector, where index <EM>i</EM> in
00073  * the vector is the next sample from the <EM>i</EM>th channel. This
00074  * is most easily handled by sending the output to a
00075  * gr_vector_to_streams block to handle the conversion and passing
00076  * <EM>M</EM> streams out.
00077  *
00078  * The input and output formatting is done using a hier_block2 called
00079  * pfb_channelizer_ccf. This can take in a single stream and outputs
00080  * <EM>M</EM> streams based on the behavior described above.
00081  *
00082  * The filter's taps should be based on the input sampling rate.
00083  *
00084  * For example, using the GNU Radio's firdes utility to building
00085  * filters, we build a low-pass filter with a sampling rate of
00086  * <EM>fs</EM>, a 3-dB bandwidth of <EM>BW</EM> and a transition
00087  * bandwidth of <EM>TB</EM>. We can also specify the out-of-band
00088  * attenuation to use, <EM>ATT</EM>, and the filter window
00089  * function (a Blackman-harris window in this case). The first input
00090  *  is the gain of the filter, which we specify here as unity.
00091  *
00092  *      <B><EM>self._taps = gr.firdes.low_pass_2(1, fs, BW, TB,
00093  *           attenuation_dB=ATT, window=gr.firdes.WIN_BLACKMAN_hARRIS)</EM></B>
00094  *
00095  * The filter output can also be overs ampled. The over sampling rate
00096  * is the ratio of the the actual output sampling rate to the normal
00097  * output sampling rate. It must be rationally related to the number
00098  * of channels as N/i for i in [1,N], which gives an outputsample rate
00099  * of [fs/N, fs] where fs is the input sample rate and N is the number
00100  * of channels.
00101  *
00102  * For example, for 6 channels with fs = 6000 Hz, the normal rate is
00103  * 6000/6 = 1000 Hz. Allowable oversampling rates are 6/6, 6/5, 6/4,
00104  * 6/3, 6/2, and 6/1 where the output sample rate of a 6/1 oversample
00105  * ratio is 6000 Hz, or 6 times the normal 1000 Hz. A rate of 6/5 = 1.2,
00106  * so the output rate would be 1200 Hz.
00107  *
00108  * The theory behind this block can be found in Chapter 6 of
00109  * the following book.
00110  *
00111  *    <B><EM>f. harris, "Multirate Signal Processing for Communication
00112  *       Systems," Upper Saddle River, NJ: Prentice Hall, Inc. 2004.</EM></B>
00113  *
00114  */
00115 
00116 class GR_CORE_API gr_pfb_channelizer_ccf : public gr_block
00117 {
00118  private:
00119   /*!
00120    * Build the polyphase filterbank decimator.
00121    * \param numchans (unsigned integer) Specifies the number of channels <EM>M</EM>
00122    * \param taps    (vector/list of floats) The prototype filter to populate the filterbank.
00123    * \param oversample_rate (float)   The over sampling rate is the ratio of the the actual
00124    *                                  output sampling rate to the normal output sampling rate.
00125    *                                   It must be rationally related to the number of channels
00126    *                                  as N/i for i in [1,N], which gives an outputsample rate
00127    *                                  of [fs/N, fs] where fs is the input sample rate and N is
00128    *                                  the number of channels.
00129    *
00130    *                                  For example, for 6 channels with fs = 6000 Hz, the normal
00131    *                                  rate is 6000/6 = 1000 Hz. Allowable oversampling rates
00132    *                                  are 6/6, 6/5, 6/4, 6/3, 6/2, and 6/1 where the output
00133    *                                  sample rate of a 6/1 oversample ratio is 6000 Hz, or
00134    *                                  6 times the normal 1000 Hz.
00135    */
00136   friend GR_CORE_API gr_pfb_channelizer_ccf_sptr gr_make_pfb_channelizer_ccf (unsigned int numchans,
00137                                                                   const std::vector<float> &taps,
00138                                                                   float oversample_rate);
00139 
00140   bool                     d_updated;
00141   unsigned int             d_numchans;
00142   float                    d_oversample_rate;
00143   std::vector<gr_fir_ccf*> d_filters;
00144   std::vector< std::vector<float> > d_taps;
00145   unsigned int             d_taps_per_filter;
00146   gri_fft_complex         *d_fft;
00147   int                     *d_idxlut;
00148   int                      d_rate_ratio;
00149   int                      d_output_multiple;
00150   std::vector<int>         d_channel_map;
00151   gruel::mutex             d_mutex; // mutex to protect set/work access
00152 
00153   /*!
00154    * Build the polyphase filterbank decimator.
00155    * \param numchans (unsigned integer) Specifies the number of channels <EM>M</EM>
00156    * \param taps    (vector/list of floats) The prototype filter to populate the filterbank.
00157    * \param oversample_rate (float)   The output over sampling rate.
00158    */
00159   gr_pfb_channelizer_ccf (unsigned int numchans,
00160                           const std::vector<float> &taps,
00161                           float oversample_rate);
00162 
00163 public:
00164   ~gr_pfb_channelizer_ccf ();
00165 
00166   /*!
00167    * Resets the filterbank's filter taps with the new prototype filter
00168    * \param taps    (vector/list of floats) The prototype filter to populate the filterbank.
00169    */
00170   void set_taps (const std::vector<float> &taps);
00171 
00172   /*!
00173    * Print all of the filterbank taps to screen.
00174    */
00175   void print_taps();
00176 
00177   /*!
00178    * Return a vector<vector<>> of the filterbank taps
00179    */
00180   std::vector<std::vector<float> > taps() const;
00181 
00182   /*!
00183    * Set the channel map. Channels are numbers as:
00184    *
00185    *     N/2+1 | ... | N-1 | 0 | 1 |  2 | ... | N/2
00186    *    <------------------- 0 -------------------->
00187    *                        freq
00188    *
00189    * So output stream 0 comes from channel 0, etc. Setting a new
00190    * channel map allows the user to specify which channel in frequency
00191    * he/she wants to got to which output stream.
00192    *
00193    * The map should have the same number of elements as the number of
00194    * output connections from the block. The minimum value of the map
00195    * is 0 (for the 0th channel) and the maximum number is N-1 where N
00196    * is the number of channels.
00197    *
00198    * We specify M as the number of output connections made where M <=
00199    * N, so only M out of N channels are driven to an output
00200    * stream. The number of items in the channel map should be at least
00201    * M long. If there are more channels specified, any value in the
00202    * map over M-1 will be ignored. If the size of the map is less than
00203    * M the behavior is unknown (we don't wish to check every entry
00204    * into the work function).
00205    *
00206    * This means that if the channelizer is splitting the signal up
00207    * into N channels but only M channels are specified in the map
00208    * (where M <= N), then M output streams must be connected and the
00209    * map and the channel numbers used must be less than N-1. Output
00210    * channel number can be reused, too. By default, the map is
00211    * [0...M-1] with M = N.
00212    */
00213   void set_channel_map(const std::vector<int> &map);
00214 
00215   /*!
00216    * Gets the current channel map.
00217    */
00218   std::vector<int> channel_map() const;
00219 
00220   int general_work (int noutput_items,
00221                     gr_vector_int &ninput_items,
00222                     gr_vector_const_void_star &input_items,
00223                     gr_vector_void_star &output_items);
00224 };
00225 
00226 #endif