StationaryOffsetFilter.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 
31 #include <Eigen/Dense>
32 
33 namespace utils
34 {
35 
40 {
41  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
42 
53  StationaryOffsetFilter(double dt, double timeConstant, const Eigen::Vector3d & initValue = Eigen::Vector3d::Zero())
54  : average_(dt, timeConstant, initValue)
55  {
56  filteredValue_ = initValue;
57  rawValue_ = initValue;
58  }
59 
65  void update(const Eigen::Vector3d & value)
66  {
67  average_.append(value);
68  filteredValue_ = value - average_.eval();
69  rawValue_ = value;
70  }
71 
75  const Eigen::Vector3d & eval() const
76  {
77  return filteredValue_;
78  }
79 
83  const Eigen::Vector3d & raw() const
84  {
85  return rawValue_;
86  }
87 
91  void setZero()
92  {
93  average_.setZero();
94  filteredValue_.setZero();
95  rawValue_.setZero();
96  }
97 
101  double timeConstant() const
102  {
103  return average_.timeConstant();
104  }
105 
111  void timeConstant(double T)
112  {
113  average_.timeConstant(T);
114  }
115 
116 private:
117  Eigen::Vector3d filteredValue_;
118  Eigen::Vector3d rawValue_;
119  ExponentialMovingAverage average_;
120 };
121 
122 } // namespace utils
123 
Exponential Moving Average.
double timeConstant() const
Get time constant of the filter.
const Eigen::Vector3d & raw() const
Get raw value of input signal.
EIGEN_MAKE_ALIGNED_OPERATOR_NEW StationaryOffsetFilter(double dt, double timeConstant, const Eigen::Vector3d &initValue=Eigen::Vector3d::Zero())
Constructor.
const Eigen::Vector3d & eval() const
Evaluate the smoothed statistic.
void append(const Eigen::Vector3d &value)
Append a new reading to the series.
Remove stationary offset from an input signal.
void setZero()
Reset everything to zero.
void setZero()
Reset average to zero.
void update(const Eigen::Vector3d &value)
Update input signal value.
void timeConstant(double T)
Update time constant.
const Eigen::Vector3d & eval() const
Get output value where the stationary offset has been filtered.
Utility functions and classes.
Definition: clamp.h:35
double timeConstant() const
Get time constant of the filter.