写一个低通滤波算法c语言代码
时间: 2023-11-10 14:57:11 浏览: 42
以下是一个简单的低通滤波器的C语言代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.14159265358979323846
void low_pass_filter(double* input, double* output, int length, double cutoff_freq, double sample_rate) {
double RC = 1.0 / (cutoff_freq * 2.0 * PI);
double dt = 1.0 / sample_rate;
double alpha = dt / (RC + dt);
output[0] = input[0];
for (int i = 1; i < length; i++) {
output[i] = alpha * input[i] + (1 - alpha) * output[i-1];
}
}
int main() {
double input[10] = {1,2,3,4,5,6,7,8,9,10};
double output[10];
double cutoff_freq = 5.0; // Hz
double sample_rate = 50.0; // Hz
int length = 10;
low_pass_filter(input, output, length, cutoff_freq, sample_rate);
for (int i = 0; i < length; i++) {
printf("%f\n", output[i]);
}
return 0;
}
```
本代码使用的是一阶低通滤波器,通过计算RC常数并计算时间常数alpha来实现滤波。该函数将输入数组input中的数据进行滤波,并将结果存储到输出数组output中。在主函数中,我们可以将输入数组设置为{1,2,3,4,5,6,7,8,9,10},并将采样率设置为50Hz,截止频率设置为5Hz。该代码将输出滤波后的数组。