ExponentialMovingAverage.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2019, CNRS-UM LIRMM
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #pragma once
29 
30 #include <Eigen/Dense>
31 
32 namespace utils
33 {
34 
57 {
58  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
59 
69  ExponentialMovingAverage(double dt, double timeConstant, const Eigen::Vector3d & initValue = Eigen::Vector3d::Zero())
70  : dt_(dt)
71  {
72  average_ = initValue;
73  rawValue_ = initValue;
74  this->timeConstant(timeConstant);
75  }
76 
82  void append(const Eigen::Vector3d & value)
83  {
84  average_ += alpha_ * (value - average_);
85  rawValue_ = value;
86  if(saturation_ > 0.)
87  {
88  saturate_();
89  }
90  }
91 
95  const Eigen::Vector3d & eval() const
96  {
97  return average_;
98  }
99 
103  const Eigen::Vector3d & raw() const
104  {
105  return rawValue_;
106  }
107 
111  inline void reset()
112  {
113  return setZero();
114  }
115 
121  void saturation(double limit)
122  {
123  saturation_ = limit;
124  }
125 
129  void setZero()
130  {
131  average_.setZero();
132  }
133 
137  double timeConstant() const
138  {
139  return timeConstant_;
140  }
141 
147  void timeConstant(double T)
148  {
149  T = std::max(T, 2 * dt_); // Nyquist–Shannon sampling theorem
150  alpha_ = 1. - std::exp(-dt_ / T);
151  timeConstant_ = T;
152  }
153 
154 private:
158  void saturate_()
159  {
160  for(unsigned i = 0; i < 3; i++)
161  {
162  if(average_(i) < -saturation_)
163  {
164  average_(i) = -saturation_;
165  }
166  else if(average_(i) > saturation_)
167  {
168  average_(i) = saturation_;
169  }
170  }
171  }
172 
173 protected:
174  Eigen::Vector3d average_ = Eigen::Vector3d::Zero();
175  Eigen::Vector3d rawValue_ = Eigen::Vector3d::Zero();
176  double alpha_;
177  double dt_;
178  double saturation_ = -1.;
180 };
181 
182 } // namespace utils
183 
Exponential Moving Average.
double timeConstant() const
Get time constant of the filter.
const Eigen::Vector3d & eval() const
Evaluate the smoothed statistic.
const Eigen::Vector3d & raw() const
Last raw value provided to the integrator.
void append(const Eigen::Vector3d &value)
Append a new reading to the series.
void reset()
Reset average to zero.
void saturation(double limit)
Set output saturation; disable by providing a negative value.
void setZero()
Reset average to zero.
Utility functions and classes.
Definition: clamp.h:35
void timeConstant(double T)
Update time constant.
EIGEN_MAKE_ALIGNED_OPERATOR_NEW ExponentialMovingAverage(double dt, double timeConstant, const Eigen::Vector3d &initValue=Eigen::Vector3d::Zero())
Constructor.