1 /*
2 Copyright 1992, 1993, 1994 by Jutta Degener and Carsten Bormann,
3 Technische Universitaet Berlin
4
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.
10
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.
14
15 Berlin, 28.11.1994
16 Jutta Degener
17 Carsten Bormann
18
19
20 Code modified by Jean-Marc Valin
21
22 Speex License:
23
24 Redistribution and use in source and binary forms, with or without
25 modification, are permitted provided that the following conditions
26 are met:
27
28 - Redistributions of source code must retain the above copyright
29 notice, this list of conditions and the following disclaimer.
30
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.
34
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.
38
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.
50 */
51
52 #ifdef HAVE_CONFIG_H
53 #include "config.h"
54 #endif
55
56
57
58 /* LPC analysis
59 *
60 * The next two functions calculate linear prediction coefficients
61 * and/or the related reflection coefficients from the first P_MAX+1
62 * values of the autocorrelation function.
63 */
64
65 /* Invented by N. Levinson in 1947, modified by J. Durbin in 1959.
66 */
67
68 #include "lpc.h"
69
70 /* returns minimum mean square error */
_spx_lpc(spx_coef_t * lpc,const spx_word16_t * ac,int p)71 spx_word32_t _spx_lpc(
72 spx_coef_t *lpc, /* out: [0...p-1] LPC coefficients */
73 const spx_word16_t *ac, /* in: [0...p] autocorrelation values */
74 int p
75 )
76 {
77 int i, j;
78 spx_word16_t r;
79 spx_word16_t error = ac[0];
80
81 if (ac[0] == 0)
82 {
83 for (i = 0; i < p; i++)
84 lpc[i] = 0;
85 return 0;
86 }
87
88 for (i = 0; i < p; i++) {
89
90 /* Sum up this iteration's reflection coefficient */
91 spx_word32_t rr = NEG32(SHL32(EXTEND32(ac[i + 1]),13));
92 for (j = 0; j < i; j++)
93 rr = SUB32(rr,MULT16_16(lpc[j],ac[i - j]));
94 #ifdef FIXED_POINT
95 r = DIV32_16(rr,ADD16(error,16));
96 #else
97 r = rr/(error+.003*ac[0]);
98 #endif
99 /* Update LPC coefficients and total error */
100 lpc[i] = r;
101 for (j = 0; j < i>>1; j++)
102 {
103 spx_word16_t tmp = lpc[j];
104 lpc[j] = MAC16_16_Q13(lpc[j],r,lpc[i-1-j]);
105 lpc[i-1-j] = MAC16_16_Q13(lpc[i-1-j],r,tmp);
106 }
107 if (i & 1)
108 lpc[j] = MAC16_16_Q13(lpc[j],lpc[j],r);
109
110 error = SUB16(error,MULT16_16_Q13(r,MULT16_16_Q13(error,r)));
111 }
112 return error;
113 }
114
115
116 #ifdef FIXED_POINT
117
118 /* Compute the autocorrelation
119 * ,--,
120 * ac(i) = > x(n) * x(n-i) for all n
121 * `--'
122 * for lags between 0 and lag-1, and x == 0 outside 0...n-1
123 */
124
_spx_autocorr(const spx_word16_t * x,spx_word16_t * ac,int lag,int n)125 void _spx_autocorr(
126 const spx_word16_t *x, /* in: [0...n-1] samples x */
127 spx_word16_t *ac, /* out: [0...lag-1] ac values */
128 int lag,
129 int n
130 )
131 {
132 spx_word32_t d;
133 int i, j;
134 spx_word32_t ac0=1;
135 int shift, ac_shift;
136
137 for (j=0;j<n;j++)
138 ac0 = ADD32(ac0,SHR32(MULT16_16(x[j],x[j]),8));
139 ac0 = ADD32(ac0,n);
140 shift = 8;
141 while (shift && ac0<0x40000000)
142 {
143 shift--;
144 ac0 <<= 1;
145 }
146 ac_shift = 18;
147 while (ac_shift && ac0<0x40000000)
148 {
149 ac_shift--;
150 ac0 <<= 1;
151 }
152
153
154 for (i=0;i<lag;i++)
155 {
156 d=0;
157 for (j=i;j<n;j++)
158 {
159 d = ADD32(d,SHR32(MULT16_16(x[j],x[j-i]), shift));
160 }
161
162 ac[i] = SHR32(d, ac_shift);
163 }
164 }
165
166
167 #else
168
169
170
171 /* Compute the autocorrelation
172 * ,--,
173 * ac(i) = > x(n) * x(n-i) for all n
174 * `--'
175 * for lags between 0 and lag-1, and x == 0 outside 0...n-1
176 */
_spx_autocorr(const spx_word16_t * x,float * ac,int lag,int n)177 void _spx_autocorr(
178 const spx_word16_t *x, /* in: [0...n-1] samples x */
179 float *ac, /* out: [0...lag-1] ac values */
180 int lag,
181 int n
182 )
183 {
184 float d;
185 int i;
186 while (lag--)
187 {
188 for (i = lag, d = 0; i < n; i++)
189 d += x[i] * x[i-lag];
190 ac[lag] = d;
191 }
192 ac[0] += 10;
193 }
194
195 #endif
196
197
198