blob: 8eb12d54c87339f279cd8637059ddc1fbcf294ea [file] [log] [blame]
Jean-Marc Valine14fe902009-12-11 00:07:31 -05001
Jean-Marc Valind69c1cb2009-12-28 00:34:29 -05002#ifndef NEW_PLC
3#define NEW_PLC
4#endif
Jean-Marc Valine14fe902009-12-11 00:07:31 -05005
Jean-Marc Valind69c1cb2009-12-28 00:34:29 -05006float _celt_lpc(
7 float *lpc, /* out: [0...p-1] LPC coefficients */
8const float *ac, /* in: [0...p] autocorrelation values */
Jean-Marc Valine14fe902009-12-11 00:07:31 -05009int p
10)
11{
12 int i, j;
Jean-Marc Valind69c1cb2009-12-28 00:34:29 -050013 float r;
14 float error = ac[0];
Jean-Marc Valine14fe902009-12-11 00:07:31 -050015
16 if (ac[0] == 0)
17 {
18 for (i = 0; i < p; i++)
19 lpc[i] = 0;
20 return 0;
21 }
22
23 for (i = 0; i < p; i++) {
24
25 /* Sum up this iteration's reflection coefficient */
Jean-Marc Valind69c1cb2009-12-28 00:34:29 -050026 float rr = -ac[i + 1];
Jean-Marc Valine14fe902009-12-11 00:07:31 -050027 for (j = 0; j < i; j++)
Jean-Marc Valind69c1cb2009-12-28 00:34:29 -050028 rr = rr - lpc[j]*ac[i - j];
Jean-Marc Valin5a0fae52009-12-14 21:19:37 -050029 r = rr/(error+1e-15);
Jean-Marc Valine14fe902009-12-11 00:07:31 -050030 /* Update LPC coefficients and total error */
31 lpc[i] = r;
32 for (j = 0; j < i>>1; j++)
33 {
Jean-Marc Valind69c1cb2009-12-28 00:34:29 -050034 float tmp = lpc[j];
35 lpc[j] = lpc[j ] + r*lpc[i-1-j];
36 lpc[i-1-j] = lpc[i-1-j] + r*tmp;
Jean-Marc Valine14fe902009-12-11 00:07:31 -050037 }
38 if (i & 1)
Jean-Marc Valind69c1cb2009-12-28 00:34:29 -050039 lpc[j] = lpc[j] + lpc[j]*r;
Jean-Marc Valine14fe902009-12-11 00:07:31 -050040
Jean-Marc Valind69c1cb2009-12-28 00:34:29 -050041 error = error - r*r*error;
Jean-Marc Valin5a0fae52009-12-14 21:19:37 -050042 if (error<.00001*ac[0])
43 break;
Jean-Marc Valine14fe902009-12-11 00:07:31 -050044 }
45 return error;
46}
47
Jean-Marc Valind69c1cb2009-12-28 00:34:29 -050048void fir(const float *x,
49 const float *num,
50 float *y,
Jean-Marc Valine14fe902009-12-11 00:07:31 -050051 int N,
52 int ord,
Jean-Marc Valind69c1cb2009-12-28 00:34:29 -050053 float *mem)
Jean-Marc Valine14fe902009-12-11 00:07:31 -050054{
55 int i,j;
56
57 for (i=0;i<N;i++)
58 {
59 float sum = x[i];
60 for (j=0;j<ord;j++)
61 {
62 sum += num[j]*mem[j];
63 }
64 for (j=ord-1;j>=1;j--)
65 {
66 mem[j]=mem[j-1];
67 }
68 mem[0] = x[i];
69 y[i] = sum;
70 }
71}
72
Jean-Marc Valind69c1cb2009-12-28 00:34:29 -050073void iir(const celt_word32 *x,
74 const float *den,
75 celt_word32 *y,
Jean-Marc Valine14fe902009-12-11 00:07:31 -050076 int N,
77 int ord,
Jean-Marc Valind69c1cb2009-12-28 00:34:29 -050078 float *mem)
Jean-Marc Valine14fe902009-12-11 00:07:31 -050079{
80 int i,j;
81 for (i=0;i<N;i++)
82 {
83 float sum = x[i];
84 for (j=0;j<ord;j++)
85 {
86 sum -= den[j]*mem[j];
87 }
88 for (j=ord-1;j>=1;j--)
89 {
90 mem[j]=mem[j-1];
91 }
92 mem[0] = sum;
93 y[i] = sum;
94 }
95}
96
97void _celt_autocorr(
Jean-Marc Valind69c1cb2009-12-28 00:34:29 -050098 const float *x, /* in: [0...n-1] samples x */
Jean-Marc Valine14fe902009-12-11 00:07:31 -050099 float *ac, /* out: [0...lag-1] ac values */
Jean-Marc Valind69c1cb2009-12-28 00:34:29 -0500100 const celt_word16 *window,
Jean-Marc Valine14fe902009-12-11 00:07:31 -0500101 int overlap,
102 int lag,
103 int n
104 )
105{
106 float d;
107 int i;
Thorvald Natvigb8002a02009-12-11 13:19:09 +0100108 VARDECL(float, xx);
109 SAVE_STACK;
110 ALLOC(xx, n, float);
Jean-Marc Valine14fe902009-12-11 00:07:31 -0500111 for (i=0;i<n;i++)
112 xx[i] = x[i];
113 for (i=0;i<overlap;i++)
114 {
Jean-Marc Valind69c1cb2009-12-28 00:34:29 -0500115 xx[i] *= (1./Q15ONE)*window[i];
116 xx[n-i-1] *= (1./Q15ONE)*window[i];
Jean-Marc Valine14fe902009-12-11 00:07:31 -0500117 }
118 while (lag>=0)
119 {
120 for (i = lag, d = 0; i < n; i++)
121 d += x[i] * x[i-lag];
122 ac[lag] = d;
123 lag--;
124 }
125 ac[0] += 10;
Thorvald Natvigb8002a02009-12-11 13:19:09 +0100126 RESTORE_STACK;
Jean-Marc Valine14fe902009-12-11 00:07:31 -0500127}