gri_iir.h
Go to the documentation of this file.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_n(0),d_latest_m(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_ff () const { return d_fftaps.size (); }
00084 unsigned ntaps_fb () const { return d_fbtaps.size (); }
00085
00089 void set_taps (const std::vector<tap_type> &fftaps,
00090 const std::vector<tap_type> &fbtaps) throw (std::invalid_argument)
00091 {
00092
00093
00094 d_latest_n = 0;
00095 d_latest_m = 0;
00096 d_fftaps = fftaps;
00097 d_fbtaps = fbtaps;
00098
00099 int n = fftaps.size ();
00100 int m = fbtaps.size ();
00101 d_prev_input.resize (2 * n);
00102 d_prev_output.resize (2 * m);
00103
00104 for (int i = 0; i < 2 * n; i++){
00105 d_prev_input[i] = 0;
00106 }
00107 for (int i = 0; i < 2 * m; i++){
00108 d_prev_output[i] = 0;
00109 }
00110 }
00111
00112 protected:
00113 std::vector<tap_type> d_fftaps;
00114 std::vector<tap_type> d_fbtaps;
00115 int d_latest_n;
00116 int d_latest_m;
00117 std::vector<tap_type> d_prev_output;
00118 std::vector<i_type> d_prev_input;
00119 };
00120
00121
00122
00123
00124
00125 template<class i_type, class o_type, class tap_type>
00126 o_type
00127 gri_iir<i_type, o_type, tap_type>::filter (const i_type input)
00128 {
00129 tap_type acc;
00130 unsigned i = 0;
00131 unsigned n = ntaps_ff ();
00132 unsigned m = ntaps_fb ();
00133
00134 if (n == 0)
00135 return (o_type) 0;
00136
00137 int latest_n = d_latest_n;
00138 int latest_m = d_latest_m;
00139
00140 acc = d_fftaps[0] * input;
00141 for (i = 1; i < n; i ++)
00142 acc += (d_fftaps[i] * d_prev_input[latest_n + i]);
00143 for (i = 1; i < m; i ++)
00144 acc += (d_fbtaps[i] * d_prev_output[latest_m + i]);
00145
00146
00147 d_prev_output[latest_m] = acc;
00148 d_prev_output[latest_m+m] = acc;
00149 d_prev_input[latest_n] = input;
00150 d_prev_input[latest_n+n] = input;
00151
00152 latest_n--;
00153 latest_m--;
00154 if (latest_n < 0)
00155 latest_n += n;
00156 if (latest_m < 0)
00157 latest_m += m;
00158
00159 d_latest_m = latest_m;
00160 d_latest_n = latest_n;
00161 return (o_type) acc;
00162 }
00163
00164
00165 template<class i_type, class o_type, class tap_type>
00166 void
00167 gri_iir<i_type, o_type, tap_type>::filter_n (o_type output[],
00168 const i_type input[],
00169 long n)
00170 {
00171 for (int i = 0; i < n; i++)
00172 output[i] = filter (input[i]);
00173 }
00174
00175 #endif
00176