cor_h.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. *****************************************************************************
  3. *
  4. * GSM AMR-NB speech codec R98 Version 7.6.0 December 12, 2001
  5. * R99 Version 3.3.0
  6. * REL-4 Version 4.1.0
  7. *
  8. ******************************************************************************
  9. *
  10. * File : cor_h.c
  11. * Purpose : correlation functions for codebook search
  12. *
  13. *****************************************************************************
  14. */
  15. /*
  16. *****************************************************************************
  17. * MODULE INCLUDE FILE AND VERSION ID
  18. *****************************************************************************
  19. */
  20. #include "cor_h.h"
  21. const char cor_h_id[] = "@(#)$Id $" cor_h_h;
  22. /*
  23. *****************************************************************************
  24. * INCLUDE FILES
  25. *****************************************************************************
  26. */
  27. #include "typedef.h"
  28. #include "basic_op.h"
  29. #include "count.h"
  30. #include "inv_sqrt_ex.h"
  31. #include "cnst.h"
  32. /*
  33. *****************************************************************************
  34. * PUBLIC PROGRAM CODE
  35. *****************************************************************************
  36. */
  37. /*************************************************************************
  38. *
  39. * FUNCTION: cor_h_x_ex()
  40. *
  41. * PURPOSE: Computes correlation between target signal "x[]" and
  42. * impulse response"h[]".
  43. *
  44. * DESCRIPTION:
  45. * The correlation is given by:
  46. * d[n] = sum_{i=n}^{L-1} x[i] h[i-n] n=0,...,L-1
  47. *
  48. * d[n] is normalized such that the sum of 5 maxima of d[n] corresponding
  49. * to each position track does not saturate.
  50. *
  51. *************************************************************************/
  52. void cor_h_x_ex (
  53. Word16 h[], /* (i): impulse response of weighted synthesis filter */
  54. Word16 x[], /* (i): target */
  55. Word16 dn[], /* (o): correlation between target and h[] */
  56. Word16 sf /* (i): scaling factor: 2 for 12.2, 1 for others */
  57. )
  58. {
  59. cor_h_x2(h, x, dn, sf, NB_TRACK, STEP);
  60. }
  61. /*************************************************************************
  62. *
  63. * FUNCTION: cor_h_x2()
  64. *
  65. * PURPOSE: Computes correlation between target signal "x[]" and
  66. * impulse response"h[]".
  67. *
  68. * DESCRIPTION:
  69. * See cor_h_x_ex, d[n] can be normalized regards to sum of the
  70. * five MR122 maxima or the four MR102 maxima.
  71. *
  72. *************************************************************************/
  73. void cor_h_x2 (
  74. Word16 h[], /* (i): impulse response of weighted synthesis filter */
  75. Word16 x[], /* (i): target */
  76. Word16 dn[], /* (o): correlation between target and h[] */
  77. Word16 sf, /* (i): scaling factor: 2 for 12.2, 1 for others */
  78. Word16 nb_track,/* (i): the number of ACB tracks */
  79. Word16 step /* (i): step size from one pulse position to the next
  80. in one track */
  81. )
  82. {
  83. Word16 i, j, k;
  84. Word32 s, y32[L_CODE], max, tot;
  85. /* first keep the result on 32 bits and find absolute maximum */
  86. tot = 5; move32 ();
  87. for (k = 0; k < nb_track; k++)
  88. {
  89. max = 0; move32 ();
  90. for (i = k; i < L_CODE; i += step)
  91. {
  92. s = 0; move32 ();
  93. for (j = i; j < L_CODE; j++)
  94. s = L_mac_ex (s, x[j], h[j - i]);
  95. y32[i] = s; move32 ();
  96. s = L_abs_ex (s);
  97. test ();
  98. if (L_sub_ex (s, max) > (Word32) 0L)
  99. max = s; move32 ();
  100. }
  101. tot = L_add_ex (tot, L_shr_ex (max, 1));
  102. }
  103. j = sub_ex (norm_l_ex (tot), sf);
  104. for (i = 0; i < L_CODE; i++)
  105. {
  106. dn[i] = round_ex (L_shl_ex (y32[i], j)); move16 ();
  107. }
  108. }
  109. /*************************************************************************
  110. *
  111. * FUNCTION: cor_h()
  112. *
  113. * PURPOSE: Computes correlations of h[] needed for the codebook search;
  114. * and includes the sign information into the correlations.
  115. *
  116. * DESCRIPTION: The correlations are given by
  117. * rr[i][j] = sum_{n=i}^{L-1} h[n-i] h[n-j]; i>=j; i,j=0,...,L-1
  118. *
  119. * and the sign information is included by
  120. * rr[i][j] = rr[i][j]*sign[i]*sign[j]
  121. *
  122. *************************************************************************/
  123. void cor_h (
  124. Word16 h[], /* (i) : impulse response of weighted synthesis
  125. filter */
  126. Word16 sign[], /* (i) : sign of d[n] */
  127. Word16 rr[][L_CODE] /* (o) : matrix of autocorrelation */
  128. )
  129. {
  130. Word16 i, j, k, dec, h2[L_CODE];
  131. Word32 s;
  132. /* Scaling for maximum precision */
  133. s = 2; move32 ();
  134. for (i = 0; i < L_CODE; i++)
  135. s = L_mac_ex (s, h[i], h[i]);
  136. j = sub_ex (extract_h_ex (s), 32767);
  137. test ();
  138. if (j == 0)
  139. {
  140. for (i = 0; i < L_CODE; i++)
  141. {
  142. h2[i] = shr_ex (h[i], 1); move16 ();
  143. }
  144. }
  145. else
  146. {
  147. s = L_shr_ex (s, 1);
  148. k = extract_h_ex (L_shl_ex (Inv_sqrt_ex (s), 7));
  149. k = mult_ex (k, 32440); /* k = 0.99*k */
  150. for (i = 0; i < L_CODE; i++)
  151. {
  152. h2[i] = round_ex (L_shl_ex (L_mult_ex (h[i], k), 9));
  153. move16 ();
  154. }
  155. }
  156. /* build matrix rr[] */
  157. s = 0; move32 ();
  158. i = L_CODE - 1;
  159. for (k = 0; k < L_CODE; k++, i--)
  160. {
  161. s = L_mac_ex (s, h2[k], h2[k]);
  162. rr[i][i] = round_ex (s); move16 ();
  163. }
  164. for (dec = 1; dec < L_CODE; dec++)
  165. {
  166. s = 0; move32 ();
  167. j = L_CODE - 1;
  168. i = sub_ex (j, dec);
  169. for (k = 0; k < (L_CODE - dec); k++, i--, j--)
  170. {
  171. s = L_mac_ex (s, h2[k], h2[k + dec]);
  172. rr[j][i] = mult_ex (round_ex (s), mult_ex (sign[i], sign[j]));
  173. move16 ();
  174. rr[i][j] = rr[j][i]; move16 ();
  175. }
  176. }
  177. }