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.
10 #include "gsm610_priv.h"
13 * 4.2.4 .. 4.2.7 LPC ANALYSIS SECTION
19 static void Autocorrelation (
20 word * s, /* [0..159] IN/OUT */
21 longword * L_ACF) /* [0..8] OUT */
23 * The goal is to compute the array L_ACF[k]. The signal s[i] must
24 * be scaled in order to avoid an overflow situation.
29 word temp, smax, scalauto;
35 /* Dynamic scaling of the array s[0..159]
38 /* Search for the maximum.
41 for (k = 0; k <= 159; k++) {
42 temp = GSM_ABS( s[k] );
43 if (temp > smax) smax = temp;
46 /* Computation of the scaling factor.
48 if (smax == 0) scalauto = 0;
51 scalauto = 4 - gsm_norm( (longword)smax << 16 );/* sub(4,..) */
54 /* Scaling of the array s[0...159]
61 case n: for (k = 0; k <= 159; k++) \
62 float_s[k] = (float) \
63 (s[k] = GSM_MULT_R(s[k], 16384 >> (n-1)));\
67 case n: for (k = 0; k <= 159; k++) \
68 s[k] = GSM_MULT_R( s[k], 16384 >> (n-1) );\
70 # endif /* USE_FLOAT_MUL */
81 else for (k = 0; k <= 159; k++) float_s[k] = (float) s[k];
84 /* Compute the L_ACF[..].
88 register float * sp = float_s;
89 register float sl = *sp;
91 # define STEP(k) L_ACF[k] += (longword)(sl * sp[ -(k) ]);
96 # define STEP(k) L_ACF[k] += ((longword)sl * sp[ -(k) ]);
99 # define NEXTI sl = *++sp
102 for (k = 9; k--; L_ACF[k] = 0) ;
108 STEP(0); STEP(1); STEP(2);
110 STEP(0); STEP(1); STEP(2); STEP(3);
112 STEP(0); STEP(1); STEP(2); STEP(3); STEP(4);
114 STEP(0); STEP(1); STEP(2); STEP(3); STEP(4); STEP(5);
116 STEP(0); STEP(1); STEP(2); STEP(3); STEP(4); STEP(5); STEP(6);
118 STEP(0); STEP(1); STEP(2); STEP(3); STEP(4); STEP(5); STEP(6); STEP(7);
120 for (i = 8; i <= 159; i++) {
125 STEP(1); STEP(2); STEP(3); STEP(4);
126 STEP(5); STEP(6); STEP(7); STEP(8);
129 for (k = 9; k--; L_ACF[k] <<= 1) ;
132 /* Rescaling of the array s[0..159]
135 assert(scalauto <= 4);
136 for (k = 160; k--; *s++ <<= scalauto) ;
140 #if defined(USE_FLOAT_MUL) && defined(FAST)
142 static void Fast_Autocorrelation (
143 word * s, /* [0..159] IN/OUT */
144 longword * L_ACF) /* [0..8] OUT */
151 register float *sf = s_f;
153 for (i = 0; i < 160; ++i) sf[i] = s[i];
154 for (k = 0; k <= 8; k++) {
155 register float L_temp2 = 0;
156 register float *sfl = sf - k;
157 for (i = k; i < 160; ++i) L_temp2 += sf[i] * sfl[i];
158 f_L_ACF[k] = L_temp2;
160 scale = MAX_LONGWORD / f_L_ACF[0];
162 for (k = 0; k <= 8; k++) {
163 L_ACF[k] = f_L_ACF[k] * scale;
166 #endif /* defined (USE_FLOAT_MUL) && defined (FAST) */
170 static void Reflection_coefficients (
171 longword * L_ACF, /* 0...8 IN */
172 register word * r /* 0...7 OUT */
175 register int i, m, n;
177 word ACF[9]; /* 0..8 */
178 word P[ 9]; /* 0..8 */
179 word K[ 9]; /* 2..8 */
181 /* Schur recursion with 16 bits arithmetic.
185 for (i = 8; i--; *r++ = 0) ;
189 assert( L_ACF[0] != 0 );
190 temp = gsm_norm( L_ACF[0] );
192 assert(temp >= 0 && temp < 32);
195 for (i = 0; i <= 8; i++) ACF[i] = SASR_L( L_ACF[i] << temp, 16 );
197 /* Initialize array P[..] and K[..] for the recursion.
200 for (i = 1; i <= 7; i++) K[ i ] = ACF[ i ];
201 for (i = 0; i <= 8; i++) P[ i ] = ACF[ i ];
203 /* Compute reflection coefficients
205 for (n = 1; n <= 8; n++, r++) {
208 temp = GSM_ABS(temp);
210 for (i = n; i <= 8; i++) *r++ = 0;
214 *r = gsm_div( temp, P[0] );
217 if (P[1] > 0) *r = -*r; /* r[n] = sub(0, r[n]) */
218 assert (*r != MIN_WORD);
223 temp = GSM_MULT_R( P[1], *r );
224 P[0] = GSM_ADD( P[0], temp );
226 for (m = 1; m <= 8 - n; m++) {
227 temp = GSM_MULT_R( K[ m ], *r );
228 P[m] = GSM_ADD( P[ m+1 ], temp );
230 temp = GSM_MULT_R( P[ m+1 ], *r );
231 K[m] = GSM_ADD( K[ m ], temp );
238 static void Transformation_to_Log_Area_Ratios (
239 register word * r /* 0..7 IN/OUT */
242 * The following scaling for r[..] and LAR[..] has been used:
244 * r[..] = integer( real_r[..]*32768. ); -1 <= real_r < 1.
245 * LAR[..] = integer( real_LAR[..] * 16384 );
246 * with -1.625 <= real_LAR <= 1.625
253 /* Computation of the LAR[0..7] from the r[0..7]
255 for (i = 1; i <= 8; i++, r++) {
258 temp = GSM_ABS(temp);
263 } else if (temp < 31130) {
264 assert( temp >= 11059 );
267 assert( temp >= 26112 );
272 *r = *r < 0 ? -temp : temp;
273 assert( *r != MIN_WORD );
279 static void Quantization_and_coding (
280 register word * LAR /* [0..7] IN/OUT */
285 /* This procedure needs four tables; the following equations
286 * give the optimum scaling for the constants:
288 * A[0..7] = integer( real_A[0..7] * 1024 )
289 * B[0..7] = integer( real_B[0..7] * 512 )
290 * MAC[0..7] = maximum of the LARc[0..7]
291 * MIC[0..7] = minimum of the LARc[0..7]
295 # define STEP( A, B, MAC, MIC ) \
296 temp = GSM_MULT( A, *LAR ); \
297 temp = GSM_ADD( temp, B ); \
298 temp = GSM_ADD( temp, 256 ); \
299 temp = SASR_W( temp, 9 ); \
300 *LAR = temp>MAC ? MAC - MIC : (temp<MIC ? 0 : temp - MIC); \
303 STEP( 20480, 0, 31, -32 );
304 STEP( 20480, 0, 31, -32 );
305 STEP( 20480, 2048, 15, -16 );
306 STEP( 20480, -2560, 15, -16 );
308 STEP( 13964, 94, 7, -8 );
309 STEP( 15360, -1792, 7, -8 );
310 STEP( 8534, -341, 3, -4 );
311 STEP( 9036, -1144, 3, -4 );
316 void Gsm_LPC_Analysis (
318 word * s, /* 0..159 signals IN/OUT */
319 word * LARc) /* 0..7 LARc's OUT */
323 #if defined(USE_FLOAT_MUL) && defined(FAST)
324 if (S->fast) Fast_Autocorrelation (s, L_ACF );
327 Autocorrelation (s, L_ACF );
328 Reflection_coefficients (L_ACF, LARc );
329 Transformation_to_Log_Area_Ratios (LARc);
330 Quantization_and_coding (LARc);