From 830703589deece2f000bbf16dee41079511136a7 Mon Sep 17 00:00:00 2001 From: Thomas Schmid Date: Sun, 19 Jun 2022 12:33:59 +0200 Subject: [PATCH] pt1: implement as template class Signed-off-by: Thomas Schmid --- firmware/include/pt1.h | 22 +++++++++++++++------- firmware/src/pt1.cpp | 15 --------------- firmware/temp/vumeter.cpp | 11 ++++------- 3 files changed, 19 insertions(+), 29 deletions(-) delete mode 100644 firmware/src/pt1.cpp diff --git a/firmware/include/pt1.h b/firmware/include/pt1.h index 8e9a039..4d08d54 100644 --- a/firmware/include/pt1.h +++ b/firmware/include/pt1.h @@ -1,10 +1,18 @@ #pragma once -struct pt1_state { - float y_n1; - float K; - float T; -}; +template +struct Pt1 { + T y_n1; + T K; + T T1; -void pt1_init(struct pt1_state *state, float K, float T); -float pt1_update(struct pt1_state *state, float u, float dt); \ No newline at end of file + /* PT1: y = y_(n-1) + (Ku - y_(n-1)) * dt/T1 */ + Pt1(T K, T T1) : T1(T1), K(K) {}; + + T update(T u, T dt) + { + T y = this->y_n1 + (this->K * u - this->y_n1) * dt / this->T1; + this->y_n1 = y; + return y; + } +}; \ No newline at end of file diff --git a/firmware/src/pt1.cpp b/firmware/src/pt1.cpp deleted file mode 100644 index 7c9b623..0000000 --- a/firmware/src/pt1.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include "pt1.h" - -void pt1_init(struct pt1_state *state, float K, float T) -{ - state->K = K; - state->T = T; - state->y_n1 = 0.0f; -} - -float pt1_update(struct pt1_state *state, float u, float dt) -{ - float y = state->y_n1 + (state->K * u - state->y_n1) * dt / state->T; - state->y_n1 = y; - return y; -} \ No newline at end of file diff --git a/firmware/temp/vumeter.cpp b/firmware/temp/vumeter.cpp index 2d5ac39..09bc8f9 100644 --- a/firmware/temp/vumeter.cpp +++ b/firmware/temp/vumeter.cpp @@ -9,17 +9,14 @@ float vu_filt = 0.0f; float vu_filt_slow = 0.0f; float dt; -struct pt1_state vu_pt1_fast; -struct pt1_state vu_pt1_slow; +Pt1 vu_pt1_fast{1.f, 1.f}; +Pt1 vu_pt1_slow{1.f, 0.05f}; void setup() { zauberstab_init(); Serial.begin(115200); FastLED.setBrightness(100); - - pt1_init(&vu_pt1_slow, 1, 1.f); - pt1_init(&vu_pt1_fast, 1, 0.05); } void loop() { @@ -34,8 +31,8 @@ void loop() { EVERY_N_MILLIS(10){ float vu = 20 * log10f(rms_avg); - vu_filt = pt1_update(&vu_pt1_fast, vu, 0.01f); - vu_filt_slow = pt1_update(&vu_pt1_slow, vu_filt, 0.01f); + vu_filt = vu_pt1_fast.update(vu, 0.01f); + vu_filt_slow = vu_pt1_slow.update(vu_filt, 0.01f); //Serial.println(vu); int max_led = vu_filt; int top_led = vu_filt_slow;