diff --git a/src/main/common/filter.c b/src/main/common/filter.c index cd44aa1b5..f3582f213 100644 --- a/src/main/common/filter.c +++ b/src/main/common/filter.c @@ -46,11 +46,12 @@ void pt1FilterInit(pt1Filter_t *filter, uint8_t f_cut, float dT) { filter->RC = 1.0f / ( 2.0f * M_PI_FLOAT * f_cut ); filter->dT = dT; + filter->k = filter->dT / (filter->RC + filter->dT); } float pt1FilterApply(pt1Filter_t *filter, float input) { - filter->state = filter->state + filter->dT / (filter->RC + filter->dT) * (input - filter->state); + filter->state = filter->state + filter->k * (input - filter->state); return filter->state; } @@ -60,9 +61,10 @@ float pt1FilterApply4(pt1Filter_t *filter, float input, uint8_t f_cut, float dT) if (!filter->RC) { filter->RC = 1.0f / ( 2.0f * M_PI_FLOAT * f_cut ); filter->dT = dT; + filter->k = filter->dT / (filter->RC + filter->dT); } - filter->state = filter->state + filter->dT / (filter->RC + filter->dT) * (input - filter->state); + filter->state = filter->state + filter->k * (input - filter->state); return filter->state; } diff --git a/src/main/common/filter.h b/src/main/common/filter.h index 35b5ca8f1..4d86bc431 100644 --- a/src/main/common/filter.h +++ b/src/main/common/filter.h @@ -25,6 +25,7 @@ typedef struct pt1Filter_s { float state; + float k; float RC; float dT; } pt1Filter_t;