1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
18 /****************************************************************************************
19 Portions of this file are derived from the following 3GPP standard:
22 ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec
23 Available from http://www.3gpp.org
25 (C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC)
26 Permission to distribute, modify and use this file under the standard license
27 terms listed above has been obtained from the copyright holder.
28 ****************************************************************************************/
30 ------------------------------------------------------------------------------
34 Filename: autocorr.cpp
36 ----------------------------------------------------------------------------*/
38 /*----------------------------------------------------------------------------
40 ----------------------------------------------------------------------------*/
47 /*----------------------------------------------------------------------------
49 ; Define module specific macros here
50 ----------------------------------------------------------------------------*/
53 /*----------------------------------------------------------------------------
55 ; Include all pre-processor statements here. Include conditional
56 ; compile variables also.
57 ----------------------------------------------------------------------------*/
60 /*----------------------------------------------------------------------------
61 ; LOCAL FUNCTION DEFINITIONS
62 ; Function Prototype declaration
63 ----------------------------------------------------------------------------*/
65 /*----------------------------------------------------------------------------
66 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
67 ; Variable declaration - defined here and used outside this module
68 ----------------------------------------------------------------------------*/
70 /*----------------------------------------------------------------------------
71 ; EXTERNAL FUNCTION REFERENCES
72 ; Declare functions defined elsewhere and referenced in this module
73 ----------------------------------------------------------------------------*/
76 /*----------------------------------------------------------------------------
77 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
78 ; Declare variables used in this module but defined elsewhere
79 ----------------------------------------------------------------------------*/
82 ------------------------------------------------------------------------------
83 FUNCTION NAME: Autocorr
84 ----------------------------------------------------------------------------
85 INPUT AND OUTPUT DEFINITIONS
88 x = buffer of input signals of type Word16
89 m = LPC order of type Word16
90 wind = buffer of window signals of type Word16
91 r_h = buffer containing the high word of the autocorrelation values
93 r_l = buffer containing the low word of the autocorrelation values
96 pOverflow = pointer to variable of type Flag *, which indicates if
100 r_h buffer contains the high word of the new autocorrelation values
101 r_l buffer contains the low word of the new autocorrelation values
102 pOverflow -> 1 if overflow occurs.
105 norm = normalized autocorrelation at lag zero of type Word16
107 Global Variables Used:
110 Local Variables Needed:
113 ------------------------------------------------------------------------------
116 This function windows the input signal with the provided window
117 then calculates the autocorrelation values for lags of 0,1,...m,
118 where m is the passed in LPC order.
120 ------------------------------------------------------------------------------
125 ------------------------------------------------------------------------------
128 autocorr.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
130 ------------------------------------------------------------------------------
134 Word16 x[], // (i) : Input signal (L_WINDOW)
135 Word16 m, // (i) : LPC order
136 Word16 r_h[], // (o) : Autocorrelations (msb)
137 Word16 r_l[], // (o) : Autocorrelations (lsb)
138 const Word16 wind[] // (i) : window for LPC analysis (L_WINDOW)
144 Word16 overfl, overfl_shft;
146 // Windowing of signal
148 for (i = 0; i < L_WINDOW; i++)
150 y[i] = mult_r (x[i], wind[i]);
153 // Compute r[0] and test for overflow
162 for (i = 0; i < L_WINDOW; i++)
164 sum = L_mac (sum, y[i], y[i]);
167 // If overflow divide y[] by 4
169 if (L_sub (sum, MAX_32) == 0L)
171 overfl_shft = add (overfl_shft, 4);
172 overfl = 1; // Set the overflow flag
174 for (i = 0; i < L_WINDOW; i++)
176 y[i] = shr (y[i], 2);
182 sum = L_add (sum, 1L); // Avoid the case of all zeros
184 // Normalization of r[0]
187 sum = L_shl (sum, norm);
188 L_Extract (sum, &r_h[0], &r_l[0]); // Put in DPF format (see oper_32b)
192 for (i = 1; i <= m; i++)
196 for (j = 0; j < L_WINDOW - i; j++)
198 sum = L_mac (sum, y[j], y[j + i]);
201 sum = L_shl (sum, norm);
202 L_Extract (sum, &r_h[i], &r_l[i]);
205 norm = sub (norm, overfl_shft);
211 ------------------------------------------------------------------------------
213 [State any special notes, constraints or cautions for users of this function]
215 ------------------------------------------------------------------------------
219 Word16 x[], /* (i) : Input signal (L_WINDOW) */
220 Word16 m, /* (i) : LPC order */
221 Word16 r_h[], /* (o) : Autocorrelations (msb) */
222 Word16 r_l[], /* (o) : Autocorrelations (lsb) */
223 const Word16 wind[], /* (i) : window for LPC analysis (L_WINDOW) */
224 Flag *pOverflow /* (o) : indicates overflow */
229 register Word16 norm;
236 /* Added for optimization */
246 const Word16 *p_wind;
251 * Windowing of the signal
254 OSCL_UNUSED_ARG(pOverflow);
259 for (i = L_WINDOW; i != 0; i--)
261 temp = (Word16)((amrnb_fxp_mac_16_by_16bb((Word32) * (p_x++), (Word32) * (p_wind++), 0x04000)) >> 15);
264 sum += ((Word32)temp * temp) << 1;
268 * if oveflow exist, then stop accumulation
276 * if oveflow existed, complete windowing operation
277 * without computing energy
282 p_y = &y[L_WINDOW-i];
283 p_x = &x[L_WINDOW-i];
284 p_wind = &wind[L_WINDOW-i];
288 temp = (Word16)((amrnb_fxp_mac_16_by_16bb((Word32) * (p_x++), (Word32) * (p_wind++), 0x04000)) >> 15);
295 * Compute r[0] and test for overflow
301 * scale down by 1/4 only when needed
305 /* If overflow divide y[] by 4 */
306 /* FYI: For better resolution, we could */
307 /* divide y[] by 2 */
312 for (i = (L_WINDOW >> 1); i != 0 ; i--)
316 sum += ((Word32)temp * temp) << 1;
319 sum += ((Word32)temp * temp) << 1;
328 sum += 1L; /* Avoid the case of all zeros */
330 /* Normalization of r[0] */
336 /* Put in DPF format (see oper_32b) */
337 r_h[0] = (Word16)(sum >> 16);
338 r_l[0] = (Word16)((sum >> 1) - ((Word32)(r_h[0]) << 15));
342 p_y_ref = &y[L_WINDOW - 1 ];
346 for (i = m; i > 0; i--)
350 p_y = &y[L_WINDOW - i - 1];
353 for (j = (L_WINDOW - i - 1) >> 1; j != 0; j--)
355 sum = amrnb_fxp_mac_16_by_16bb((Word32) * (p_y--), (Word32) * (p_y_1--), sum);
356 sum = amrnb_fxp_mac_16_by_16bb((Word32) * (p_y--), (Word32) * (p_y_1--), sum);
359 sum = amrnb_fxp_mac_16_by_16bb((Word32) * (p_y--), (Word32) * (p_y_1--), sum);
361 if (((L_WINDOW - i - 1) & 1))
363 sum = amrnb_fxp_mac_16_by_16bb((Word32) * (p_y--), (Word32) * (p_y_1--), sum);
368 *(p_rh) = (Word16)(sum >> 16);
369 *(p_rl--) = (Word16)((sum >> 1) - ((Word32) * (p_rh--) << 15));