g_pitch.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 : g_pitch.c
  11. * Purpose : Compute the pitch (adaptive codebook) gain.
  12. *
  13. ********************************************************************************
  14. */
  15. /*
  16. ********************************************************************************
  17. * MODULE INCLUDE FILE AND VERSION ID
  18. ********************************************************************************
  19. */
  20. #include "g_pitch.h"
  21. const char g_pitch_id[] = "@(#)$Id $" g_pitch_h;
  22. /*
  23. ********************************************************************************
  24. * INCLUDE FILES
  25. ********************************************************************************
  26. */
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include "typedef.h"
  30. #include "mode.h"
  31. #include "basic_op.h"
  32. #include "oper_32b.h"
  33. #include "count.h"
  34. #include "cnst.h"
  35. /*
  36. ********************************************************************************
  37. * PUBLIC PROGRAM CODE
  38. ********************************************************************************
  39. */
  40. /*************************************************************************
  41. *
  42. * FUNCTION: G_pitch
  43. *
  44. * PURPOSE: Compute the pitch (adaptive codebook) gain.
  45. * Result in Q14 (NOTE: 12.2 bit exact using Q12)
  46. *
  47. * DESCRIPTION:
  48. * The adaptive codebook gain is given by
  49. *
  50. * g = <x[], y[]> / <y[], y[]>
  51. *
  52. * where x[] is the target vector, y[] is the filtered adaptive
  53. * codevector, and <> denotes dot product.
  54. * The gain is limited to the range [0,1.2] (=0..19661 Q14)
  55. *
  56. *************************************************************************/
  57. Word16 G_pitch ( /* o : Gain of pitch lag saturated to 1.2 */
  58. enum Mode mode, /* i : AMR mode */
  59. Word16 xn[], /* i : Pitch target. */
  60. Word16 y1[], /* i : Filtered adaptive codebook. */
  61. Word16 g_coeff[], /* i : Correlations need for gain quantization */
  62. Word16 L_subfr /* i : Length of subframe. */
  63. )
  64. {
  65. Word16 i;
  66. Word16 xy, yy, exp_xy, exp_yy, gain;
  67. Word32 s;
  68. Word16 scaled_y1[L_SUBFR]; /* Usually dynamic allocation of (L_subfr) */
  69. /* divide "y1[]" by 4 to avoid overflow */
  70. for (i = 0; i < L_subfr; i++)
  71. {
  72. scaled_y1[i] = shr_ex (y1[i], 2); move16 ();
  73. }
  74. /* Compute scalar product <y1[],y1[]> */
  75. /* Q12 scaling / MR122 */
  76. Overflow = 0; move16 ();
  77. s = 1L; move32 (); /* Avoid case of all zeros */
  78. for (i = 0; i < L_subfr; i++)
  79. {
  80. s = L_mac_ex (s, y1[i], y1[i]);
  81. }
  82. test ();
  83. if (Overflow == 0) /* Test for overflow */
  84. {
  85. exp_yy = norm_l_ex (s);
  86. yy = round_ex (L_shl_ex (s, exp_yy));
  87. }
  88. else
  89. {
  90. s = 1L; move32 (); /* Avoid case of all zeros */
  91. for (i = 0; i < L_subfr; i++)
  92. {
  93. s = L_mac_ex (s, scaled_y1[i], scaled_y1[i]);
  94. }
  95. exp_yy = norm_l_ex (s);
  96. yy = round_ex (L_shl_ex (s, exp_yy));
  97. exp_yy = sub_ex (exp_yy, 4);
  98. }
  99. /* Compute scalar product <xn[],y1[]> */
  100. Overflow = 0; move16 ();
  101. s = 1L; move32 (); /* Avoid case of all zeros */
  102. for (i = 0; i < L_subfr; i++)
  103. {
  104. s = L_mac_ex(s, xn[i], y1[i]);
  105. }
  106. test ();
  107. if (Overflow == 0)
  108. {
  109. exp_xy = norm_l_ex (s);
  110. xy = round_ex (L_shl_ex (s, exp_xy));
  111. }
  112. else
  113. {
  114. s = 1L; move32 (); /* Avoid case of all zeros */
  115. for (i = 0; i < L_subfr; i++)
  116. {
  117. s = L_mac_ex (s, xn[i], scaled_y1[i]);
  118. }
  119. exp_xy = norm_l_ex (s);
  120. xy = round_ex (L_shl_ex (s, exp_xy));
  121. exp_xy = sub_ex (exp_xy, 2);
  122. }
  123. g_coeff[0] = yy; move16 ();
  124. g_coeff[1] = sub_ex (15, exp_yy); move16 ();
  125. g_coeff[2] = xy; move16 ();
  126. g_coeff[3] = sub_ex (15, exp_xy); move16 ();
  127. /* If (xy < 4) gain = 0 */
  128. i = sub_ex (xy, 4);
  129. test ();
  130. if (i < 0)
  131. return ((Word16) 0);
  132. /* compute gain = xy/yy */
  133. xy = shr_ex (xy, 1); /* Be sure xy < yy */
  134. gain = div_s (xy, yy);
  135. i = sub_ex (exp_xy, exp_yy); /* Denormalization of division */
  136. gain = shr_ex (gain, i);
  137. /* if(gain >1.2) gain = 1.2 */
  138. test ();
  139. if (sub_ex (gain, 19661) > 0)
  140. {
  141. gain = 19661; move16 ();
  142. }
  143. test ();
  144. if (sub_ex(mode, MR122) == 0)
  145. {
  146. /* clear 2 LSBits */
  147. gain = gain & 0xfffC; logic16 ();
  148. }
  149. return (gain);
  150. }