compair.asv
上传用户:doudz874
上传日期:2007-03-30
资源大小:792k
文件大小:4k
源码类别:

语音合成与识别

开发平台:

Matlab

  1. %function compair;
  2. %load model;
  3. %disp('press any key to start');
  4. %input('');
  5. Fs=8000;
  6. mywave=wavrecord(2*Fs,Fs);
  7. wavplay(mywave,Fs);
  8. %ave_wave=sum(mywave)/(2*Fs);
  9. end_wave=mywave-mean(mywave);
  10. %E(i),Z(i),Z_opt(i)
  11. Z_cmp=mean(abs(end_wave(100:500)));%0.02;
  12. Nframe=2*Fs/80-1;
  13. for i=1:Nframe;
  14.       E(i)=(end_wave((i-1)*80+1))^2;
  15.       Z(i)=0;
  16.       Z_opt(i)=0;  
  17.     for j=2:160
  18.         E(i)=E(i)+end_wave((i-1)*80+j)^2;
  19.         Z(i)=Z(i)+abs(sign(end_wave((i-1)*80+j))-sign(end_wave((i-1)*80+j-1)));
  20.         Z_opt(i)=Z_opt(i)+abs(sign(end_wave((i-1)*80+j)-Z_cmp)-sign(end_wave((i-1)*80+j-1)-Z_cmp));   
  21.     end
  22.     Z(i)=Z(i)/2;
  23.     Z_opt(i)= Z_opt(i)/2;
  24.    
  25. end
  26. %start and end
  27. E_high= 0.3;
  28. E_low=20*mean(E(1:10));%%%%%%%%%0.2;
  29. Z_low=2*mean(Z_opt(1:10));%%%%%%%%%%%15;
  30. start=1;
  31. end0=1;
  32. for i=2:Nframe
  33.     if E(i)>E_high && E(i-1)<E_high
  34.         start=i;
  35.     end 
  36.     if E(i)<E_high && E(i-1)>E_high 
  37.         end0=i;
  38.     end
  39.     if end0-start>15
  40.         start1=start;
  41.         end1=end0;
  42.     end
  43.     %%%%%%%%%%%%%
  44. end
  45. i=start1;
  46. while E(i)>E_low && i>1
  47.     i=i-1;
  48. end
  49. start2=i+1;
  50. i=end1;
  51. while E(i)>E_low && i<Nframe
  52.     i=i+1;
  53. end
  54. end2=i-1;
  55. i=start2;
  56. while Z_opt(i)>Z_low && i>1
  57.     i=i-1;
  58. end
  59. start3=i;
  60. %plot
  61. %i=1:Fs*2;%%%%%%%%
  62. i=(start3-5)*80:(end2+5)*80;
  63. y=-1:0.01:1;
  64.  
  65. figure(1);
  66. plot(i,end_wave(i));
  67. hold;
  68. plot(start3*80,y);
  69. plot(end2*80+160,y);
  70. hold off;
  71. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%lpc%%%%%%%
  72. model_sample=zeros(end2-start3+1,44);
  73. for i=start3:end2
  74.     [A,C,F]=lpc(end_wave((i-1)*80+1:(i-1)*80+160));
  75.     acf=[A,C,F];
  76.     model_sample(i-start3+1,:)=acf;
  77. end
  78. %dtw
  79. %A
  80. D_model_A=zeros(11,1);
  81. D_model_A(1)=dtwdistance(model_sample(:,1:12),model1(:,1:12));%
  82. D_model_A(2)=dtwdistance(model_sample(:,1:12),model2(:,1:12));%
  83. D_model_A(3)=dtwdistance(model_sample(:,1:12),model3(:,1:12));%
  84. D_model_A(4)=dtwdistance(model_sample(:,1:12),model4(:,1:12));%
  85. D_model_A(5)=dtwdistance(model_sample(:,1:12),model5(:,1:12));%
  86. D_model_A(6)=dtwdistance(model_sample(:,1:12),model6(:,1:12));%
  87. D_model_A(7)=dtwdistance(model_sample(:,1:12),model7(:,1:12));%
  88. D_model_A(8)=dtwdistance(model_sample(:,1:12),model8(:,1:12));%
  89. D_model_A(9)=dtwdistance(model_sample(:,1:12),model9(:,1:12));%
  90. D_model_A(10)=dtwdistance(model_sample(:,1:12),model10(:,1:12));%
  91. D_model_A(11)=dtwdistance(model_sample(:,1:12),model0(:,1:12));%
  92. [Dmin,ai]=min(D_model_A);
  93. if ai==11
  94.     ai=0;
  95. end;
  96. %A
  97. D_model_C=zeros(11,1);
  98. D_model_C(1)=dtwdistance(model_sample(:,13:28),model1(:,13:28));%
  99. D_model_C(2)=dtwdistance(model_sample(:,13:28),model2(:,13:28));%
  100. D_model_C(3)=dtwdistance(model_sample(:,13:28),model3(:,13:28));%
  101. D_model_C(4)=dtwdistance(model_sample(:,13:28),model4(:,13:28));%
  102. D_model_C(5)=dtwdistance(model_sample(:,13:28),model5(:,13:28));%
  103. D_model_C(6)=dtwdistance(model_sample(:,13:28),model6(:,13:28));%
  104. D_model_C(7)=dtwdistance(model_sample(:,13:28),model7(:,13:28));%
  105. D_model_C(8)=dtwdistance(model_sample(:,13:28),model8(:,13:28));%
  106. D_model_C(9)=dtwdistance(model_sample(:,13:28),model9(:,13:28));%
  107. D_model_C(10)=dtwdistance(model_sample(:,13:28),model10(:,13:28));%
  108. D_model_C(11)=dtwdistance(model_sample(:,13:28),model0(:,13:28));%
  109. [Dmin,ci]=min(D_model_C);
  110. if ci==11
  111.     ci=0;
  112. end;
  113. %disp(i);
  114. %F
  115. D_model_F=zeros(11,1);
  116. D_model_F(1)=dtwdistance(model_sample(:,30:44),model1(:,30:44));%
  117. D_model_F(2)=dtwdistance(model_sample(:,29:44),model2(:,30:44));%
  118. D_model_F(3)=dtwdistance(model_sample(:,29:44),model3(:,30:44));%
  119. D_model_F(4)=dtwdistance(model_sample(:,29:44),model4(:,30:44));%
  120. D_model_F(5)=dtwdistance(model_sample(:,29:44),model5(:,30:44));%
  121. D_model_F(6)=dtwdistance(model_sample(:,29:44),model6(:,30:44));%
  122. D_model_F(7)=dtwdistance(model_sample(:,29:44),model7(:,30:44));%
  123. D_model_F(8)=dtwdistance(model_sample(:,29:44),model8(:,30:44));%
  124. D_model_F(9)=dtwdistance(model_sample(:,29:44),model9(:,30:44));%
  125. D_model_F(10)=dtwdistance(model_sample(:,30:44),model10(:,30:44));%
  126. D_model_F(11)=dtwdistance(model_sample(:,30:44),model0(:,30:44));%
  127. [Dmin,fi]=min(D_model_F);
  128. if fi==11
  129.     fi=0;
  130. end;
  131. disp('   alfa  ceps  fft');
  132. %fprintf(' alfa  cepstrum  fft    ');
  133. disp([ai,ci,fi]);