GNU Radio Manual and C++ API Reference  3.7.4.1
The Free & Open Software Radio Ecosystem
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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  template<class i_type, class o_type, class tap_type>
39  class iir_filter
40  {
41  public:
42  /*!
43  * \brief Construct an IIR with the given taps.
44  *
45  * This filter uses the Direct Form I implementation, where
46  * \p fftaps contains the feed-forward taps, and \p fbtaps the feedback ones.
47  *
48  * \p fftaps and \p fbtaps must have equal numbers of taps
49  *
50  * \p oldstyle: The old style of the IIR filter uses feedback
51  * taps that are negative of what most definitions use (scipy
52  * and Matlab among them). This parameter keeps using the old
53  * GNU Radio style and is set to TRUE by default. When taps
54  * generated from scipy, Matlab, or gr_filter_design, use the
55  * new style by setting this to FALSE.
56  *
57  * The input and output satisfy a difference equation of the form
58 
59  \f[
60  y[n] \pm \sum_{k=1}^{M} a_k y[n-k] = \sum_{k=0}^{N} b_k x[n-k]
61  \f]
62 
63  * with the corresponding rational system function
64 
65  \f[
66  H(z) = \frac{\sum_{k=0}^{N} b_k z^{-k}}{1 \pm \sum_{k=1}^{M} a_k z^{-k}}
67  \f]
68  */
69  iir_filter(const std::vector<tap_type>& fftaps,
70  const std::vector<tap_type>& fbtaps,
71  bool oldstyle=true)
72  throw (std::invalid_argument)
73  {
74  d_oldstyle = oldstyle;
75  set_taps(fftaps, fbtaps);
76  }
77 
79 
81 
82  /*!
83  * \brief compute a single output value.
84  * \returns the filtered input value.
85  */
86  o_type filter(const i_type input);
87 
88  /*!
89  * \brief compute an array of N output values.
90  * \p input must have N valid entries.
91  */
92  void filter_n(o_type output[], const i_type input[], long n);
93 
94  /*!
95  * \return number of taps in filter.
96  */
97  unsigned ntaps_ff() const { return d_fftaps.size(); }
98  unsigned ntaps_fb() const { return d_fbtaps.size(); }
99 
100  /*!
101  * \brief install new taps.
102  */
103  void set_taps(const std::vector<tap_type> &fftaps,
104  const std::vector<tap_type> &fbtaps)
105  throw (std::invalid_argument)
106  {
107  d_latest_n = 0;
108  d_latest_m = 0;
109  d_fftaps = fftaps;
110 
111  if(d_oldstyle) {
112  d_fbtaps = fbtaps;
113  }
114  else {
115  // New style negates taps a[1:N-1] to fit with most IIR
116  // tap generating programs.
117  d_fbtaps.resize(fbtaps.size());
118  d_fbtaps[0] = fbtaps[0];
119  for(size_t i = 1; i < fbtaps.size(); i++) {
120  d_fbtaps[i] = -fbtaps[i];
121  }
122  }
123 
124  int n = fftaps.size();
125  int m = fbtaps.size();
126  d_prev_input.clear();
127  d_prev_output.clear();
128  d_prev_input.resize(2 * n, 0);
129  d_prev_output.resize(2 * m, 0);
130  }
131 
132  protected:
134  std::vector<tap_type> d_fftaps;
135  std::vector<tap_type> d_fbtaps;
138  std::vector<o_type> d_prev_output;
139  std::vector<i_type> d_prev_input;
140  };
141 
142  //
143  // general case. We may want to specialize this
144  //
145  template<class i_type, class o_type, class tap_type>
146  o_type
148  {
149  o_type acc;
150  unsigned i = 0;
151  unsigned n = ntaps_ff();
152  unsigned m = ntaps_fb();
153 
154  if(n == 0)
155  return (o_type)0;
156 
157  int latest_n = d_latest_n;
158  int latest_m = d_latest_m;
159 
160  acc = d_fftaps[0] * input;
161  for(i = 1; i < n; i ++)
162  acc += (d_fftaps[i] * d_prev_input[latest_n + i]);
163  for(i = 1; i < m; i ++)
164  acc += (d_fbtaps[i] * d_prev_output[latest_m + i]);
165 
166  // store the values twice to avoid having to handle wrap-around in the loop
167  d_prev_output[latest_m] = acc;
168  d_prev_output[latest_m+m] = acc;
169  d_prev_input[latest_n] = input;
170  d_prev_input[latest_n+n] = input;
171 
172  latest_n--;
173  latest_m--;
174  if(latest_n < 0)
175  latest_n += n;
176  if(latest_m < 0)
177  latest_m += m;
178 
179  d_latest_m = latest_m;
180  d_latest_n = latest_n;
181  return (o_type)acc;
182  }
183 
184  template<class i_type, class o_type, class tap_type>
185  void
187  const i_type input[],
188  long n)
189  {
190  for(int i = 0; i < n; i++)
191  output[i] = filter(input[i]);
192  }
193 
194  template<>
195  gr_complex
197 
198  template<>
199  gr_complex
201 
202  template<>
203  gr_complex
205 
206  } /* namespace kernel */
207  } /* namespace filter */
208 } /* namespace gr */
209 
210 #endif /* INCLUDED_IIR_FILTER_H */
211 
int d_latest_m
Definition: iir_filter.h:137
o_type filter(const i_type input)
compute a single output value.
Definition: iir_filter.h:147
std::vector< i_type > d_prev_input
Definition: iir_filter.h:139
bool d_oldstyle
Definition: iir_filter.h:133
base class template for Infinite Impulse Response filter (IIR)
Definition: iir_filter.h:39
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:69
std::vector< tap_type > d_fftaps
Definition: iir_filter.h:134
void set_taps(const std::vector< tap_type > &fftaps, const std::vector< tap_type > &fbtaps)
install new taps.
Definition: iir_filter.h:103
std::complex< float > gr_complex
Definition: gr_complex.h:27
~iir_filter()
Definition: iir_filter.h:80
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:186
std::vector< tap_type > d_fbtaps
Definition: iir_filter.h:135
unsigned ntaps_ff() const
Definition: iir_filter.h:97
int d_latest_n
Definition: iir_filter.h:136
unsigned ntaps_fb() const
Definition: iir_filter.h:98
iir_filter()
Definition: iir_filter.h:78
std::vector< o_type > d_prev_output
Definition: iir_filter.h:138