vmmlib  1.10.0
Templatized C++ vector and matrix math library
lowpass_filter.hpp
1 /*
2  * Copyright (c) 2006-2014, Visualization and Multimedia Lab,
3  * University of Zurich <http://vmml.ifi.uzh.ch>,
4  * Eyescale Software GmbH,
5  * Blue Brain Project, EPFL
6  *
7  * This file is part of VMMLib <https://github.com/VMML/vmmlib/>
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  * Redistributions of source code must retain the above copyright notice, this
13  * list of conditions and the following disclaimer. Redistributions in binary
14  * form must reproduce the above copyright notice, this list of conditions and
15  * the following disclaimer in the documentation and/or other materials provided
16  * with the distribution. Neither the name of the Visualization and Multimedia
17  * Lab, University of Zurich nor the names of its contributors may be used to
18  * endorse or promote products derived from this software without specific prior
19  * written permission.
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /* @author Jafet Villafranca
34  *
35  * Implements low pass filtering on a templated data type, which needs to
36  * implement multiplication by a scalar float for smoothing
37  */
38 
39 #ifndef __VMML__LOWPASS_FILTER__HPP__
40 #define __VMML__LOWPASS_FILTER__HPP__
41 
42 #include <deque>
43 
44 namespace vmml
45 {
46 template< size_t M, typename T > class lowpass_filter
47 {
48 public:
53  lowpass_filter( const float F ) : _smooth_factor(F) {}
54  ~lowpass_filter() {}
55 
60  const T& get() const { return _value; }
61 
63  const T* operator->() const { return &_value; }
64 
66  const T& operator*() const { return _value; }
67 
73  T add( const T& value );
74 
79  void set_smooth_factor( const float& f );
80 
81 private:
82  std::deque< T > _data;
83  float _smooth_factor;
84  T _value;
85 };
86 
87 
88 template< size_t M, typename T > T lowpass_filter< M, T >::add( const T& value )
89 {
90  _data.push_front( value );
91 
92  while( _data.size() > M )
93  _data.pop_back();
94 
95  // update
96  typename std::deque< T >::const_iterator i = _data.begin();
97  _value = *i;
98  double weight = _smooth_factor;
99 
100  for( ++i ; i != _data.end(); ++i )
101  {
102  _value = _value * (1 - weight) + (*i) * weight;
103  weight *= _smooth_factor;
104  }
105 
106  return _value;
107 }
108 
109 template< size_t M, typename T >
111 {
112  _smooth_factor = f;
113 }
114 
115 } // namespace vmml
116 
117 #endif
const T * operator->() const
Access the filtered value.
void set_smooth_factor(const float &f)
Sets the smooth factor.
T add(const T &value)
Add a value to the data set and return the filtered output.
heavily inspired by boost::enable_if http://www.boost.org, file: boost/utility/enable_if.hpp, Copyright 2003 Jaakko Järvi, Jeremiah Willcock, Andrew Lumsdaine
Definition: aabb.hpp:40
lowpass_filter(const float F)
Constructor.
const T & operator*() const
Access the filtered value.