short_term.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. * Copyright 1992 by Jutta Degener and Carsten Bormann, Technische
  3. * Universitaet Berlin. See the accompanying file "COPYRIGHT" for
  4. * details. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
  5. */
  6. /* $Header: /tmp_amd/presto/export/kbs/jutta/src/gsm/RCS/short_term.c,v 1.2 1994/05/10 20:18:47 jutta Exp $ */
  7. #include "config.h"
  8. #include <stdio.h>
  9. #include <assert.h>
  10. #include "private.h"
  11. #include "gsm.h"
  12. #include "proto.h"
  13. /*
  14. * SHORT TERM ANALYSIS FILTERING SECTION
  15. */
  16. /* 4.2.8 */
  17. static void Decoding_of_the_coded_Log_Area_Ratios P2((LARc,LARpp),
  18. word * LARc, /* coded log area ratio [0..7] IN */
  19. word * LARpp) /* out: decoded .. */
  20. {
  21. register word temp1 /* , temp2 */;
  22. register long ltmp; /* for GSM_ADD */
  23. /* This procedure requires for efficient implementation
  24. * two tables.
  25. *
  26. * INVA[1..8] = integer( (32768 * 8) / real_A[1..8])
  27. * MIC[1..8] = minimum value of the LARc[1..8]
  28. */
  29. /* Compute the LARpp[1..8]
  30. */
  31. /* for (i = 1; i <= 8; i++, B++, MIC++, INVA++, LARc++, LARpp++) {
  32. *
  33. * temp1 = GSM_ADD( *LARc, *MIC ) << 10;
  34. * temp2 = *B << 1;
  35. * temp1 = GSM_SUB( temp1, temp2 );
  36. *
  37. * assert(*INVA != MIN_WORD);
  38. *
  39. * temp1 = GSM_MULT_R( *INVA, temp1 );
  40. * *LARpp = GSM_ADD( temp1, temp1 );
  41. * }
  42. */
  43. #undef STEP
  44. #define STEP( B, MIC, INVA ) \
  45. temp1 = GSM_ADD( *LARc++, MIC ) << 10; \
  46. temp1 = GSM_SUB( temp1, B << 1 ); \
  47. temp1 = GSM_MULT_R( INVA, temp1 ); \
  48. *LARpp++ = GSM_ADD( temp1, temp1 );
  49. STEP( 0, -32, 13107 );
  50. STEP( 0, -32, 13107 );
  51. STEP( 2048, -16, 13107 );
  52. STEP( -2560, -16, 13107 );
  53. STEP( 94, -8, 19223 );
  54. STEP( -1792, -8, 17476 );
  55. STEP( -341, -4, 31454 );
  56. STEP( -1144, -4, 29708 );
  57. /* NOTE: the addition of *MIC is used to restore
  58. * the sign of *LARc.
  59. */
  60. }
  61. /* 4.2.9 */
  62. /* Computation of the quantized reflection coefficients
  63. */
  64. /* 4.2.9.1 Interpolation of the LARpp[1..8] to get the LARp[1..8]
  65. */
  66. /*
  67. * Within each frame of 160 analyzed speech samples the short term
  68. * analysis and synthesis filters operate with four different sets of
  69. * coefficients, derived from the previous set of decoded LARs(LARpp(j-1))
  70. * and the actual set of decoded LARs (LARpp(j))
  71. *
  72. * (Initial value: LARpp(j-1)[1..8] = 0.)
  73. */
  74. static void Coefficients_0_12 P3((LARpp_j_1, LARpp_j, LARp),
  75. register word * LARpp_j_1,
  76. register word * LARpp_j,
  77. register word * LARp)
  78. {
  79. register int i;
  80. register longword ltmp;
  81. for (i = 1; i <= 8; i++, LARp++, LARpp_j_1++, LARpp_j++) {
  82. *LARp = GSM_ADD( SASR( *LARpp_j_1, 2 ), SASR( *LARpp_j, 2 ));
  83. *LARp = GSM_ADD( *LARp, SASR( *LARpp_j_1, 1));
  84. }
  85. }
  86. static void Coefficients_13_26 P3((LARpp_j_1, LARpp_j, LARp),
  87. register word * LARpp_j_1,
  88. register word * LARpp_j,
  89. register word * LARp)
  90. {
  91. register int i;
  92. register longword ltmp;
  93. for (i = 1; i <= 8; i++, LARpp_j_1++, LARpp_j++, LARp++) {
  94. *LARp = GSM_ADD( SASR( *LARpp_j_1, 1), SASR( *LARpp_j, 1 ));
  95. }
  96. }
  97. static void Coefficients_27_39 P3((LARpp_j_1, LARpp_j, LARp),
  98. register word * LARpp_j_1,
  99. register word * LARpp_j,
  100. register word * LARp)
  101. {
  102. register int i;
  103. register longword ltmp;
  104. for (i = 1; i <= 8; i++, LARpp_j_1++, LARpp_j++, LARp++) {
  105. *LARp = GSM_ADD( SASR( *LARpp_j_1, 2 ), SASR( *LARpp_j, 2 ));
  106. *LARp = GSM_ADD( *LARp, SASR( *LARpp_j, 1 ));
  107. }
  108. }
  109. static void Coefficients_40_159 P2((LARpp_j, LARp),
  110. register word * LARpp_j,
  111. register word * LARp)
  112. {
  113. register int i;
  114. for (i = 1; i <= 8; i++, LARp++, LARpp_j++)
  115. *LARp = *LARpp_j;
  116. }
  117. /* 4.2.9.2 */
  118. static void LARp_to_rp P1((LARp),
  119. register word * LARp) /* [0..7] IN/OUT */
  120. /*
  121. * The input of this procedure is the interpolated LARp[0..7] array.
  122. * The reflection coefficients, rp[i], are used in the analysis
  123. * filter and in the synthesis filter.
  124. */
  125. {
  126. register int i;
  127. register word temp;
  128. register longword ltmp;
  129. for (i = 1; i <= 8; i++, LARp++) {
  130. /* temp = GSM_ABS( *LARp );
  131. *
  132. * if (temp < 11059) temp <<= 1;
  133. * else if (temp < 20070) temp += 11059;
  134. * else temp = GSM_ADD( temp >> 2, 26112 );
  135. *
  136. * *LARp = *LARp < 0 ? -temp : temp;
  137. */
  138. if (*LARp < 0) {
  139. temp = *LARp == MIN_WORD ? MAX_WORD : -(*LARp);
  140. *LARp = - ((temp < 11059) ? temp << 1
  141. : ((temp < 20070) ? temp + 11059
  142. : GSM_ADD( temp >> 2, 26112 )));
  143. } else {
  144. temp = *LARp;
  145. *LARp = (temp < 11059) ? temp << 1
  146. : ((temp < 20070) ? temp + 11059
  147. : GSM_ADD( temp >> 2, 26112 ));
  148. }
  149. }
  150. }
  151. /* 4.2.10 */
  152. static void Short_term_analysis_filtering P4((S,rp,k_n,s),
  153. struct gsm_state * S,
  154. register word * rp, /* [0..7] IN */
  155. register int k_n, /* k_end - k_start */
  156. register word * s /* [0..n-1] IN/OUT */
  157. )
  158. /*
  159. * This procedure computes the short term residual signal d[..] to be fed
  160. * to the RPE-LTP loop from the s[..] signal and from the local rp[..]
  161. * array (quantized reflection coefficients). As the call of this
  162. * procedure can be done in many ways (see the interpolation of the LAR
  163. * coefficient), it is assumed that the computation begins with index
  164. * k_start (for arrays d[..] and s[..]) and stops with index k_end
  165. * (k_start and k_end are defined in 4.2.9.1). This procedure also
  166. * needs to keep the array u[0..7] in memory for each call.
  167. */
  168. {
  169. register word * u = S->u;
  170. register int i;
  171. register word di, zzz, ui, sav, rpi;
  172. register longword ltmp;
  173. for (; k_n--; s++) {
  174. di = sav = *s;
  175. for (i = 0; i < 8; i++) { /* YYY */
  176. ui = u[i];
  177. rpi = rp[i];
  178. u[i] = sav;
  179. zzz = GSM_MULT_R(rpi, di);
  180. sav = GSM_ADD( ui, zzz);
  181. zzz = GSM_MULT_R(rpi, ui);
  182. di = GSM_ADD( di, zzz );
  183. }
  184. *s = di;
  185. }
  186. }
  187. #if defined(USE_FLOAT_MUL) && defined(FAST)
  188. static void Fast_Short_term_analysis_filtering P4((S,rp,k_n,s),
  189. struct gsm_state * S,
  190. register word * rp, /* [0..7] IN */
  191. register int k_n, /* k_end - k_start */
  192. register word * s /* [0..n-1] IN/OUT */
  193. )
  194. {
  195. register word * u = S->u;
  196. register int i;
  197. float uf[8],
  198. rpf[8];
  199. register float scalef = 3.0517578125e-5;
  200. register float sav, di, temp;
  201. for (i = 0; i < 8; ++i) {
  202. uf[i] = u[i];
  203. rpf[i] = rp[i] * scalef;
  204. }
  205. for (; k_n--; s++) {
  206. sav = di = *s;
  207. for (i = 0; i < 8; ++i) {
  208. register float rpfi = rpf[i];
  209. register float ufi = uf[i];
  210. uf[i] = sav;
  211. temp = rpfi * di + ufi;
  212. di += rpfi * ufi;
  213. sav = temp;
  214. }
  215. *s = di;
  216. }
  217. for (i = 0; i < 8; ++i) u[i] = uf[i];
  218. }
  219. #endif /* ! (defined (USE_FLOAT_MUL) && defined (FAST)) */
  220. static void Short_term_synthesis_filtering P5((S,rrp,k,wt,sr),
  221. struct gsm_state * S,
  222. register word * rrp, /* [0..7] IN */
  223. register int k, /* k_end - k_start */
  224. register word * wt, /* [0..k-1] IN */
  225. register word * sr /* [0..k-1] OUT */
  226. )
  227. {
  228. register word * v = S->v;
  229. register int i;
  230. register word sri, tmp1, tmp2;
  231. register longword ltmp; /* for GSM_ADD & GSM_SUB */
  232. while (k--) {
  233. sri = *wt++;
  234. for (i = 8; i--;) {
  235. /* sri = GSM_SUB( sri, gsm_mult_r( rrp[i], v[i] ) );
  236. */
  237. tmp1 = rrp[i];
  238. tmp2 = v[i];
  239. tmp2 = ( tmp1 == MIN_WORD && tmp2 == MIN_WORD
  240. ? MAX_WORD
  241. : 0x0FFFF & (( (longword)tmp1 * (longword)tmp2
  242. + 16384) >> 15)) ;
  243. sri = GSM_SUB( sri, tmp2 );
  244. /* v[i+1] = GSM_ADD( v[i], gsm_mult_r( rrp[i], sri ) );
  245. */
  246. tmp1 = ( tmp1 == MIN_WORD && sri == MIN_WORD
  247. ? MAX_WORD
  248. : 0x0FFFF & (( (longword)tmp1 * (longword)sri
  249. + 16384) >> 15)) ;
  250. v[i+1] = GSM_ADD( v[i], tmp1);
  251. }
  252. *sr++ = v[0] = sri;
  253. }
  254. }
  255. #if defined(FAST) && defined(USE_FLOAT_MUL)
  256. static void Fast_Short_term_synthesis_filtering P5((S,rrp,k,wt,sr),
  257. struct gsm_state * S,
  258. register word * rrp, /* [0..7] IN */
  259. register int k, /* k_end - k_start */
  260. register word * wt, /* [0..k-1] IN */
  261. register word * sr /* [0..k-1] OUT */
  262. )
  263. {
  264. register word * v = S->v;
  265. register int i;
  266. float va[9], rrpa[8];
  267. register float scalef = 3.0517578125e-5, temp;
  268. for (i = 0; i < 8; ++i) {
  269. va[i] = v[i];
  270. rrpa[i] = (float)rrp[i] * scalef;
  271. }
  272. while (k--) {
  273. register float sri = *wt++;
  274. for (i = 8; i--;) {
  275. sri -= rrpa[i] * va[i];
  276. if (sri < -32768.) sri = -32768.;
  277. else if (sri > 32767.) sri = 32767.;
  278. temp = va[i] + rrpa[i] * sri;
  279. if (temp < -32768.) temp = -32768.;
  280. else if (temp > 32767.) temp = 32767.;
  281. va[i+1] = temp;
  282. }
  283. *sr++ = va[0] = sri;
  284. }
  285. for (i = 0; i < 9; ++i) v[i] = va[i];
  286. }
  287. #endif /* defined(FAST) && defined(USE_FLOAT_MUL) */
  288. void Gsm_Short_Term_Analysis_Filter P3((S,LARc,s),
  289. struct gsm_state * S,
  290. word * LARc, /* coded log area ratio [0..7] IN */
  291. word * s /* signal [0..159] IN/OUT */
  292. )
  293. {
  294. word * LARpp_j = S->LARpp[ S->j ];
  295. word * LARpp_j_1 = S->LARpp[ S->j ^= 1 ];
  296. word LARp[8];
  297. #undef FILTER
  298. #if defined(FAST) && defined(USE_FLOAT_MUL)
  299. # define FILTER (* (S->fast \
  300. ? Fast_Short_term_analysis_filtering \
  301. : Short_term_analysis_filtering ))
  302. #else
  303. # define FILTER Short_term_analysis_filtering
  304. #endif
  305. Decoding_of_the_coded_Log_Area_Ratios( LARc, LARpp_j );
  306. Coefficients_0_12( LARpp_j_1, LARpp_j, LARp );
  307. LARp_to_rp( LARp );
  308. FILTER( S, LARp, 13, s);
  309. Coefficients_13_26( LARpp_j_1, LARpp_j, LARp);
  310. LARp_to_rp( LARp );
  311. FILTER( S, LARp, 14, s + 13);
  312. Coefficients_27_39( LARpp_j_1, LARpp_j, LARp);
  313. LARp_to_rp( LARp );
  314. FILTER( S, LARp, 13, s + 27);
  315. Coefficients_40_159( LARpp_j, LARp);
  316. LARp_to_rp( LARp );
  317. FILTER( S, LARp, 120, s + 40);
  318. }
  319. void Gsm_Short_Term_Synthesis_Filter P4((S, LARcr, wt, s),
  320. struct gsm_state * S,
  321. word * LARcr, /* received log area ratios [0..7] IN */
  322. word * wt, /* received d [0..159] IN */
  323. word * s /* signal s [0..159] OUT */
  324. )
  325. {
  326. word * LARpp_j = S->LARpp[ S->j ];
  327. word * LARpp_j_1 = S->LARpp[ S->j ^=1 ];
  328. word LARp[8];
  329. #undef FILTER
  330. #if defined(FAST) && defined(USE_FLOAT_MUL)
  331. # define FILTER (* (S->fast \
  332. ? Fast_Short_term_synthesis_filtering \
  333. : Short_term_synthesis_filtering ))
  334. #else
  335. # define FILTER Short_term_synthesis_filtering
  336. #endif
  337. Decoding_of_the_coded_Log_Area_Ratios( LARcr, LARpp_j );
  338. Coefficients_0_12( LARpp_j_1, LARpp_j, LARp );
  339. LARp_to_rp( LARp );
  340. FILTER( S, LARp, 13, wt, s );
  341. Coefficients_13_26( LARpp_j_1, LARpp_j, LARp);
  342. LARp_to_rp( LARp );
  343. FILTER( S, LARp, 14, wt + 13, s + 13 );
  344. Coefficients_27_39( LARpp_j_1, LARpp_j, LARp);
  345. LARp_to_rp( LARp );
  346. FILTER( S, LARp, 13, wt + 27, s + 27 );
  347. Coefficients_40_159( LARpp_j, LARp );
  348. LARp_to_rp( LARp );
  349. FILTER(S, LARp, 120, wt + 40, s + 40);
  350. }