vad.m
上传用户:luvkyhj
上传日期:2013-06-25
资源大小:3k
文件大小:2k
源码类别:

屏幕保护

开发平台:

Matlab

  1. function [x1,x2]=vad(x)%端点检测子程序
  2. %幅度归一化到(-1,1)
  3. x=double(x);
  4. x=x/max(abs(x));
  5. FrameLen=160;
  6. FrameInc=80;
  7. amp1=10;
  8. amp2=2;
  9. zcr1=10;
  10. zcr2=5;
  11. ener1=0.1;
  12. ener2=0.05;
  13. maxsilence=3;
  14. minlen=15;
  15. status=0;
  16. count=0;
  17. silence=0;
  18. tmp1=enframe(x(1:length(x)-1),FrameLen,FrameInc);
  19. tmp2=enframe(x(2:length(x)),FrameLen,FrameInc);
  20. signs=(tmp1.*tmp2)<0;
  21. diffs=abs(tmp1-tmp2)>0.02;
  22. zcr=sum(signs.*diffs,2);
  23. amp=sum(abs(enframe(filter([1 -0.9375],1,x),FrameLen,FrameInc)),2);
  24. amp1=min(amp1,max(amp)/4);
  25. amp2=min(amp2,max(amp)/8);
  26. ener=sum((enframe(filter([1 -0.9375],1,x),FrameLen,FrameInc)).^2,2);
  27. ener1=min(ener1,max(ener)/8);
  28. x1=0;
  29. x2=0;
  30. for n=1:length(zcr)
  31.     goto=0;
  32.     switch status
  33.         case{0,1}
  34.             if amp(n)>amp1
  35.                 x1=max(n-count-1,1);
  36.                 status=2;
  37.                 silence=0;
  38.                 count=count+1;
  39.             elseif amp(n)>amp2 | zcr(n)>zcr2|ener(n)>ener2
  40.                 status=1;
  41.                 count=count+1;
  42.             else
  43.                 status=0;
  44.                 count=0;
  45.             end
  46.         case 2,
  47.             if amp(n)>amp2 | zcr(n)>zcr2|ener(n)>ener2
  48.                 count=count+1;
  49.             else
  50.                 silence=silence+1;
  51.                 if silence<maxsilence
  52.                     count=count+1;
  53.                 elseif count<minlen
  54.                     status=0;
  55.                     silence=0;
  56.                     count=0;
  57.                 else
  58.                     status=3;
  59.                 end
  60.             end
  61.         case 3,
  62.             break;
  63.     end
  64. end
  65. count=count-silence/2;
  66. x2=x1+count-1;
  67. subplot(313)
  68. plot(x);
  69. axis([1 length(x)/2 -1 1])
  70. ylabel('speech');
  71. xlabel('time');
  72. line([x1*FrameInc x1*FrameInc],[-1 1],'Color','red');
  73. line([x2*FrameInc x2*FrameInc],[-1 1],'Color','red');
  74. %subplot(412)
  75. %plot(amp)
  76. %axis([1 length(amp) 0 max(amp)])
  77. %ylabel('Amplitude');
  78. %xlabel('帧');
  79. %line([x1 x1],[min(amp),max(amp)],'Color','red');
  80. %line([x2 x2],[min(amp),max(amp)],'Color','red');
  81. %subplot(413)
  82. %plot(ener)
  83. %axis([1 length(ener) 0 max(ener)])
  84. %ylabel('Energy');
  85. %xlabel('帧');
  86. %line([x1 x1],[min(ener),max(ener)],'Color','red');
  87. %line([x2 x2],[min(ener),max(ener)],'Color','red');
  88. %subplot(414)
  89. %plot(zcr)
  90. %axis([1 length(zcr) 0 max(zcr)])
  91. %ylabel('ZCR');
  92. %xlabel('帧');
  93. %line([x1 x1],[min(zcr),max(zcr)],'Color','red');
  94. %line([x2 x2],[min(zcr),max(zcr)],'Color','red');