Asterisk - The Open Source Telephony Project GIT-master-7921072
long_term.c
Go to the documentation of this file.
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
7/* $Header$ */
8
9#include <stdio.h>
10#include <assert.h>
11
12#include "private.h"
13
14#include "gsm.h"
15#include "proto.h"
16#ifdef K6OPT
17#include "k6opt.h"
18#endif
19/*
20 * 4.2.11 .. 4.2.12 LONG TERM PREDICTOR (LTP) SECTION
21 */
22
23
24/*
25 * This module computes the LTP gain (bc) and the LTP lag (Nc)
26 * for the long term analysis filter. This is done by calculating a
27 * maximum of the cross-correlation function between the current
28 * sub-segment short term residual signal d[0..39] (output of
29 * the short term analysis filter; for simplification the index
30 * of this array begins at 0 and ends at 39 for each sub-segment of the
31 * RPE-LTP analysis) and the previous reconstructed short term
32 * residual signal dp[ -120 .. -1 ]. A dynamic scaling must be
33 * performed to avoid overflow.
34 */
35
36 /* The next procedure exists in six versions. First two integer
37 * version (if USE_FLOAT_MUL is not defined); then four floating
38 * point versions, twice with proper scaling (USE_FLOAT_MUL defined),
39 * once without (USE_FLOAT_MUL and FAST defined, and fast run-time
40 * option used). Every pair has first a Cut version (see the -C
41 * option to toast or the LTP_CUT option to gsm_option()), then the
42 * uncut one. (For a detailed explanation of why this is altogether
43 * a bad idea, see Henry Spencer and Geoff Collyer, ``#ifdef Considered
44 * Harmful''.)
45 */
46
47#ifndef USE_FLOAT_MUL
48
49#ifdef LTP_CUT
50
51static void Cut_Calculation_of_the_LTP_parameters P5((st, d,dp,bc_out,Nc_out),
52
53 struct gsm_state * st,
54
55 register word * d, /* [0..39] IN */
56 register word * dp, /* [-120..-1] IN */
57 word * bc_out, /* OUT */
58 word * Nc_out /* OUT */
59)
60{
61 register int k, lambda;
62 word Nc, bc;
63 word wt[40];
64
65 longword L_result;
66 longword L_max, L_power;
67 word R, S, dmax, scal, best_k;
68 word ltp_cut;
69
70 register word temp, wt_k;
71
72 /* Search of the optimum scaling of d[0..39].
73 */
74 dmax = 0;
75 for (k = 0; k <= 39; k++) {
76 temp = d[k];
77 temp = GSM_ABS( temp );
78 if (temp > dmax) {
79 dmax = temp;
80 best_k = k;
81 }
82 }
83 temp = 0;
84 if (dmax == 0) scal = 0;
85 else {
86 assert(dmax > 0);
87 temp = gsm_norm( (longword)dmax << 16 );
88 }
89 if (temp > 6) scal = 0;
90 else scal = 6 - temp;
91 assert(scal >= 0);
92
93 /* Search for the maximum cross-correlation and coding of the LTP lag
94 */
95 L_max = 0;
96 Nc = 40; /* index for the maximum cross-correlation */
97 wt_k = SASR(d[best_k], scal);
98
99 for (lambda = 40; lambda <= 120; lambda++) {
100 L_result = (longword)wt_k * dp[best_k - lambda];
101 if (L_result > L_max) {
102 Nc = lambda;
103 L_max = L_result;
104 }
105 }
106 *Nc_out = Nc;
107 L_max <<= 1;
108
109 /* Rescaling of L_max
110 */
111 assert(scal <= 100 && scal >= -100);
112 L_max = L_max >> (6 - scal); /* sub(6, scal) */
113
114 assert( Nc <= 120 && Nc >= 40);
115
116 /* Compute the power of the reconstructed short term residual
117 * signal dp[..]
118 */
119 L_power = 0;
120 for (k = 0; k <= 39; k++) {
121
122 register longword L_temp;
123
124 L_temp = SASR( dp[k - Nc], 3 );
125 L_power += L_temp * L_temp;
126 }
127 L_power <<= 1; /* from L_MULT */
128
129 /* Normalization of L_max and L_power
130 */
131
132 if (L_max <= 0) {
133 *bc_out = 0;
134 return;
135 }
136 if (L_max >= L_power) {
137 *bc_out = 3;
138 return;
139 }
140
141 temp = gsm_norm( L_power );
142
143 R = SASR( L_max << temp, 16 );
144 S = SASR( L_power << temp, 16 );
145
146 /* Coding of the LTP gain
147 */
148
149 /* Table 4.3a must be used to obtain the level DLB[i] for the
150 * quantization of the LTP gain b to get the coded version bc.
151 */
152 for (bc = 0; bc <= 2; bc++) if (R <= gsm_mult(S, gsm_DLB[bc])) break;
153 *bc_out = bc;
154}
155
156#endif /* LTP_CUT */
157
158static void Calculation_of_the_LTP_parameters P4((d,dp,bc_out,Nc_out),
159 register word * d, /* [0..39] IN */
160 register word * dp, /* [-120..-1] IN */
161 word * bc_out, /* OUT */
162 word * Nc_out /* OUT */
163)
164{
165 register int k;
166#ifndef K6OPT
167 register int lambda;
168#endif
169 word Nc, bc;
170 word wt[40];
171
172 longword L_max, L_power;
173 word R, S, dmax, scal;
174 register word temp;
175
176 /* Search of the optimum scaling of d[0..39].
177 */
178 dmax = 0;
179
180 for (k = 0; k <= 39; k++) {
181 temp = d[k];
182 temp = GSM_ABS( temp );
183 if (temp > dmax) dmax = temp;
184 }
185
186 temp = 0;
187 if (dmax == 0) scal = 0;
188 else {
189 assert(dmax > 0);
190 temp = gsm_norm( (longword)dmax << 16 );
191 }
192
193 if (temp > 6) scal = 0;
194 else scal = 6 - temp;
195
196 assert(scal >= 0);
197
198 /* Initialization of a working array wt
199 */
200
201 for (k = 0; k <= 39; k++) wt[k] = SASR( d[k], scal );
202
203 /* Search for the maximum cross-correlation and coding of the LTP lag
204 */
205# ifdef K6OPT
206 L_max = k6maxcc(wt,dp,&Nc);
207# else
208 L_max = 0;
209 Nc = 40; /* index for the maximum cross-correlation */
210
211 for (lambda = 40; lambda <= 120; lambda++) {
212
213# undef STEP
214# define STEP(k) (longword)wt[k] * dp[k - lambda]
215
216 register longword L_result;
217
218 L_result = STEP(0) ; L_result += STEP(1) ;
219 L_result += STEP(2) ; L_result += STEP(3) ;
220 L_result += STEP(4) ; L_result += STEP(5) ;
221 L_result += STEP(6) ; L_result += STEP(7) ;
222 L_result += STEP(8) ; L_result += STEP(9) ;
223 L_result += STEP(10) ; L_result += STEP(11) ;
224 L_result += STEP(12) ; L_result += STEP(13) ;
225 L_result += STEP(14) ; L_result += STEP(15) ;
226 L_result += STEP(16) ; L_result += STEP(17) ;
227 L_result += STEP(18) ; L_result += STEP(19) ;
228 L_result += STEP(20) ; L_result += STEP(21) ;
229 L_result += STEP(22) ; L_result += STEP(23) ;
230 L_result += STEP(24) ; L_result += STEP(25) ;
231 L_result += STEP(26) ; L_result += STEP(27) ;
232 L_result += STEP(28) ; L_result += STEP(29) ;
233 L_result += STEP(30) ; L_result += STEP(31) ;
234 L_result += STEP(32) ; L_result += STEP(33) ;
235 L_result += STEP(34) ; L_result += STEP(35) ;
236 L_result += STEP(36) ; L_result += STEP(37) ;
237 L_result += STEP(38) ; L_result += STEP(39) ;
238
239 if (L_result > L_max) {
240
241 Nc = lambda;
242 L_max = L_result;
243 }
244 }
245# endif
246 *Nc_out = Nc;
247
248 L_max <<= 1;
249
250 /* Rescaling of L_max
251 */
252 assert(scal <= 100 && scal >= -100);
253 L_max = L_max >> (6 - scal); /* sub(6, scal) */
254
255 assert( Nc <= 120 && Nc >= 40);
256
257 /* Compute the power of the reconstructed short term residual
258 * signal dp[..]
259 */
260 L_power = 0;
261 for (k = 0; k <= 39; k++) {
262
263 register longword L_temp;
264
265 L_temp = SASR( dp[k - Nc], 3 );
266 L_power += L_temp * L_temp;
267 }
268 L_power <<= 1; /* from L_MULT */
269
270 /* Normalization of L_max and L_power
271 */
272
273 if (L_max <= 0) {
274 *bc_out = 0;
275 return;
276 }
277 if (L_max >= L_power) {
278 *bc_out = 3;
279 return;
280 }
281
282 temp = gsm_norm( L_power );
283
284 R = (word)SASR( L_max << temp, 16 );
285 S = (word)SASR( L_power << temp, 16 );
286
287 /* Coding of the LTP gain
288 */
289
290 /* Table 4.3a must be used to obtain the level DLB[i] for the
291 * quantization of the LTP gain b to get the coded version bc.
292 */
293 for (bc = 0; bc <= 2; bc++) if (R <= gsm_mult(S, gsm_DLB[bc])) break;
294 *bc_out = bc;
295}
296
297#else /* USE_FLOAT_MUL */
298
299#ifdef LTP_CUT
300
301static void Cut_Calculation_of_the_LTP_parameters P5((st, d,dp,bc_out,Nc_out),
302 struct gsm_state * st, /* IN */
303 register word * d, /* [0..39] IN */
304 register word * dp, /* [-120..-1] IN */
305 word * bc_out, /* OUT */
306 word * Nc_out /* OUT */
307)
308{
309 register int k, lambda;
310 word Nc, bc;
311 word ltp_cut;
312
313 float wt_float[40];
314 float dp_float_base[120], * dp_float = dp_float_base + 120;
315
316 longword L_max, L_power;
317 word R, S, dmax, scal;
318 register word temp;
319
320 /* Search of the optimum scaling of d[0..39].
321 */
322 dmax = 0;
323
324 for (k = 0; k <= 39; k++) {
325 temp = d[k];
326 temp = GSM_ABS( temp );
327 if (temp > dmax) dmax = temp;
328 }
329
330 temp = 0;
331 if (dmax == 0) scal = 0;
332 else {
333 assert(dmax > 0);
334 temp = gsm_norm( (longword)dmax << 16 );
335 }
336
337 if (temp > 6) scal = 0;
338 else scal = 6 - temp;
339
340 assert(scal >= 0);
341 ltp_cut = (longword)SASR(dmax, scal) * st->ltp_cut / 100;
342
343
344 /* Initialization of a working array wt
345 */
346
347 for (k = 0; k < 40; k++) {
348 register word w = SASR( d[k], scal );
349 if (w < 0 ? w > -ltp_cut : w < ltp_cut) {
350 wt_float[k] = 0.0;
351 }
352 else {
353 wt_float[k] = w;
354 }
355 }
356 for (k = -120; k < 0; k++) dp_float[k] = dp[k];
357
358 /* Search for the maximum cross-correlation and coding of the LTP lag
359 */
360 L_max = 0;
361 Nc = 40; /* index for the maximum cross-correlation */
362
363 for (lambda = 40; lambda <= 120; lambda += 9) {
364
365 /* Calculate L_result for l = lambda .. lambda + 9.
366 */
367 register float *lp = dp_float - lambda;
368
369 register float W;
370 register float a = lp[-8], b = lp[-7], c = lp[-6],
371 d = lp[-5], e = lp[-4], f = lp[-3],
372 g = lp[-2], h = lp[-1];
373 register float E;
374 register float S0 = 0, S1 = 0, S2 = 0, S3 = 0, S4 = 0,
375 S5 = 0, S6 = 0, S7 = 0, S8 = 0;
376
377# undef STEP
378# define STEP(K, a, b, c, d, e, f, g, h) \
379 if ((W = wt_float[K]) != 0.0) { \
380 E = W * a; S8 += E; \
381 E = W * b; S7 += E; \
382 E = W * c; S6 += E; \
383 E = W * d; S5 += E; \
384 E = W * e; S4 += E; \
385 E = W * f; S3 += E; \
386 E = W * g; S2 += E; \
387 E = W * h; S1 += E; \
388 a = lp[K]; \
389 E = W * a; S0 += E; } else (a = lp[K])
390
391# define STEP_A(K) STEP(K, a, b, c, d, e, f, g, h)
392# define STEP_B(K) STEP(K, b, c, d, e, f, g, h, a)
393# define STEP_C(K) STEP(K, c, d, e, f, g, h, a, b)
394# define STEP_D(K) STEP(K, d, e, f, g, h, a, b, c)
395# define STEP_E(K) STEP(K, e, f, g, h, a, b, c, d)
396# define STEP_F(K) STEP(K, f, g, h, a, b, c, d, e)
397# define STEP_G(K) STEP(K, g, h, a, b, c, d, e, f)
398# define STEP_H(K) STEP(K, h, a, b, c, d, e, f, g)
399
400 STEP_A( 0); STEP_B( 1); STEP_C( 2); STEP_D( 3);
401 STEP_E( 4); STEP_F( 5); STEP_G( 6); STEP_H( 7);
402
403 STEP_A( 8); STEP_B( 9); STEP_C(10); STEP_D(11);
404 STEP_E(12); STEP_F(13); STEP_G(14); STEP_H(15);
405
406 STEP_A(16); STEP_B(17); STEP_C(18); STEP_D(19);
407 STEP_E(20); STEP_F(21); STEP_G(22); STEP_H(23);
408
409 STEP_A(24); STEP_B(25); STEP_C(26); STEP_D(27);
410 STEP_E(28); STEP_F(29); STEP_G(30); STEP_H(31);
411
412 STEP_A(32); STEP_B(33); STEP_C(34); STEP_D(35);
413 STEP_E(36); STEP_F(37); STEP_G(38); STEP_H(39);
414
415 if (S0 > L_max) { L_max = S0; Nc = lambda; }
416 if (S1 > L_max) { L_max = S1; Nc = lambda + 1; }
417 if (S2 > L_max) { L_max = S2; Nc = lambda + 2; }
418 if (S3 > L_max) { L_max = S3; Nc = lambda + 3; }
419 if (S4 > L_max) { L_max = S4; Nc = lambda + 4; }
420 if (S5 > L_max) { L_max = S5; Nc = lambda + 5; }
421 if (S6 > L_max) { L_max = S6; Nc = lambda + 6; }
422 if (S7 > L_max) { L_max = S7; Nc = lambda + 7; }
423 if (S8 > L_max) { L_max = S8; Nc = lambda + 8; }
424
425 }
426 *Nc_out = Nc;
427
428 L_max <<= 1;
429
430 /* Rescaling of L_max
431 */
432 assert(scal <= 100 && scal >= -100);
433 L_max = L_max >> (6 - scal); /* sub(6, scal) */
434
435 assert( Nc <= 120 && Nc >= 40);
436
437 /* Compute the power of the reconstructed short term residual
438 * signal dp[..]
439 */
440 L_power = 0;
441 for (k = 0; k <= 39; k++) {
442
443 register longword L_temp;
444
445 L_temp = SASR( dp[k - Nc], 3 );
446 L_power += L_temp * L_temp;
447 }
448 L_power <<= 1; /* from L_MULT */
449
450 /* Normalization of L_max and L_power
451 */
452
453 if (L_max <= 0) {
454 *bc_out = 0;
455 return;
456 }
457 if (L_max >= L_power) {
458 *bc_out = 3;
459 return;
460 }
461
462 temp = gsm_norm( L_power );
463
464 R = SASR( L_max << temp, 16 );
465 S = SASR( L_power << temp, 16 );
466
467 /* Coding of the LTP gain
468 */
469
470 /* Table 4.3a must be used to obtain the level DLB[i] for the
471 * quantization of the LTP gain b to get the coded version bc.
472 */
473 for (bc = 0; bc <= 2; bc++) if (R <= gsm_mult(S, gsm_DLB[bc])) break;
474 *bc_out = bc;
475}
476
477#endif /* LTP_CUT */
478
479static void Calculation_of_the_LTP_parameters P4((d,dp,bc_out,Nc_out),
480 register word * d, /* [0..39] IN */
481 register word * dp, /* [-120..-1] IN */
482 word * bc_out, /* OUT */
483 word * Nc_out /* OUT */
484)
485{
486 register int k, lambda;
487 word Nc, bc;
488
489 float wt_float[40];
490 float dp_float_base[120], * dp_float = dp_float_base + 120;
491
492 longword L_max, L_power;
493 word R, S, dmax, scal;
494 register word temp;
495
496 /* Search of the optimum scaling of d[0..39].
497 */
498 dmax = 0;
499
500 for (k = 0; k <= 39; k++) {
501 temp = d[k];
502 temp = GSM_ABS( temp );
503 if (temp > dmax) dmax = temp;
504 }
505
506 temp = 0;
507 if (dmax == 0) scal = 0;
508 else {
509 assert(dmax > 0);
510 temp = gsm_norm( (longword)dmax << 16 );
511 }
512
513 if (temp > 6) scal = 0;
514 else scal = 6 - temp;
515
516 assert(scal >= 0);
517
518 /* Initialization of a working array wt
519 */
520
521 for (k = 0; k < 40; k++) wt_float[k] = SASR( d[k], scal );
522 for (k = -120; k < 0; k++) dp_float[k] = dp[k];
523
524 /* Search for the maximum cross-correlation and coding of the LTP lag
525 */
526 L_max = 0;
527 Nc = 40; /* index for the maximum cross-correlation */
528
529 for (lambda = 40; lambda <= 120; lambda += 9) {
530
531 /* Calculate L_result for l = lambda .. lambda + 9.
532 */
533 register float *lp = dp_float - lambda;
534
535 register float W;
536 register float a = lp[-8], b = lp[-7], c = lp[-6],
537 d = lp[-5], e = lp[-4], f = lp[-3],
538 g = lp[-2], h = lp[-1];
539 register float E;
540 register float S0 = 0, S1 = 0, S2 = 0, S3 = 0, S4 = 0,
541 S5 = 0, S6 = 0, S7 = 0, S8 = 0;
542
543# undef STEP
544# define STEP(K, a, b, c, d, e, f, g, h) \
545 W = wt_float[K]; \
546 E = W * a; S8 += E; \
547 E = W * b; S7 += E; \
548 E = W * c; S6 += E; \
549 E = W * d; S5 += E; \
550 E = W * e; S4 += E; \
551 E = W * f; S3 += E; \
552 E = W * g; S2 += E; \
553 E = W * h; S1 += E; \
554 a = lp[K]; \
555 E = W * a; S0 += E
556
557# define STEP_A(K) STEP(K, a, b, c, d, e, f, g, h)
558# define STEP_B(K) STEP(K, b, c, d, e, f, g, h, a)
559# define STEP_C(K) STEP(K, c, d, e, f, g, h, a, b)
560# define STEP_D(K) STEP(K, d, e, f, g, h, a, b, c)
561# define STEP_E(K) STEP(K, e, f, g, h, a, b, c, d)
562# define STEP_F(K) STEP(K, f, g, h, a, b, c, d, e)
563# define STEP_G(K) STEP(K, g, h, a, b, c, d, e, f)
564# define STEP_H(K) STEP(K, h, a, b, c, d, e, f, g)
565
566 STEP_A( 0); STEP_B( 1); STEP_C( 2); STEP_D( 3);
567 STEP_E( 4); STEP_F( 5); STEP_G( 6); STEP_H( 7);
568
569 STEP_A( 8); STEP_B( 9); STEP_C(10); STEP_D(11);
570 STEP_E(12); STEP_F(13); STEP_G(14); STEP_H(15);
571
572 STEP_A(16); STEP_B(17); STEP_C(18); STEP_D(19);
573 STEP_E(20); STEP_F(21); STEP_G(22); STEP_H(23);
574
575 STEP_A(24); STEP_B(25); STEP_C(26); STEP_D(27);
576 STEP_E(28); STEP_F(29); STEP_G(30); STEP_H(31);
577
578 STEP_A(32); STEP_B(33); STEP_C(34); STEP_D(35);
579 STEP_E(36); STEP_F(37); STEP_G(38); STEP_H(39);
580
581 if (S0 > L_max) { L_max = S0; Nc = lambda; }
582 if (S1 > L_max) { L_max = S1; Nc = lambda + 1; }
583 if (S2 > L_max) { L_max = S2; Nc = lambda + 2; }
584 if (S3 > L_max) { L_max = S3; Nc = lambda + 3; }
585 if (S4 > L_max) { L_max = S4; Nc = lambda + 4; }
586 if (S5 > L_max) { L_max = S5; Nc = lambda + 5; }
587 if (S6 > L_max) { L_max = S6; Nc = lambda + 6; }
588 if (S7 > L_max) { L_max = S7; Nc = lambda + 7; }
589 if (S8 > L_max) { L_max = S8; Nc = lambda + 8; }
590 }
591 *Nc_out = Nc;
592
593 L_max <<= 1;
594
595 /* Rescaling of L_max
596 */
597 assert(scal <= 100 && scal >= -100);
598 L_max = L_max >> (6 - scal); /* sub(6, scal) */
599
600 assert( Nc <= 120 && Nc >= 40);
601
602 /* Compute the power of the reconstructed short term residual
603 * signal dp[..]
604 */
605 L_power = 0;
606 for (k = 0; k <= 39; k++) {
607
608 register longword L_temp;
609
610 L_temp = SASR( dp[k - Nc], 3 );
611 L_power += L_temp * L_temp;
612 }
613 L_power <<= 1; /* from L_MULT */
614
615 /* Normalization of L_max and L_power
616 */
617
618 if (L_max <= 0) {
619 *bc_out = 0;
620 return;
621 }
622 if (L_max >= L_power) {
623 *bc_out = 3;
624 return;
625 }
626
627 temp = gsm_norm( L_power );
628
629 R = SASR( L_max << temp, 16 );
630 S = SASR( L_power << temp, 16 );
631
632 /* Coding of the LTP gain
633 */
634
635 /* Table 4.3a must be used to obtain the level DLB[i] for the
636 * quantization of the LTP gain b to get the coded version bc.
637 */
638 for (bc = 0; bc <= 2; bc++) if (R <= gsm_mult(S, gsm_DLB[bc])) break;
639 *bc_out = bc;
640}
641
642#ifdef FAST
643#ifdef LTP_CUT
644
645static void Cut_Fast_Calculation_of_the_LTP_parameters P5((st,
646 d,dp,bc_out,Nc_out),
647 struct gsm_state * st, /* IN */
648 register word * d, /* [0..39] IN */
649 register word * dp, /* [-120..-1] IN */
650 word * bc_out, /* OUT */
651 word * Nc_out /* OUT */
652)
653{
654 register int k, lambda;
655 register float wt_float;
656 word Nc, bc;
657 word wt_max, best_k, ltp_cut;
658
659 float dp_float_base[120], * dp_float = dp_float_base + 120;
660
661 register float L_result, L_max, L_power;
662
663 wt_max = 0;
664
665 for (k = 0; k < 40; ++k) {
666 if ( d[k] > wt_max) wt_max = d[best_k = k];
667 else if (-d[k] > wt_max) wt_max = -d[best_k = k];
668 }
669
670 assert(wt_max >= 0);
671 wt_float = (float)wt_max;
672
673 for (k = -120; k < 0; ++k) dp_float[k] = (float)dp[k];
674
675 /* Search for the maximum cross-correlation and coding of the LTP lag
676 */
677 L_max = 0;
678 Nc = 40; /* index for the maximum cross-correlation */
679
680 for (lambda = 40; lambda <= 120; lambda++) {
681 L_result = wt_float * dp_float[best_k - lambda];
682 if (L_result > L_max) {
683 Nc = lambda;
684 L_max = L_result;
685 }
686 }
687
688 *Nc_out = Nc;
689 if (L_max <= 0.) {
690 *bc_out = 0;
691 return;
692 }
693
694 /* Compute the power of the reconstructed short term residual
695 * signal dp[..]
696 */
697 dp_float -= Nc;
698 L_power = 0;
699 for (k = 0; k < 40; ++k) {
700 register float f = dp_float[k];
701 L_power += f * f;
702 }
703
704 if (L_max >= L_power) {
705 *bc_out = 3;
706 return;
707 }
708
709 /* Coding of the LTP gain
710 * Table 4.3a must be used to obtain the level DLB[i] for the
711 * quantization of the LTP gain b to get the coded version bc.
712 */
713 lambda = L_max / L_power * 32768.;
714 for (bc = 0; bc <= 2; ++bc) if (lambda <= gsm_DLB[bc]) break;
715 *bc_out = bc;
716}
717
718#endif /* LTP_CUT */
719
720static void Fast_Calculation_of_the_LTP_parameters P4((d,dp,bc_out,Nc_out),
721 register word * d, /* [0..39] IN */
722 register word * dp, /* [-120..-1] IN */
723 word * bc_out, /* OUT */
724 word * Nc_out /* OUT */
725)
726{
727 register int k, lambda;
728 word Nc, bc;
729
730 float wt_float[40];
731 float dp_float_base[120], * dp_float = dp_float_base + 120;
732
733 register float L_max, L_power;
734
735 for (k = 0; k < 40; ++k) wt_float[k] = (float)d[k];
736 for (k = -120; k < 0; ++k) dp_float[k] = (float)dp[k];
737
738 /* Search for the maximum cross-correlation and coding of the LTP lag
739 */
740 L_max = 0;
741 Nc = 40; /* index for the maximum cross-correlation */
742
743 for (lambda = 40; lambda <= 120; lambda += 9) {
744
745 /* Calculate L_result for l = lambda .. lambda + 9.
746 */
747 register float *lp = dp_float - lambda;
748
749 register float W;
750 register float a = lp[-8], b = lp[-7], c = lp[-6],
751 d = lp[-5], e = lp[-4], f = lp[-3],
752 g = lp[-2], h = lp[-1];
753 register float E;
754 register float S0 = 0, S1 = 0, S2 = 0, S3 = 0, S4 = 0,
755 S5 = 0, S6 = 0, S7 = 0, S8 = 0;
756
757# undef STEP
758# define STEP(K, a, b, c, d, e, f, g, h) \
759 W = wt_float[K]; \
760 E = W * a; S8 += E; \
761 E = W * b; S7 += E; \
762 E = W * c; S6 += E; \
763 E = W * d; S5 += E; \
764 E = W * e; S4 += E; \
765 E = W * f; S3 += E; \
766 E = W * g; S2 += E; \
767 E = W * h; S1 += E; \
768 a = lp[K]; \
769 E = W * a; S0 += E
770
771# define STEP_A(K) STEP(K, a, b, c, d, e, f, g, h)
772# define STEP_B(K) STEP(K, b, c, d, e, f, g, h, a)
773# define STEP_C(K) STEP(K, c, d, e, f, g, h, a, b)
774# define STEP_D(K) STEP(K, d, e, f, g, h, a, b, c)
775# define STEP_E(K) STEP(K, e, f, g, h, a, b, c, d)
776# define STEP_F(K) STEP(K, f, g, h, a, b, c, d, e)
777# define STEP_G(K) STEP(K, g, h, a, b, c, d, e, f)
778# define STEP_H(K) STEP(K, h, a, b, c, d, e, f, g)
779
780 STEP_A( 0); STEP_B( 1); STEP_C( 2); STEP_D( 3);
781 STEP_E( 4); STEP_F( 5); STEP_G( 6); STEP_H( 7);
782
783 STEP_A( 8); STEP_B( 9); STEP_C(10); STEP_D(11);
784 STEP_E(12); STEP_F(13); STEP_G(14); STEP_H(15);
785
786 STEP_A(16); STEP_B(17); STEP_C(18); STEP_D(19);
787 STEP_E(20); STEP_F(21); STEP_G(22); STEP_H(23);
788
789 STEP_A(24); STEP_B(25); STEP_C(26); STEP_D(27);
790 STEP_E(28); STEP_F(29); STEP_G(30); STEP_H(31);
791
792 STEP_A(32); STEP_B(33); STEP_C(34); STEP_D(35);
793 STEP_E(36); STEP_F(37); STEP_G(38); STEP_H(39);
794
795 if (S0 > L_max) { L_max = S0; Nc = lambda; }
796 if (S1 > L_max) { L_max = S1; Nc = lambda + 1; }
797 if (S2 > L_max) { L_max = S2; Nc = lambda + 2; }
798 if (S3 > L_max) { L_max = S3; Nc = lambda + 3; }
799 if (S4 > L_max) { L_max = S4; Nc = lambda + 4; }
800 if (S5 > L_max) { L_max = S5; Nc = lambda + 5; }
801 if (S6 > L_max) { L_max = S6; Nc = lambda + 6; }
802 if (S7 > L_max) { L_max = S7; Nc = lambda + 7; }
803 if (S8 > L_max) { L_max = S8; Nc = lambda + 8; }
804 }
805 *Nc_out = Nc;
806
807 if (L_max <= 0.) {
808 *bc_out = 0;
809 return;
810 }
811
812 /* Compute the power of the reconstructed short term residual
813 * signal dp[..]
814 */
815 dp_float -= Nc;
816 L_power = 0;
817 for (k = 0; k < 40; ++k) {
818 register float f = dp_float[k];
819 L_power += f * f;
820 }
821
822 if (L_max >= L_power) {
823 *bc_out = 3;
824 return;
825 }
826
827 /* Coding of the LTP gain
828 * Table 4.3a must be used to obtain the level DLB[i] for the
829 * quantization of the LTP gain b to get the coded version bc.
830 */
831 lambda = L_max / L_power * 32768.;
832 for (bc = 0; bc <= 2; ++bc) if (lambda <= gsm_DLB[bc]) break;
833 *bc_out = bc;
834}
835
836#endif /* FAST */
837#endif /* USE_FLOAT_MUL */
838
839
840/* 4.2.12 */
841
842static void Long_term_analysis_filtering P6((bc,Nc,dp,d,dpp,e),
843 word bc, /* IN */
844 word Nc, /* IN */
845 register word * dp, /* previous d [-120..-1] IN */
846 register word * d, /* d [0..39] IN */
847 register word * dpp, /* estimate [0..39] OUT */
848 register word * e /* long term res. signal [0..39] OUT */
849)
850/*
851 * In this part, we have to decode the bc parameter to compute
852 * the samples of the estimate dpp[0..39]. The decoding of bc needs the
853 * use of table 4.3b. The long term residual signal e[0..39]
854 * is then calculated to be fed to the RPE encoding section.
855 */
856{
857 register int k;
858
859# undef STEP
860# define STEP(BP) \
861 for (k = 0; k <= 39; k++) { \
862 dpp[k] = (word)GSM_MULT_R( BP, dp[k - Nc]); \
863 e[k] = GSM_SUB( d[k], dpp[k] ); \
864 }
865
866 switch (bc) {
867 case 0: STEP( 3277 ); break;
868 case 1: STEP( 11469 ); break;
869 case 2: STEP( 21299 ); break;
870 case 3: STEP( 32767 ); break;
871 }
872}
873
874void Gsm_Long_Term_Predictor P7((S,d,dp,e,dpp,Nc,bc), /* 4x for 160 samples */
875
876 struct gsm_state * S,
877
878 word * d, /* [0..39] residual signal IN */
879 word * dp, /* [-120..-1] d' IN */
880
881 word * e, /* [0..39] OUT */
882 word * dpp, /* [0..39] OUT */
883 word * Nc, /* correlation lag OUT */
884 word * bc /* gain factor OUT */
885)
886{
887 assert( d ); assert( dp ); assert( e );
888 assert( dpp); assert( Nc ); assert( bc );
889
890#if defined(FAST) && defined(USE_FLOAT_MUL)
891 if (S->fast)
892#if defined (LTP_CUT)
893 if (S->ltp_cut)
894 Cut_Fast_Calculation_of_the_LTP_parameters(S,
895 d, dp, bc, Nc);
896 else
897#endif /* LTP_CUT */
898 Fast_Calculation_of_the_LTP_parameters(d, dp, bc, Nc );
899 else
900#endif /* FAST & USE_FLOAT_MUL */
901#ifdef LTP_CUT
902 if (S->ltp_cut)
903 Cut_Calculation_of_the_LTP_parameters(S, d, dp, bc, Nc);
904 else
905#endif
906 Calculation_of_the_LTP_parameters(d, dp, bc, Nc);
907
908 Long_term_analysis_filtering( *bc, *Nc, dp, d, dpp, e );
909}
910
911/* 4.3.2 */
912void Gsm_Long_Term_Synthesis_Filtering P5((S,Ncr,bcr,erp,drp),
913 struct gsm_state * S,
914
915 word Ncr,
916 word bcr,
917 register word * erp, /* [0..39] IN */
918 register word * drp /* [-120..-1] IN, [-120..40] OUT */
919)
920/*
921 * This procedure uses the bcr and Ncr parameter to realize the
922 * long term synthesis filtering. The decoding of bcr needs
923 * table 4.3b.
924 */
925{
926 register int k;
927 word brp, drpp, Nr;
928
929 /* Check the limits of Nr.
930 */
931 Nr = Ncr < 40 || Ncr > 120 ? S->nrp : Ncr;
932 S->nrp = Nr;
933 assert(Nr >= 40 && Nr <= 120);
934
935 /* Decoding of the LTP gain bcr
936 */
937 brp = gsm_QLB[ bcr ];
938
939 /* Computation of the reconstructed short term residual
940 * signal drp[0..39]
941 */
942 assert(brp != MIN_WORD);
943
944 for (k = 0; k <= 39; k++) {
945 drpp = (word)GSM_MULT_R( brp, drp[ k - Nr ] );
946 drp[k] = GSM_ADD( erp[k], drpp );
947 }
948
949 /*
950 * Update of the reconstructed short term residual signal
951 * drp[ -1..-120 ]
952 */
953
954 for (k = 0; k <= 119; k++) drp[ -120 + k ] = drp[ -80 + k ];
955}
#define S(e)
short word
#define MIN_WORD
#define SASR(x, by)
word gsm_QLB[4]
#define GSM_MULT_R(a, b)
word gsm_DLB[4]
Definition: table.c:39
#define GSM_ABS(a)
static word GSM_ADD(longword a, longword b)
long longword
#define dmax(a, b)
Definition: f2c.h:200
#define bc
#define Nc
#define STEP(k)
static void Calculation_of_the_LTP_parameters P4((d, dp, bc_out, Nc_out), register word *d, register word *dp, word *bc_out, word *Nc_out)
Definition: long_term.c:158
void Gsm_Long_Term_Predictor P7((S, d, dp, e, dpp, Nc, bc), struct gsm_state *S, word *d, word *dp, word *e, word *dpp, word *Nc, word *bc)
Definition: long_term.c:874
void Gsm_Long_Term_Synthesis_Filtering P5((S, Ncr, bcr, erp, drp), struct gsm_state *S, word Ncr, word bcr, register word *erp, register word *drp)
Definition: long_term.c:912
static void Long_term_analysis_filtering P6((bc, Nc, dp, d, dpp, e), word bc, word Nc, register word *dp, register word *d, register word *dpp, register word *e)
Definition: long_term.c:842
static struct test_val b
static struct test_val a
static struct test_val d
static struct test_val c