pt1: implement as template class
Signed-off-by: Thomas Schmid <tom@lfence.de>
This commit is contained in:
parent
ea9aa04022
commit
830703589d
@ -1,10 +1,18 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
struct pt1_state {
|
template<class T>
|
||||||
float y_n1;
|
struct Pt1 {
|
||||||
float K;
|
T y_n1;
|
||||||
float T;
|
T K;
|
||||||
};
|
T T1;
|
||||||
|
|
||||||
void pt1_init(struct pt1_state *state, float K, float T);
|
/* PT1: y = y_(n-1) + (Ku - y_(n-1)) * dt/T1 */
|
||||||
float pt1_update(struct pt1_state *state, float u, float dt);
|
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;
|
||||||
|
}
|
||||||
|
};
|
@ -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;
|
|
||||||
}
|
|
@ -9,17 +9,14 @@ float vu_filt = 0.0f;
|
|||||||
float vu_filt_slow = 0.0f;
|
float vu_filt_slow = 0.0f;
|
||||||
float dt;
|
float dt;
|
||||||
|
|
||||||
struct pt1_state vu_pt1_fast;
|
Pt1<float> vu_pt1_fast{1.f, 1.f};
|
||||||
struct pt1_state vu_pt1_slow;
|
Pt1<float> vu_pt1_slow{1.f, 0.05f};
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
zauberstab_init();
|
zauberstab_init();
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
FastLED.setBrightness(100);
|
FastLED.setBrightness(100);
|
||||||
|
|
||||||
pt1_init(&vu_pt1_slow, 1, 1.f);
|
|
||||||
pt1_init(&vu_pt1_fast, 1, 0.05);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
@ -34,8 +31,8 @@ void loop() {
|
|||||||
EVERY_N_MILLIS(10){
|
EVERY_N_MILLIS(10){
|
||||||
|
|
||||||
float vu = 20 * log10f(rms_avg);
|
float vu = 20 * log10f(rms_avg);
|
||||||
vu_filt = pt1_update(&vu_pt1_fast, vu, 0.01f);
|
vu_filt = vu_pt1_fast.update(vu, 0.01f);
|
||||||
vu_filt_slow = pt1_update(&vu_pt1_slow, vu_filt, 0.01f);
|
vu_filt_slow = vu_pt1_slow.update(vu_filt, 0.01f);
|
||||||
//Serial.println(vu);
|
//Serial.println(vu);
|
||||||
int max_led = vu_filt;
|
int max_led = vu_filt;
|
||||||
int top_led = vu_filt_slow;
|
int top_led = vu_filt_slow;
|
||||||
|
Loading…
Reference in New Issue
Block a user