cbphase.m 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. % cbphase.m
  2. % David Rowe Aug 2012
  3. % Used to experiment with critical band phase perception and smoothing
  4. function cbphase
  5. Wo = 100.0*pi/4000;
  6. L = floor(pi/Wo);
  7. A = zeros(1,L);
  8. phi = zeros(1,L);
  9. % three harmonics in this band
  10. b = 4; a = b-1; c = b+1;
  11. % set up phases and mags for 2nd order system (see phasesecord.m)
  12. wres = b*Wo;
  13. phi(a) = 3*pi/4 + wres;
  14. phi(b) = pi/2 + wres;
  15. phi(c) = pi/4 + wres;
  16. A(a) = 0.707;
  17. A(b) = 1;
  18. A(c) = 0.707;
  19. % add linear component
  20. phi(1) = pi;
  21. phi(2:L) = phi(2:L) + (2:L)*phi(1);
  22. phi = phi - 2*pi*(floor(phi/(2*pi)) + 0.5);
  23. N = 16000;
  24. Nplot = 250;
  25. s = zeros(1,N);
  26. for m=a:c
  27. s_m = A(m)*cos(m*Wo*(0:(N-1)) + phi(m));
  28. s = s + s_m;
  29. endfor
  30. figure(2);
  31. clf;
  32. subplot(211)
  33. plot((1:L)*Wo*4000/pi, A,'+');
  34. subplot(212)
  35. plot((1:L)*Wo*4000/pi, phi,'+');
  36. %v = A(a)*exp(j*phi(a)) + A(b)*exp(j*phi(b)) + A(c)*exp(j*phi(c));
  37. %compass(v,"r")
  38. %hold off;
  39. % est phi1
  40. diff = phi(b) - phi(a)
  41. sumi = sin(diff);
  42. sumr = cos(diff);
  43. diff = phi(c) - phi(b)
  44. sumi += sin(diff);
  45. sumr += cos(diff);
  46. phi1_ = atan2(sumi, sumr)
  47. s_v = cos(Wo*(0:(N-1)) + phi1_);
  48. figure(1);
  49. clf;
  50. subplot(211)
  51. plot(s(1:Nplot));
  52. hold on;
  53. plot(s_v(1:Nplot),"r");
  54. hold off;
  55. % build (hopefully) perceptually similar phase
  56. phi_(a) = a*phi1_;
  57. phi_(b) = b*phi1_;
  58. phi_(c) = c*phi1_;
  59. s_ = zeros(1,N);
  60. for m=a:c
  61. s_m = A(m)*cos(m*Wo*(0:(N-1)) + phi_(m));
  62. s_ = s_ + s_m;
  63. endfor
  64. subplot(212)
  65. plot(s_(1:Nplot));
  66. gain = 8000;
  67. fs=fopen("orig_ph.raw","wb");
  68. fwrite(fs,gain*s,"short");
  69. fclose(fs);
  70. fs=fopen("mod_ph.raw","wb");
  71. fwrite(fs,gain*s_,"short");
  72. fclose(fs);
  73. endfunction