50Hz陷波器c代码实现
时间: 2023-11-02 19:05:00 浏览: 136
50HZ陷波器的C程序源码.
4星 · 用户满意度95%
下面是一个实现50Hz陷波器的C代码示例:
```c
#include <stdio.h>
#include <math.h>
#define PI 3.141592653589793238
void notch_filter_50Hz(double *input, double *output, int len, double fs)
{
double f0 = 50;
double Q = 10;
double w0 = 2 * PI * f0 / fs;
double alpha = sin(w0) / (2 * Q);
double a0 = 1 + alpha;
double a1 = -2 * cos(w0);
double a2 = 1 - alpha;
double b0 = 1;
double b1 = -2 * cos(w0);
double b2 = 1;
double x1 = 0, x2 = 0, y1 = 0, y2 = 0;
for (int i = 0; i < len; i++) {
double x0 = input[i];
output[i] = (b0 / a0) * x0 + (b1 / a0) * x1 + (b2 / a0) * x2
- (a1 / a0) * y1 - (a2 / a0) * y2;
x2 = x1;
x1 = x0;
y2 = y1;
y1 = output[i];
}
}
int main()
{
int len = 100;
double input[100];
double output[100];
for (int i = 0; i < len; i++) {
input[i] = sin(2 * PI * i * 50 / 1000) + sin(2 * PI * i * 150 / 1000);
}
double fs = 1000;
notch_filter_50Hz(input, output, len, fs);
for (int i = 0; i < len; i++) {
printf("%f\n", output[i]);
}
return 0;
}
```
该代码实现了一个50Hz陷波器,可以过滤掉50Hz的信号。与通用的陷波器代码类似,只需要将中心频率`f0`设置为50Hz即可。在`main`函数中,生成了一个包含50Hz和150Hz信号的合成信号,并对其进行滤波处理。
阅读全文