Fix yellow.
[kugel-rb.git] / apps / codecs / libspeex / lpc.c
blob9845633a6c036b19d2d4acc3c179fda3af488862
1 /*
2 Copyright 1992, 1993, 1994 by Jutta Degener and Carsten Bormann,
3 Technische Universitaet Berlin
5 Any use of this software is permitted provided that this notice is not
6 removed and that neither the authors nor the Technische Universitaet Berlin
7 are deemed to have made any representations as to the suitability of this
8 software for any purpose nor are held responsible for any defects of
9 this software. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
11 As a matter of courtesy, the authors request to be informed about uses
12 this software has found, about bugs in this software, and about any
13 improvements that may be of general interest.
15 Berlin, 28.11.1994
16 Jutta Degener
17 Carsten Bormann
20 Code modified by Jean-Marc Valin
22 Speex License:
24 Redistribution and use in source and binary forms, with or without
25 modification, are permitted provided that the following conditions
26 are met:
28 - Redistributions of source code must retain the above copyright
29 notice, this list of conditions and the following disclaimer.
31 - Redistributions in binary form must reproduce the above copyright
32 notice, this list of conditions and the following disclaimer in the
33 documentation and/or other materials provided with the distribution.
35 - Neither the name of the Xiph.org Foundation nor the names of its
36 contributors may be used to endorse or promote products derived from
37 this software without specific prior written permission.
39 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
40 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
41 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
42 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
43 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
45 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
46 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
47 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
48 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
49 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 #ifdef HAVE_CONFIG_H
53 #include "config-speex.h"
54 #endif
56 #include "lpc.h"
58 #ifdef BFIN_ASM
59 #include "lpc_bfin.h"
60 #endif
62 /* LPC analysis
64 * The next two functions calculate linear prediction coefficients
65 * and/or the related reflection coefficients from the first P_MAX+1
66 * values of the autocorrelation function.
69 /* Invented by N. Levinson in 1947, modified by J. Durbin in 1959.
72 /* returns minimum mean square error */
73 spx_word32_t _spx_lpc(
74 spx_coef_t *lpc, /* out: [0...p-1] LPC coefficients */
75 const spx_word16_t *ac, /* in: [0...p] autocorrelation values */
76 int p
79 int i, j;
80 spx_word16_t r;
81 spx_word16_t error = ac[0];
83 if (ac[0] == 0)
85 for (i = 0; i < p; i++)
86 lpc[i] = 0;
87 return 0;
90 for (i = 0; i < p; i++) {
92 /* Sum up this iteration's reflection coefficient */
93 spx_word32_t rr = NEG32(SHL32(EXTEND32(ac[i + 1]),13));
94 for (j = 0; j < i; j++)
95 rr = SUB32(rr,MULT16_16(lpc[j],ac[i - j]));
96 #ifdef FIXED_POINT
97 r = DIV32_16(rr+PSHR32(error,1),ADD16(error,8));
98 #else
99 r = rr/(error+.003*ac[0]);
100 #endif
101 /* Update LPC coefficients and total error */
102 lpc[i] = r;
103 for (j = 0; j < i>>1; j++)
105 spx_word16_t tmp = lpc[j];
106 lpc[j] = MAC16_16_P13(lpc[j],r,lpc[i-1-j]);
107 lpc[i-1-j] = MAC16_16_P13(lpc[i-1-j],r,tmp);
109 if (i & 1)
110 lpc[j] = MAC16_16_P13(lpc[j],lpc[j],r);
112 error = SUB16(error,MULT16_16_Q13(r,MULT16_16_Q13(error,r)));
114 return error;
118 #ifdef FIXED_POINT
120 /* Compute the autocorrelation
121 * ,--,
122 * ac(i) = > x(n) * x(n-i) for all n
123 * `--'
124 * for lags between 0 and lag-1, and x == 0 outside 0...n-1
127 #ifndef OVERRIDE_SPEEX_AUTOCORR
128 void _spx_autocorr(
129 const spx_word16_t *x, /* in: [0...n-1] samples x */
130 spx_word16_t *ac, /* out: [0...lag-1] ac values */
131 int lag,
132 int n
135 spx_word32_t d;
136 int i, j;
137 spx_word32_t ac0=1;
138 int shift, ac_shift;
140 for (j=0;j<n;j++)
141 ac0 = ADD32(ac0,SHR32(MULT16_16(x[j],x[j]),8));
142 ac0 = ADD32(ac0,n);
143 shift = 8;
144 while (shift && ac0<0x40000000)
146 shift--;
147 ac0 <<= 1;
149 ac_shift = 18;
150 while (ac_shift && ac0<0x40000000)
152 ac_shift--;
153 ac0 <<= 1;
157 for (i=0;i<lag;i++)
159 d=0;
160 for (j=i;j<n;j++)
162 d = ADD32(d,SHR32(MULT16_16(x[j],x[j-i]), shift));
165 ac[i] = SHR32(d, ac_shift);
168 #endif
171 #else
175 /* Compute the autocorrelation
176 * ,--,
177 * ac(i) = > x(n) * x(n-i) for all n
178 * `--'
179 * for lags between 0 and lag-1, and x == 0 outside 0...n-1
181 void _spx_autocorr(
182 const spx_word16_t *x, /* in: [0...n-1] samples x */
183 float *ac, /* out: [0...lag-1] ac values */
184 int lag,
185 int n
188 float d;
189 int i;
190 while (lag--)
192 for (i = lag, d = 0; i < n; i++)
193 d += x[i] * x[i-lag];
194 ac[lag] = d;
196 ac[0] += 10;
199 #endif