2015-08-19 09:15:14 -03:00
|
|
|
//
|
|
|
|
/// @file LowPassFilter.cpp
|
|
|
|
/// @brief A class to implement a low pass filter without losing precision even for int types
|
|
|
|
/// the downside being that it's a little slower as it internally uses a float
|
|
|
|
/// and it consumes an extra 4 bytes of memory to hold the constant gain
|
|
|
|
|
|
|
|
|
|
|
|
#include "LowPassFilter.h"
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// DigitalLPF
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
DigitalLPF<T>::DigitalLPF() {
|
|
|
|
// built in initialization
|
|
|
|
_output = T();
|
|
|
|
}
|
|
|
|
|
|
|
|
// add a new raw value to the filter, retrieve the filtered result
|
|
|
|
template <class T>
|
|
|
|
T DigitalLPF<T>::apply(const T &sample, float cutoff_freq, float dt) {
|
|
|
|
if (cutoff_freq <= 0.0f || dt <= 0.0f) {
|
|
|
|
_output = sample;
|
|
|
|
return _output;
|
|
|
|
}
|
2016-02-25 13:13:02 -04:00
|
|
|
float rc = 1.0f/(M_2PI*cutoff_freq);
|
2016-11-09 20:42:58 -04:00
|
|
|
alpha = constrain_float(dt/(dt+rc), 0.0f, 1.0f);
|
2015-08-19 09:15:14 -03:00
|
|
|
_output += (sample - _output) * alpha;
|
2021-04-03 20:14:57 -03:00
|
|
|
if (!initialised) {
|
|
|
|
initialised = true;
|
|
|
|
_output = sample;
|
|
|
|
}
|
2015-08-19 09:15:14 -03:00
|
|
|
return _output;
|
|
|
|
}
|
|
|
|
|
2016-11-09 20:42:58 -04:00
|
|
|
template <class T>
|
|
|
|
T DigitalLPF<T>::apply(const T &sample) {
|
|
|
|
_output += (sample - _output) * alpha;
|
2021-04-03 20:14:57 -03:00
|
|
|
if (!initialised) {
|
|
|
|
initialised = true;
|
|
|
|
_output = sample;
|
|
|
|
}
|
2016-11-09 20:42:58 -04:00
|
|
|
return _output;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
void DigitalLPF<T>::compute_alpha(float sample_freq, float cutoff_freq) {
|
2020-11-08 06:57:01 -04:00
|
|
|
if (sample_freq <= 0) {
|
|
|
|
alpha = 1;
|
2016-11-09 20:42:58 -04:00
|
|
|
} else {
|
2020-11-08 06:57:01 -04:00
|
|
|
alpha = calc_lowpass_alpha_dt(1.0/sample_freq, cutoff_freq);
|
2016-11-09 20:42:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-19 09:15:14 -03:00
|
|
|
// get latest filtered value from filter (equal to the value returned by latest call to apply method)
|
|
|
|
template <class T>
|
|
|
|
const T &DigitalLPF<T>::get() const {
|
|
|
|
return _output;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
void DigitalLPF<T>::reset(T value) {
|
2021-04-03 20:14:57 -03:00
|
|
|
_output = value;
|
|
|
|
initialised = true;
|
2015-08-19 09:15:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// LowPassFilter
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////
|
2016-11-21 01:48:10 -04:00
|
|
|
|
|
|
|
// constructors
|
2015-08-19 09:15:14 -03:00
|
|
|
template <class T>
|
2016-11-21 01:48:10 -04:00
|
|
|
LowPassFilter<T>::LowPassFilter() :
|
|
|
|
_cutoff_freq(0.0f) {}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
LowPassFilter<T>::LowPassFilter(float cutoff_freq) :
|
|
|
|
_cutoff_freq(cutoff_freq) {}
|
|
|
|
|
2015-08-19 09:15:14 -03:00
|
|
|
template <class T>
|
2016-11-21 01:48:10 -04:00
|
|
|
LowPassFilter<T>::LowPassFilter(float sample_freq, float cutoff_freq)
|
|
|
|
{
|
|
|
|
set_cutoff_frequency(sample_freq, cutoff_freq);
|
2015-08-19 09:15:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// change parameters
|
|
|
|
template <class T>
|
|
|
|
void LowPassFilter<T>::set_cutoff_frequency(float cutoff_freq) {
|
|
|
|
_cutoff_freq = cutoff_freq;
|
|
|
|
}
|
|
|
|
|
2016-11-09 20:42:58 -04:00
|
|
|
template <class T>
|
|
|
|
void LowPassFilter<T>::set_cutoff_frequency(float sample_freq, float cutoff_freq) {
|
|
|
|
_cutoff_freq = cutoff_freq;
|
|
|
|
_filter.compute_alpha(sample_freq, cutoff_freq);
|
|
|
|
}
|
|
|
|
|
2015-08-19 09:15:14 -03:00
|
|
|
// return the cutoff frequency
|
|
|
|
template <class T>
|
|
|
|
float LowPassFilter<T>::get_cutoff_freq(void) const {
|
|
|
|
return _cutoff_freq;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
T LowPassFilter<T>::apply(T sample, float dt) {
|
|
|
|
return _filter.apply(sample, _cutoff_freq, dt);
|
|
|
|
}
|
|
|
|
|
2016-11-09 20:42:58 -04:00
|
|
|
template <class T>
|
|
|
|
T LowPassFilter<T>::apply(T sample) {
|
|
|
|
return _filter.apply(sample);
|
|
|
|
}
|
|
|
|
|
2015-08-19 09:15:14 -03:00
|
|
|
template <class T>
|
|
|
|
const T &LowPassFilter<T>::get() const {
|
|
|
|
return _filter.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
void LowPassFilter<T>::reset(T value) {
|
|
|
|
_filter.reset(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make an instances
|
|
|
|
* Otherwise we have to move the constructor implementations to the header file :P
|
|
|
|
*/
|
|
|
|
template class LowPassFilter<int>;
|
|
|
|
template class LowPassFilter<long>;
|
|
|
|
template class LowPassFilter<float>;
|
|
|
|
template class LowPassFilter<Vector2f>;
|
2015-11-24 15:33:52 -04:00
|
|
|
template class LowPassFilter<Vector3f>;
|
|
|
|
|