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