set_sign.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 : set_sign.c
  11. * Purpose : Builds sign vector according to "dn[]" and "cn[]".
  12. *
  13. ********************************************************************************
  14. */
  15. /*
  16. ********************************************************************************
  17. * MODULE INCLUDE FILE AND VERSION ID
  18. ********************************************************************************
  19. */
  20. #include "set_sign.h"
  21. const char set_sign_id[] = "@(#)$Id $" set_sign_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 set_sign()
  40. *
  41. * PURPOSE: Builds sign[] vector according to "dn[]" and "cn[]".
  42. * Also finds the position of maximum of correlation in each track
  43. * and the starting position for each pulse.
  44. *
  45. *************************************************************************/
  46. void set_sign(Word16 dn[], /* i/o : correlation between target and h[] */
  47. Word16 sign[], /* o : sign of dn[] */
  48. Word16 dn2[], /* o : maximum of correlation in each track. */
  49. Word16 n /* i : # of maximum correlations in dn2[] */
  50. )
  51. {
  52. Word16 i, j, k;
  53. Word16 val, min;
  54. Word16 pos = 0; /* initialization only needed to keep gcc silent */
  55. /* set sign according to dn[] */
  56. for (i = 0; i < L_CODE; i++) {
  57. val = dn[i]; move16 ();
  58. test ();
  59. if (val >= 0) {
  60. sign[i] = 32767; move16 ();
  61. } else {
  62. sign[i] = -32767; move16 ();
  63. val = negate_ex(val);
  64. }
  65. dn[i] = val; move16 (); /* modify dn[] according to the fixed sign */
  66. dn2[i] = val; move16 ();
  67. }
  68. /* keep 8-n maximum positions/8 of each track and store it in dn2[] */
  69. for (i = 0; i < NB_TRACK; i++)
  70. {
  71. for (k = 0; k < (8-n); k++)
  72. {
  73. min = 0x7fff; move16 ();
  74. for (j = i; j < L_CODE; j += STEP)
  75. {
  76. test (); move16 ();
  77. if (dn2[j] >= 0)
  78. {
  79. val = sub_ex(dn2[j], min);
  80. test ();
  81. if (val < 0)
  82. {
  83. min = dn2[j]; move16 ();
  84. pos = j; move16 ();
  85. }
  86. }
  87. }
  88. dn2[pos] = -1; move16 ();
  89. }
  90. }
  91. return;
  92. }
  93. /*************************************************************************
  94. *
  95. * FUNCTION set_sign12k2()
  96. *
  97. * PURPOSE: Builds sign[] vector according to "dn[]" and "cn[]", and modifies
  98. * dn[] to include the sign information (dn[i]=sign[i]*dn[i]).
  99. * Also finds the position of maximum of correlation in each track
  100. * and the starting position for each pulse.
  101. *
  102. *************************************************************************/
  103. void set_sign12k2 (
  104. Word16 dn[], /* i/o : correlation between target and h[] */
  105. Word16 cn[], /* i : residual after long term prediction */
  106. Word16 sign[], /* o : sign of d[n] */
  107. Word16 pos_max[], /* o : position of maximum correlation */
  108. Word16 nb_track, /* i : number of tracks tracks */
  109. Word16 ipos[], /* o : starting position for each pulse */
  110. Word16 step /* i : the step size in the tracks */
  111. )
  112. {
  113. Word16 i, j;
  114. Word16 val, cor, k_cn, k_dn, max, max_of_all;
  115. Word16 pos = 0; /* initialization only needed to keep gcc silent */
  116. Word16 en[L_CODE]; /* correlation vector */
  117. Word32 s;
  118. /* calculate energy for normalization of cn[] and dn[] */
  119. s = 256; move32 ();
  120. for (i = 0; i < L_CODE; i++)
  121. {
  122. s = L_mac_ex (s, cn[i], cn[i]);
  123. }
  124. s = Inv_sqrt_ex (s); move32 ();
  125. k_cn = extract_h_ex (L_shl_ex (s, 5));
  126. s = 256; move32 ();
  127. for (i = 0; i < L_CODE; i++)
  128. {
  129. s = L_mac_ex (s, dn[i], dn[i]);
  130. }
  131. s = Inv_sqrt_ex (s); move32 ();
  132. k_dn = extract_h_ex (L_shl_ex (s, 5));
  133. for (i = 0; i < L_CODE; i++)
  134. {
  135. val = dn[i]; move16 ();
  136. cor = round_ex (L_shl_ex (L_mac_ex (L_mult_ex (k_cn, cn[i]), k_dn, val), 10));
  137. test ();
  138. if (cor >= 0)
  139. {
  140. sign[i] = 32767; move16 (); /* sign = +1 */
  141. }
  142. else
  143. {
  144. sign[i] = -32767; move16 (); /* sign = -1 */
  145. cor = negate_ex (cor);
  146. val = negate_ex (val);
  147. }
  148. /* modify dn[] according to the fixed sign */
  149. dn[i] = val; move16 ();
  150. en[i] = cor; move16 ();
  151. }
  152. max_of_all = -1; move16 ();
  153. for (i = 0; i < nb_track; i++)
  154. {
  155. max = -1; move16 ();
  156. for (j = i; j < L_CODE; j += step)
  157. {
  158. cor = en[j]; move16 ();
  159. val = sub_ex (cor, max);
  160. test ();
  161. if (val > 0)
  162. {
  163. max = cor; move16 ();
  164. pos = j; move16 ();
  165. }
  166. }
  167. /* store maximum correlation position */
  168. pos_max[i] = pos; move16 ();
  169. val = sub_ex (max, max_of_all);
  170. test ();
  171. if (val > 0)
  172. {
  173. max_of_all = max; move16 ();
  174. /* starting position for i0 */
  175. ipos[0] = i; move16 ();
  176. }
  177. }
  178. /*----------------------------------------------------------------*
  179. * Set starting position of each pulse. *
  180. *----------------------------------------------------------------*/
  181. pos = ipos[0]; move16 ();
  182. ipos[nb_track] = pos; move16 ();
  183. for (i = 1; i < nb_track; i++)
  184. {
  185. pos = add_ex (pos, 1);
  186. test ();
  187. if (sub_ex (pos, nb_track) >= 0)
  188. {
  189. pos = 0; move16 ();
  190. }
  191. ipos[i] = pos; move16 ();
  192. ipos[add_ex(i, nb_track)] = pos; move16 ();
  193. }
  194. }