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