|
这是一个对语音信号(0.3~3.4kHz)进行低通滤波的C语言程序,低通滤波的截止频率为800Hz,滤波器采用19点的有限冲击响应FIR滤波。语音信号的采样频率为8kHz,每个语音样值按16位整型数存放在insp.dat文件中。
例1.7语音信号800Hz 19点FIR低通滤波C语言浮点程序。
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">#i nclude <stdio.h></stdio.h></font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">const int length=180/*语音帧长为180点=22.5ms@8kHz采样*/</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">void filter(int xin[],int xout[],int n,float h[]);/*滤波子程序说明*/</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">/*19点滤波器系数*/</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">static float h[19]= </font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">{0.01218354,-0.009012882,-0.02881839,-0.04743239,-0.04584568,</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">-0.008692503,0.06446265,0.1544655,0.2289794,0.257883,</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">0.2289794,0.1544655,0.06446265,-0.008692503,-0.04584568,</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">-0.04743239,-0.02881839,-0.009012882,O.01218354};</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">static int xl[length+20];</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">/*低通滤波浮点子程序*/</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">void filter(int xin[],int xout[],int n,float h[])</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">{</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">int i,j;</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">float sum;</font>
- <font size="4" style="color: rgb(51, 51, 51); font-family: " microsoft="" yahei";"="">for(i=0;i<length;i++)x1[n+i-1]=xin;< font="">
- <font size="4">for(i=0;i<length;i++)</font>
- <font size="4">{</font>
- <font size="4">sum=0.0;</font>
- <font size="4">for(j=0;j<n;j++)sum+=h[j]*x1[i-j+n-1];</font>
- <font size="4">xout=(int)sum;</font>
- <font size="4">for(i=0;i<(n-l);i++)x1[n-i-2]=xin[length-1-i];</font>
- <font size="4">} </font>
- <font size="4">/*主程序*/</font>
- <font size="4">void main()</font>
- <font size="4">FILE *fp1,*fp2;</font>
- <font size="4">int ,indata[length],outdata[length];</font>
- <font size="4">fp1=fopen(insp.dat,"rb");/* 输入语音文件*/</font>
- <font size="4">fp2=fopen(Outsp.dat,"wb");/* 滤波后语音文件*/</font>
- <font size="4">=0;</font>
- <font size="4">while(feof(fp1) ==0) </font>
- <font size="4">{</font>
- <font size="4">++;</font>
- <font size="4">printf(“=%d\n”,);</font>
- <font size="4">for(i=0;i<length;i++)indata=getw(fp1); /*取一帧语音数据*/</font>
- <font size="4">filter(indata,outdata,19,h);/*调用低通滤波子程序*/</font>
- <font size="4">for(i=0;i<length;i++)putw(outdata,fp2);/*将滤波后的样值写入文件*/</font>
- <font size="4">} </font>
- <font size="4">fcloseall();/*关闭文件*/</font>
- <font size="4">return(0);</font>
- <font size="4">}</font>
- <font size="4">例1.8语音信号800Hz l9点FIR低通滤波C语言定点程序。</font>
- <font size="4">#i nclude <stdio.h></stdio.h></font>
- <font size="4">const int length=180; </font>
- <font size="4">void filter (int xin[],int xout[],int n,int h[]);</font>
- <font size="4">static int h[19]={399,-296,-945,-1555,-1503,-285,2112,5061,7503,8450,</font>
- <font size="4">7503,5061,2112,-285,-1503,-1555,-945,-296,399};/*Q15*/</font>
- <font size="4">static int x1[length+20];</font>
- <font size="4">/*低通滤波定点子程序*/</font>
- <font size="4">void filter(int xin[],int xout[],int n,int h[])</font>
- <font size="4">int i,j;</font>
- <font size="4">long sum;</font>
- <font size="4">for(i=0;i<length;i++)x1[n+i-111=xin];</font>
- <font size="4">for(i=0;i<1ength;i++)</font>
- <font size="4">sum=0;</font>
- <font size="4">for(j=0;j<n;j++)sum+=(long)h[j]*x1[i-j+n-1];</font>
- <font size="4">xout=sum>>15;</font>
- <font size="4">for(i=0;i<(n-1);i++)x1[n-i-2]=xin[length-i-1];</font>
- <font size="4">}</font></length;i++)x1[n+i-1]=xin;<></font>
复制代码
<length;i++)x1[n+i-1]=xin;主程序与浮点的完全一样。“</length;i++)x1[n+i-1]=xin;
|
|