GNU Radio Manual and C++ API Reference  3.7.9.2
The Free & Open Software Radio Ecosystem
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
iir_filter.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2002,2012 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 #ifndef INCLUDED_IIR_FILTER_H
24 #define INCLUDED_IIR_FILTER_H
25 
26 #include <gnuradio/filter/api.h>
27 #include <gnuradio/gr_complex.h>
28 #include <vector>
29 #include <stdexcept>
30 
31 namespace gr {
32  namespace filter {
33  namespace kernel {
34 
35  /*!
36  * \brief Base class template for Infinite Impulse Response filter (IIR)
37  *
38  * \details
39  *
40  * This class provides a templated kernel for IIR filters. These
41  * iir_filters can be instantiated with a set of feed-forward
42  * and feed-back taps in the constructor. We then call the
43  * iir_filter::filter function to add a new sample to the
44  * filter, or iir_filter::filter_n to add a vector of samples to
45  * be filtered.
46  *
47  * Instantiating a filter means defining the templates for the
48  * data types being processed by the filter. There are four templates:
49  *
50  * \li i_type the data type of the input data (i.e., float).
51  * \li o_type the data type of the output data (i.e., float).
52  * \li tap_type the data type of the filter taps (i.e., double).
53  * \li acc_type the data type of the internal accumulator (i.e., double).
54  *
55  * The acc_type is specified to control how data is handled
56  * internally in the filter. This should always be the highest
57  * precision data type of any of the first three. Often, IIR
58  * filters require double-precision values in the taps for
59  * stability, and so the internal accumulator should also be
60  * double precision.
61  *
62  * Example:
63  *
64  * \code
65  * gr::filter::kernel::iir_filter<float,float,double,double> iir_filt(fftaps, fbtaps);
66  * ...
67  * float y = iir_filt.filter(x);
68  *
69  * <or>
70  *
71  * iir_filt.filter(y, x, N); // y and x are float arrays
72  * \endcode
73  *
74  * Another example for handling complex samples with
75  * double-precision taps (see filter::iir_filter_ccz):
76  *
77  * \code
78  * gr:;filter::kernel::iir_filter<gr_complex, gr_complex, gr_complexd, gr_complexd> iir_filt(fftaps, fbtaps);
79  * \endcode
80  */
81  template<class i_type, class o_type, class tap_type, class acc_type>
83  {
84  public:
85  /*!
86  * \brief Construct an IIR with the given taps.
87  *
88  * This filter uses the Direct Form I implementation, where
89  * \p fftaps contains the feed-forward taps, and \p fbtaps the feedback ones.
90  *
91  * \p fftaps and \p fbtaps must have equal numbers of taps
92  *
93  * \p oldstyle: The old style of the IIR filter uses feedback
94  * taps that are negative of what most definitions use (scipy
95  * and Matlab among them). This parameter keeps using the old
96  * GNU Radio style and is set to TRUE by default. When taps
97  * generated from scipy, Matlab, or gr_filter_design, use the
98  * new style by setting this to FALSE.
99  *
100  * The input and output satisfy a difference equation of the form
101 
102  \f[
103  y[n] \pm \sum_{k=1}^{M} a_k y[n-k] = \sum_{k=0}^{N} b_k x[n-k]
104  \f]
105 
106  * with the corresponding rational system function
107 
108  \f[
109  H(z) = \frac{\sum_{k=0}^{N} b_k z^{-k}}{1 \pm \sum_{k=1}^{M} a_k z^{-k}}
110  \f]
111  */
112  iir_filter(const std::vector<tap_type>& fftaps,
113  const std::vector<tap_type>& fbtaps,
114  bool oldstyle=true)
115  throw (std::invalid_argument)
116  {
117  d_oldstyle = oldstyle;
118  set_taps(fftaps, fbtaps);
119  }
120 
121  iir_filter() : d_latest_n(0),d_latest_m(0) { }
122 
124 
125  /*!
126  * \brief compute a single output value.
127  * \returns the filtered input value.
128  */
129  o_type filter(const i_type input);
130 
131  /*!
132  * \brief compute an array of N output values.
133  * \p input must have N valid entries.
134  */
135  void filter_n(o_type output[], const i_type input[], long n);
136 
137  /*!
138  * \return number of taps in filter.
139  */
140  unsigned ntaps_ff() const { return d_fftaps.size(); }
141  unsigned ntaps_fb() const { return d_fbtaps.size(); }
142 
143  /*!
144  * \brief install new taps.
145  */
146  void set_taps(const std::vector<tap_type> &fftaps,
147  const std::vector<tap_type> &fbtaps)
148  throw (std::invalid_argument)
149  {
150  d_latest_n = 0;
151  d_latest_m = 0;
152  d_fftaps = fftaps;
153 
154  if(d_oldstyle) {
155  d_fbtaps = fbtaps;
156  }
157  else {
158  // New style negates taps a[1:N-1] to fit with most IIR
159  // tap generating programs.
160  d_fbtaps.resize(fbtaps.size());
161  d_fbtaps[0] = fbtaps[0];
162  for(size_t i = 1; i < fbtaps.size(); i++) {
163  d_fbtaps[i] = -fbtaps[i];
164  }
165  }
166 
167  int n = fftaps.size();
168  int m = fbtaps.size();
169  d_prev_input.clear();
170  d_prev_output.clear();
171  d_prev_input.resize(2 * n, 0);
172  d_prev_output.resize(2 * m, 0);
173  }
174 
175  protected:
177  std::vector<tap_type> d_fftaps;
178  std::vector<tap_type> d_fbtaps;
181  std::vector<acc_type> d_prev_output;
182  std::vector<i_type> d_prev_input;
183  };
184 
185  //
186  // general case. We may want to specialize this
187  //
188  template<class i_type, class o_type, class tap_type, class acc_type>
189  o_type
191  {
192  acc_type acc;
193  unsigned i = 0;
194  unsigned n = ntaps_ff();
195  unsigned m = ntaps_fb();
196 
197  if(n == 0)
198  return (o_type)0;
199 
200  int latest_n = d_latest_n;
201  int latest_m = d_latest_m;
202 
203  acc = d_fftaps[0] * input;
204  for(i = 1; i < n; i ++)
205  acc += (d_fftaps[i] * d_prev_input[latest_n + i]);
206  for(i = 1; i < m; i ++)
207  acc += (d_fbtaps[i] * d_prev_output[latest_m + i]);
208 
209  // store the values twice to avoid having to handle wrap-around in the loop
210  d_prev_output[latest_m] = acc;
211  d_prev_output[latest_m+m] = acc;
212  d_prev_input[latest_n] = input;
213  d_prev_input[latest_n+n] = input;
214 
215  latest_n--;
216  latest_m--;
217  if(latest_n < 0)
218  latest_n += n;
219  if(latest_m < 0)
220  latest_m += m;
221 
222  d_latest_m = latest_m;
223  d_latest_n = latest_n;
224  return (o_type)acc;
225  }
226 
227  template<class i_type, class o_type, class tap_type, class acc_type>
228  void
230  const i_type input[],
231  long n)
232  {
233  for(int i = 0; i < n; i++)
234  output[i] = filter(input[i]);
235  }
236 
237  template<>
238  gr_complex
240 
241  template<>
242  gr_complex
244 
245  template<>
246  gr_complex
248 
249  } /* namespace kernel */
250  } /* namespace filter */
251 } /* namespace gr */
252 
253 #endif /* INCLUDED_IIR_FILTER_H */
std::vector< i_type > d_prev_input
Definition: iir_filter.h:182
std::vector< tap_type > d_fftaps
Definition: iir_filter.h:177
o_type filter(const i_type input)
compute a single output value.
Definition: iir_filter.h:190
iir_filter()
Definition: iir_filter.h:121
std::vector< acc_type > d_prev_output
Definition: iir_filter.h:181
Base class template for Infinite Impulse Response filter (IIR)
Definition: iir_filter.h:82
STL namespace.
void set_taps(const std::vector< tap_type > &fftaps, const std::vector< tap_type > &fbtaps)
install new taps.
Definition: iir_filter.h:146
unsigned ntaps_ff() const
Definition: iir_filter.h:140
std::complex< float > gr_complex
Definition: gr_complex.h:27
Include this header to use the message passing features.
Definition: logger.h:131
int d_latest_n
Definition: iir_filter.h:179
unsigned ntaps_fb() const
Definition: iir_filter.h:141
bool d_oldstyle
Definition: iir_filter.h:176
iir_filter(const std::vector< tap_type > &fftaps, const std::vector< tap_type > &fbtaps, bool oldstyle=true)
Construct an IIR with the given taps.
Definition: iir_filter.h:112
void filter_n(o_type output[], const i_type input[], long n)
compute an array of N output values. input must have N valid entries.
Definition: iir_filter.h:229
~iir_filter()
Definition: iir_filter.h:123
std::vector< tap_type > d_fbtaps
Definition: iir_filter.h:178
int d_latest_m
Definition: iir_filter.h:180
#define FILTER_API
Definition: gr-filter/include/gnuradio/filter/api.h:30