-
Notifications
You must be signed in to change notification settings - Fork 3
/
postfilt.c
454 lines (354 loc) · 13.9 KB
/
postfilt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
ITU-T G.729A Speech Coder with Annex B ANSI-C Source Code
Version 1.3 Last modified: August 1997
Copyright (c) 1996,
AT&T, France Telecom, NTT, Universite de Sherbrooke, Lucent Technologies,
Rockwell International
All rights reserved.
*/
/*------------------------------------------------------------------------*
* POSTFILTER.C *
*------------------------------------------------------------------------*
* Performs adaptive postfiltering on the synthesis speech *
* This file contains all functions related to the post filter. *
*------------------------------------------------------------------------*/
#include "typedef.h"
#include "basic_op.h"
#include "ld8a.h"
#include "oper_32b.h"
/*---------------------------------------------------------------*
* Postfilter constant parameters (defined in "ld8a.h") *
*---------------------------------------------------------------*
* L_FRAME : Frame size. *
* L_SUBFR : Sub-frame size. *
* M : LPC order. *
* MP1 : LPC order+1 *
* PIT_MAX : Maximum pitch lag. *
* GAMMA2_PST : Formant postfiltering factor (numerator) *
* GAMMA1_PST : Formant postfiltering factor (denominator) *
* GAMMAP : Harmonic postfiltering factor *
* MU : Factor for tilt compensation filter *
* AGC_FAC : Factor for automatic gain control *
*---------------------------------------------------------------*/
/*------------------------------------------------------------*
* static vectors *
*------------------------------------------------------------*/
/* inverse filtered synthesis (with A(z/GAMMA2_PST)) */
static int16_t res2_buf[PIT_MAX+L_SUBFR];
static int16_t *res2;
static int16_t scal_res2_buf[PIT_MAX+L_SUBFR];
static int16_t *scal_res2;
/* memory of filter 1/A(z/GAMMA1_PST) */
static int16_t mem_syn_pst[M];
/*---------------------------------------------------------------*
* Procedure Init_Post_Filter: *
* ~~~~~~~~~~~~~~~~ *
* Initializes the postfilter parameters: *
*---------------------------------------------------------------*/
void Init_Post_Filter(void)
{
res2 = res2_buf + PIT_MAX;
scal_res2 = scal_res2_buf + PIT_MAX;
Set_zero(mem_syn_pst, M);
Set_zero(res2_buf, PIT_MAX+L_SUBFR);
Set_zero(scal_res2_buf, PIT_MAX+L_SUBFR);
return;
}
/*------------------------------------------------------------------------*
* Procedure Post_Filter: *
* ~~~~~~~~~~~ *
*------------------------------------------------------------------------*
* The postfiltering process is described as follows: *
* *
* - inverse filtering of syn[] through A(z/GAMMA2_PST) to get res2[] *
* - use res2[] to compute pitch parameters *
* - perform pitch postfiltering *
* - tilt compensation filtering; 1 - MU*k*z^-1 *
* - synthesis filtering through 1/A(z/GAMMA1_PST) *
* - adaptive gain control *
*------------------------------------------------------------------------*/
void Post_Filter(
int16_t *syn, /* in/out: synthesis speech (postfiltered is output) */
int16_t *Az_4, /* input : interpolated LPC parameters in all subframes */
int16_t *T, /* input : decoded pitch lags in all subframes */
int16_t Vad
)
{
/*-------------------------------------------------------------------*
* Declaration of parameters *
*-------------------------------------------------------------------*/
int16_t res2_pst[L_SUBFR]; /* res2[] after pitch postfiltering */
int16_t syn_pst[L_FRAME]; /* post filtered synthesis speech */
int16_t Ap3[MP1], Ap4[MP1]; /* bandwidth expanded LP parameters */
int16_t *Az; /* pointer to Az_4: */
/* LPC parameters in each subframe */
int16_t t0_max, t0_min; /* closed-loop pitch search range */
int16_t i_subfr; /* index for beginning of subframe */
int16_t h[L_H];
int16_t i, j;
int16_t temp1, temp2;
int32_t L_tmp;
/*-----------------------------------------------------*
* Post filtering *
*-----------------------------------------------------*/
Az = Az_4;
for (i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR)
{
/* Find pitch range t0_min - t0_max */
t0_min = sub(*T++, 3);
t0_max = add(t0_min, 6);
if (sub(t0_max, PIT_MAX) > 0) {
t0_max = PIT_MAX;
t0_min = sub(t0_max, 6);
}
/* Find weighted filter coefficients Ap3[] and ap[4] */
Weight_Az(Az, GAMMA2_PST, M, Ap3);
Weight_Az(Az, GAMMA1_PST, M, Ap4);
/* filtering of synthesis speech by A(z/GAMMA2_PST) to find res2[] */
Residu(Ap3, &syn[i_subfr], res2, L_SUBFR);
/* scaling of "res2[]" to avoid energy overflow */
for (j=0; j<L_SUBFR; j++)
{
scal_res2[j] = shr(res2[j], 2);
}
/* pitch postfiltering */
if (sub(Vad, 1) == 0)
pit_pst_filt(res2, scal_res2, t0_min, t0_max, L_SUBFR, res2_pst);
else
for (j=0; j<L_SUBFR; j++)
res2_pst[j] = res2[j];
/* tilt compensation filter */
/* impulse response of A(z/GAMMA2_PST)/A(z/GAMMA1_PST) */
Copy(Ap3, h, M+1);
Set_zero(&h[M+1], L_H-M-1);
Syn_filt(Ap4, h, h, L_H, &h[M+1], 0);
/* 1st correlation of h[] */
L_tmp = L_mult(h[0], h[0]);
for (i=1; i<L_H; i++) L_tmp = L_mac(L_tmp, h[i], h[i]);
temp1 = extract_h(L_tmp);
L_tmp = L_mult(h[0], h[1]);
for (i=1; i<L_H-1; i++) L_tmp = L_mac(L_tmp, h[i], h[i+1]);
temp2 = extract_h(L_tmp);
if(temp2 <= 0) {
temp2 = 0;
}
else {
temp2 = mult(temp2, MU);
temp2 = div_s(temp2, temp1);
}
preemphasis(res2_pst, temp2, L_SUBFR);
/* filtering through 1/A(z/GAMMA1_PST) */
Syn_filt(Ap4, res2_pst, &syn_pst[i_subfr], L_SUBFR, mem_syn_pst, 1);
/* scale output to input */
agc(&syn[i_subfr], &syn_pst[i_subfr], L_SUBFR);
/* update res2[] buffer; shift by L_SUBFR */
Copy(&res2[L_SUBFR-PIT_MAX], &res2[-PIT_MAX], PIT_MAX);
Copy(&scal_res2[L_SUBFR-PIT_MAX], &scal_res2[-PIT_MAX], PIT_MAX);
Az += MP1;
}
/* update syn[] buffer */
Copy(&syn[L_FRAME-M], &syn[-M], M);
/* overwrite synthesis speech by postfiltered synthesis speech */
Copy(syn_pst, syn, L_FRAME);
return;
}
/*---------------------------------------------------------------------------*
* procedure pitch_pst_filt *
* ~~~~~~~~~~~~~~~~~~~~~~~~ *
* Find the pitch period around the transmitted pitch and perform *
* harmonic postfiltering. *
* Filtering through (1 + g z^-T) / (1+g) ; g = min(pit_gain*gammap, 1) *
*--------------------------------------------------------------------------*/
void pit_pst_filt(
int16_t *signal, /* (i) : input signal */
int16_t *scal_sig, /* (i) : input signal (scaled, divided by 4) */
int16_t t0_min, /* (i) : minimum value in the searched range */
int16_t t0_max, /* (i) : maximum value in the searched range */
int16_t L_subfr, /* (i) : size of filtering */
int16_t *signal_pst /* (o) : harmonically postfiltered signal */
)
{
int16_t i, j, t0;
int16_t g0, gain, cmax, en, en0;
int16_t *p, *p1, *deb_sig;
int32_t corr, cor_max, ener, ener0, temp;
int32_t L_temp;
/*---------------------------------------------------------------------------*
* Compute the correlations for all delays *
* and select the delay which maximizes the correlation *
*---------------------------------------------------------------------------*/
deb_sig = &scal_sig[-t0_min];
cor_max = MIN_32;
t0 = t0_min; /* Only to remove warning from some compilers */
for (i=t0_min; i<=t0_max; i++)
{
corr = 0;
p = scal_sig;
p1 = deb_sig;
for (j=0; j<L_subfr; j++)
corr = L_mac(corr, *p++, *p1++);
L_temp = L_sub(corr, cor_max);
if (L_temp > (int32_t)0)
{
cor_max = corr;
t0 = i;
}
deb_sig--;
}
/* Compute the energy of the signal delayed by t0 */
ener = 1;
p = scal_sig - t0;
for ( i=0; i<L_subfr ;i++, p++)
ener = L_mac(ener, *p, *p);
/* Compute the signal energy in the present subframe */
ener0 = 1;
p = scal_sig;
for ( i=0; i<L_subfr; i++, p++)
ener0 = L_mac(ener0, *p, *p);
if (cor_max < 0)
{
cor_max = 0;
}
/* scale "cor_max", "ener" and "ener0" on 16 bits */
temp = cor_max;
if (ener > temp)
{
temp = ener;
}
if (ener0 > temp)
{
temp = ener0;
}
j = norm_l(temp);
cmax = _round(L_shl(cor_max, j));
en = _round(L_shl(ener, j));
en0 = _round(L_shl(ener0, j));
/* prediction gain (dB)= -10 log(1-cor_max*cor_max/(ener*ener0)) */
/* temp = (cor_max * cor_max) - (0.5 * ener * ener0) */
temp = L_mult(cmax, cmax);
temp = L_sub(temp, L_shr(L_mult(en, en0), 1));
if (temp < (int32_t)0) /* if prediction gain < 3 dB */
{ /* switch off pitch postfilter */
for (i = 0; i < L_subfr; i++)
signal_pst[i] = signal[i];
return;
}
if (sub(cmax, en) > 0) /* if pitch gain > 1 */
{
g0 = INV_GAMMAP;
gain = GAMMAP_2;
}
else {
cmax = shr(mult(cmax, GAMMAP), 1); /* cmax(Q14) = cmax(Q15) * GAMMAP */
en = shr(en, 1); /* Q14 */
i = add(cmax, en);
if(i > 0)
{
gain = div_s(cmax, i); /* gain(Q15) = cor_max/(cor_max+ener) */
g0 = sub(32767, gain); /* g0(Q15) = 1 - gain */
}
else
{
g0 = 32767;
gain = 0;
}
}
for (i = 0; i < L_subfr; i++)
{
/* signal_pst[i] = g0*signal[i] + gain*signal[i-t0]; */
signal_pst[i] = add(mult(g0, signal[i]), mult(gain, signal[i-t0]));
}
return;
}
/*---------------------------------------------------------------------*
* routine preemphasis() *
* ~~~~~~~~~~~~~~~~~~~~~ *
* Preemphasis: filtering through 1 - g z^-1 *
*---------------------------------------------------------------------*/
void preemphasis(
int16_t *signal, /* (i/o) : input signal overwritten by the output */
int16_t g, /* (i) Q15 : preemphasis coefficient */
int16_t L /* (i) : size of filtering */
)
{
static int16_t mem_pre = 0;
int16_t *p1, *p2, temp, i;
p1 = signal + L - 1;
p2 = p1 - 1;
temp = *p1;
for (i = 0; i <= L-2; i++)
{
*p1 = sub(*p1, mult(g, *p2));
*p1 = *p1 - 1;
*p2 = *p2 - 1;
}
*p1 = sub(*p1, mult(g, mem_pre));
mem_pre = temp;
return;
}
/*----------------------------------------------------------------------*
* routine agc() *
* ~~~~~~~~~~~~~ *
* Scale the postfilter output on a subframe basis by automatic control *
* of the subframe gain. *
* gain[n] = AGC_FAC * gain[n-1] + (1 - AGC_FAC) g_in/g_out *
*----------------------------------------------------------------------*/
void agc(
int16_t *sig_in, /* (i) : postfilter input signal */
int16_t *sig_out, /* (i/o) : postfilter output signal */
int16_t l_trm /* (i) : subframe size */
)
{
static int16_t past_gain=4096; /* past_gain = 1.0 (Q12) */
int16_t i, exp;
int16_t gain_in, gain_out, g0, gain; /* Q12 */
int32_t s;
int16_t signal[L_SUBFR];
/* calculate gain_out with exponent */
for(i=0; i<l_trm; i++)
signal[i] = shr(sig_out[i], 2);
s = 0;
for(i=0; i<l_trm; i++)
s = L_mac(s, signal[i], signal[i]);
if (s == 0) {
past_gain = 0;
return;
}
exp = sub(norm_l(s), 1);
gain_out = _round(L_shl(s, exp));
/* calculate gain_in with exponent */
for(i=0; i<l_trm; i++)
signal[i] = shr(sig_in[i], 2);
s = 0;
for(i=0; i<l_trm; i++)
s = L_mac(s, signal[i], signal[i]);
if (s == 0) {
g0 = 0;
}
else {
i = norm_l(s);
gain_in = _round(L_shl(s, i));
exp = sub(exp, i);
/*---------------------------------------------------*
* g0(Q12) = (1-AGC_FAC) * sqrt(gain_in/gain_out); *
*---------------------------------------------------*/
s = L_deposit_l(div_s(gain_out,gain_in)); /* Q15 */
s = L_shl(s, 7); /* s(Q22) = gain_out / gain_in */
s = L_shr(s, exp); /* Q22, add exponent */
/* i(Q12) = s(Q19) = 1 / sqrt(s(Q22)) */
s = Inv_sqrt(s); /* Q19 */
i = _round(L_shl(s,9)); /* Q12 */
/* g0(Q12) = i(Q12) * (1-AGC_FAC)(Q15) */
g0 = mult(i, AGC_FAC1); /* Q12 */
}
/* compute gain(n) = AGC_FAC gain(n-1) + (1-AGC_FAC)gain_in/gain_out */
/* sig_out(n) = gain(n) sig_out(n) */
gain = past_gain;
for(i=0; i<l_trm; i++) {
gain = mult(gain, AGC_FAC);
gain = add(gain, g0);
sig_out[i] = extract_h(L_shl(L_mult(sig_out[i], gain), 3));
}
past_gain = gain;
return;
}