mirror of
https://github.com/betaflight/betaflight.git
synced 2025-07-24 16:55:36 +03:00
Lagged Moving Average smoothing/filter
* We plan to use this for RC command filter based interpolation to satisfy CSRF use case, but not for gyro filtering.
This commit is contained in:
parent
2dd1d742bf
commit
10da8313e8
2 changed files with 35 additions and 0 deletions
|
@ -192,3 +192,26 @@ FAST_CODE float biquadFilterApply(biquadFilter_t *filter, float input)
|
|||
filter->x2 = filter->b2 * input - filter->a2 * result;
|
||||
return result;
|
||||
}
|
||||
|
||||
void laggedMovingAverageInit(laggedMovingAverage_t *filter, uint16_t windowSize, float *buf)
|
||||
{
|
||||
filter->movingWindowIndex = 0;
|
||||
filter->windowSize = windowSize;
|
||||
filter->buf = buf;
|
||||
filter->primed = false;
|
||||
}
|
||||
|
||||
FAST_CODE float laggedMovingAverageUpdate(laggedMovingAverage_t *filter, float input)
|
||||
{
|
||||
filter->movingSum -= filter->buf[filter->movingWindowIndex];
|
||||
filter->buf[filter->movingWindowIndex] = input;
|
||||
filter->movingSum += input;
|
||||
|
||||
if (++filter->movingWindowIndex == filter->windowSize) {
|
||||
filter->movingWindowIndex = 0;
|
||||
filter->primed = true;
|
||||
}
|
||||
|
||||
const uint16_t denom = filter->primed ? filter->windowSize : filter->movingWindowIndex;
|
||||
return filter->movingSum / denom;
|
||||
}
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
*/
|
||||
|
||||
#pragma once
|
||||
#include <stdbool.h>
|
||||
|
||||
struct filter_s;
|
||||
typedef struct filter_s filter_t;
|
||||
|
@ -40,6 +41,14 @@ typedef struct biquadFilter_s {
|
|||
float x1, x2, y1, y2;
|
||||
} biquadFilter_t;
|
||||
|
||||
typedef struct laggedMovingAverage_s {
|
||||
uint16_t movingWindowIndex;
|
||||
uint16_t windowSize;
|
||||
float movingSum;
|
||||
float *buf;
|
||||
bool primed;
|
||||
} laggedMovingAverage_t;
|
||||
|
||||
typedef enum {
|
||||
FILTER_PT1 = 0,
|
||||
FILTER_BIQUAD,
|
||||
|
@ -63,6 +72,9 @@ float biquadFilterApplyDF1(biquadFilter_t *filter, float input);
|
|||
float biquadFilterApply(biquadFilter_t *filter, float input);
|
||||
float filterGetNotchQ(float centerFreq, float cutoffFreq);
|
||||
|
||||
void laggedMovingAverageInit(laggedMovingAverage_t *filter, uint16_t windowSize, float *buf);
|
||||
float laggedMovingAverageUpdate(laggedMovingAverage_t *filter, float input);
|
||||
|
||||
float pt1FilterGain(uint16_t f_cut, float dT);
|
||||
void pt1FilterInit(pt1Filter_t *filter, float k);
|
||||
float pt1FilterApply(pt1Filter_t *filter, float input);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue