GNU Radio 3.6.3.1 C++ API
gr_pfb_arb_resampler_fff.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2009-2011 Free Software Foundation, Inc.
4  *
5  * This file is part of GNU Radio
6  *
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  *
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING. If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 
24 #ifndef INCLUDED_GR_PFB_ARB_RESAMPLER_FFF_H
25 #define INCLUDED_GR_PFB_ARB_RESAMPLER_FFF_H
26 
27 #include <gr_core_api.h>
28 #include <gr_block.h>
29 
33  const std::vector<float> &taps,
34  unsigned int filter_size=32);
35 
36 class gr_fir_fff;
37 
38 /*!
39  * \class gr_pfb_arb_resampler_fff
40  *
41  * \brief Polyphase filterbank arbitrary resampler with
42  * float input, float output and float taps
43  *
44  * \ingroup filter_blk
45  * \ingroup pfb_blk
46  *
47  * This block takes in a signal stream and performs arbitrary
48  * resampling. The resampling rate can be any real
49  * number <EM>r</EM>. The resampling is done by constructing
50  * <EM>N</EM> filters where <EM>N</EM> is the interpolation rate. We
51  * then calculate <EM>D</EM> where <EM>D = floor(N/r)</EM>.
52  *
53  * Using <EM>N</EM> and <EM>D</EM>, we can perform rational resampling
54  * where <EM>N/D</EM> is a rational number close to the input rate
55  * <EM>r</EM> where we have <EM>N</EM> filters and we cycle through
56  * them as a polyphase filterbank with a stride of <EM>D</EM> so that
57  * <EM>i+1 = (i + D) % N</EM>.
58  *
59  * To get the arbitrary rate, we want to interpolate between two
60  * points. For each value out, we take an output from the current
61  * filter, <EM>i</EM>, and the next filter <EM>i+1</EM> and then
62  * linearly interpolate between the two based on the real resampling
63  * rate we want.
64  *
65  * The linear interpolation only provides us with an approximation to
66  * the real sampling rate specified. The error is a quantization error
67  * between the two filters we used as our interpolation points. To
68  * this end, the number of filters, <EM>N</EM>, used determines the
69  * quantization error; the larger <EM>N</EM>, the smaller the
70  * noise. You can design for a specified noise floor by setting the
71  * filter size (parameters <EM>filter_size</EM>). The size defaults to
72  * 32 filters, which is about as good as most implementations need.
73  *
74  * The trick with designing this filter is in how to specify the taps
75  * of the prototype filter. Like the PFB interpolator, the taps are
76  * specified using the interpolated filter rate. In this case, that
77  * rate is the input sample rate multiplied by the number of filters
78  * in the filterbank, which is also the interpolation rate. All other
79  * values should be relative to this rate.
80  *
81  * For example, for a 32-filter arbitrary resampler and using the
82  * GNU Radio's firdes utility to build the filter, we build a low-pass
83  * filter with a sampling rate of <EM>fs</EM>, a 3-dB bandwidth of
84  * <EM>BW</EM> and a transition bandwidth of <EM>TB</EM>. We can also
85  * specify the out-of-band attenuation to use, <EM>ATT</EM>, and the
86  * filter window function (a Blackman-harris window in this case). The
87  * first input is the gain of the filter, which we specify here as the
88  * interpolation rate (<EM>32</EM>).
89  *
90  * <B><EM>self._taps = gr.firdes.low_pass_2(32, 32*fs, BW, TB,
91  * attenuation_dB=ATT, window=gr.firdes.WIN_BLACKMAN_hARRIS)</EM></B>
92  *
93  * The theory behind this block can be found in Chapter 7.5 of
94  * the following book.
95  *
96  * <B><EM>f. harris, "Multirate Signal Processing for Communication
97  * Systems", Upper Saddle River, NJ: Prentice Hall, Inc. 2004.</EM></B>
98  */
99 
101 {
102  private:
103  /*!
104  * Build the polyphase filterbank arbitray resampler.
105  * \param rate (float) Specifies the resampling rate to use
106  * \param taps (vector/list of floats) The prototype filter to populate the filterbank. The taps
107  * should be generated at the filter_size sampling rate.
108  * \param filter_size (unsigned int) The number of filters in the filter bank. This is directly
109  related to quantization noise introduced during the resampling.
110  Defaults to 32 filters.
111  */
113  const std::vector<float> &taps,
114  unsigned int filter_size);
115 
116  std::vector<gr_fir_fff*> d_filters;
117  std::vector<gr_fir_fff*> d_diff_filters;
118  std::vector< std::vector<float> > d_taps;
119  std::vector< std::vector<float> > d_dtaps;
120  unsigned int d_int_rate; // the number of filters (interpolation rate)
121  unsigned int d_dec_rate; // the stride through the filters (decimation rate)
122  float d_flt_rate; // residual rate for the linear interpolation
123  float d_acc;
124  unsigned int d_last_filter;
125  int d_start_index;
126  unsigned int d_taps_per_filter;
127  bool d_updated;
128 
129  /*!
130  * Build the polyphase filterbank arbitray resampler.
131  * \param rate (float) Specifies the resampling rate to use
132  * \param taps (vector/list of floats) The prototype filter to populate the filterbank. The taps
133  * should be generated at the filter_size sampling rate.
134  * \param filter_size (unsigned int) The number of filters in the filter bank. This is directly
135  related to quantization noise introduced during the resampling.
136  Defaults to 32 filters.
137  */
138  gr_pfb_arb_resampler_fff (float rate,
139  const std::vector<float> &taps,
140  unsigned int filter_size);
141 
142  void create_diff_taps(const std::vector<float> &newtaps,
143  std::vector<float> &difftaps);
144 
145  /*!
146  * Resets the filterbank's filter taps with the new prototype filter
147  * \param newtaps (vector of floats) The prototype filter to populate the filterbank.
148  * The taps should be generated at the interpolated sampling rate.
149  * \param ourtaps (vector of floats) Reference to our internal member of holding the taps.
150  * \param ourfilter (vector of filters) Reference to our internal filter to set the taps for.
151  */
152  void create_taps (const std::vector<float> &newtaps,
153  std::vector< std::vector<float> > &ourtaps,
154  std::vector<gr_fir_fff*> &ourfilter);
155 
156 
157 public:
159 
160  // FIXME: See about a set_taps function during runtime.
161 
162  /*!
163  * Print all of the filterbank taps to screen.
164  */
165  void print_taps();
166  void set_rate (float rate) {
167  d_dec_rate = (unsigned int)floor(d_int_rate/rate);
168  d_flt_rate = (d_int_rate/rate) - d_dec_rate;
169  set_relative_rate(rate);
170  }
171 
172  int general_work (int noutput_items,
173  gr_vector_int &ninput_items,
174  gr_vector_const_void_star &input_items,
175  gr_vector_void_star &output_items);
176 };
177 
178 #endif