ExponentialMovingAverage.h
Go to the documentation of this file.
1 /*
2  * Copyright 2015-2019 CNRS-UM LIRMM, CNRS-AIST JRL
3  *
4  * This file is inspired by Stephane's Caron original implementation as part of
5  * lipm_walking_controller <https://github.com/stephane-caron/lipm_walking_controller>
6  */
7 
8 #pragma once
10 #include <mc_rtc/logging.h>
11 #include <algorithm>
12 #include <cmath>
13 
14 namespace mc_filter
15 {
38 template<typename VectorT>
40 {
50  ExponentialMovingAverage(double dt, double timeConstant, const VectorT & initValue = VectorT::Zero()) : dt_(dt)
51  {
52  this->reset(initValue);
53  this->timeConstant(timeConstant);
54  }
55 
60  void append(const VectorT & value)
61  {
62  average_ += alpha_ * (value - average_);
64  }
65 
69  const VectorT & eval() const { return average_; }
70 
75  void saturation(double limit) { saturation_ = limit; }
76 
81  void reset(const VectorT & initVal) { average_ = initVal; }
82 
86  double timeConstant() const { return timeConstant_; }
87 
94  void timeConstant(double T)
95  {
96  if(T < 2 * dt_)
97  {
98  mc_rtc::log::warning("Time constant must be at least twice the timestep (Nyquist–Shannon sampling theorem)");
99  T = 2 * dt_;
100  }
101  alpha_ = 1. - std::exp(-dt_ / T);
102  timeConstant_ = T;
103  }
104 
105 protected:
106  VectorT average_ = VectorT::Zero();
107  double alpha_;
108  double dt_;
110  double saturation_ = -1.;
111 };
112 
113 } // namespace mc_filter
mc_filter::ExponentialMovingAverage::alpha_
double alpha_
Definition: ExponentialMovingAverage.h:107
mc_filter::ExponentialMovingAverage::dt_
double dt_
Definition: ExponentialMovingAverage.h:108
mc_filter::ExponentialMovingAverage::average_
VectorT average_
Definition: ExponentialMovingAverage.h:106
mc_filter
Definition: ExponentialMovingAverage.h:14
mc_filter::ExponentialMovingAverage::timeConstant
void timeConstant(double T)
Definition: ExponentialMovingAverage.h:94
mc_filter::ExponentialMovingAverage::reset
void reset(const VectorT &initVal)
Definition: ExponentialMovingAverage.h:81
mc_filter::ExponentialMovingAverage::saturation_
double saturation_
Definition: ExponentialMovingAverage.h:110
mc_filter::ExponentialMovingAverage::timeConstant
double timeConstant() const
Definition: ExponentialMovingAverage.h:86
mc_filter::ExponentialMovingAverage::eval
const VectorT & eval() const
Definition: ExponentialMovingAverage.h:69
mc_filter::ExponentialMovingAverage::saturation
void saturation(double limit)
Definition: ExponentialMovingAverage.h:75
mc_rtc::log::warning
void warning(Args &&... args)
Definition: logging.h:69
mc_filter::utils::clampInPlace
void clampInPlace(double &value, double lower, double upper)
Definition: clamp.h:38
logging.h
clamp.h
mc_filter::ExponentialMovingAverage::ExponentialMovingAverage
ExponentialMovingAverage(double dt, double timeConstant, const VectorT &initValue=VectorT::Zero())
Definition: ExponentialMovingAverage.h:50
mc_filter::ExponentialMovingAverage::timeConstant_
double timeConstant_
Definition: ExponentialMovingAverage.h:109
mc_filter::ExponentialMovingAverage
Definition: ExponentialMovingAverage.h:39
mc_filter::ExponentialMovingAverage::append
void append(const VectorT &value)
Definition: ExponentialMovingAverage.h:60