pt1: implement as template class

Signed-off-by: Thomas Schmid <tom@lfence.de>
This commit is contained in:
Thomas 2022-06-19 12:33:59 +02:00
parent ea9aa04022
commit 830703589d
3 changed files with 19 additions and 29 deletions

View File

@ -1,10 +1,18 @@
#pragma once
struct pt1_state {
float y_n1;
float K;
float T;
};
template<class T>
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);
/* 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;
}
};

View File

@ -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;
}

View File

@ -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<float> vu_pt1_fast{1.f, 1.f};
Pt1<float> 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;