00001 // 00002 // Copyright(c) 1993-1996 Tony Kirke 00003 // author="Tony Kirke" * 00004 /* 00005 * SPUC - Signal processing using C++ - A DSP library 00006 * 00007 * This program 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 * This program 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 this program; if not, write to the Free Software 00019 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00020 */ 00021 #ifndef FIR 00022 #define FIR 00023 //#include <iostream.h> 00024 //#include <fstream.h> 00025 #include <vector.h> 00026 namespace SPUC { 00035 00036 00037 00038 00039 template <class Numeric> class fir 00040 { 00041 public: 00042 long num_taps; 00043 Numeric* coeff; 00044 // protected: 00045 Numeric* z; 00046 Numeric output; 00047 00048 public: 00050 void settap(long i, Numeric tap) { coeff[i] = tap; } 00052 void reset() { 00053 for (int i=0;i<num_taps;i++) z[i] = 0; 00054 output = 0; 00055 } 00057 Numeric coeff_sum() { 00058 int i; 00059 Numeric s; 00060 for (s=0,i=0;i<num_taps;i++) s += coeff[i]; 00061 return(s); 00062 } 00064 Numeric out() { return(output); } 00066 Numeric check(long i) { return(z[i]); } 00067 ~fir(void) { 00068 if (num_taps>0) { 00069 delete [] z; 00070 delete [] coeff; 00071 } 00072 } 00074 fir(void) : coeff(NULL), z(NULL) { ;}; 00076 fir(long n) : coeff(NULL), z(NULL), num_taps(n) 00077 { 00078 int i; 00079 if (n>0) { 00080 coeff = new Numeric[n]; 00081 z = new Numeric[n]; 00082 for (i=0;i<n;i++) z[i] = coeff[i] = 0; 00083 } 00084 } 00086 void set_size(long n) 00087 { 00088 int i; 00089 num_taps = n; 00090 if (n>0) { 00091 coeff = new Numeric[n]; 00092 z = new Numeric[n]; 00093 for (i=0;i<n;i++) z[i] = coeff[i] = 0; 00094 } 00095 } 00096 long get_size(void) { return(num_taps); } 00098 fir(const char* file) { read_taps(file); }; 00100 Numeric clock(Numeric in) { return(update(in)); } 00101 Numeric update(Numeric in) { 00102 int i; 00103 // Update history of inputs 00104 for (i=num_taps-1;i>0;i--) z[i] = z[i-1]; 00105 // Add new input 00106 z[0] = in; 00107 // Perform FIR 00108 for (output=0,i=0;i<num_taps;i++) output += coeff[i]*z[i]; 00109 return(output); 00110 } 00111 // Tapped delay line uses previous outputs (to behave like an IIR) 00112 Numeric iir(Numeric in) { 00113 int i; 00114 for (output=0,i=0;i<num_taps;i++) output += coeff[i]*z[i]; 00115 // Update history of outputs 00116 for (i=num_taps-1;i>0;i--) z[i] = z[i-1]; 00117 output += in; 00118 // Add new output to delay line 00119 z[0] = output; 00120 return(output); 00121 } 00122 int read_taps(const char* file); 00123 int read_complex_taps(const char* file); 00124 void print(void); 00125 00126 template <class N> friend Vector<N> Vector_taps(fir<N> x); 00127 template <class N> friend Vector<N> Vector_input(fir<N> y); 00128 void settap(Vector<Numeric> z) { 00129 for (int i=0;i<num_taps;i++) coeff[i] = z[i]; 00130 } 00131 }; 00132 00133 template <class T> Vector<T> Vector_taps(fir<T> f) { 00134 long N = f.num_taps; 00135 Vector<T> V(N); 00136 for (int i=0;i<N;i++) V[i] = f.coeff[i]; 00137 return(V); 00138 } 00139 template <class T> Vector<T> Vector_input(fir<T> f) { 00140 long N = f.num_taps; 00141 Vector<T> V(N); 00142 for (int i=0;i<N;i++) V[i] = f.z[i]; 00143 return(V); 00144 } 00145 } // namespace SPUC 00146 #endif