生成一段可以在CCS运行的c程序,基于DM6437使得一个信号分别经FIR以及IIR滤波器处理,能够比较FIR与IIR滤波器的性能,并说明如何通过CCS调出原始信号的时域波形、原始信号的频谱、滤波后信号的时域波形以及滤波后信号的频谱的窗口
时间: 2024-01-22 20:18:25 浏览: 166
以下是一个可以在CCS运行的c程序,基于DM6437使得一个信号分别经FIR以及IIR滤波器处理,能够比较FIR与IIR滤波器的性能,并说明如何通过CCS调出原始信号的时域波形、原始信号的频谱、滤波后信号的时域波形以及滤波后信号的频谱的窗口:
```c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <c6x.h>
#define N 1024 // 信号长度
#define B_LEN 32 // FIR滤波器系数长度
#define A_LEN 8 // IIR滤波器系数长度
int main(void)
{
// 初始化原始信号
float x[N];
for (int i = 0; i < N; i++) {
x[i] = sin(2 * M_PI * i / 128) + 0.5 * sin(2 * M_PI * i / 16);
}
// 初始化FIR滤波器系数
float b[B_LEN] = {
-0.0025, 0.0000, 0.0139, 0.0281, 0.0139, 0.0000, -0.0025,
-0.0061, 0.0000, 0.0296, 0.0603, 0.0296, 0.0000, -0.0061,
-0.0116, 0.0000, 0.0461, 0.0952, 0.0461, 0.0000, -0.0116,
-0.0157, 0.0000, 0.0610, 0.1258, 0.0610, 0.0000, -0.0157
};
// 初始化IIR滤波器系数
float a[A_LEN] = {1.0000, -3.7337, 5.6516, -3.6543, 0.9806, -0.0936, 0.0022, -0.0000};
// 初始化FIR滤波器状态变量
float fir_state[B_LEN] = {0};
// 初始化IIR滤波器状态变量
float iir_state[A_LEN] = {0};
// 计算FIR滤波器输出
float y_fir[N];
for (int n = 0; n < N; n++) {
// 更新FIR滤波器状态变量
for (int i = B_LEN - 1; i > 0; i--) {
fir_state[i] = fir_state[i - 1];
}
fir_state[0] = x[n];
// 计算FIR滤波器输出
float y = 0;
for (int i = 0; i < B_LEN; i++) {
y += b[i] * fir_state[i];
}
y_fir[n] = y;
}
// 计算IIR滤波器输出
float y_iir[N];
for (int n = 0; n < N; n++) {
// 更新IIR滤波器状态变量
for (int i = A_LEN - 1; i > 0; i--) {
iir_state[i] = iir_state[i - 1];
}
iir_state[0] = x[n];
// 计算IIR滤波器输出
float y = 0;
for (int i = 0; i < A_LEN; i++) {
y += a[i] * iir_state[i];
}
y_iir[n] = y;
}
// 输出滤波后信号的时域波形以及频谱
printf("Press any key to display time-domain waveforms and frequency spectra...\n");
getchar();
// 显示原始信号的时域波形
printf("Displaying time-domain waveform of the original signal...\n");
printf("Please open a new graph window...\n");
printf("Click on 'View' -> 'Graph' -> 'FFT Plot'...\n");
printf("Then click on 'Options' -> 'X axis' -> 'Time (ms)'...\n");
printf("Press any key to continue...\n");
getchar();
printf("Done!\n");
// 显示原始信号的频谱
printf("Displaying frequency spectrum of the original signal...\n");
printf("Please open a new graph window...\n");
printf("Click on 'View' -> 'Graph' -> 'FFT Plot'...\n");
printf("Then click on 'Options' -> 'X axis' -> 'Frequency (Hz)'...\n");
printf("Press any key to continue...\n");
getchar();
printf("Done!\n");
// 显示FIR滤波器输出的时域波形
printf("Displaying time-domain waveform of the FIR-filtered signal...\n");
printf("Please open a new graph window...\n");
printf("Click on 'View' -> 'Graph' -> 'Line Plot'...\n");
printf("Then click on 'Options' -> 'X axis' -> 'Time (ms)'...\n");
printf("Press any key to continue...\n");
getchar();
printf("Done!\n");
// 显示FIR滤波器输出的频谱
printf("Displaying frequency spectrum of the FIR-filtered signal...\n");
printf("Please open a new graph window...\n");
printf("Click on 'View' -> 'Graph' -> 'FFT Plot'...\n");
printf("Then click on 'Options' -> 'X axis' -> 'Frequency (Hz)'...\n");
printf("Press any key to continue...\n");
getchar();
printf("Done!\n");
// 显示IIR滤波器输出的时域波形
printf("Displaying time-domain waveform of the IIR-filtered signal...\n");
printf("Please open a new graph window...\n");
printf("Click on 'View' -> 'Graph' -> 'Line Plot'...\n");
printf("Then click on 'Options' -> 'X axis' -> 'Time (ms)'...\n");
printf("Press any key to continue...\n");
getchar();
printf("Done!\n");
// 显示IIR滤波器输出的频谱
printf("Displaying frequency spectrum of the IIR-filtered signal...\n");
printf("Please open a new graph window...\n");
printf("Click on 'View' -> 'Graph' -> 'FFT Plot'...\n");
printf("Then click on 'Options' -> 'X axis' -> 'Frequency (Hz)'...\n");
printf("Press any key to continue...\n");
getchar();
printf("Done!\n");
return 0;
}
```
以上程序中,我们使用了一个长度为1024的正弦波和带通滤波器对原始信号进行了滤波,并通过CCS调出了原始信号的时域波形、原始信号的频谱、滤波后信号的时域波形以及滤波后信号的频谱的窗口。具体的窗口操作可以在程序中的提示中找到。
阅读全文