查看: 1902|回复: 0

[经验] 浮点至定点变换的C程序举例

[复制链接]

该用户从未签到

发表于 2021-3-2 09:13:49 | 显示全部楼层 |阅读模式
分享到:
通过一个例子来说明C程序从浮点变换至定点的方法。这是一个对语音信号(0.3~3.4kHz)进行低通滤波的C语言程序,低通滤波的截止频率为800Hz,滤波器采用19点的有限冲击响应FIR滤波。语音信号的采样频率为8kHz,每个语音样值按16位整型数存放在insp.dat文件中。
例1.7语音信号800Hz 19点FIR低通滤波C语言浮点程序。
  1. <font color="#252525" microsoft="" yahei";="" font-size:="" large;"="">#i nclude  <stdio.h></stdio.h></font>
  2. <font color="#252525" microsoft="" yahei";="" font-size:="" large;"="">const int length=180/*语音帧长为180点=22.5ms@8kHz采样*/</font>
  3. <font color="#252525" microsoft="" yahei";="" font-size:="" large;"="">void filter(int xin[],int xout[],int n,float h[]);/*滤波子程序说明*/</font>
  4. <font color="#252525" microsoft="" yahei";="" font-size:="" large;"="">/*19点滤波器系数*/</font>
  5. <font color="#252525" microsoft="" yahei";="" font-size:="" large;"="">static float h[19]= </font>
  6. <font color="#252525" microsoft="" yahei";="" font-size:="" large;"="">{0.01218354,-0.009012882,-0.02881839,-0.04743239,-0.04584568,</font>
  7. <font color="#252525" microsoft="" yahei";="" font-size:="" large;"="">-0.008692503,0.06446265,0.1544655,0.2289794,0.257883,</font>
  8. <font color="#252525" microsoft="" yahei";="" font-size:="" large;"="">0.2289794,0.1544655,0.06446265,-0.008692503,-0.04584568,</font>
  9. <font color="#252525" microsoft="" yahei";="" font-size:="" large;"="">-0.04743239,-0.02881839,-0.009012882,O.01218354};</font>
  10. <font color="#252525" microsoft="" yahei";="" font-size:="" large;"="">static int xl[length+20];</font>
  11. <font color="#252525" microsoft="" yahei";="" font-size:="" large;"="">/*低通滤波浮点子程序*/</font>
  12. <font color="#252525" microsoft="" yahei";="" font-size:="" large;"="">void filter(int xin[],int xout[],int n,float h[])</font>
  13. <font color="#252525" microsoft="" yahei";="" font-size:="" large;"="">{</font>
  14. <font color="#252525" microsoft="" yahei";="" font-size:="" large;"="">int i,j;</font>
  15. <font color="#252525" microsoft="" yahei";="" font-size:="" large;"="">float sum;</font>
  16. <font color="#252525" microsoft="" yahei";="" font-size:="" large;"="">for(i=0;i<length;i++)x1[n+i-1]=xin;< font="">
  17. <font color="#252525">for(i=0;i<length;i++)</font>
  18. <font color="#252525">{</font>
  19. <font color="#252525">sum=0.0;</font>
  20. <font color="#252525">for(j=0;j<n;j++)sum+=h[j]*x1[i-j+n-1];</font>
  21. <font color="#252525">xout=(int)sum;</font>
  22. <font color="#252525">for(i=0;i<(n-l);i++)x1[n-i-2]=xin[length-1-i];</font>
  23. <font color="#252525">} </font>
  24. <font color="#252525">/*主程序*/</font>
  25. <font color="#252525">void main()</font>
  26. <font color="#252525">FILE *fp1,*fp2;</font>
  27. <font color="#252525">int frame,indata[length],outdata[length];</font>
  28. <font color="#252525">fp1=fopen(insp.dat,"rb");/* 输入语音文件*/</font>
  29. <font color="#252525">fp2=fopen(Outsp.dat,"wb");/* 滤波后语音文件*/</font>
  30. <font color="#252525">frame=0;</font>
  31. <font color="#252525">while(feof(fp1) ==0) </font>
  32. <font color="#252525">{</font>
  33. <font color="#252525">frame++;</font>
  34. <font color="#252525">printf(“frame=%d\n”,frame);</font>
  35. <font color="#252525">for(i=0;i<length;i++)indata=getw(fp1);  /*取一帧语音数据*/</font>
  36. <font color="#252525">filter(indata,outdata,19,h);/*调用低通滤波子程序*/</font>
  37. <font color="#252525">for(i=0;i<length;i++)putw(outdata,fp2);/*将滤波后的样值写入文件*/</font>
  38. <font color="#252525">} </font>
  39. <font color="#252525">fcloseall();/*关闭文件*/</font>
  40. <font color="#252525">return(0);</font>
  41. <font color="#252525">}</font></length;i++)x1[n+i-1]=xin;<></font>
复制代码

<length;i++)x1[n+i-1]=xin;例1.8语音信号800Hz l9点FIR低通滤波C语言定点程序。
</length;i++)x1[n+i-1]=xin;
  1. <font color="#252525">#i nclude <stdio.h></stdio.h></font>
  2. <font color="#252525">const int length=180; </font>
  3. <font color="#252525">void  filter (int xin[],int xout[],int n,int h[]);</font>
  4. <font color="#252525">static int h[19]={399,-296,-945,-1555,-1503,-285,2112,5061,7503,8450,</font>
  5. <font color="#252525">7503,5061,2112,-285,-1503,-1555,-945,-296,399};/*Q15*/</font>
  6. <font color="#252525">static int x1[length+20];</font>
  7. <font color="#252525">/*低通滤波定点子程序*/</font>
  8. <font color="#252525">void filter(int xin[],int xout[],int n,int h[])</font>
  9. <font color="#252525">int i,j;</font>
  10. <font color="#252525">long sum;</font>
  11. <font color="#252525">for(i=0;i<length;i++)x1[n+i-111=xin];</font>
  12. <font color="#252525">for(i=0;i<1ength;i++)</font>
  13. <font color="#252525">sum=0;</font>
  14. <font color="#252525">for(j=0;j<n;j++)sum+=(long)h[j]*x1[i-j+n-1];</font>
  15. <font color="#252525">xout=sum>>15;</font>
  16. <font color="#252525">for(i=0;i<(n-1);i++)x1[n-i-2]=xin[length-i-1];</font>
  17. <font color="#252525">}</font>
复制代码

<length;i++)x1[n+i-1]=xin;主程序与浮点的完全一样。</length;i++)x1[n+i-1]=xin;
回复

使用道具 举报

您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /4 下一条



手机版|小黑屋|与非网

GMT+8, 2024-11-25 01:51 , Processed in 0.112427 second(s), 15 queries , MemCache On.

ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

苏公网安备 32059002001037号

Powered by Discuz! X3.4

Copyright © 2001-2024, Tencent Cloud.