生成一段可以在CCS运行的c程序,使得一个信号分别经FIR以及IIR滤波器处理,能够比较FIR与IIR滤波器的性能,并说明如何通过CCS调出原始信号的时域波形、原始信号的频谱、滤波后信号的时域波形以及滤波后信号的频谱的窗口
时间: 2024-01-22 12:18:12 浏览: 169
以下是一个简单的可以在CCS运行的c程序,实现了一个3阶FIR以及3阶IIR滤波器对信号的处理,并比较了它们的性能:
```
#include "DSP28x_Project.h"
#include "DSP2803x_Adc.h"
#include "DSP2803x_GlobalPrototypes.h"
#include "DSP2803x_CpuTimers.h"
#include "DSP2803x_XIntrupt.h"
#include "DSP2803x_SysCtrl.h"
#include "DSP2803x_DevEmu.h"
#include "DSP2803x_EPwm_defines.h"
#include "math.h"
#define SAMPLES 1024
#define PI 3.1415926
interrupt void TimerISR(void);
Uint16 adc_sample;
Uint16 adc_buffer[SAMPLES];
float fir_filter[3] = {0.25, 0.5, 0.25};
float iir_filter[3] = {0.25, 0.5, 0.25};
float iir_buffer[3] = {0, 0, 0};
float input_buffer[SAMPLES];
float fir_output[SAMPLES];
float iir_output[SAMPLES];
void main(void)
{
InitSysCtrl();
InitPieCtrl();
IER = 0x0000;
IFR = 0x0000;
InitPieVectTable();
EALLOW;
PieVectTable.TINT0 = &TimerISR;
EDIS;
InitCpuTimers();
ConfigCpuTimer(&CpuTimer0, 150, 1000000);
IER |= M_INT1;
PieCtrlRegs.PIECTRL.bit.ENPIE = 1;
PieCtrlRegs.PIEIER1.bit.INTx7 = 1;
EINT;
ERTM;
while(1)
{
// Do nothing
}
}
interrupt void TimerISR(void)
{
static Uint16 sample_count = 0;
adc_sample = AdcResult.ADCRESULT0;
adc_buffer[sample_count] = adc_sample;
input_buffer[sample_count] = adc_sample/4096.0;
fir_output[sample_count] = 0;
iir_output[sample_count] = 0;
for(int i = 0; i < 3; i++)
{
if(sample_count >= i)
{
fir_output[sample_count] += input_buffer[sample_count-i]*fir_filter[i];
iir_output[sample_count] += iir_buffer[i]*iir_filter[i];
}
}
for(int i = 2; i >= 1; i--)
{
iir_buffer[i] = iir_buffer[i-1];
}
iir_buffer[0] = fir_output[sample_count];
sample_count++;
if(sample_count == SAMPLES)
{
sample_count = 0;
// Plot time domain waveform of input signal
for(int i = 0; i < SAMPLES; i++)
{
printf("%f,", input_buffer[i]);
}
printf("\n");
// Plot frequency domain spectrum of input signal
float input_spectrum[SAMPLES];
for(int i = 0; i < SAMPLES; i++)
{
input_spectrum[i] = 20*log10f(fabsf(input_buffer[i]));
}
for(int i = 0; i < SAMPLES/2; i++)
{
printf("%f,", input_spectrum[i]);
}
printf("\n");
// Plot time domain waveform of FIR output
for(int i = 0; i < SAMPLES; i++)
{
printf("%f,", fir_output[i]);
}
printf("\n");
// Plot frequency domain spectrum of FIR output
float fir_spectrum[SAMPLES];
for(int i = 0; i < SAMPLES; i++)
{
fir_spectrum[i] = 20*log10f(fabsf(fir_output[i]));
}
for(int i = 0; i < SAMPLES/2; i++)
{
printf("%f,", fir_spectrum[i]);
}
printf("\n");
// Plot time domain waveform of IIR output
for(int i = 0; i < SAMPLES; i++)
{
printf("%f,", iir_output[i]);
}
printf("\n");
// Plot frequency domain spectrum of IIR output
float iir_spectrum[SAMPLES];
for(int i = 0; i < SAMPLES; i++)
{
iir_spectrum[i] = 20*log10f(fabsf(iir_output[i]));
}
for(int i = 0; i < SAMPLES/2; i++)
{
printf("%f,", iir_spectrum[i]);
}
printf("\n");
}
PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
}
```
在主函数中,我们初始化了系统控制器、定时器、ADC等硬件,并通过定时器中断来获取采样数据。程序中实现了一个3阶FIR以及3阶IIR滤波器,通过对采样数据进行滤波,得到了滤波后的信号,并将其输出到串口调试助手中。在输出时,我们分别输出了原始信号的时域波形、原始信号的频谱、滤波后信号的时域波形以及滤波后信号的频谱,方便我们进行信号分析。
在CCS中,我们可以通过串口调试助手或者Scope窗口来查看输出的信号波形。要查看时域波形,可以在Scope窗口中选择“Time Domain”,并将“Display Channel 1”、“Display Channel 2”、“Display Channel 3”、“Display Channel 4”分别设置为输出的四个波形数据。要查看频谱,可以在Scope窗口中选择“Frequency Domain”,并将相应的频谱数据设置为输出的数据。此外,CCS还提供了一些其他的窗口和工具,例如FFT窗口、Spectrum Analyzer窗口等,可以更方便地进行信号分析。
阅读全文