有没有FIR滤波器的C语言实现
时间: 2024-05-12 19:20:09 浏览: 56
FIR滤波器的C语言实现
4星 · 用户满意度95%
有的,下面是一个简单的FIR滤波器的C语言实现:
```c
#define MAX_LENGTH 1000
double FIR_filter(double input, double *coefficients, int length, double *history)
{
double output = 0;
// shift the history buffer
for (int i = length - 1; i > 0; i--)
{
history[i] = history[i - 1];
}
// add the new input to the history buffer
history[0] = input;
// calculate the output using the filter coefficients and history buffer
for (int i = 0; i < length; i++)
{
output += coefficients[i] * history[i];
}
return output;
}
int main()
{
double coefficients[] = {0.1, 0.2, 0.3, 0.2, 0.1}; // filter coefficients
double history[MAX_LENGTH] = {0}; // history buffer
double input = 0; // input signal
double output = 0; // filtered output
// loop over input signal and filter the data
for (int i = 0; i < MAX_LENGTH; i++)
{
// get input signal (e.g. from ADC)
// ...
// apply the filter
output = FIR_filter(input, coefficients, 5, history);
// do something with the filtered output (e.g. send to DAC)
// ...
}
return 0;
}
```
在上面的代码中,`FIR_filter`函数接受一个输入信号、一组滤波器系数、滤波器长度和历史缓冲区作为参数,并返回滤波后的输出信号。在主函数中,我们可以循环读取输入信号,并将其传递给滤波器函数进行滤波。滤波后的输出信号可以用于其他用途,例如传输到DAC。
阅读全文