g_code.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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_code.c
  11. * Purpose : Compute the innovative codebook gain.
  12. *
  13. ********************************************************************************
  14. */
  15. /*
  16. ********************************************************************************
  17. * MODULE INCLUDE FILE AND VERSION ID
  18. ********************************************************************************
  19. */
  20. #include "g_code.h"
  21. const char g_code_id[] = "@(#)$Id $" g_code_h;
  22. /*
  23. ********************************************************************************
  24. * INCLUDE FILES
  25. ********************************************************************************
  26. */
  27. #include "typedef.h"
  28. #include "basic_op.h"
  29. #include "count.h"
  30. #include "cnst.h"
  31. /*
  32. ********************************************************************************
  33. * LOCAL VARIABLES AND TABLES
  34. ********************************************************************************
  35. */
  36. /*
  37. ********************************************************************************
  38. * PUBLIC PROGRAM CODE
  39. ********************************************************************************
  40. */
  41. /*************************************************************************
  42. *
  43. * FUNCTION: G_code_ex
  44. *
  45. * PURPOSE: Compute the innovative codebook gain.
  46. *
  47. * DESCRIPTION:
  48. * The innovative codebook gain is given by
  49. *
  50. * g = <x[], y[]> / <y[], y[]>
  51. *
  52. * where x[] is the target vector, y[] is the filtered innovative
  53. * codevector, and <> denotes dot product.
  54. *
  55. *************************************************************************/
  56. Word16 G_code_ex ( /* out : Gain of innovation code */
  57. Word16 xn2[], /* in : target vector */
  58. Word16 y2[] /* in : filtered innovation vector */
  59. )
  60. {
  61. Word16 i;
  62. Word16 xy, yy, exp_xy, exp_yy, gain;
  63. Word16 scal_y2[L_SUBFR];
  64. Word32 s;
  65. /* Scale down Y[] by 2 to avoid overflow */
  66. for (i = 0; i < L_SUBFR; i++)
  67. {
  68. scal_y2[i] = shr_ex (y2[i], 1); move16 ();
  69. }
  70. /* Compute scalar product <X[],Y[]> */
  71. s = 1L; move32 (); /* Avoid case of all zeros */
  72. for (i = 0; i < L_SUBFR; i++)
  73. {
  74. s = L_mac_ex (s, xn2[i], scal_y2[i]);
  75. }
  76. exp_xy = norm_l_ex (s);
  77. xy = extract_h_ex (L_shl_ex (s, exp_xy));
  78. /* If (xy < 0) gain = 0 */
  79. test ();
  80. if (xy <= 0)
  81. return ((Word16) 0);
  82. /* Compute scalar product <Y[],Y[]> */
  83. s = 0L; move32 ();
  84. for (i = 0; i < L_SUBFR; i++)
  85. {
  86. s = L_mac_ex (s, scal_y2[i], scal_y2[i]);
  87. }
  88. exp_yy = norm_l_ex (s);
  89. yy = extract_h_ex (L_shl_ex (s, exp_yy));
  90. /* compute gain = xy/yy */
  91. xy = shr_ex (xy, 1); /* Be sure xy < yy */
  92. gain = div_s (xy, yy);
  93. /* Denormalization of division */
  94. i = add_ex (exp_xy, 5); /* 15-1+9-18 = 5 */
  95. i = sub_ex (i, exp_yy);
  96. gain = shl_ex (shr_ex (gain, i), 1); /* Q0 -> Q1 */
  97. return (gain);
  98. }