mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-29 02:23:34 +08:00
AlphaFilter: add function to set param from cutoff frequency
This commit is contained in:
committed by
Mathieu Bresciani
parent
b5d401b20c
commit
1b58202b0d
@@ -43,6 +43,9 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <float.h>
|
#include <float.h>
|
||||||
|
#include <mathlib/math/Functions.hpp>
|
||||||
|
|
||||||
|
using namespace math;
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class AlphaFilter
|
class AlphaFilter
|
||||||
@@ -70,6 +73,21 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setCutoffFreq(float sample_freq, float cutoff_freq)
|
||||||
|
{
|
||||||
|
if ((sample_freq <= 0.f) || (cutoff_freq <= 0.f) || (cutoff_freq >= sample_freq / 2.f)
|
||||||
|
|| !isFinite(sample_freq) || !isFinite(cutoff_freq)) {
|
||||||
|
|
||||||
|
// No filtering
|
||||||
|
_alpha = 1.f;
|
||||||
|
_cutoff_freq = 0.f;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setParameters(1.f / sample_freq, 1.f / (2.f * M_PI_F * cutoff_freq));
|
||||||
|
_cutoff_freq = cutoff_freq;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set filter parameter alpha directly without time abstraction
|
* Set filter parameter alpha directly without time abstraction
|
||||||
*
|
*
|
||||||
@@ -96,10 +114,12 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
const T &getState() const { return _filter_state; }
|
const T &getState() const { return _filter_state; }
|
||||||
|
float getCutoffFreq() const { return _cutoff_freq; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
T updateCalculation(const T &sample) { return (1.f - _alpha) * _filter_state + _alpha * sample; }
|
T updateCalculation(const T &sample) { return (1.f - _alpha) * _filter_state + _alpha * sample; }
|
||||||
|
|
||||||
|
float _cutoff_freq{0.f};
|
||||||
float _alpha{0.f};
|
float _alpha{0.f};
|
||||||
T _filter_state{};
|
T _filter_state{};
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user