分享MSP430单片机实现的FIR滤波器C语言程序
[复制链接]
#include
#define FRAME 180
short int h[19] = {
399,-296,-945,-1555,
-1503,-285,2112,5061,
7503,8450,7503,5061,
2112,-285,-1503,-1555,
-945,-296,399
};
static short int nBuff[FRAME + 20];
void LowpassFilter(short int nIn[],short int nOut[],int nLen,short int h[]);
void main()
{
FILE *m_pInput;
FILE *m_pOutput;
short int input[FRAME];
short int output[FRAME];
int count;
// 打开输入文件
if((m_pInput = fopen("input.wav", "rb")) == NULL)
{
return;
}
//打开输出文件
if((m_pOutput = fopen("output.wav", "wb")) == NULL)
{
return;
}
count = 0;
while( fread(&input[0], sizeof(short int), FRAME, m_pInput) == FRAME)
{
printf("Frame =%d\r", count++);
//滤波处理
LowpassFilter(input,output,19,h);
//将滤波后的数据写到文件里
fwrite(output, sizeof(short int), FRAME, m_pOutput);
}
fclose(m_pOutput);
fclose(m_pInput);
}
void LowpassFilter(short int nIn[],short int nOut[],int nLen,short int h[])
{
short int i,j;
int sum;
//缓冲区的内容更新
for(i = 0;i < FRAME;i++)
{
nBuff[i + nLen -1] = nIn[i];
}
//FIR滤波处理
for(i = 0;i < FRAME;i++)
{
sum = 0;
for(j = 0;j < nLen;j++)
{
sum += h[j] * nBuff[i - j + nLen -1];
}
nOut[i] = sum >> 15;
}
//更新缓冲区的内容
for(i = 0;i < nLen - 1;i++)
{
nBuff[nLen - i - 2] = nIn[FRAME - i - 1];
}
}