仓库提交练习
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.6 KiB

/*
* bsp_slide_averager.c
*
* Created on: 2026320
* Author: L1ng the codeGod
*/
#include <stdio.h>
#include <stdint.h>
#include "bsp_slide_averager.h"
// 滑动均值配置(按需修改)
#define WINDOW_SIZE 5 // 窗口长度
/**
* @brief
* @param buffer:
* @param p_index:
* @param p_count:
*/
void slide_averager_init(float buffer[], uint8_t *p_index, uint8_t *p_count) {
// 清空缓冲区
for (uint8_t i = 0; i < WINDOW_SIZE; i++) {
buffer[i] = 0.0f;
}
// 初始化索引和计数
*p_index = 0;
*p_count = 0;
}
/**
* @brief
* @param buffer:
* @param p_index:
* @param p_count:
* @param new_data:
* @retval
*/
float slide_averager_calc(float buffer[], uint8_t *p_index, uint8_t *p_count, float new_data) {
// 1. 存入新数据(覆盖最旧数据)
buffer[*p_index] = new_data;
// 2. 更新索引(循环滑动)
*p_index = (*p_index + 1) % WINDOW_SIZE;
// 3. 更新有效数据计数(窗口未满时累加)
if (*p_count < WINDOW_SIZE) {
(*p_count)++;
}
// 4. 计算均值
float sum = 0.0f;
for (uint8_t i = 0; i < *p_count; i++) {
sum += buffer[i];
}
return sum / *p_count;
}