From 37401d6fd1e56a0cad08425642ee0cb4a1c37066 Mon Sep 17 00:00:00 2001 From: bresch Date: Mon, 30 Sep 2024 11:19:57 +0200 Subject: [PATCH] AlphaFilter: allow setting dt in update call --- src/lib/mathlib/math/filter/AlphaFilter.hpp | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/lib/mathlib/math/filter/AlphaFilter.hpp b/src/lib/mathlib/math/filter/AlphaFilter.hpp index 4e651cfee9..b270bd43f9 100644 --- a/src/lib/mathlib/math/filter/AlphaFilter.hpp +++ b/src/lib/mathlib/math/filter/AlphaFilter.hpp @@ -71,6 +71,8 @@ public: if (denominator > FLT_EPSILON) { setAlpha(sample_interval / denominator); } + + _time_constant = time_constant; } bool setCutoffFreq(float sample_freq, float cutoff_freq) @@ -82,8 +84,7 @@ public: return false; } - setParameters(1.f / sample_freq, 1.f / (2.f * M_PI_F * cutoff_freq)); - _cutoff_freq = cutoff_freq; + setParameters(1.f / sample_freq, 1.f / (M_TWOPI_F * cutoff_freq)); return true; } @@ -112,14 +113,19 @@ public: return _filter_state; } + const T update(const T &sample, float dt) + { + setParameters(dt, _time_constant); + return update(sample); + } + const T &getState() const { return _filter_state; } - float getCutoffFreq() const { return _cutoff_freq; } + float getCutoffFreq() const { return 1.f / (M_TWOPI_F * _time_constant); } protected: T updateCalculation(const T &sample); - - float _cutoff_freq{0.f}; + float _time_constant{0.f}; float _alpha{0.f}; T _filter_state{}; };