h文件
#ifndef _LPF_FIRST_ORDER_H
#define _LPF_FIRST_ORDER_H
typedef struct _lpf_first_order
{
float fc; // cut-off frequency
float uk;
float alpha; // filter coefficient
float T; // samping period
}LpfFirstOderObj;
void lpf_first_order_init(LpfFirstOderObj *filter, float alpha);
float lpf_first_order(LpfFirstOderObj *filter, float k);
void lpf_first_order_fc_set(LpfFirstOderObj *filter, float fc, float T);
#endif
C文件
#include "lpf_first_order.h"
#define LPF_PI 3.1415926
void lpf_first_order_init(LpfFirstOderObj *filter, float alpha)
{
filter->alpha = alpha;
filter->fc = 0;
filter->T = 0;
filter->uk = 0;
}
float lpf_first_order(LpfFirstOderObj *filter, float k)
{
float uo;
uo = filter->alpha * k + (1 - filter->alpha) * filter->uk;
filter->uk = uo;
return uo;
}
void lpf_first_order_fc_set(LpfFirstOderObj *filter, float fc, float T)
{
filter->fc = fc;
filter->T = T;
filter->alpha = (2 * LPF_PI * fc * T) / (1 + 2 * LPF_PI * fc * T);
}
|