00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef INCLUDED_GRI_IIR_H
00024 #define INCLUDED_GRI_IIR_H
00025
00026 #include <vector>
00027 #include <stdexcept>
00028
00032 template<class i_type, class o_type, class tap_type>
00033 class gri_iir {
00034 public:
00058 gri_iir (const std::vector<tap_type>& fftaps,
00059 const std::vector<tap_type>& fbtaps) throw (std::invalid_argument)
00060 {
00061 set_taps (fftaps, fbtaps);
00062 }
00063
00064 gri_iir () : d_latest(0) { }
00065
00066 ~gri_iir () {}
00067
00072 o_type filter (const i_type input);
00073
00078 void filter_n (o_type output[], const i_type input[], long n);
00079
00083 unsigned ntaps () const { return d_fftaps.size (); }
00084
00088 void set_taps (const std::vector<tap_type> &fftaps,
00089 const std::vector<tap_type> &fbtaps) throw (std::invalid_argument)
00090 {
00091 if (fftaps.size () != fbtaps.size ())
00092 throw std::invalid_argument ("gri_iir::set_taps");
00093
00094 d_latest = 0;
00095 d_fftaps = fftaps;
00096 d_fbtaps = fbtaps;
00097
00098 int n = fftaps.size ();
00099 d_prev_input.resize (2 * n);
00100 d_prev_output.resize (2 * n);
00101
00102 for (int i = 0; i < 2 * n; i++){
00103 d_prev_input[i] = 0;
00104 d_prev_output[i] = 0;
00105 }
00106 }
00107
00108 protected:
00109 std::vector<tap_type> d_fftaps;
00110 std::vector<tap_type> d_fbtaps;
00111 int d_latest;
00112 std::vector<tap_type> d_prev_output;
00113 std::vector<i_type> d_prev_input;
00114 };
00115
00116
00117
00118
00119
00120 template<class i_type, class o_type, class tap_type>
00121 o_type
00122 gri_iir<i_type, o_type, tap_type>::filter (const i_type input)
00123 {
00124 tap_type acc;
00125 unsigned i = 0;
00126 unsigned n = ntaps ();
00127
00128 if (n == 0)
00129 return (o_type) 0;
00130
00131 int latest = d_latest;
00132
00133 acc = d_fftaps[0] * input;
00134 for (i = 1; i < n; i ++)
00135 acc += (d_fftaps[i] * d_prev_input[latest + i]
00136 + d_fbtaps[i] * d_prev_output[latest + i]);
00137
00138
00139 d_prev_output[latest] = acc;
00140 d_prev_output[latest+n] = acc;
00141 d_prev_input[latest] = input;
00142 d_prev_input[latest+n] = input;
00143
00144 latest--;
00145 if (latest < 0)
00146 latest += n;
00147
00148 d_latest = latest;
00149 return (o_type) acc;
00150 }
00151
00152
00153 template<class i_type, class o_type, class tap_type>
00154 void
00155 gri_iir<i_type, o_type, tap_type>::filter_n (o_type output[],
00156 const i_type input[],
00157 long n)
00158 {
00159 for (int i = 0; i < n; i++)
00160 output[i] = filter (input[i]);
00161 }
00162
00163 #endif
00164