用c++写一个陷波滤波器
时间: 2023-04-06 15:02:00 浏览: 119
可以使用以下代码实现一个陷波滤波器:
```c
#include <stdio.h>
#include <math.h>
#define PI 3.14159265358979323846
void trap_filter(double *input, double *output, int n, double fs, double f0, double bw)
{
double w0 = 2 * PI * f0 / fs;
double alpha = sin(w0) * sinh(log(2) / 2 * bw * w0 / sin(w0));
double beta = cos(w0);
double a0 = 1 + alpha;
double a1 = -2 * beta;
double a2 = 1 - alpha;
double b0 = 1 - alpha;
double b1 = -2 * beta;
double b2 = 1 + alpha;
double x1 = 0, x2 = 0, y1 = 0, y2 = 0;
for (int i = 0; i < n; i++) {
double x = input[i];
double y = b0 / a0 * x + b1 / a0 * x1 + b2 / a0 * x2 - a1 / a0 * y1 - a2 / a0 * y2;
output[i] = y;
x2 = x1;
x1 = x;
y2 = y1;
y1 = y;
}
}
int main()
{
double fs = 1000; // 采样率
double f0 = 50; // 中心频率
double bw = 0.1; // 带宽
int n = 1000; // 采样点数
double input[n];
double output[n];
// 生成测试信号
for (int i = 0; i < n; i++) {
double t = i / fs;
input[i] = sin(2 * PI * 100 * t) + sin(2 * PI * 200 * t) + sin(2 * PI * 300 * t);
}
// 陷波滤波
trap_filter(input, output, n, fs, f0, bw);
// 输出结果
for (int i = 0; i < n; i++) {
printf("%f\n", output[i]);
}
return 0;
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)