1 /* Copyright (c) 2009-2010 Xiph.Org Foundation
2 Written by Jean-Marc Valin */
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
11 - Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
15 - Neither the name of the Xiph.org Foundation nor the names of its
16 contributors may be used to endorse or promote products derived from
17 this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
23 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include "stack_alloc.h"
44 celt_word16 *_lpc, /* out: [0...p-1] LPC coefficients */
45 const celt_word32 *ac, /* in: [0...p] autocorrelation values */
51 celt_word32 error = ac[0];
53 celt_word32 lpc[LPC_ORDER];
58 for (i = 0; i < p; i++)
62 for (i = 0; i < p; i++) {
63 /* Sum up this iteration's reflection coefficient */
65 for (j = 0; j < i; j++)
66 rr += MULT32_32_Q31(lpc[j],ac[i - j]);
67 rr += SHR32(ac[i + 1],3);
68 r = -frac_div32(SHL32(rr,3), error);
69 /* Update LPC coefficients and total error */
71 for (j = 0; j < (i+1)>>1; j++)
73 celt_word32 tmp1, tmp2;
76 lpc[j] = tmp1 + MULT32_32_Q31(r,tmp2);
77 lpc[i-1-j] = tmp2 + MULT32_32_Q31(r,tmp1);
80 error = error - MULT32_32_Q31(MULT32_32_Q31(r,r),error);
81 /* Bail out once we get 30 dB gain */
83 if (error<SHR32(ac[0],10))
86 if (error<.001f*ac[0])
93 _lpc[i] = ROUND16(lpc[i],16);
97 void fir(const celt_word16 *x,
98 const celt_word16 *num,
108 celt_word32 sum = SHL32(EXTEND32(x[i]), SIG_SHIFT);
111 sum += MULT16_16(num[j],mem[j]);
113 for (j=ord-1;j>=1;j--)
118 y[i] = ROUND16(sum, SIG_SHIFT);
122 void iir(const celt_word32 *x,
123 const celt_word16 *den,
132 celt_word32 sum = x[i];
135 sum -= MULT16_16(den[j],mem[j]);
137 for (j=ord-1;j>=1;j--)
141 mem[0] = ROUND16(sum,SIG_SHIFT);
147 const celt_word16 *x, /* in: [0...n-1] samples x */
148 celt_word32 *ac, /* out: [0...lag-1] ac values */
149 const celt_word16 *window,
157 VARDECL(celt_word16, xx);
159 ALLOC(xx, n, celt_word16);
162 for (i=0;i<overlap;i++)
164 xx[i] = MULT16_16_Q15(x[i],window[i]);
165 xx[n-i-1] = MULT16_16_Q15(x[n-i-1],window[i]);
172 ac0 += SHR32(MULT16_16(xx[i],xx[i]),8);
175 shift = celt_ilog2(ac0)-30+9;
178 xx[i] = VSHR32(xx[i], shift);
183 for (i = lag, d = 0; i < n; i++)
184 d += xx[i] * xx[i-lag];
186 /*printf ("%f ", ac[lag]);*/