基于DSP的IIR滤波器的设计
2019-07-13 12:37 发布
生成海报
一、基本要求
1 设计FIR滤波器,其中 N=10、Fs = 8000Hz、Fc = 2500Hz;
2 定点实现,不允许使用float、double;
二、FIR滤波器基本知识介绍
IIR(Infinite Impulse Response)数字滤波器,又名“无限脉冲响应”数字滤波器,具有反馈, 我们下面设计的IIR滤波器的基本结构如下:
三、DSP程序设计
#include "math.h"
#define MCA_LENGTHDATA 256
#define MCA_aLength 11
#define MCA_bLength 11
#define N 8
#define MCA_F1 1000;
#define MCA_F2 3500;
#define MCA_Fs 8000;
#define PI 3.1415926
float MCA_coef_b[MCA_bLength] = {
0.01686334051, 0.1686334014, 0.7588502765, 2.023600817, 3.54130125,
4.249561787, 3.54130125, 2.023600817, 0.7588502765, 0.1686334014,
0.01686334051
};
float MCA_coef_a[MCA_aLength] = {
1, 2.490851402, 3.966767073, 4.083814621, 3.087100267,
1.698986053, 0.6925140619, 0.2022430897, 0.04052210972, 0.004977103323,
0.000284461712
};
float MCA_tepInDataS1[MCA_LENGTHDATA];
long long MCA_coef_aNom[MCA_aLength];
long long MCA_coef_bNom[MCA_bLength];
long long MCA_inData[MCA_LENGTHDATA];
float MCA_outData[MCA_LENGTHDATA];
long long Qn = 0;
float MCA_tepInData[MCA_LENGTHDATA];
int f1 = MCA_F1;
int f2 = MCA_F2;
int fs = MCA_Fs;
/***********************************************************
* 合成语音文件,并且转换为整型,同时乘以2的N次方
************************************************************/
void MCA_readVoiceData()
{
int i = 0;
for(i = 0; i < MCA_LENGTHDATA; i++)
{
MCA_tepInData[i] = 0; }
for(i = 0; i < MCA_LENGTHDATA; i++) {
MCA_tepInDataS1[i] = sin(2*PI*i*f1/fs);
MCA_tepInData[i] = sin(2*PI*i*f1/fs) +sin(2*PI*i*f2/fs) ; MCA_inData[i] = (int)(MCA_tepInData[i] * Qn); }
}
/***********************************************************
* IIR滤波器系数变为整型,同时乘以2的N次方
************************************************************/
void MAC_normalizedIIRCoeff()
{ int i = 0; for(i = 0; i < MCA_bLength; i++) { MCA_coef_bNom[i] = (int)(MCA_coef_b[i] * Qn); } for(i = 0; i {
MCA_coef_aNom[i] = (int)(MCA_coef_a[i] * Qn); }
}
void MAC_iir()
{ int i = 0; int a= 0; int j = 0; int k = 0; long long int sum1 = 0; long long int sum2 = 0;
long long MCA_tepOutData[MCA_LENGTHDATA]; for (i = 0; i {
MCA_tepOutData[i] = 0; } for (i=0; i {
sum1 = 0;
sum2 = 0; for (j = 0; j
{
sum1 += MCA_tepOutData[i + MCA_aLength - j]*MCA_coef_aNom[j];
} for (k = 0; k
{
sum2 += MCA_inData[i + MCA_bLength - k]*MCA_coef_bNom[k];
}
MCA_tepOutData[i] = sum1+sum2;
a = MCA_tepOutData[i]>>N;
MCA_outData[i] = (a)*1.0/Qn; }
}
void MCA_write()
{ FILE *fidOut = NULL; fidOut = fopen("signal.dat", "wb");
fwrite(MCA_outData, sizeof(float), MCA_LENGTHDATA, fidOut); fclose(fidOut);
}
void main()
{ Qn = pow(2,N);
MCA_readVoiceData(); MAC_normalizedIIRCoeff(); MAC_iir();
MCA_write(); while(1);
}
四、实验结果
下图上面为正弦波合成的波形,下面为FIR滤波后输出波形。
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮