C语言编写一阶低通滤波器
时间: 2023-05-16 19:07:29 浏览: 98
可以使用以下代码实现:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.14159265358979323846
int main()
{
double fc, fs, w, T, alpha, a0, a1, a2, b0, b1, b2;
double x[3] = {0}, y[3] = {0}, input, output;
int i;
printf("请输入截止频率fc和采样频率fs:\n");
scanf("%lf%lf", &fc, &fs);
w = 2 * PI * fc / fs;
T = 1 / fs;
alpha = sin(w) / (2 * sqrt(2) * cos(w) + sin(w));
a0 = (1 - cos(w)) / 2;
a1 = 1 - cos(w);
a2 = (1 - cos(w)) / 2;
b0 = (1 - cos(w) + sin(w) / 2) / 2;
b1 = cos(w) - 1;
b2 = (1 - cos(w) - sin(w) / 2) / 2;
printf("a0 = %lf, a1 = %lf, a2 = %lf, b0 = %lf, b1 = %lf, b2 = %lf\n", a0, a1, a2, b0, b1, b2);
while (1) {
printf("请输入输入信号:\n");
scanf("%lf", &input);
output = b0 * input + b1 * x[1] + b2 * x[2] - a1 * y[1] - a2 * y[2];
x[2] = x[1];
x[1] = input;
y[2] = y[1];
y[1] = output;
printf("输出信号为:%lf\n", output);
}
return 0;
}