GNU Radio 3.6.5.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  * \brief Polyphase filterbank arbitrary resampler with
40  * float input, float output and float taps
41  *
42  * This block takes in a signal stream and performs arbitrary
43  * resampling. The resampling rate can be any real
44  * number <EM>r</EM>. The resampling is done by constructing
45  * <EM>N</EM> filters where <EM>N</EM> is the interpolation rate. We
46  * then calculate <EM>D</EM> where <EM>D = floor(N/r)</EM>.
47  *
48  * Using <EM>N</EM> and <EM>D</EM>, we can perform rational resampling
49  * where <EM>N/D</EM> is a rational number close to the input rate
50  * <EM>r</EM> where we have <EM>N</EM> filters and we cycle through
51  * them as a polyphase filterbank with a stride of <EM>D</EM> so that
52  * <EM>i+1 = (i + D) % N</EM>.
53  *
54  * To get the arbitrary rate, we want to interpolate between two
55  * points. For each value out, we take an output from the current
56  * filter, <EM>i</EM>, and the next filter <EM>i+1</EM> and then
57  * linearly interpolate between the two based on the real resampling
58  * rate we want.
59  *
60  * The linear interpolation only provides us with an approximation to
61  * the real sampling rate specified. The error is a quantization error
62  * between the two filters we used as our interpolation points. To
63  * this end, the number of filters, <EM>N</EM>, used determines the
64  * quantization error; the larger <EM>N</EM>, the smaller the
65  * noise. You can design for a specified noise floor by setting the
66  * filter size (parameters <EM>filter_size</EM>). The size defaults to
67  * 32 filters, which is about as good as most implementations need.
68  *
69  * The trick with designing this filter is in how to specify the taps
70  * of the prototype filter. Like the PFB interpolator, the taps are
71  * specified using the interpolated filter rate. In this case, that
72  * rate is the input sample rate multiplied by the number of filters
73  * in the filterbank, which is also the interpolation rate. All other
74  * values should be relative to this rate.
75  *
76  * For example, for a 32-filter arbitrary resampler and using the
77  * GNU Radio's firdes utility to build the filter, we build a low-pass
78  * filter with a sampling rate of <EM>fs</EM>, a 3-dB bandwidth of
79  * <EM>BW</EM> and a transition bandwidth of <EM>TB</EM>. We can also
80  * specify the out-of-band attenuation to use, <EM>ATT</EM>, and the
81  * filter window function (a Blackman-harris window in this case). The
82  * first input is the gain of the filter, which we specify here as the
83  * interpolation rate (<EM>32</EM>).
84  *
85  * <B><EM>self._taps = gr.firdes.low_pass_2(32, 32*fs, BW, TB,
86  * attenuation_dB=ATT, window=gr.firdes.WIN_BLACKMAN_hARRIS)</EM></B>
87  *
88  * The theory behind this block can be found in Chapter 7.5 of
89  * the following book.
90  *
91  * <B><EM>f. harris, "Multirate Signal Processing for Communication
92  * Systems", Upper Saddle River, NJ: Prentice Hall, Inc. 2004.</EM></B>
93  */
94 
96 {
97  private:
98  /*!
99  * Build the polyphase filterbank arbitray resampler.
100  * \param rate (float) Specifies the resampling rate to use
101  * \param taps (vector/list of floats) The prototype filter to populate the filterbank. The taps
102  * should be generated at the filter_size sampling rate.
103  * \param filter_size (unsigned int) The number of filters in the filter bank. This is directly
104  related to quantization noise introduced during the resampling.
105  Defaults to 32 filters.
106  */
108  const std::vector<float> &taps,
109  unsigned int filter_size);
110 
111  std::vector<gr_fir_fff*> d_filters;
112  std::vector<gr_fir_fff*> d_diff_filters;
113  std::vector< std::vector<float> > d_taps;
114  std::vector< std::vector<float> > d_dtaps;
115  unsigned int d_int_rate; // the number of filters (interpolation rate)
116  unsigned int d_dec_rate; // the stride through the filters (decimation rate)
117  float d_flt_rate; // residual rate for the linear interpolation
118  float d_acc;
119  unsigned int d_last_filter;
120  int d_start_index;
121  unsigned int d_taps_per_filter;
122  bool d_updated;
123 
124  /*!
125  * Build the polyphase filterbank arbitray resampler.
126  * \param rate (float) Specifies the resampling rate to use
127  * \param taps (vector/list of floats) The prototype filter to populate the filterbank. The taps
128  * should be generated at the filter_size sampling rate.
129  * \param filter_size (unsigned int) The number of filters in the filter bank. This is directly
130  related to quantization noise introduced during the resampling.
131  Defaults to 32 filters.
132  */
133  gr_pfb_arb_resampler_fff (float rate,
134  const std::vector<float> &taps,
135  unsigned int filter_size);
136 
137  void create_diff_taps(const std::vector<float> &newtaps,
138  std::vector<float> &difftaps);
139 
140  /*!
141  * Resets the filterbank's filter taps with the new prototype filter
142  * \param newtaps (vector of floats) The prototype filter to populate the filterbank.
143  * The taps should be generated at the interpolated sampling rate.
144  * \param ourtaps (vector of floats) Reference to our internal member of holding the taps.
145  * \param ourfilter (vector of filters) Reference to our internal filter to set the taps for.
146  */
147  void create_taps (const std::vector<float> &newtaps,
148  std::vector< std::vector<float> > &ourtaps,
149  std::vector<gr_fir_fff*> &ourfilter);
150 
151 
152 public:
154 
155  // FIXME: See about a set_taps function during runtime.
156 
157  /*!
158  * Print all of the filterbank taps to screen.
159  */
160  void print_taps();
161  void set_rate (float rate) {
162  d_dec_rate = (unsigned int)floor(d_int_rate/rate);
163  d_flt_rate = (d_int_rate/rate) - d_dec_rate;
164  set_relative_rate(rate);
165  }
166 
167  int general_work (int noutput_items,
168  gr_vector_int &ninput_items,
169  gr_vector_const_void_star &input_items,
170  gr_vector_void_star &output_items);
171 };
172 
173 #endif