00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef _GR_IIR_H_
00023 #define _GR_IIR_H_
00024
00025 #include <vector>
00026 using std::vector;
00027
00031 template<class o_type, class i_type, class tap_type>
00032 class gr_iir {
00033 public:
00044 gr_iir (const vector<tap_type>& fftaps, const vector<tap_type>& fbtaps)
00045 : fftaps (fftaps),fbtaps(fbtaps),
00046 latest(0),prev_output(fftaps.size()),prev_input(fftaps.size()) {}
00047
00048 gr_iir () :latest(0) { }
00049
00050 virtual ~gr_iir () {}
00051
00056 virtual o_type filter (const i_type input);
00057
00062 virtual void filterN (o_type output[], const i_type input[],
00063 unsigned long n);
00064
00068 unsigned ntaps () const { return fftaps.size (); }
00069
00073 virtual void set_taps (const vector<tap_type> &newfftaps,
00074 const vector<tap_type> &newfbtaps)
00075 {
00076 fftaps = newfftaps;
00077 fbtaps = newfbtaps;
00078 prev_output.resize (fftaps.size ());
00079 prev_input.resize (fftaps.size ());
00080 }
00081
00082 protected:
00083 vector<tap_type> fftaps;
00084 vector<tap_type> fbtaps;
00085 int latest;
00086 vector<o_type> prev_output;
00087 vector<o_type> prev_input;
00088 };
00089
00090
00091
00092
00093
00094 template<class o_type, class i_type, class tap_type>
00095 o_type
00096 gr_iir<o_type, i_type, tap_type>::filter (const i_type input)
00097 {
00098 o_type acc;
00099 unsigned i = 0;
00100 unsigned n = ntaps ();
00101
00102 acc = fftaps[0] * input;
00103 for (i = 1; i < n; i ++)
00104 acc += fftaps[i] * prev_input[(latest + i) % n] + fbtaps[i] * prev_output[(latest + i) % n];
00105
00106 prev_output[latest] = acc;
00107 prev_input[latest] = input;
00108 latest--;
00109 if(latest<0)
00110 latest+=n;
00111 return (o_type) acc;
00112 }
00113
00114
00115 template<class o_type, class i_type, class tap_type>
00116 void
00117 gr_iir<o_type, i_type, tap_type>::filterN (o_type output[],
00118 const i_type input[],
00119 unsigned long n)
00120 {
00121 for (unsigned i = 0; i < n; i++)
00122 output[i] = filter (input[i]);
00123 }
00124
00125
00126 #endif