1 /* Copyright (C) 2002 Jean-Marc Valin
3 Various analysis/synthesis filters
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
9 - Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
12 - Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
16 - Neither the name of the Xiph.org Foundation nor the names of its
17 contributors may be used to endorse or promote products derived from
18 this software without specific prior written permission.
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
24 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "stack_alloc.h"
38 void bw_lpc(float gamma
, float *lpc_in
, float *lpc_out
, int order
)
42 for (i
=0;i
<order
+1;i
++)
44 lpc_out
[i
] = tmp
* lpc_in
[i
];
50 #include "filters_sse.h"
52 void filter_mem2(float *x
, float *num
, float *den
, float *y
, int N
, int ord
, float *mem
)
59 y
[i
] = num
[0]*xi
+ mem
[0];
63 mem
[j
] = mem
[j
+1] + num
[j
+1]*xi
- den
[j
+1]*yi
;
65 mem
[ord
-1] = num
[ord
]*xi
- den
[ord
]*yi
;
70 void iir_mem2(float *x
, float *den
, float *y
, int N
, int ord
, float *mem
)
78 mem
[j
] = mem
[j
+1] - den
[j
+1]*y
[i
];
80 mem
[ord
-1] = - den
[ord
]*y
[i
];
85 void fir_mem2(float *x
, float *num
, float *y
, int N
, int ord
, float *mem
)
92 y
[i
] = num
[0]*xi
+ mem
[0];
95 mem
[j
] = mem
[j
+1] + num
[j
+1]*xi
;
97 mem
[ord
-1] = num
[ord
]*xi
;
101 void syn_percep_zero(float *xx
, float *ak
, float *awk1
, float *awk2
, float *y
, int N
, int ord
, char *stack
)
104 float *mem
= PUSH(stack
,ord
, float);
107 filter_mem2(xx
, awk1
, ak
, y
, N
, ord
, mem
);
110 iir_mem2(y
, awk2
, y
, N
, ord
, mem
);
113 void residue_percep_zero(float *xx
, float *ak
, float *awk1
, float *awk2
, float *y
, int N
, int ord
, char *stack
)
116 float *mem
= PUSH(stack
,ord
, float);
119 filter_mem2(xx
, ak
, awk1
, y
, N
, ord
, mem
);
122 fir_mem2(y
, awk2
, y
, N
, ord
, mem
);
126 void qmf_decomp(float *xx
, float *aa
, float *y1
, float *y2
, int N
, int M
, float *mem
, char *stack
)
133 a
= PUSH(stack
, M
, float);
134 x
= PUSH(stack
, N
+M
-1, float);
143 for (i
=0,k
=0;i
<N
;i
+=2,k
++)
149 y1
[k
]+=a
[j
]*(x
[i
+j
]+x2
[i
-j
]);
150 y2
[k
]-=a
[j
]*(x
[i
+j
]-x2
[i
-j
]);
152 y1
[k
]+=a
[j
]*(x
[i
+j
]+x2
[i
-j
]);
153 y2
[k
]+=a
[j
]*(x
[i
+j
]-x2
[i
-j
]);
161 void fir_mem_up(float *x
, float *a
, float *y
, int N
, int M
, float *mem
, char *stack
)
163 all odd x[i] are zero -- well, actually they are left out of the array now
164 N and M are multiples of 4 */
167 float *xx
=PUSH(stack
, M
+N
-1, float);
169 for (i
= 0; i
< N
/2; i
++)
170 xx
[2*i
] = x
[N
/2-1-i
];
171 for (i
= 0; i
< M
- 1; i
+= 2)
174 for (i
= 0; i
< N
; i
+= 4) {
175 float y0
, y1
, y2
, y3
;
178 y0
= y1
= y2
= y3
= 0.f
;
181 for (j
= 0; j
< M
; j
+= 4) {
209 for (i
= 0; i
< M
- 1; i
+= 2)
214 void comp_filter_mem_init (CombFilterMem
*mem
)
217 mem
->last_pitch_gain
[0]=mem
->last_pitch_gain
[1]=mem
->last_pitch_gain
[2]=0;
222 float *exc
, /*decoded excitation*/
223 float *new_exc
, /*enhanced excitation*/
224 float *ak
, /*LPC filter coefs*/
226 int nsf
, /*sub-frame size*/
227 int pitch
, /*pitch period*/
228 float *pitch_gain
, /*pitch gain (3-tap)*/
229 float comb_gain
, /*gain of comb filter*/
234 float exc_energy
=0, new_exc_energy
=0;
238 /*Compute excitation energy prior to enhancement*/
240 exc_energy
+=exc
[i
]*exc
[i
];
242 /*Some gain adjustment is pitch is too high or if unvoiced*/
245 g
= .5*fabs(pitch_gain
[0]+pitch_gain
[1]+pitch_gain
[2] +
246 mem
->last_pitch_gain
[0] + mem
->last_pitch_gain
[1] + mem
->last_pitch_gain
[2]);
254 /*Apply pitch comb-filter (filter out noise between pitch harmonics)*/
259 new_exc
[i
] = exc
[i
] + comb_gain
* fact
* (
260 pitch_gain
[0]*exc
[i
-pitch
+1] +
261 pitch_gain
[1]*exc
[i
-pitch
] +
262 pitch_gain
[2]*exc
[i
-pitch
-1]
264 + comb_gain
* (1-fact
) * (
265 mem
->last_pitch_gain
[0]*exc
[i
-mem
->last_pitch
+1] +
266 mem
->last_pitch_gain
[1]*exc
[i
-mem
->last_pitch
] +
267 mem
->last_pitch_gain
[2]*exc
[i
-mem
->last_pitch
-1]
271 mem
->last_pitch_gain
[0] = pitch_gain
[0];
272 mem
->last_pitch_gain
[1] = pitch_gain
[1];
273 mem
->last_pitch_gain
[2] = pitch_gain
[2];
274 mem
->last_pitch
= pitch
;
276 /*Gain after enhancement*/
278 new_exc_energy
+=new_exc
[i
]*new_exc
[i
];
280 /*Compute scaling factor and normalize energy*/
281 gain
= sqrt(exc_energy
)/sqrt(.1+new_exc_energy
);
289 mem
->smooth_gain
= .96*mem
->smooth_gain
+ .04*gain
;
290 new_exc
[i
] *= mem
->smooth_gain
;