// 文件编码: GB18030
/*
 * filter.h
 *
 *  Created on: 2025年4月6日
 *      Author: LHYe200
 */

#ifndef CODE_FILTER_H_
#define CODE_FILTER_H_

#ifndef M_PI
#define M_PI (3.141592f)
#endif

typedef struct
{
    float ts;       //采样周期(s)
    float fc;       //截至频率(hz)
    float lastYn;   //上一次滤波值
    float alpha;    //滤波系数
} low_pass_filter_t;


typedef struct
{
    float ts;       //采样周期(s)
    float fc;       //下限频率(hz)
    float lastYn;   //上一次滤波值
    float lastXn;   //上一次采样值
    float alpha;    //滤波系数
} hight_pass_filter_t;

typedef struct
{
    float avg;      // 滑动平均值
    int size;       // 窗口大小
    float *window;  // 存储窗口数据
} moving_average_filter_t;

typedef struct
{
    float prevData; // 先前数值
    float p;        // 估计协方差
    float q;        // 过程噪声协方差
    float r;        // 测量噪声协方差
    float kGain;    // 卡尔曼增益
} Kalman_filter_t;


// 低通滤波器
void Init_lowPass_alpha(low_pass_filter_t* const filter,const float ts, const float fc);
float Low_pass_filter(low_pass_filter_t* const filter, float data);

// 高通滤波器
void Init_hightPass_alpha(hight_pass_filter_t* const filter,const float ts, const float fc);
float Hight_pass_filter(hight_pass_filter_t* const filter, float data);

// 滑动平均滤波器
void Moving_Average_filter_init(moving_average_filter_t* const filter, int size);
float Moving_Average_filter(moving_average_filter_t* const filter, float newData);

// 卡尔曼滤波器
void Kalman_filter_init(Kalman_filter_t* const filter, const float q, const float r);
float Kalman_filter(Kalman_filter_t* const filter, float inData);

// 无迹卡尔曼滤波器
void Unscented_Kalman_filter_init(Kalman_filter_t* const filter, const float q, const float r);
float Unscented_Kalman_filter(Kalman_filter_t* const filter, float inData);

// 自适应卡尔曼滤波器
void Adaptive_Kalman_filter_init(Kalman_filter_t* const filter, const float q, const float r);
float Adaptive_Kalman_filter(Kalman_filter_t* const filter, float inData);


#endif /* CODE_FILTER_H_ */
