/trunk/Level 2/psycho.m

http://aac-codec-matlab.googlecode.com/ · MATLAB · 150 lines · 100 code · 20 blank · 30 comment · 28 complexity · 30c6c753ff3b3404e7e998b72fb7ded9 MD5 · raw file

  1. function [ T , P ] = psycho( frameT , frameType )
  2. %PSYCHO Calculates the psychoacoustic thresholds and the energy spectrum
  3. % Inputs: frameT : the frame array with the samples in time
  4. % : the type of the frame
  5. % Outputs: T: a <numberofbands X numberofchannels> matrix with the
  6. % threshold for every band.
  7. % P: a <numberofbands X numberofchannels> with the energy
  8. % spectrum of every band
  9. %We check what type of frame this is
  10. if strcmp(frameType,'ESH')==1
  11. %defining the bjs, keep in mind that I added another value in the end
  12. %of the array b(43)=127
  13. b=[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 19 21 23 25 27 29 31 34 37 40 43 46 50 54 58 63 68 74 80 87 95 104 114 126 128];
  14. %defining the number of bands
  15. numberofbands=42;
  16. %defining B, keep in mind that I added another value in the end
  17. %of the array B(43)=24
  18. B=[0 1.88 3.70 5.39 6.93 8.29 9.49 10.53 11.45 12.26 12.96 13.59 14.15 14.65 15.11 15.52 15.90 16.56 17.15 17.66 18.13 18.54 18.93 19.28 19.69 20.14 20.54 20.92 21.27 21.64 22.03 22.39 22.76 23.13 23.49 23.5 24.00 24.00 24.00 24.00 24.00 24.00 24.00];
  19. %defining Tquiet
  20. Tquiet=[27.28 22.28 14.28 12.28 12.28 12.28 12.28 12.28 12.28 12.28 12.28 12.28 12.28 12.28 12.28 12.28 12.28 15.29 15.29 15.29 15.29 15.29 15.29 15.29 17.05 20.05 20.05 20.05 20.05 23.30 28.30 28.30 29.27 39.27 40.06 40.06 50.73 51.31 51.82 52.28 53.07 53.07];
  21. else
  22. %defining the bjs, keep in mind that I added another value in the end
  23. %of the array b(70)=1023
  24. b=[0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 41 44 47 50 53 56 59 62 66 70 74 78 82 87 92 97 103 109 116 123 131 139 148 158 168 179 191 204 218 233 249 266 284 304 325 348 372 398 426 457 491 528 568 613 663 719 782 854 938 1024];
  25. %defining the number of bands
  26. numberofbands=69;
  27. %defining B, keep in mind that I added another value in the end
  28. %of the array B(43)=24
  29. B=[0.24 0.71 1.18 1.65 2.12 2.58 3.03 3.48 3.92 4.35 4.77 5.19 5.59 5.99 6.37 6.74 7.10 7.45 7.80 8.20 8.68 9.13 9.55 9.96 10.35 10.71 11.06 11.45 11.86 12.25 12.62 12.96 13.32 13.70 14.05 14.41 14.77 15.13 15.49 15.85 16.20 16.55 16.91 17.25 17.59 17.93 18.28 18.62 18.96 19.30 19.64 19.97 20.31 20.95 20.99 21.33 21.66 21.99 22.32 22.66 23.00 23.33 23.67 24.00 24.00 24.00 24.00 24.00 24.00 24.00];
  30. %defining Tquiet
  31. Tquiet=[40.29 40.29 35.29 35.29 32.29 32.29 27.29 27.29 27.29 25.29 25.29 25.29 25.29 25.29 25.29 25.29 25.29 25.29 25.29 27.05 27.05 27.05 27.05 27.05 27.05 27.05 27.05 28.30 28.30 28.30 28.30 28.30 29.27 29.27 29.27 30.06 30.06 30.73 30.73 31.31 31.31 31.82 32.28 32.28 32.69 33.07 33.42 33.74 34.04 34.32 34.58 34.83 38.29 38.50 38.89 41.08 41.43 41.75 47.19 47.59 47.96 58.30 28.81 69.27 69.76 70.27 70.85 71.52 70.20];
  32. end
  33. %Calling filterbank
  34. %define the wintype
  35. winType='KAIZER';
  36. frameF=filterbank(frameT,frameType,winType);
  37. %repeat for each channel
  38. channels=2;
  39. for channel=1:1:channels
  40. %Calculating the energy spectrum Pi(j)
  41. if strcmp(frameType,'ESH')==1
  42. for i=0:1:7
  43. for j=1:1:numberofbands
  44. P(j+i*42,channel)=0;
  45. for k=(b(j)+1):1:b(j+1)
  46. P(j+i*42,channel)=P(j+i*42,channel)+ frameF(channel,k+i*128)*frameF(channel,k+i*128);
  47. end
  48. end
  49. end
  50. else
  51. for j=1:1:numberofbands
  52. P(j,channel)=0;
  53. for k=(b(j)+1):1:b(j+1)
  54. P(j,channel)=P(j,channel)+ frameF(channel,k)*frameF(channel,k);
  55. end
  56. end
  57. end
  58. end
  59. %Call the TNS level
  60. [frameF,TNScoeffs]=TNS(frameF,frameType,P);
  61. %repeat for each channel
  62. for channel=1:1:channels
  63. %ReCalculating the energy spectrum Pi(j)
  64. if strcmp(frameType,'ESH')==1
  65. for i=0:1:7
  66. for j=1:1:numberofbands
  67. P(j+i*42,channel)=0;
  68. for k=(b(j)+1):1:b(j+1)
  69. P(j+i*42,channel)=P(j+i*42,channel)+ frameF(channel,k+i*128)*frameF(channel,k+i*128);
  70. end
  71. end
  72. end
  73. else
  74. for j=1:1:numberofbands
  75. P(j,channel)=0;
  76. for k=(b(j)+1):1:b(j+1)
  77. P(j,channel)=P(j,channel)+ frameF(channel,k)*frameF(channel,k);
  78. end
  79. end
  80. end
  81. end
  82. %repeat for each channel
  83. for channel=1:1:channels
  84. %Calculate the acoustic threshold Ti(j)
  85. if strcmp(frameType,'ESH')==1
  86. for i=0:1:7
  87. for j=2:1:numberofbands
  88. DB=(B(j+1)+B(j-1))/2;
  89. sl(j)=20*DB;
  90. sh(j)=15*DB;
  91. Tsc(j+i*42)=10*log10(P(j+i*42,channel))-29;
  92. T(j+i*42,channel)=max(Tsc(j+i*42),Tsc(j+i*42-1)-sh(j));
  93. end
  94. end
  95. else
  96. for j=2:1:numberofbands
  97. DB=(B(j+1)+B(j-1))/2;
  98. sl(j)=30*DB;
  99. sh(j)=20*DB;
  100. Tsc(j)=10*log10(P(j,channel))-29;
  101. T(j,channel)=max(Tsc(j),Tsc(j-1)-sh(j));
  102. end
  103. end
  104. %Calculate the Ti,spread
  105. if strcmp(frameType,'ESH')==1
  106. for i=0:1:7
  107. for j=1:1:(numberofbands-1)
  108. Tspread(j+i*42)=max(T(j+i*42,channel),T(j+i*42+1,channel)-sl(j));
  109. end
  110. Tspread(numberofbands+i*42)=T(numberofbands+i*42,channel);
  111. end
  112. else
  113. for j=1:1:(numberofbands-1)
  114. Tspread(j)=max(T(j,channel),T(j+1,channel)-sl(j));
  115. end
  116. Tspread(numberofbands)=T(numberofbands,channel);
  117. end
  118. if strcmp(frameType,'ESH')==1
  119. for i=0:1:7
  120. for j=1:1:numberofbands
  121. T(j+i*42,channel)=max(Tspread(j+i*42),Tquiet(j));
  122. end
  123. end
  124. else
  125. for j=1:1:numberofbands
  126. T(j,channel)=max(Tspread(j),Tquiet(j));
  127. end
  128. end
  129. end
  130. end