GNU Radio's CYBERRADIO Package
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 <stdexcept>
28 #include <gnuradio/gr_complex.h>
29 
30 namespace gr {
31  namespace CyberRadio {
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  d_reset_output = true;
50  }
51 
52  /*!
53  * \brief compute a single output value.
54  * \returns the filtered input value.
55  */
56  o_type filter(const i_type input);
57 
58  /*!
59  * \brief compute an array of N output values.
60  * \p input must have n valid entries.
61  */
62  void filterN(o_type output[], const i_type input[], unsigned long n);
63 
64  /*!
65  * \brief install \p alpha as the current taps.
66  */
67  void set_taps(tap_type alpha)
68  {
69  if(alpha < 0 || alpha > 1)
70  throw std::out_of_range("Alpha must be in [0, 1]\n");
71 
72  d_alpha = alpha;
73  d_one_minus_alpha = 1.0 - alpha;
74  }
75 
76  //! reset state to zero
77  void reset()
78  {
79  d_prev_output = 0;
80  d_reset_output = true;
81  }
82 
83  o_type prev_output() const { return d_prev_output; }
84 
85  protected:
86  tap_type d_alpha;
88  o_type d_prev_output;
90  };
91 
92  //
93  // general case. We may want to specialize this
94  //
95  template<class o_type, class i_type, class tap_type>
96  o_type
98  {
99  o_type output;
100 
101  if (d_reset_output) {
102  output = input;
103  d_reset_output = false;
104  } else {
105  output = d_alpha * input + d_one_minus_alpha * d_prev_output;
106  }
107  d_prev_output = output;
108 
109  return (o_type) output;
110  }
111 
112 
113  template<class o_type, class i_type, class tap_type>
114  void
116  const i_type input[],
117  unsigned long n)
118  {
119  for(unsigned i = 0; i < n; i++)
120  output[i] = filter(input[i]);
121  }
122 
123 
124  //
125  // Specialized case for gr_complex output and double taps
126  // We need to have a gr_complexd type for the calculations and prev_output variable (in stead of double)
127 
128  template<class i_type>
129  class single_pole_iir<gr_complex, i_type, double>
130  {
131  public:
132  /*!
133  * \brief construct new single pole IIR with given alpha
134  *
135  * computes y(i) = (1-alpha) * y(i-1) + alpha * x(i)
136  */
137  single_pole_iir(double alpha = 1.0)
138  {
139  d_prev_output = 0;
140  set_taps(alpha);
141  }
142 
143  /*!
144  * \brief compute a single output value.
145  * \returns the filtered input value.
146  */
147  gr_complex filter(const i_type input);
148 
149  /*!
150  * \brief compute an array of N output values.
151  * \p input must have n valid entries.
152  */
153  void filterN(gr_complex output[], const i_type input[], unsigned long n);
154 
155  /*!
156  * \brief install \p alpha as the current taps.
157  */
158  void set_taps(double alpha)
159  {
160  if(alpha < 0 || alpha > 1)
161  throw std::out_of_range("Alpha must be in [0, 1]\n");
162 
163  d_alpha = alpha;
164  d_one_minus_alpha = 1.0 - alpha;
165  }
166 
167  //! reset state to zero
168  void reset()
169  {
170  d_prev_output = 0;
171  }
172 
173  gr_complexd prev_output() const { return d_prev_output; }
174 
175  protected:
176  double d_alpha;
178  gr_complexd d_prev_output;
179  };
180 
181  template< class i_type>
182  gr_complex
184  {
185  gr_complexd output;
186 
187  output = d_alpha * (gr_complexd)input + d_one_minus_alpha * d_prev_output;
188  d_prev_output = output;
189 
190  return (gr_complex) output;
191  }
192 
193  //Do we need to specialize this, although it is the same as the general case?
194 
195  template<class i_type>
196  void
198  const i_type input[],
199  unsigned long n)
200  {
201  for(unsigned i = 0; i < n; i++)
202  output[i] = filter(input[i]);
203  }
204 
205  } /* namespace filter */
206 } /* namespace gr */
207 
208 #endif /* INCLUDED_SINGLE_POLE_IIR_H */
bool d_reset_output
Definition: single_pole_iir.h:89
void reset()
reset state to zero
Definition: single_pole_iir.h:168
single_pole_iir(tap_type alpha=1.0)
construct new single pole IIR with given alpha
Definition: single_pole_iir.h:45
double d_one_minus_alpha
Definition: single_pole_iir.h:177
void reset()
reset state to zero
Definition: single_pole_iir.h:77
gr_complexd d_prev_output
Definition: single_pole_iir.h:178
single_pole_iir(double alpha=1.0)
construct new single pole IIR with given alpha
Definition: single_pole_iir.h:137
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:115
o_type filter(const i_type input)
compute a single output value.
Definition: single_pole_iir.h:97
tap_type d_one_minus_alpha
Definition: single_pole_iir.h:87
tap_type d_alpha
Definition: single_pole_iir.h:86
void set_taps(tap_type alpha)
install alpha as the current taps.
Definition: single_pole_iir.h:67
void set_taps(double alpha)
install alpha as the current taps.
Definition: single_pole_iir.h:158
Provides GNU Radio blocks.
Definition: NDR651_duc_sink_mk2.h:21
o_type d_prev_output
Definition: single_pole_iir.h:88
o_type prev_output() const
Definition: single_pole_iir.h:83
double d_alpha
Definition: single_pole_iir.h:176
class template for single pole IIR filter
Definition: single_pole_iir.h:37
gr_complexd prev_output() const
Definition: single_pole_iir.h:173