autocorr.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 : autocorr.c
  11. *
  12. ********************************************************************************
  13. */
  14. /*
  15. ********************************************************************************
  16. * MODULE INCLUDE FILE AND VERSION ID
  17. ********************************************************************************
  18. */
  19. #include "autocorr.h"
  20. const char autocorr_id[] = "@(#)$Id $" autocorr_h;
  21. /*
  22. ********************************************************************************
  23. * INCLUDE FILES
  24. ********************************************************************************
  25. */
  26. #include "typedef.h"
  27. #include "basic_op.h"
  28. #include "oper_32b.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. *
  44. * Function : autocorr
  45. * Purpose : Compute autocorrelations of signal with windowing
  46. *
  47. **************************************************************************
  48. */
  49. Word16 Autocorr (
  50. Word16 x[], /* (i) : Input signal (L_WINDOW) */
  51. Word16 m, /* (i) : LPC order */
  52. Word16 r_h[], /* (o) : Autocorrelations (msb) */
  53. Word16 r_l[], /* (o) : Autocorrelations (lsb) */
  54. const Word16 wind[] /* (i) : window for LPC analysis (L_WINDOW) */
  55. )
  56. {
  57. Word16 i, j, norm;
  58. Word16 y[L_WINDOW];
  59. Word32 sum;
  60. Word16 overfl, overfl_shft;
  61. /* Windowing of signal */
  62. for (i = 0; i < L_WINDOW; i++)
  63. {
  64. y[i] = mult_r_ex (x[i], wind[i]); move16 ();
  65. }
  66. /* Compute r[0] and test for overflow */
  67. overfl_shft = 0; move16 ();
  68. do
  69. {
  70. overfl = 0; move16 ();
  71. sum = 0L; move32 ();
  72. for (i = 0; i < L_WINDOW; i++)
  73. {
  74. sum = L_mac_ex (sum, y[i], y[i]);
  75. }
  76. /* If overflow divide y[] by 4 */
  77. test ();
  78. if (L_sub_ex (sum, MAX_32) == 0L)
  79. {
  80. overfl_shft = add_ex (overfl_shft, 4);
  81. overfl = 1; move16 (); /* Set the overflow flag */
  82. for (i = 0; i < L_WINDOW; i++)
  83. {
  84. y[i] = shr_ex (y[i], 2); move16 ();
  85. }
  86. }
  87. test ();
  88. }
  89. while (overfl != 0);
  90. sum = L_add_ex (sum, 1L); /* Avoid the case of all zeros */
  91. /* Normalization of r[0] */
  92. norm = norm_l_ex (sum);
  93. sum = L_shl_ex (sum, norm);
  94. L_Extract (sum, &r_h[0], &r_l[0]); /* Put in DPF format (see oper_32b) */
  95. /* r[1] to r[m] */
  96. for (i = 1; i <= m; i++)
  97. {
  98. sum = 0; move32 ();
  99. for (j = 0; j < L_WINDOW - i; j++)
  100. {
  101. sum = L_mac_ex (sum, y[j], y[j + i]);
  102. }
  103. sum = L_shl_ex (sum, norm);
  104. L_Extract (sum, &r_h[i], &r_l[i]);
  105. }
  106. norm = sub_ex (norm, overfl_shft);
  107. return norm;
  108. }