//1. 移动平均滤波器(信号处理)
#define WINDOW_SIZE 5
float moving_average(float* buffer, float new_sample) {
static float sum = 0;
static int index = 0;
static float samples[WINDOW_SIZE] = {0};
sum -= samples[index];
samples[index] = new_sample;
sum += new_sample;
index = (index + 1) % WINDOW_SIZE;
return sum / WINDOW_SIZE;
}
//2. 中值滤波器(图像处理)
void median_filter(uint8_t* src, uint8_t* dst, int width, int height) {
const int kernel_size = 3;
const int pad = kernel_size / 2;
for (int y = pad; y < height - pad; y++) {
for (int x = pad; x < width - pad; x++) {
uint8_t window[9];
int k = 0;
for (int dy = -pad; dy <= pad; dy++) {
for (int dx = -pad; dx <= pad; dx++) {
window[k++] = src[(y+dy)*width + (x+dx)];
}
}
// 快速排序实现中值查找
qsort(window, 9, sizeof(uint8_t), compare_uint8);
dst[y*width + x] = window[4];
}
}
}
int compare_uint8(const void* a, const void* b) {
return (*(uint8_t*)a - *(uint8_t*)b);
}
//3. FIR滤波器(信号处理)
#define FIR_TAPS 51
typedef struct {
float coeffs[FIR_TAPS];
float buffer[FIR_TAPS];
int index;
} FIRFilter;
void fir_init(FIRFilter* filter, const float* coefficients) {
memcpy(filter->coeffs, coefficients, FIR_TAPS*sizeof(float));
memset(filter->buffer, 0, FIR_TAPS*sizeof(float));
filter->index = 0;
}
float fir_process(FIRFilter* filter, float input) {
filter->buffer[filter->index] = input;
float output = 0.0f;
int sum_index = filter->index;
for (int n = 0; n < FIR_TAPS; n++) {
output += filter->coeffs[n] * filter->buffer[sum_index];
if (--sum_index < 0) sum_index = FIR_TAPS - 1;
}
filter->index = (filter->index + 1) % FIR_TAPS;
return output;
}
//4. IIR滤波器(信号处理)
typedef struct {
float a[3]; // 分母系数
float b[3]; // 分子系数
float x[2]; // 输入延迟线
float y[2]; // 输出延迟线
} IIRFilter;
float iir_process(IIRFilter* filter, float input) {
float output =
filter->b[0] * input +
filter->b[1] * filter->x[0] +
filter->b[2] * filter->x[1] -
filter->a[1] * filter->y[0] -
filter->a[2] * filter->y[1];
// 更新延迟线
filter->x[1] = filter->x[0];
filter->x[0] = input;
filter->y[1] = filter->y[0];
filter->y[0] = output;
return output;
}
//5. 卡尔曼滤波器(传感器融合)
typedef struct {
float q; // 过程噪声协方差
float r; // 测量噪声协方差
float x; // 估计值
float p; // 估计误差协方差
float k; // 卡尔曼增益
} KalmanFilter;
void kalman_init(KalmanFilter* kf, float q, float r, float initial_value) {
kf->q = q;
kf->r = r;
kf->x = initial_value;
kf->p = 1.0f; // 初始不确定度
}
float kalman_update(KalmanFilter* kf, float measurement) {
// 预测步骤
kf->p += kf->q;
// 更新步骤
kf->k = kf->p / (kf->p + kf->r);
kf->x += kf->k * (measurement - kf->x);
kf->p *= (1 - kf->k);
return kf->x;
}
//6. 滑动窗口限幅滤波器
#define WINDOW_SIZE 5
#define DELTA_TH 0.2f
float sliding_window_clipping(float* buffer, float new_sample) {
static float samples[WINDOW_SIZE];
static int index = 0;
// 限幅检查
float avg = 0;
for(int i=0; i DELTA_TH) {
new_sample = avg;
}
samples[index] = new_sample;
index = (index + 1) % WINDOW_SIZE;
return new_sample;
}
//7. 指数加权移动平均滤波器
float ema_filter(float alpha, float new_sample) {
static float ema = 0;
ema = alpha * new_sample + (1 - alpha) * ema;
return ema;
}
//8. 自适应阈值滤波器(ECG信号处理)
void adaptive_threshold_filter(float* signal, int length) {
float threshold = 0.0f;
const float alpha = 0.1f;
for(int i=0; i
//9. 形态学滤波器(图像处理)
void morphological_filter(uint8_t* img, int width, int height, int op) {
// op: 0-膨胀,1-腐蚀
uint8_t* temp = malloc(width*height);
const int kernel[3][3] = {{1,1,1},{1,1,1},{1,1,1}};
for(int y=1; y extremum)) {
extremum = val;
}
}
}
}
temp[y*width + x] = extremum;
}
}
memcpy(img, temp, width*height);
free(temp);
}
//10. 巴特沃斯低通滤波器(信号处理)
typedef struct {
float a[3];
float b[3];
float x[2];
float y[2];
} ButterworthLPF;
void butterworth_init(ButterworthLPF* filter, float cutoff, float sample_rate) {
float omega = 2 * M_PI * cutoff / sample_rate;
float sn = sin(omega);
float cs = cos(omega);
float alpha = sn / (2 * 0.7071); // Q=0.7071
float b0 = (1 - cs)/2;
float b1 = 1 - cs;
float b2 = b0;
float a0 = 1 + alpha;
float a1 = -2 * cs;
float a2 = 1 - alpha;
// 归一化系数
filter->b[0] = b0/a0;
filter->b[1] = b1/a0;
filter->b[2] = b2/a0;
filter->a[0] = 1.0f;
filter->a[1] = a1/a0;
filter->a[2] = a2/a0;
memset(filter->x, 0, sizeof(filter->x));
memset(filter->y, 0, sizeof(filter->y));
}
float butterworth_process(ButterworthLPF* filter, float input) {
float output =
filter->b[0] * input +
filter->b[1] * filter->x[0] +
filter->b[2] * filter->x[1] -
filter->a[1] * filter->y[0] -
filter->a[2] * filter->y[1];
filter->x[1] = filter->x[0];
filter->x[0] = input;
filter->y[1] = filter->y[0];
filter->y[0] = output;
return output;
}
优化建议:
定点数优化:对嵌入式系统可将float改为q15/q31格式,使用ARM的CMSIS-DSP库
循环展开:对FIR等滤波器手动展开循环提升性能
SIMD指令:使用NEON指令集优化图像处理滤波器
DMA传输:在MCU中使用DMA加速图像/数组操作
查表法:对三角函数等复杂计算预先生成查找表
多级滤波:复杂滤波器拆分为多个二阶节(Biquad)串联
图像处理使用双缓冲避免原地修改
动态内存分配尽量在初始化阶段完成
使用静态变量保持滤波器状态
关键数组使用内存对齐(attribute((aligned(16))))
这些实现可以直接嵌入到嵌入式系统中,根据具体需求调整数据类型(如改用定点数)和优化等级。