LowPassCompose.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <mc_rtc/logging.h>
4 #include <algorithm>
5 
6 namespace mc_filter
7 {
8 
14 template<typename T>
16 {
24  LowPassCompose(double dt, double period = 0)
25  {
26  cutoffPeriod(period);
27  dt_ = dt;
28  reset(T::Zero());
29  }
30 
32  double cutoffPeriod() const { return cutoffPeriod_; }
33 
41  void cutoffPeriod(double period)
42  {
43  if(period < 2 * dt_)
44  {
45  mc_rtc::log::warning("Time constant must be at least twice the timestep (Nyquist–Shannon sampling theorem)");
46  period = 2 * dt_;
47  }
48  cutoffPeriod_ = period;
49  }
50 
56  void reset(const T & value)
57  {
58  eval_ = value;
59  eval_hp_ = eval_;
60  eval_lp_ = eval_;
61  }
62 
70  void update(const T & newValue_hp, const T & newValue_lp)
71  {
72  double x = (cutoffPeriod_ <= dt_) ? 1. : dt_ / cutoffPeriod_;
73 
74  // Update the low pass filter
75  eval_lp_ = x * newValue_lp + (1. - x) * eval_lp_;
76 
77  // Update the high pass filter output
78  T eval_hp = (newValue_hp - eval_hp_);
79 
80  // Summation of both components
81  eval_ = eval_hp + eval_lp_;
82 
83  // Update high pass filter state
84  eval_hp_ = (x * newValue_hp + (1. - x) * eval_hp_);
85 
86  // Use for logs
87  value_hp_ = newValue_hp;
88  value_lp_ = newValue_lp;
89  }
90 
94  const T & eval() const { return eval_; }
95 
96  const T & input_lp() const { return value_lp_; }
97  const T & input_hp() const { return value_hp_; }
98 
102  double dt() const { return dt_; }
103 
111  void dt(double dt)
112  {
113  dt_ = dt;
114  cutoffPeriod(cutoffPeriod_);
115  }
116 
117 private:
118  T eval_;
119  T eval_lp_;
120  T eval_hp_;
121  T value_hp_;
122  T value_lp_;
123  double cutoffPeriod_ = 0.;
124 
125 protected:
126  double dt_ = 0.005; // [s]
127 };
128 
129 } // namespace mc_filter
mc_filter::LowPassCompose::input_hp
const T & input_hp() const
Definition: LowPassCompose.h:97
mc_filter::LowPassCompose
Definition: LowPassCompose.h:15
mc_filter::LowPassCompose::cutoffPeriod
void cutoffPeriod(double period)
Definition: LowPassCompose.h:41
mc_filter
Definition: ExponentialMovingAverage.h:14
mc_filter::LowPassCompose::update
void update(const T &newValue_hp, const T &newValue_lp)
Definition: LowPassCompose.h:70
mc_filter::LowPassCompose::dt
void dt(double dt)
Definition: LowPassCompose.h:111
mc_filter::LowPassCompose::dt
double dt() const
Definition: LowPassCompose.h:102
mc_rtc::log::warning
void warning(Args &&... args)
Definition: logging.h:69
mc_filter::LowPassCompose::dt_
double dt_
Definition: LowPassCompose.h:126
logging.h
mc_filter::LowPassCompose::LowPassCompose
LowPassCompose(double dt, double period=0)
Definition: LowPassCompose.h:24
mc_filter::LowPassCompose::input_lp
const T & input_lp() const
Definition: LowPassCompose.h:96
mc_filter::LowPassCompose::reset
void reset(const T &value)
Definition: LowPassCompose.h:56
mc_filter::LowPassCompose::cutoffPeriod
double cutoffPeriod() const
Definition: LowPassCompose.h:32
mc_filter::LowPassCompose::eval
const T & eval() const
Definition: LowPassCompose.h:94