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
|
|
|
|
/// @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
|
2016-11-09 20:42:58 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
Note that this filter can be used in 2 ways:
|
|
|
|
|
|
|
|
1) providing dt on every sample, and calling apply like this:
|
|
|
|
|
|
|
|
// call once
|
|
|
|
filter.set_cutoff_frequency(frequency_hz);
|
|
|
|
|
|
|
|
// then on each sample
|
|
|
|
output = filter.apply(sample, dt);
|
|
|
|
|
|
|
|
2) providing a sample freq and cutoff_freq once at start
|
|
|
|
|
|
|
|
// 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-10-09 21:07:25 -03:00
|
|
|
#include "FilterClass.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:
|
2015-08-19 09:15:14 -03:00
|
|
|
DigitalLPF();
|
2015-04-16 21:54:06 -03:00
|
|
|
// add a new raw value to the filter, retrieve the filtered result
|
2015-08-19 09:15:14 -03:00
|
|
|
T apply(const T &sample, float cutoff_freq, float dt);
|
2016-11-09 20:42:58 -04:00
|
|
|
T apply(const T &sample);
|
|
|
|
|
|
|
|
void compute_alpha(float sample_freq, float cutoff_freq);
|
|
|
|
|
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;
|
|
|
|
void reset(T value);
|
2012-08-17 03:22:11 -03:00
|
|
|
|
|
|
|
private:
|
2015-08-19 09:15:14 -03:00
|
|
|
T _output;
|
2016-11-09 20:42:58 -04:00
|
|
|
float alpha = 1.0f;
|
2012-03-25 04:15:25 -03:00
|
|
|
};
|
|
|
|
|
2015-04-16 21:54:06 -03:00
|
|
|
// LPF base class
|
2015-08-19 09:15:14 -03:00
|
|
|
template <class T>
|
|
|
|
class LowPassFilter {
|
2015-04-16 16:07:44 -03:00
|
|
|
public:
|
2015-08-19 09:15:14 -03:00
|
|
|
LowPassFilter();
|
|
|
|
LowPassFilter(float cutoff_freq);
|
2016-11-21 01:48:10 -04:00
|
|
|
LowPassFilter(float sample_freq, float cutoff_freq);
|
2012-03-25 04:15:25 -03:00
|
|
|
|
2015-04-16 16:07:44 -03:00
|
|
|
// change parameters
|
2015-08-19 09:15:14 -03:00
|
|
|
void set_cutoff_frequency(float cutoff_freq);
|
2016-11-09 20:42:58 -04:00
|
|
|
void set_cutoff_frequency(float sample_freq, float cutoff_freq);
|
|
|
|
|
2015-04-16 16:07:44 -03:00
|
|
|
// return the cutoff frequency
|
2015-08-19 09:15:14 -03:00
|
|
|
float get_cutoff_freq(void) const;
|
|
|
|
T apply(T sample, float dt);
|
2016-11-09 20:42:58 -04:00
|
|
|
T apply(T sample);
|
2015-08-19 09:15:14 -03:00
|
|
|
const T &get() const;
|
|
|
|
void reset(T value);
|
2016-11-09 20:42:58 -04:00
|
|
|
void reset(void) { reset(T()); }
|
2015-08-19 09:15:14 -03:00
|
|
|
|
2015-04-16 16:07:44 -03:00
|
|
|
protected:
|
|
|
|
float _cutoff_freq;
|
2014-09-21 05:33:59 -03:00
|
|
|
|
2015-04-16 16:07:44 -03:00
|
|
|
private:
|
2015-08-19 09:15:14 -03:00
|
|
|
DigitalLPF<T> _filter;
|
2015-04-16 16:07:44 -03:00
|
|
|
};
|
2012-11-18 12:06:06 -04:00
|
|
|
|
2015-08-19 09:15:14 -03:00
|
|
|
// Uncomment this, if you decide to remove the instantiations in the implementation file
|
|
|
|
/*
|
|
|
|
template <class T>
|
|
|
|
LowPassFilter<T>::LowPassFilter() : _cutoff_freq(0.0f) {
|
|
|
|
|
|
|
|
}
|
|
|
|
// constructor
|
|
|
|
template <class T>
|
|
|
|
LowPassFilter<T>::LowPassFilter(float cutoff_freq) : _cutoff_freq(cutoff_freq) {
|
|
|
|
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
// typedefs for compatibility
|
|
|
|
typedef LowPassFilter<int> LowPassFilterInt;
|
|
|
|
typedef LowPassFilter<long> LowPassFilterLong;
|
|
|
|
typedef LowPassFilter<float> LowPassFilterFloat;
|
|
|
|
typedef LowPassFilter<Vector2f> LowPassFilterVector2f;
|
|
|
|
typedef LowPassFilter<Vector3f> LowPassFilterVector3f;
|