在一阶低通滤波器后,去除波动较大的波动值,解决了有水时触摸导致的风速突变

This commit is contained in:
95384 2025-01-18 08:52:38 +08:00
parent 955999ece9
commit 117777c092
2 changed files with 36 additions and 4 deletions

View File

@ -10,14 +10,44 @@ void initLowPassFilter(SL_LowPassFilter* filter, float alpha)
filter->alpha = alpha; filter->alpha = alpha;
// 初始化上一次的输出值为0 // 初始化上一次的输出值为0
filter->previous = 0.0f; filter->previous = 0.0f;
filter->x = 0;
filter->times = 0;
} }
// 更新滤波器输出值 // 更新滤波器输出值
float updateFilter(SL_LowPassFilter* filter, float new_input) float updateFilter(SL_LowPassFilter* filter, float new_input)
{ {
// 更新滤波后的值 // 滤波新值
float output = (1.0f - filter->alpha) * filter->previous + filter->alpha * new_input; float filtedData = (1.0f - filter->alpha) * filter->previous + filter->alpha * new_input;
// 更新上一次的输出值
filter->previous = output; //差值大于一定值认为有问题暂定4
return output; if((filter->x) - filtedData > 4 || (filtedData) - filter->x > 4 )
{
// 将有问题的值存起来
(filter->x) = filtedData;
// 使用上一次的正确值当结果
filtedData = filter->previous;
// 清空计数
filter->times = 0;
}else
{
// 差值在允许范围内
// 差值合理一定时间后认为数据没问题
if(filter->times < 3)
{
filter->times++;
// 没满足次数,使用旧值
filtedData = filter->previous;
}
else
{
// 将值存起来,直接使用滤波后的值
(filter->x) = filtedData;
}
}
// 更新上一次的输出值
filter->previous = filtedData;
return filtedData;
} }

View File

@ -9,6 +9,8 @@
typedef struct { typedef struct {
float alpha; // 滤波器系数 float alpha; // 滤波器系数
float previous; // 上一次的输出值 float previous; // 上一次的输出值
float x; // 上一次的输入值
uint8_t times; // 次数
} SL_LowPassFilter; } SL_LowPassFilter;
extern SL_LowPassFilter low_pass_filter_x; extern SL_LowPassFilter low_pass_filter_x;