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 <gnuradio/gr_complex.h>
28#include <stdexcept>
29
30namespace gr {
31namespace CyberRadio {
32
33/*!
34 * \brief class template for single pole IIR filter
35 */
36template <class o_type, class i_type, class tap_type>
38{
39public:
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
85protected:
86 tap_type d_alpha;
90};
91
92//
93// general case. We may want to specialize this
94//
95template <class o_type, class i_type, class tap_type>
97{
98 o_type output;
99
100 if (d_reset_output) {
101 output = input;
102 d_reset_output = false;
103 } else {
104 output = d_alpha * input + d_one_minus_alpha * d_prev_output;
105 }
106 d_prev_output = output;
107
108 return (o_type)output;
109}
110
111
112template <class o_type, class i_type, class tap_type>
114 const i_type input[],
115 unsigned long n)
116{
117 for (unsigned i = 0; i < n; i++)
118 output[i] = filter(input[i]);
119}
120
121
122//
123// Specialized case for gr_complex output and double taps
124// We need to have a gr_complexd type for the calculations and prev_output variable (in
125// stead of double)
126
127template <class i_type>
128class single_pole_iir<gr_complex, i_type, double>
129{
130public:
131 /*!
132 * \brief construct new single pole IIR with given alpha
133 *
134 * computes y(i) = (1-alpha) * y(i-1) + alpha * x(i)
135 */
136 single_pole_iir(double alpha = 1.0)
137 {
138 d_prev_output = 0;
139 set_taps(alpha);
140 }
141
142 /*!
143 * \brief compute a single output value.
144 * \returns the filtered input value.
145 */
146 gr_complex filter(const i_type input);
147
148 /*!
149 * \brief compute an array of N output values.
150 * \p input must have n valid entries.
151 */
152 void filterN(gr_complex output[], const i_type input[], unsigned long n);
153
154 /*!
155 * \brief install \p alpha as the current taps.
156 */
157 void set_taps(double alpha)
158 {
159 if (alpha < 0 || alpha > 1)
160 throw std::out_of_range("Alpha must be in [0, 1]\n");
161
162 d_alpha = alpha;
163 d_one_minus_alpha = 1.0 - alpha;
164 }
165
166 //! reset state to zero
167 void reset() { d_prev_output = 0; }
168
169 gr_complexd prev_output() const { return d_prev_output; }
170
171protected:
172 double d_alpha;
174 gr_complexd d_prev_output;
175};
176
177template <class i_type>
179{
180 gr_complexd output;
181
182 output = d_alpha * (gr_complexd)input + d_one_minus_alpha * d_prev_output;
183 d_prev_output = output;
184
185 return (gr_complex)output;
186}
187
188// Do we need to specialize this, although it is the same as the general case?
189
190template <class i_type>
192 const i_type input[],
193 unsigned long n)
194{
195 for (unsigned i = 0; i < n; i++)
196 output[i] = filter(input[i]);
197}
198
199} // namespace CyberRadio
200} /* namespace gr */
201
202#endif /* INCLUDED_SINGLE_POLE_IIR_H */
gr_complex filter(const i_type input)
compute a single output value.
Definition single_pole_iir.h:178
void reset()
reset state to zero
Definition single_pole_iir.h:167
double d_one_minus_alpha
Definition single_pole_iir.h:173
void set_taps(double alpha)
install alpha as the current taps.
Definition single_pole_iir.h:157
gr_complexd prev_output() const
Definition single_pole_iir.h:169
single_pole_iir(double alpha=1.0)
construct new single pole IIR with given alpha
Definition single_pole_iir.h:136
gr_complexd d_prev_output
Definition single_pole_iir.h:174
bool d_reset_output
Definition single_pole_iir.h:89
o_type prev_output() const
Definition single_pole_iir.h:83
tap_type d_alpha
Definition single_pole_iir.h:86
tap_type d_one_minus_alpha
Definition single_pole_iir.h:87
void set_taps(tap_type alpha)
install alpha as the current taps.
Definition single_pole_iir.h:67
o_type d_prev_output
Definition single_pole_iir.h:88
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:113
single_pole_iir(tap_type alpha=1.0)
construct new single pole IIR with given alpha
Definition single_pole_iir.h:45
void reset()
reset state to zero
Definition single_pole_iir.h:77
o_type filter(const i_type input)
compute a single output value.
Definition single_pole_iir.h:96
Provides programming elements for controlling the CyberRadio Solutions NDR651 radio.
Definition single_pole_iir.h:31
Provides programming elements for controlling CyberRadio Solutions products.
Definition single_pole_iir.h:30