FilterFunc.h

Go to the documentation of this file.
00001 #ifndef FILTERFUNC_H_
00002 #define FILTERFUNC_H_
00003 
00004 #include "types.h"
00005 #include <complex>
00006 #include <algorithm>
00007 #include <iostream>
00008 #include <iterator>
00009 namespace gplib
00010   {
00011 
00012     /** \addtogroup tstools Time series analysis methods */
00013     /* @{ */
00014 
00015     /*! \file FilterFunc.h
00016      * This file defines several function objects to be used as filters
00017      *  to Filter time series data in the time domain.
00018      * They can be used in conjunction with Convolve.
00019      */
00020 
00021     //! Transform from frequency domain to w-domain
00022     double FreqToW(const double f)
00023       {
00024         std::complex<double> compexp = exp(
00025             std::complex<double>(0.0, 2 * PI * f));
00026         return -((-compexp + 1.0) / (compexp + 1.0)).imag();
00027       }
00028 
00029     //! A simple low pass
00030 
00031     /*! This class defines a simple IIR low pass filter as described in Numerical Recipes p. 562.
00032      * The filter coefficients are calculated by a bilinear transformation method. The corner frequency
00033      * in the constructor has to be given in terms of the Nyquist frequency, i.e. in the range \f$ 0 \ldots 0.5 \f$.
00034      */
00035     class SimpleLp: public std::unary_function<double, double>
00036       {
00037     private:
00038       double tsvalues[2];
00039       double filtervalues[3];
00040     public:
00041       //! The constructor takes the dimensionless corner frequency, i.e. the corner frequency in Hz times the sampling rate
00042       SimpleLp(const double cornerfreq)
00043         {
00044           // transform the corner frequency
00045           double b = FreqToW(cornerfreq);
00046           // calculate the filter coefficients
00047           filtervalues[0] = -b / (1.0 + b);
00048           filtervalues[1] = -b / (1.0 + b);
00049           filtervalues[2] = (1.0 - b) / (1.0 + b);
00050           // initialize the storage of previous input and output values
00051           std::fill_n(tsvalues, 2, 0.0);
00052         }
00053       //! This version of the operator is suitable for use with std::transform, it returns a filtered value for each call with the current value
00054       double operator()(const double currvalue)
00055         {
00056           // calculate the output and store it
00057           tsvalues[1] = filtervalues[0] * currvalue + filtervalues[1]
00058               * tsvalues[0] + filtervalues[2] * tsvalues[1];
00059           // store the last input
00060           tsvalues[0] = currvalue;
00061           return tsvalues[1];
00062         }
00063       };
00064 
00065     /*! This class defines a simple IIR band pass filter as described in Numerical Recipes p. 562.
00066      * The filter coefficients are calculated by a bilinear transformation method. The upper and lower corner frequencies
00067      * in the constructor have to be given in terms of the Nyquist frequency, i.e. in the range $\f 0 \ldots 0.5 \f$.
00068      */
00069     class SimpleBp: public std::unary_function<double, double>
00070       {
00071     private:
00072       double tsvalues[4];
00073       double filtervalues[4];
00074     public:
00075       //! The constructor takes the dimensionless corner frequencies, i.e. the corner frequencies in Hz times the sampling rate
00076       SimpleBp(const double lowercornerfreq, const double uppercornerfreq)
00077         {
00078           // transform the corner frequencies
00079           double b = FreqToW(uppercornerfreq);
00080           double a = FreqToW(lowercornerfreq);
00081           // calculate the filter coefficients
00082           filtervalues[0] = -b / ((1.0 + a) * (1.0 + b));
00083           filtervalues[1] = b / ((1.0 + a) * (1.0 + b));
00084           filtervalues[2] = ((1.0 + a) * (1.0 - b) + (1.0 - a) * (1.0 + b))
00085               / ((1.0 + a) * (1.0 + b));
00086           filtervalues[3] = -(1.0 - a) * (1.0 - b) / ((1.0 + a) * (1.0 + b));
00087           // initialize the storage of previous input and output values
00088           std::fill_n(tsvalues, 4, 0.0);
00089         }
00090       //! This version of the operator is suitable for use with std::transform, it returns a filtered value for each call with the current value
00091       double operator()(const double currvalue)
00092         {
00093           //save the output old value
00094           double tmp = tsvalues[2];
00095           // calculate the output and store it, we don't need tsvalues[0] here
00096           tsvalues[2] = filtervalues[0] * currvalue + filtervalues[1]
00097               * tsvalues[1] + filtervalues[2] * tsvalues[2] + filtervalues[3]
00098               * tsvalues[3];
00099           tsvalues[3] = tmp;
00100           // store the last two inputs
00101           tsvalues[1] = tsvalues[0];
00102           tsvalues[0] = currvalue;
00103           return tsvalues[2];
00104         }
00105       };
00106   }
00107 #endif /*FILTERFUNC_H_*/

Generated on Tue May 4 16:52:14 2010 for GPLIB++ by  doxygen 1.5.8