ProteoWizard
filter.hpp
Go to the documentation of this file.
1 //
2 // $Id: filter.hpp 5313 2013-12-17 18:06:54Z chambm $
3 //
4 //
5 // Original author: Witold Wolski <wewolski@gmail.com>
6 //
7 // Copyright : ETH Zurich
8 //
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
12 //
13 // http://www.apache.org/licenses/LICENSE-2.0
14 //
15 // Unless required by applicable law or agreed to in writing, software
16 // distributed under the License is distributed on an "AS IS" BASIS,
17 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 // See the License for the specific language governing permissions and
19 // limitations under the License.
20 //
21 
22 #ifndef FILTERUTILS_H
23 #define FILTERUTILS_H
24 #include <math.h>
25 #include <algorithm>
26 #include <vector>
27 #include <functional>
28 #include <numeric>
29 #include <limits>
30 
31 #include <boost/iterator/reverse_iterator.hpp>
32 #include <boost/bind.hpp>
33 #include <boost/math/distributions/normal.hpp>
34 #include <boost/cstdint.hpp>
35 
36 
38 
39 
40 namespace ralab
41 {
42  namespace base
43  {
44  namespace filter
45  {
46 
48  template <typename TIterator, typename TFilterIterator, typename TOutputIterator>
50  TIterator dataBeg, //!<[in] a univariate time series.
51  TIterator dataEnd,
52  TFilterIterator filterBeg, //!<[in] a vector of filter coefficients in reverse time order (as for AR or MA coefficients). Lenght of filter must be odd.
53  size_t fsize,
54  TOutputIterator resBeg, //!<[out] result
55  bool circular = false, //!<[in] If TRUE, wrap the filter around the ends of the series, otherwise assume external values are missing (NA).
56  uint32_t sides = 2 //!<[in] currently only sides 2 supported....
57  )
58  {
59  typedef typename std::iterator_traits<TOutputIterator>::value_type TReal;
60  if((fsize-1) % 2)
61  {
62  throw std::logic_error("filter size must be odd");
63  }
64  if(!circular)
65  {
66  //result.assign(data.size(), std::numeric_limits<TReal>::quiet_NaN() );
67 
68  size_t offset = static_cast<size_t>(fsize/2);
69  for(std::size_t i = 0 ; i< offset; ++i, ++resBeg)
70  {
71  *resBeg = std::numeric_limits<TReal>::quiet_NaN();
72  }
73 
74  for( ; dataBeg != dataEnd - (fsize -1) ; ++dataBeg, ++resBeg )
75  {
76  *resBeg = (std::inner_product(dataBeg , dataBeg + fsize, filterBeg ,0. ));
77  }
78  }
79  else
80  {
81 
82  std::vector<typename std::iterator_traits<TIterator>::value_type> tmp;
83  typename std::vector<typename std::iterator_traits<TIterator>::value_type>::iterator it;
84  it = utilities::prepareData( dataBeg, dataEnd, fsize , tmp );
85 
86  TIterator tbegin = tmp.begin();
87  TIterator tend = it;
88 
89  for( ; tbegin != tend - (fsize-1 ) ; ++tbegin, ++resBeg )
90  {
91  *resBeg = std::inner_product(tbegin , tbegin + fsize, filterBeg ,0. );
92  }
93  }
94  }// filter end
95 
96 
97 
98  /*! \brief Applies linear convolution (filtering) to a univariate time series
99 
100  The convolution filter is \f$ y[i] = f[1]*x[i+o] + ... + f[p]*x[i+o-(p-1)] \f$
101  where o is the offset: see sides for how it is determined.
102 
103  \param sides for convolution filters only.
104  If sides=1 the filter coefficients are for past values only;
105  if sides=2 they are centred around lag 0.
106  In this case the length of the filter should be odd,
107  but if it is even, more of the filter is forward in time than backward
108 
109 
110  */
111  template <typename TContainer>
112  void filter(
113  const TContainer & data, //!<[in] a univariate time series.
114  const TContainer & filter, //!<[in] a vector of filter coefficients in reverse time order (as for AR or MA coefficients). Lenght of filter must be odd.
115  TContainer & result, //!<[out] result
116  bool circular = false, //!<[in] If TRUE, wrap the filter around the ends of the series, otherwise assume external values are missing (NA).
117  uint32_t sides = 2 //!<[in] currently only sides 2 supported....
118  )
119  {
120  result.resize(data.size());
122  (
123  data.begin(),
124  data.end(),
125  filter.begin(),
126  filter.size(),
127  result.begin(),
128  circular,
129  sides
130  );
131  }// filter end
132  }//filter
133  }//base
134 }//ralab
135 
136 
137 
138 
139 
140 #endif
141 
142 
boost::uint32_t uint32_t
Definition: filter.hpp:47
TContainer::iterator prepareData(TIterator dataBeg, TIterator dataEnd, size_t fsize, TContainer &res, bool mirror=false)
Example Sequence : 1 2 3 4 5; width 5 and mirror false: 4 5 1 2 3 4 5 1 2, if mirror true than: 2 1 1...
Definition: preparedata.hpp:40
void filter_sequence(TIterator dataBeg, TIterator dataEnd, TFilterIterator filterBeg, size_t fsize, TOutputIterator resBeg, bool circular=false, uint32_t sides=2)
Definition: filter.hpp:49
EQUISPACEINTERPOL Interpolation on a equidistantly spaced grid.
Definition: base.hpp:39
void filter(const TContainer &data, const TContainer &filter, TContainer &result, bool circular=false, uint32_t sides=2)
Applies linear convolution (filtering) to a univariate time series.
Definition: filter.hpp:112