2013-08-29 02:34:34 -03:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2012-03-25 04:15:25 -03:00
|
|
|
|
2013-08-29 02:34:34 -03:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
//
|
2012-03-25 04:15:25 -03:00
|
|
|
/// @file LowPassFilter.h
|
2024-07-27 09:16:20 -03:00
|
|
|
/// @brief A class to implement a low pass filter.
|
2016-11-09 20:42:58 -04:00
|
|
|
|
|
|
|
/*
|
2024-07-27 09:16:20 -03:00
|
|
|
Two classes are provided:
|
2016-11-09 20:42:58 -04:00
|
|
|
|
2024-07-27 09:16:20 -03:00
|
|
|
LowPassFilter: providing dt on every sample, and calling apply like this:
|
2016-11-09 20:42:58 -04:00
|
|
|
|
|
|
|
// call once
|
|
|
|
filter.set_cutoff_frequency(frequency_hz);
|
|
|
|
|
|
|
|
// then on each sample
|
|
|
|
output = filter.apply(sample, dt);
|
|
|
|
|
2024-07-27 09:16:20 -03:00
|
|
|
LowPassFilterConstDt: providing a sample freq and cutoff_freq once at start
|
2016-11-09 20:42:58 -04:00
|
|
|
|
|
|
|
// call once
|
|
|
|
filter.set_cutoff_frequency(sample_freq, frequency_hz);
|
|
|
|
|
|
|
|
// then on each sample
|
|
|
|
output = filter.apply(sample);
|
|
|
|
|
|
|
|
The second approach is more CPU efficient as it doesn't have to
|
|
|
|
recalculate alpha each time, but it assumes that dt is constant
|
|
|
|
*/
|
|
|
|
|
2016-02-17 21:25:53 -04:00
|
|
|
#pragma once
|
2012-03-25 04:15:25 -03:00
|
|
|
|
2015-08-11 03:28:46 -03:00
|
|
|
#include <AP_Math/AP_Math.h>
|
2012-03-25 04:15:25 -03:00
|
|
|
|
2015-04-16 21:54:06 -03:00
|
|
|
// DigitalLPF implements the filter math
|
2015-08-19 09:15:14 -03:00
|
|
|
template <class T>
|
|
|
|
class DigitalLPF {
|
2012-08-17 03:22:11 -03:00
|
|
|
public:
|
2024-07-27 09:16:20 -03:00
|
|
|
|
|
|
|
// constructor
|
2015-08-19 09:15:14 -03:00
|
|
|
DigitalLPF();
|
2016-11-09 20:42:58 -04:00
|
|
|
|
2021-06-06 03:47:03 -03:00
|
|
|
CLASS_NO_COPY(DigitalLPF);
|
|
|
|
|
2015-04-16 21:54:06 -03:00
|
|
|
// get latest filtered value from filter (equal to the value returned by latest call to apply method)
|
2015-08-19 09:15:14 -03:00
|
|
|
const T &get() const;
|
2024-07-27 09:16:20 -03:00
|
|
|
|
|
|
|
// Reset filter to given value
|
|
|
|
void reset(const T &value);
|
|
|
|
|
|
|
|
// Set reset flag such that the filter will be reset to the next value applied
|
|
|
|
void reset();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// add a new raw value to the filter, retrieve the filtered result
|
|
|
|
T _apply(const T &sample, const float &alpha);
|
2012-08-17 03:22:11 -03:00
|
|
|
|
|
|
|
private:
|
2024-07-27 09:16:20 -03:00
|
|
|
T output;
|
2021-04-03 20:14:57 -03:00
|
|
|
bool initialised;
|
2012-03-25 04:15:25 -03:00
|
|
|
};
|
|
|
|
|
2024-07-27 09:16:20 -03:00
|
|
|
// Low pass filter with constant time step
|
2015-08-19 09:15:14 -03:00
|
|
|
template <class T>
|
2024-07-27 09:16:20 -03:00
|
|
|
class LowPassFilterConstDt : public DigitalLPF<T> {
|
2015-04-16 16:07:44 -03:00
|
|
|
public:
|
2012-03-25 04:15:25 -03:00
|
|
|
|
2024-07-27 09:16:20 -03:00
|
|
|
// constructors
|
|
|
|
LowPassFilterConstDt() {};
|
|
|
|
LowPassFilterConstDt(const float &sample_freq, const float &cutoff_freq);
|
|
|
|
|
|
|
|
CLASS_NO_COPY(LowPassFilterConstDt);
|
2021-06-06 03:47:03 -03:00
|
|
|
|
2015-04-16 16:07:44 -03:00
|
|
|
// change parameters
|
2024-07-27 09:16:20 -03:00
|
|
|
void set_cutoff_frequency(const float &sample_freq, const float &cutoff_freq);
|
2016-11-09 20:42:58 -04:00
|
|
|
|
2015-04-16 16:07:44 -03:00
|
|
|
// return the cutoff frequency
|
2024-07-27 09:16:20 -03:00
|
|
|
float get_cutoff_freq() const;
|
2021-04-03 20:14:57 -03:00
|
|
|
|
2024-07-27 09:16:20 -03:00
|
|
|
// add a new raw value to the filter, retrieve the filtered result
|
|
|
|
T apply(const T &sample);
|
2014-09-21 05:33:59 -03:00
|
|
|
|
2015-04-16 16:07:44 -03:00
|
|
|
private:
|
2024-07-27 09:16:20 -03:00
|
|
|
float cutoff_freq;
|
|
|
|
float alpha;
|
2015-04-16 16:07:44 -03:00
|
|
|
};
|
2012-11-18 12:06:06 -04:00
|
|
|
|
2024-07-27 09:16:20 -03:00
|
|
|
typedef LowPassFilterConstDt<float> LowPassFilterConstDtFloat;
|
|
|
|
typedef LowPassFilterConstDt<Vector2f> LowPassFilterConstDtVector2f;
|
|
|
|
typedef LowPassFilterConstDt<Vector3f> LowPassFilterConstDtVector3f;
|
|
|
|
|
|
|
|
// Low pass filter with variable time step
|
2015-08-19 09:15:14 -03:00
|
|
|
template <class T>
|
2024-07-27 09:16:20 -03:00
|
|
|
class LowPassFilter : public DigitalLPF<T> {
|
|
|
|
public:
|
|
|
|
|
|
|
|
// constructors
|
|
|
|
LowPassFilter() {};
|
|
|
|
LowPassFilter(const float &cutoff_freq);
|
|
|
|
|
|
|
|
CLASS_NO_COPY(LowPassFilter);
|
|
|
|
|
|
|
|
// change parameters
|
|
|
|
void set_cutoff_frequency(const float &cutoff_freq);
|
|
|
|
|
|
|
|
// return the cutoff frequency
|
|
|
|
float get_cutoff_freq() const;
|
|
|
|
|
|
|
|
// add a new raw value to the filter, retrieve the filtered result
|
|
|
|
T apply(const T &sample, const float &dt);
|
|
|
|
|
|
|
|
private:
|
|
|
|
float cutoff_freq;
|
|
|
|
};
|
2015-08-19 09:15:14 -03:00
|
|
|
|
|
|
|
// typedefs for compatibility
|
|
|
|
typedef LowPassFilter<float> LowPassFilterFloat;
|
|
|
|
typedef LowPassFilter<Vector2f> LowPassFilterVector2f;
|
|
|
|
typedef LowPassFilter<Vector3f> LowPassFilterVector3f;
|