levinson.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 : levinson.c
  11. * Purpose : Levinson-Durbin algorithm in double precision.
  12. * : To compute the LP filter parameters from the
  13. * : speech autocorrelations.
  14. *
  15. *****************************************************************************
  16. */
  17. /*
  18. *****************************************************************************
  19. * MODULE INCLUDE FILE AND VERSION ID
  20. *****************************************************************************
  21. */
  22. #include "levinson.h"
  23. const char levinson_id[] = "@(#)$Id $" levinson_h;
  24. /*
  25. *****************************************************************************
  26. * INCLUDE FILES
  27. *****************************************************************************
  28. */
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31. #include "typedef.h"
  32. #include "basic_op.h"
  33. #include "oper_32b.h"
  34. #include "count.h"
  35. #include "cnst.h"
  36. /*
  37. *****************************************************************************
  38. * LOCAL VARIABLES AND TABLES
  39. *****************************************************************************
  40. */
  41. /*---------------------------------------------------------------*
  42. * Constants (defined in "cnst.h") *
  43. *---------------------------------------------------------------*
  44. * M : LPC order
  45. *---------------------------------------------------------------*/
  46. /*
  47. *****************************************************************************
  48. * PUBLIC PROGRAM CODE
  49. *****************************************************************************
  50. */
  51. /*************************************************************************
  52. *
  53. * Function: Levinson_init
  54. * Purpose: Allocates state memory and initializes state memory
  55. *
  56. **************************************************************************
  57. */
  58. int Levinson_init (LevinsonState **state)
  59. {
  60. LevinsonState* s;
  61. if (state == (LevinsonState **) NULL){
  62. wfprintf(stderr, "Levinson_init: invalid parameter\n");
  63. return -1;
  64. }
  65. *state = NULL;
  66. /* allocate memory */
  67. if ((s= (LevinsonState *) wmalloc(sizeof(LevinsonState))) == NULL){
  68. wfprintf(stderr, "Levinson_init: can not malloc state structure\n");
  69. return -1;
  70. }
  71. Levinson_reset(s);
  72. *state = s;
  73. return 0;
  74. }
  75. /*************************************************************************
  76. *
  77. * Function: Levinson_reset
  78. * Purpose: Initializes state memory to zero
  79. *
  80. **************************************************************************
  81. */
  82. int Levinson_reset (LevinsonState *state)
  83. {
  84. Word16 i;
  85. if (state == (LevinsonState *) NULL){
  86. wfprintf(stderr, "Levinson_reset: invalid parameter\n");
  87. return -1;
  88. }
  89. state->old_A[0] = 4096;
  90. for(i = 1; i < M + 1; i++)
  91. state->old_A[i] = 0;
  92. return 0;
  93. }
  94. /*************************************************************************
  95. *
  96. * Function: Levinson_exit
  97. * Purpose: The memory used for state memory is freed
  98. *
  99. **************************************************************************
  100. */
  101. void Levinson_exit (LevinsonState **state)
  102. {
  103. if (state == NULL || *state == NULL)
  104. return;
  105. /* deallocate memory */
  106. wfree(*state);
  107. *state = NULL;
  108. return;
  109. }
  110. /*************************************************************************
  111. *
  112. * FUNCTION: Levinson()
  113. *
  114. * PURPOSE: Levinson-Durbin algorithm in double precision. To compute the
  115. * LP filter parameters from the speech autocorrelations.
  116. *
  117. * DESCRIPTION:
  118. * R[i] autocorrelations.
  119. * A[i] filter coefficients.
  120. * K reflection coefficients.
  121. * Alpha prediction gain.
  122. *
  123. * Initialisation:
  124. * A[0] = 1
  125. * K = -R[1]/R[0]
  126. * A[1] = K
  127. * Alpha = R[0] * (1-K**2]
  128. *
  129. * Do for i = 2 to M
  130. *
  131. * S = SUM ( R[j]*A[i-j] ,j=1,i-1 ) + R[i]
  132. *
  133. * K = -S / Alpha
  134. *
  135. * An[j] = A[j] + K*A[i-j] for j=1 to i-1
  136. * where An[i] = new A[i]
  137. * An[i]=K
  138. *
  139. * Alpha=Alpha * (1-K**2)
  140. *
  141. * END
  142. *
  143. *************************************************************************/
  144. int Levinson (
  145. LevinsonState *st,
  146. Word16 Rh[], /* i : Rh[m+1] Vector of autocorrelations (msb) */
  147. Word16 Rl[], /* i : Rl[m+1] Vector of autocorrelations (lsb) */
  148. Word16 A[], /* o : A[m] LPC coefficients (m = 10) */
  149. Word16 rc[] /* o : rc[4] First 4 reflection coefficients */
  150. )
  151. {
  152. Word16 i, j;
  153. Word16 hi, lo;
  154. Word16 Kh, Kl; /* reflexion coefficient; hi and lo */
  155. Word16 alp_h, alp_l, alp_exp; /* Prediction gain; hi lo and exponent */
  156. Word16 Ah[M + 1], Al[M + 1]; /* LPC coef. in double prec. */
  157. Word16 Anh[M + 1], Anl[M + 1];/* LPC coef.for next iteration in double
  158. prec. */
  159. Word32 t0, t1, t2; /* temporary variable */
  160. /* K = A[1] = -R[1] / R[0] */
  161. t1 = L_Comp (Rh[1], Rl[1]);
  162. t2 = L_abs_ex (t1); /* abs R[1] */
  163. t0 = Div_32 (t2, Rh[0], Rl[0]); /* R[1]/R[0] */
  164. test ();
  165. if (t1 > 0)
  166. t0 = L_negate_ex (t0); /* -R[1]/R[0] */
  167. L_Extract (t0, &Kh, &Kl); /* K in DPF */
  168. rc[0] = round_ex (t0); move16 ();
  169. t0 = L_shr_ex (t0, 4); /* A[1] in */
  170. L_Extract (t0, &Ah[1], &Al[1]); /* A[1] in DPF */
  171. /* Alpha = R[0] * (1-K**2) */
  172. t0 = Mpy_32 (Kh, Kl, Kh, Kl); /* K*K */
  173. t0 = L_abs_ex (t0); /* Some case <0 !! */
  174. t0 = L_sub_ex ((Word32) 0x7fffffffL, t0); /* 1 - K*K */
  175. L_Extract (t0, &hi, &lo); /* DPF format */
  176. t0 = Mpy_32 (Rh[0], Rl[0], hi, lo); /* Alpha in */
  177. /* Normalize Alpha */
  178. alp_exp = norm_l_ex (t0);
  179. t0 = L_shl_ex (t0, alp_exp);
  180. L_Extract (t0, &alp_h, &alp_l); /* DPF format */
  181. /*--------------------------------------*
  182. * ITERATIONS I=2 to M *
  183. *--------------------------------------*/
  184. for (i = 2; i <= M; i++)
  185. {
  186. /* t0 = SUM ( R[j]*A[i-j] ,j=1,i-1 ) + R[i] */
  187. t0 = 0; move32 ();
  188. for (j = 1; j < i; j++)
  189. {
  190. t0 = L_add_ex (t0, Mpy_32 (Rh[j], Rl[j], Ah[i - j], Al[i - j]));
  191. }
  192. t0 = L_shl_ex (t0, 4);
  193. t1 = L_Comp (Rh[i], Rl[i]);
  194. t0 = L_add_ex (t0, t1); /* add R[i] */
  195. /* K = -t0 / Alpha */
  196. t1 = L_abs_ex (t0);
  197. t2 = Div_32 (t1, alp_h, alp_l); /* abs(t0)/Alpha */
  198. test ();
  199. if (t0 > 0)
  200. t2 = L_negate_ex (t2); /* K =-t0/Alpha */
  201. t2 = L_shl_ex (t2, alp_exp); /* denormalize; compare to Alpha */
  202. L_Extract (t2, &Kh, &Kl); /* K in DPF */
  203. test ();
  204. if (sub_ex (i, 5) < 0)
  205. {
  206. rc[i - 1] = round_ex (t2); move16 ();
  207. }
  208. /* Test for unstable filter. If unstable keep old A(z) */
  209. test ();
  210. if (sub_ex (abs_s_ex (Kh), 32750) > 0)
  211. {
  212. for (j = 0; j <= M; j++)
  213. {
  214. A[j] = st->old_A[j]; move16 ();
  215. }
  216. for (j = 0; j < 4; j++)
  217. {
  218. rc[j] = 0; move16 ();
  219. }
  220. return 0;
  221. }
  222. /*------------------------------------------*
  223. * Compute new LPC coeff. -> An[i] *
  224. * An[j]= A[j] + K*A[i-j] , j=1 to i-1 *
  225. * An[i]= K *
  226. *------------------------------------------*/
  227. for (j = 1; j < i; j++)
  228. {
  229. t0 = Mpy_32 (Kh, Kl, Ah[i - j], Al[i - j]);
  230. t0 = L_add_ex(t0, L_Comp(Ah[j], Al[j]));
  231. L_Extract (t0, &Anh[j], &Anl[j]);
  232. }
  233. t2 = L_shr_ex (t2, 4);
  234. L_Extract (t2, &Anh[i], &Anl[i]);
  235. /* Alpha = Alpha * (1-K**2) */
  236. t0 = Mpy_32 (Kh, Kl, Kh, Kl); /* K*K */
  237. t0 = L_abs_ex (t0); /* Some case <0 !! */
  238. t0 = L_sub_ex ((Word32) 0x7fffffffL, t0); /* 1 - K*K */
  239. L_Extract (t0, &hi, &lo); /* DPF format */
  240. t0 = Mpy_32 (alp_h, alp_l, hi, lo);
  241. /* Normalize Alpha */
  242. j = norm_l_ex (t0);
  243. t0 = L_shl_ex (t0, j);
  244. L_Extract (t0, &alp_h, &alp_l); /* DPF format */
  245. alp_exp = add_ex (alp_exp, j); /* Add normalization to
  246. alp_exp */
  247. /* A[j] = An[j] */
  248. for (j = 1; j <= i; j++)
  249. {
  250. Ah[j] = Anh[j]; move16 ();
  251. Al[j] = Anl[j]; move16 ();
  252. }
  253. }
  254. A[0] = 4096; move16 ();
  255. for (i = 1; i <= M; i++)
  256. {
  257. t0 = L_Comp (Ah[i], Al[i]);
  258. st->old_A[i] = A[i] = round_ex (L_shl_ex (t0, 1));move16 (); move16 ();
  259. }
  260. return 0;
  261. }