00001
00020 #include <assert.h>
00021
00022 namespace aureservoir
00023 {
00024
00026
00027
00028 template <typename T>
00029 void BPFilter<T>::setBPCutoff(const typename DEVector<T>::Type &f1,
00030 const typename DEVector<T>::Type &f2)
00031 throw(AUExcept)
00032 {
00033 if( f1.length() != f2.length() )
00034 throw AUExcept("BPFilter: f1 must be same size as f2!");
00035
00036 int size = f1.length();
00037
00038
00039 ema1_.resizeOrClear(size);
00040 ema2_.resizeOrClear(size);
00041 f1_.resize(size);
00042 f2_.resize(size);
00043 scale_.resize(size);
00044
00045 f1_ = f1;
00046 f2_ = f2;
00047
00048
00049
00050 for(int i=1; i<=size; ++i)
00051 scale_(i) = 1 + f2_(i)/f1_(i);
00052 }
00053
00054 template <typename T>
00055 void BPFilter<T>::calc(typename DEVector<T>::Type &x)
00056 {
00057
00058
00059
00060
00061 for(int i=1; i<=ema1_.length(); ++i)
00062 {
00063 ema1_(i) += f1_(i) * ( x(i) - ema1_(i) );
00064 ema2_(i) += f2_(i) * ( ema1_(i) - ema2_(i) );
00065 x(i) = (ema1_(i) - ema2_(i)) * scale_(i);
00066 }
00067 }
00068
00070
00071
00072
00073 template <typename T>
00074 void IIRFilter<T>::setIIRCoeff(const typename DEMatrix<T>::Type &B,
00075 const typename DEMatrix<T>::Type &A)
00076 throw(AUExcept)
00077 {
00078 if( B.numRows() != A.numRows() )
00079 throw AUExcept("BPFilter: B and A must have same rows!");
00080
00081 int cols = B.numCols() > A.numCols() ? B.numCols() : A.numCols();
00082 int rows = A.numRows();
00083
00084
00085
00086 A_.resizeOrClear( rows, cols );
00087 B_.resizeOrClear( rows, cols );
00088 std::fill_n( A_.data(), rows*cols, 0 );
00089 std::fill_n( B_.data(), rows*cols, 0 );
00090
00091 S_.resizeOrClear(rows, cols-1);
00092 y_.resizeOrClear(rows);
00093
00094
00095
00096 for(int i=1; i<=rows; ++i)
00097 {
00098 for(int j=1; j<=A.numCols(); ++j)
00099 A_(i,j) = A(i,j) / A(i,1);
00100
00101 for(int j=1; j<=B.numCols(); ++j)
00102 B_(i,j) = B(i,j) / A(i,1);
00103 }
00104 }
00105
00106 template <typename T>
00107 void IIRFilter<T>::calc(typename DEVector<T>::Type &x)
00108 {
00109 assert( x.length() == S_.numRows() );
00110
00111 int neurons = S_.numRows();
00112 int coeffs = S_.numCols();
00113
00114 for(int i=1; i<=neurons; ++i)
00115 {
00116
00117 y_(i) = B_(i,1) * x(i) + S_(i,1);
00118
00119
00120 for(int j=1; j<=(coeffs-1); ++j)
00121 S_(i,j) = B_(i,j+1) * x(i) - A_(i,j+1) * y_(i) + S_(i,j+1);
00122
00123 S_(i,coeffs) = B_(i,coeffs+1) * x(i) - A_(i,coeffs+1) * y_(i);
00124 }
00125
00126 x = y_;
00127 }
00128
00130
00131
00132
00133 template <typename T>
00134 void SerialIIRFilter<T>::setIIRCoeff(const typename DEMatrix<T>::Type &B,
00135 const typename DEMatrix<T>::Type &A,
00136 int series)
00137 throw(AUExcept)
00138 {
00139
00140 if(series==1)
00141 {
00142 IIRFilter<T> filter;
00143 filter.setIIRCoeff(B,A);
00144 filters_.push_back(filter);
00145 return;
00146 }
00147
00148
00149 int bsize = B.numCols();
00150 int asize = A.numCols();
00151 if( asize != bsize )
00152 throw AUExcept("SerialIIRFilter: serial filters must have same columns A and B!");
00153
00155 int nr = bsize / series;
00156 IIRFilter<T> filter;
00157 typename DEMatrix<T>::Type a,b;
00158
00159 for(int i=1; i<=series; ++i)
00160 {
00161 a = A( _, _((i-1)*nr+1, i*nr) );
00162 b = B( _, _((i-1)*nr+1, i*nr) );
00163 filter.setIIRCoeff(b,a);
00164 filters_.push_back(filter);
00165 }
00166 }
00167
00168 template <typename T>
00169 void SerialIIRFilter<T>::calc(typename DEVector<T>::Type &x)
00170 {
00171 int size = filters_.size();
00172 for(int i=0; i<size; ++i)
00173 filters_[i].calc( x );
00174 }
00175
00177
00178 }