查看: 1743|回复: 0

[经验] 分享:C程序从浮点变换至定点的方法

[复制链接]

该用户从未签到

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

使用道具 举报

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

本版积分规则

关闭

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



手机版|小黑屋|与非网

GMT+8, 2024-11-27 16:42 , Processed in 0.115543 second(s), 15 queries , MemCache On.

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

苏公网安备 32059002001037号

Powered by Discuz! X3.4

Copyright © 2001-2024, Tencent Cloud.