GPLIB++
FilterFunc.h
Go to the documentation of this file.
1 #ifndef FILTERFUNC_H_
2 #define FILTERFUNC_H_
3 
4 #include "types.h"
5 #include <complex>
6 #include <algorithm>
7 #include <iostream>
8 #include <iterator>
9 namespace gplib
10  {
11 
12  /** \addtogroup tstools Time series analysis methods */
13  /* @{ */
14 
15  /*! \file FilterFunc.h
16  * This file defines several function objects to be used as filters
17  * to Filter time series data in the time domain.
18  * They can be used in conjunction with Convolve.
19  */
20 
21  //! Transform from frequency domain to w-domain
22  double FreqToW(const double f)
23  {
24  std::complex<double> compexp = exp(
25  std::complex<double>(0.0, 2 * PI * f));
26  return -((-compexp + 1.0) / (compexp + 1.0)).imag();
27  }
28 
29  //! A simple low pass
30 
31  /*! This class defines a simple IIR low pass filter as described in Numerical Recipes p. 562.
32  * The filter coefficients are calculated by a bilinear transformation method. The corner frequency
33  * in the constructor has to be given in terms of the Nyquist frequency, i.e. in the range \f$ 0 \ldots 0.5 \f$.
34  */
35  class SimpleLp: public std::unary_function<double, double>
36  {
37  private:
38  double tsvalues[2];
39  double filtervalues[3];
40  public:
41  //! The constructor takes the dimensionless corner frequency, i.e. the corner frequency in Hz times the sampling rate
42  SimpleLp(const double cornerfreq)
43  {
44  // transform the corner frequency
45  double b = FreqToW(cornerfreq);
46  // calculate the filter coefficients
47  filtervalues[0] = -b / (1.0 + b);
48  filtervalues[1] = -b / (1.0 + b);
49  filtervalues[2] = (1.0 - b) / (1.0 + b);
50  // initialize the storage of previous input and output values
51  std::fill_n(tsvalues, 2, 0.0);
52  }
53  //! This version of the operator is suitable for use with std::transform, it returns a filtered value for each call with the current value
54  double operator()(const double currvalue)
55  {
56  // calculate the output and store it
57  tsvalues[1] = filtervalues[0] * currvalue + filtervalues[1]
58  * tsvalues[0] + filtervalues[2] * tsvalues[1];
59  // store the last input
60  tsvalues[0] = currvalue;
61  return tsvalues[1];
62  }
63  };
64 
65  /*! This class defines a simple IIR band pass filter as described in Numerical Recipes p. 562.
66  * The filter coefficients are calculated by a bilinear transformation method. The upper and lower corner frequencies
67  * in the constructor have to be given in terms of the Nyquist frequency, i.e. in the range $\f 0 \ldots 0.5 \f$.
68  */
69  class SimpleBp: public std::unary_function<double, double>
70  {
71  private:
72  double tsvalues[4];
73  double filtervalues[4];
74  public:
75  //! The constructor takes the dimensionless corner frequencies, i.e. the corner frequencies in Hz times the sampling rate
76  SimpleBp(const double lowercornerfreq, const double uppercornerfreq)
77  {
78  // transform the corner frequencies
79  double b = FreqToW(uppercornerfreq);
80  double a = FreqToW(lowercornerfreq);
81  // calculate the filter coefficients
82  filtervalues[0] = -b / ((1.0 + a) * (1.0 + b));
83  filtervalues[1] = b / ((1.0 + a) * (1.0 + b));
84  filtervalues[2] = ((1.0 + a) * (1.0 - b) + (1.0 - a) * (1.0 + b))
85  / ((1.0 + a) * (1.0 + b));
86  filtervalues[3] = -(1.0 - a) * (1.0 - b) / ((1.0 + a) * (1.0 + b));
87  // initialize the storage of previous input and output values
88  std::fill_n(tsvalues, 4, 0.0);
89  }
90  //! This version of the operator is suitable for use with std::transform, it returns a filtered value for each call with the current value
91  double operator()(const double currvalue)
92  {
93  //save the output old value
94  double tmp = tsvalues[2];
95  // calculate the output and store it, we don't need tsvalues[0] here
96  tsvalues[2] = filtervalues[0] * currvalue + filtervalues[1]
97  * tsvalues[1] + filtervalues[2] * tsvalues[2] + filtervalues[3]
98  * tsvalues[3];
99  tsvalues[3] = tmp;
100  // store the last two inputs
101  tsvalues[1] = tsvalues[0];
102  tsvalues[0] = currvalue;
103  return tsvalues[2];
104  }
105  };
106  }
107 #endif /*FILTERFUNC_H_*/
A simple low pass.
Definition: FilterFunc.h:35
SimpleLp(const double cornerfreq)
The constructor takes the dimensionless corner frequency, i.e. the corner frequency in Hz times the s...
Definition: FilterFunc.h:42
void f(vector< double > &v1, vector< double > &v2, vector< double > &v3, vector< double > &v4)
Definition: perftest.cpp:17
double operator()(const double currvalue)
This version of the operator is suitable for use with std::transform, it returns a filtered value for...
Definition: FilterFunc.h:54
double FreqToW(const double f)
Transform from frequency domain to w-domain.
Definition: FilterFunc.h:22