Main Page | Modules | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members

gri_iir.h

Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2002 Free Software Foundation, Inc.
00004  * 
00005  * This file is part of GNU Radio
00006  * 
00007  * GNU Radio is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2, or (at your option)
00010  * any later version.
00011  * 
00012  * GNU Radio is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with GNU Radio; see the file COPYING.  If not, write to
00019  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00020  * Boston, MA 02111-1307, USA.
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 // general case.  We may want to specialize this
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   // store the values twice to avoid having to handle wrap-around in the loop
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 /* INCLUDED_GRI_IIR_H */
00164 

Generated on Sat Jul 8 17:04:52 2006 for GNU Radio 2.x by  doxygen 1.4.1