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