c语言设计一个低通三阶巴特沃斯滤波器
时间: 2023-05-12 19:05:13 浏览: 301
以下是一个 C 语言实现的低通三阶巴特沃斯滤波器的代码:
```
#include <stdio.h>
#include <math.h>
#define PI 3.14159265358979323846
void butterworth_lowpass_filter(double *input, double *output, int length, double cutoff_frequency, int order)
{
double w0 = 2.0 * PI * cutoff_frequency;
double wc = w0 / tan(w0 / 2.0);
double a[order+1], b[order+1];
double alpha = sin(w0) / (2.0 * pow(2.0, 1.0/2.0) * pow(cos(w0/2.0), 1.0/order));
double beta = pow(1.0 + pow(alpha, 2.0), 1.0/2.0);
double a0 = 1.0 + alpha;
a[0] = 1.0;
a[1] = -2.0 * cos(w0) / a0;
a[2] = (1.0 - alpha) / a0;
b[0] = pow(beta - alpha, order);
b[1] = 2.0 * (pow(alpha, order) - pow(beta, order)) * cos(w0) / a0;
b[2] = (pow(beta + alpha, order) - 2.0 * pow(alpha, order)) / a0;
double x[order+1] = {0};
double y[order+1] = {0};
for (int i = 0; i < length; i++) {
x[0] = input[i];
y[0] = b[0] * x[0];
for (int j = 1; j <= order; j++) {
y[0] += b[j] * x[j] - a[j] * y[j];
x[j] = x[j-1];
y[j] = y[j-1];
}
output[i] = y[0];
}
}
int main()
{
double input[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
double output[10] = {0};
double cutoff_frequency = 2.0;
int order = 3;
butterworth_lowpass_filter(input, output, 10, cutoff_frequency, order);
for (int i = 0; i < 10; i++) {
printf("%f ", output[i]);
}
printf("\n");
return 0;
}
```
这个代码实现了一个三阶巴特沃斯低通滤波器,可以通过修改 `cutoff_frequency` 和 `order` 来调整滤波器的截止频率和阶数。在 `main` 函数中,我们定义了一个长度为 10 的输入信号 `input`,并将其输入到滤波器中,得到了输出信号 `output`。最后,我们将输出信号打印出来。
阅读全文