GNU Radio Manual and C++ API Reference  3.8.1.0
The Free & Open Software Radio Ecosystem
single_pole_iir.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2002,2006,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_SINGLE_POLE_IIR_H
24 #define INCLUDED_SINGLE_POLE_IIR_H
25 
26 #include <gnuradio/filter/api.h>
27 #include <gnuradio/gr_complex.h>
28 #include <stdexcept>
29 
30 namespace gr {
31 namespace filter {
32 
33 /*!
34  * \brief class template for single pole IIR filter
35  */
36 template <class o_type, class i_type, class tap_type>
38 {
39 public:
40  /*!
41  * \brief construct new single pole IIR with given alpha
42  *
43  * computes y(i) = (1-alpha) * y(i-1) + alpha * x(i)
44  */
45  single_pole_iir(tap_type alpha = 1.0)
46  {
47  d_prev_output = 0;
48  set_taps(alpha);
49  }
50 
51  /*!
52  * \brief compute a single output value.
53  * \returns the filtered input value.
54  */
55  o_type filter(const i_type input);
56 
57  /*!
58  * \brief compute an array of N output values.
59  * \p input must have n valid entries.
60  */
61  void filterN(o_type output[], const i_type input[], unsigned long n);
62 
63  /*!
64  * \brief install \p alpha as the current taps.
65  */
66  void set_taps(tap_type alpha)
67  {
68  if (alpha < 0 || alpha > 1)
69  throw std::out_of_range("Alpha must be in [0, 1]\n");
70 
71  d_alpha = alpha;
72  d_one_minus_alpha = 1.0 - alpha;
73  }
74 
75  //! reset state to zero
76  void reset() { d_prev_output = 0; }
77 
78  o_type prev_output() const { return d_prev_output; }
79 
80 protected:
81  tap_type d_alpha;
83  o_type d_prev_output;
84 };
85 
86 //
87 // general case. We may want to specialize this
88 //
89 template <class o_type, class i_type, class tap_type>
91 {
92  o_type output;
93 
94  output = d_alpha * input + d_one_minus_alpha * d_prev_output;
95  d_prev_output = output;
96 
97  return (o_type)output;
98 }
99 
100 
101 template <class o_type, class i_type, class tap_type>
103  const i_type input[],
104  unsigned long n)
105 {
106  for (unsigned i = 0; i < n; i++)
107  output[i] = filter(input[i]);
108 }
109 
110 
111 //
112 // Specialized case for gr_complex output and double taps
113 // We need to have a gr_complexd type for the calculations and prev_output variable (in
114 // stead of double)
115 
116 template <class i_type>
117 class single_pole_iir<gr_complex, i_type, double>
118 {
119 public:
120  /*!
121  * \brief construct new single pole IIR with given alpha
122  *
123  * computes y(i) = (1-alpha) * y(i-1) + alpha * x(i)
124  */
125  single_pole_iir(double alpha = 1.0)
126  {
127  d_prev_output = 0;
128  set_taps(alpha);
129  }
130 
131  /*!
132  * \brief compute a single output value.
133  * \returns the filtered input value.
134  */
135  gr_complex filter(const i_type input);
136 
137  /*!
138  * \brief compute an array of N output values.
139  * \p input must have n valid entries.
140  */
141  void filterN(gr_complex output[], const i_type input[], unsigned long n);
142 
143  /*!
144  * \brief install \p alpha as the current taps.
145  */
146  void set_taps(double alpha)
147  {
148  if (alpha < 0 || alpha > 1)
149  throw std::out_of_range("Alpha must be in [0, 1]\n");
150 
151  d_alpha = alpha;
152  d_one_minus_alpha = 1.0 - alpha;
153  }
154 
155  //! reset state to zero
156  void reset() { d_prev_output = 0; }
157 
159 
160 protected:
161  double d_alpha;
164 };
165 
166 template <class i_type>
168 {
169  gr_complexd output;
170 
171  output = d_alpha * (gr_complexd)input + d_one_minus_alpha * d_prev_output;
172  d_prev_output = output;
173 
174  return (gr_complex)output;
175 }
176 
177 // Do we need to specialize this, although it is the same as the general case?
178 
179 template <class i_type>
181  const i_type input[],
182  unsigned long n)
183 {
184  for (unsigned i = 0; i < n; i++)
185  output[i] = filter(input[i]);
186 }
187 
188 } /* namespace filter */
189 } /* namespace gr */
190 
191 #endif /* INCLUDED_SINGLE_POLE_IIR_H */
void reset()
reset state to zero
Definition: single_pole_iir.h:156
single_pole_iir(tap_type alpha=1.0)
construct new single pole IIR with given alpha
Definition: single_pole_iir.h:45
o_type d_prev_output
Definition: single_pole_iir.h:83
std::complex< double > gr_complexd
Definition: gr_complex.h:28
class template for single pole IIR filter
Definition: single_pole_iir.h:37
gr_complexd prev_output() const
Definition: single_pole_iir.h:158
single_pole_iir(double alpha=1.0)
construct new single pole IIR with given alpha
Definition: single_pole_iir.h:125
std::complex< float > gr_complex
Definition: gr_complex.h:27
void set_taps(double alpha)
install alpha as the current taps.
Definition: single_pole_iir.h:146
GNU Radio logging wrapper for log4cpp library (C++ port of log4j)
Definition: basic_block.h:43
gr_complexd d_prev_output
Definition: single_pole_iir.h:163
void filterN(o_type output[], const i_type input[], unsigned long n)
compute an array of N output values. input must have n valid entries.
Definition: single_pole_iir.h:102
tap_type d_alpha
Definition: single_pole_iir.h:81
double d_one_minus_alpha
Definition: single_pole_iir.h:162
o_type filter(const i_type input)
compute a single output value.
Definition: single_pole_iir.h:90
o_type prev_output() const
Definition: single_pole_iir.h:78
tap_type d_one_minus_alpha
Definition: single_pole_iir.h:82
double d_alpha
Definition: single_pole_iir.h:161
void reset()
reset state to zero
Definition: single_pole_iir.h:76
void set_taps(tap_type alpha)
install alpha as the current taps.
Definition: single_pole_iir.h:66