GPLIB++
WFunc.h
Go to the documentation of this file.
1 #ifndef WFUNC_H_
2 #define WFUNC_H_
3 #include "types.h"
4 #include <algorithm>
5 #include <boost/bind.hpp>
6 #include <boost/cast.hpp>
7 #include <functional>
8 
9 namespace gplib
10  {
11 
12  /** \addtogroup tstools Time series analysis methods */
13  /* @{ */
14 
15  /*! \file WFunc.h
16  * This file defines several function objects to be used as windowing functions for spectral calculations
17  * They all take one input parameter, the relative position in the time window, as a double between 0 and 1
18  * and output the corresponding weighting factor. For efficiency reasons no checking is performed on the
19  * input parameter. So make sure it is between 0 and 1, or you will get strange results. The easiest way
20  * to apply the window function to some time series is to use the provided function ApplyWindow in this header file
21  */
22  //! This functor returns the weighting factor for the Hamming window, given the relative position relpos [0..1] in the time series
23  struct Hamming: public std::unary_function<double, double>
24  {
25  double operator()(const double relpos)
26  {
27  return 0.54 - 0.46 * cos(2 * PI * relpos);
28  }
29  };
30  //! This functor returns the weighting factor for the Hanning window, given the relative position (0..1) in the time series
31  struct Hanning: public std::unary_function<double, double>
32  {
33  double operator()(const double relpos)
34  {
35  return 0.5 - 0.5 * cos(2 * PI * relpos);
36  }
37  };
38  //! A functor for the simple Boxcar function
39  struct Boxcar: public std::unary_function<double, double>
40  {
41  double operator()(const double relpos)
42  {
43  if (relpos >= 0.0 && relpos <= 1.0)
44  return 1.0;
45  else
46  return 0.0;
47  }
48  };
49 
50  //! This functor rises steeply at the edges and then has a wide range where it is unity
51  struct Steep: public std::unary_function<double, double>
52  {
53  double operator()(const double relpos)
54  {
55  const double startrange = 0.1;
56  if (relpos < startrange)
57  return 0.5 - 0.5 * cos(PI * relpos / startrange);
58  else if (relpos > (1.0 - startrange))
59  return 0.5 - 0.5 * cos(PI * ((1.0 - startrange) - relpos)
60  / startrange + PI);
61  else
62  return 1.0;
63  }
64  };
65 
66  //! The cosine squared windows of fixed width
67  struct CosSq: public std::unary_function<double, double>
68  {
69  double operator()(const double relpos)
70  {
71  double val = cos(PI * relpos / 2);
72  return val * val;
73  }
74  };
75 
76  //! A variable width cosine squared window that is zero outside
77  class TruncCosSq: public std::unary_function<double, double>
78  {
79  private:
80  double width;
81  public:
82  TruncCosSq(double w = 1) :
83  width(w)
84  {
85  }
86  double operator()(const double relpos)
87  {
88  if (std::abs(relpos) > width)
89  return 0.0;
90 
91  double val = cos(PI * relpos / (2 * width));
92  return val * val;
93  }
94  };
95 
96  //! Apply one of the above window functions to a range
97  /*!Apply one of the above window functions to a range
98  * given by the iterators inbegin and outbegin and write the result into a structure through
99  * the iterator outbegin, make sure the datastructure can hold enough elements. Input and output structures
100  * can be the same.
101  * */
102  template<typename InputIterator, typename OutputIterator,
103  typename WindowFunctype> inline
104  void ApplyWindow(InputIterator inbegin, InputIterator inend,
105  OutputIterator outbegin, WindowFunctype WFunc, double relshift = 0.0)
106  {
107  const double length = inend - inbegin;
108  for (InputIterator it = inbegin; it != inend; ++it, ++outbegin)
109  *outbegin = (*it) * WFunc(boost::numeric_cast<double>(std::distance(
110  inbegin, it)) / length - relshift);
111 
112  //std::transform(inbegin,inend,outbegin,
113  // boost::bind(std::multiplies<typename std::iterator_traits<InputIterator>::value_type>(),_1,
114  // boost::bind(WFunc,boost::bind<double>(std::divides<typename std::iterator_traits<InputIterator>::value_type >(),_1,length))));
115  }
116  /* @} */
117  }
118 #endif /*WFUNC_H_*/
The cosine squared windows of fixed width.
Definition: WFunc.h:67
double operator()(const double relpos)
Definition: WFunc.h:86
double operator()(const double relpos)
Definition: WFunc.h:33
This functor rises steeply at the edges and then has a wide range where it is unity.
Definition: WFunc.h:51
This functor returns the weighting factor for the Hamming window, given the relative position relpos ...
Definition: WFunc.h:23
double operator()(const double relpos)
Definition: WFunc.h:69
void ApplyWindow(InputIterator inbegin, InputIterator inend, OutputIterator outbegin, WindowFunctype WFunc, double relshift=0.0)
Apply one of the above window functions to a range.
Definition: WFunc.h:104
double operator()(const double relpos)
Definition: WFunc.h:41
A functor for the simple Boxcar function.
Definition: WFunc.h:39
double operator()(const double relpos)
Definition: WFunc.h:25
A variable width cosine squared window that is zero outside.
Definition: WFunc.h:77
This functor returns the weighting factor for the Hanning window, given the relative position (0...
Definition: WFunc.h:31
double operator()(const double relpos)
Definition: WFunc.h:53
TruncCosSq(double w=1)
Definition: WFunc.h:82