3 * Copyright 2002 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
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)
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.
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.
22 #ifndef _GR_SINGLE_POLE_AVG_H_
23 #define _GR_SINGLE_POLE_AVG_H_
28 * \brief class template for single pole moving average filter
31 template<class o_type, class i_type, class tap_type>
32 class gr_single_pole_avg {
35 * \brief construct new single pole moving average filter with given alpha
37 * computes y(i) = alpha * x(i) - (1-alpha) * y(i-1)
39 gr_single_pole_avg (tap_type alpha = 1.0)
46 * \brief compute a single output value.
47 * \returns the filtered input value.
49 o_type filter (const i_type input);
52 * \brief compute an array of N output values.
53 * \p input must have n valid entries.
55 void filterN (o_type output[], const i_type input[], unsigned long n);
58 * \brief install \p alpha as the current taps.
60 void set_taps (tap_type alpha)
62 if (alpha < 0 || alpha > 1)
63 throw std::out_of_range ("Alpha must be in [0, 1]\n");
66 d_one_minus_alpha = 1.0 - alpha;
69 //! reset state to zero
75 tap_type prev_input () { return d_prev_input; }
79 tap_type d_one_minus_alpha;
80 tap_type d_prev_input;
85 // general case. We may want to specialize this
87 template<class o_type, class i_type, class tap_type>
89 gr_single_pole_avg<o_type, i_type, tap_type>::filter (const i_type input)
93 output = d_alpha * input - d_one_minus_alpha * d_prev_input;
96 return (o_type) output;
100 template<class o_type, class i_type, class tap_type>
102 gr_single_pole_avg<o_type, i_type, tap_type>::filterN (o_type output[],
103 const i_type input[],
106 for (unsigned i = 0; i < n; i++)
107 output[i] = filter (input[i]);
111 #endif /* _GR_SINGLE_POLE_AVG_H_ */