This repository has been archived by the owner on Jan 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpostfila.c
371 lines (294 loc) · 12.1 KB
/
postfila.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
/*
ITU-T G.729A Speech Coder with Annex B ANSI-C Source Code
*/
/*
----------------------------------------------------------------------
COPYRIGHT NOTICE
----------------------------------------------------------------------
ITU-T G.729 Annex C ANSI C source code
Copyright (C) 1998, AT&T, France Telecom, NTT, University of
Sherbrooke. All rights reserved.
----------------------------------------------------------------------
*/
#include <math.h>
#include "typedef.h"
#include "ld8a.h"
/* prototype of local functions */
static void pit_pst_filt(
FLOAT *signal, /* input : input signal */
int t0_min, /* input : minimum value in the searched range */
int t0_max, /* input : maximum value in the searched range */
int L_subfr, /* input : size of filtering */
FLOAT *signal_pst /* output: harmonically postfiltered signal */
);
static void agc(
post_filter_state *state,
FLOAT *sig_in, /* input : postfilter input signal */
FLOAT *sig_out, /* in/out: postfilter output signal */
int l_trm /* input : subframe size */
);
static void preemphasis(
post_filter_state *state,
FLOAT *signal, /* in/out: input signal overwritten by the output */
FLOAT g, /* input : preemphasis coefficient */
int L /* input : size of filtering */
);
/*---------------------------------------------------------------*
* 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 *
*---------------------------------------------------------------*/
/*---------------------------------------------------------------*
* Procedure init_post_filter: *
* ~~~~~~~~~~~~~ *
* Initializes the postfilter parameters: *
*---------------------------------------------------------------*/
void init_post_filter(post_filter_state *state)
{
state->res2 = state->res2_buf + PIT_MAX;
set_zero(state->mem_syn_pst, M);
set_zero(state->res2_buf, PIT_MAX+L_SUBFR);
state->mem_pre = (F)0.;
state->past_gain = (F)1.0;
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(
post_filter_state *state,
FLOAT *syn, /* in/out: synthesis speech (postfiltered is output) */
FLOAT *az_4, /* input : interpolated LPC parameters in all subframes */
int *T, /* input : decoded pitch lags in all subframes */
int Vad /* input: decoded frame type */
)
{
/*-------------------------------------------------------------------*
* Declaration of parameters *
*-------------------------------------------------------------------*/
FLOAT res2_pst[L_SUBFR]; /* res2[] after pitch postfiltering */
FLOAT syn_pst[L_FRAME]; /* post filtered synthesis speech */
FLOAT ap3[MP1], ap4[MP1]; /* bandwidth expanded LP parameters */
FLOAT *az; /* pointer to Az_4:
LPC parameters in each subframe */
int t0_max, t0_min; /* closed-loop pitch search range */
int i_subfr; /* index for beginning of subframe */
FLOAT temp1, temp2;
FLOAT h[L_H];
int i;
az = az_4;
for (i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR)
{
/* Find pitch range t0_min - t0_max */
t0_min = *T++ - 3;
t0_max = t0_min+6;
if (t0_max > PIT_MAX) {
t0_max = PIT_MAX;
t0_min = 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], state->res2, L_SUBFR);
/* pitch postfiltering */
if (Vad == 1)
pit_pst_filt(state->res2, t0_min, t0_max, L_SUBFR, res2_pst);
else
for (i=0; i<L_SUBFR; i++)
res2_pst[i] = state->res2[i];
/* tilt compensation filter */
/* impulse response of A(z/GAMMA2_PST)/A(z/GAMMA1_PST) */
copy(ap3, h, MP1);
set_zero(&h[MP1],L_H-MP1);
syn_filt(ap4, h, h, L_H, &h[M+1], 0);
/* 1st correlation of h[] */
temp1 = (F)0.0;
for (i=0; i<L_H; i++) temp1 += h[i]*h[i];
temp2 = (F)0.0;
for (i=0; i<L_H-1; i++) temp2 += h[i]*h[i+1];
if(temp2 <= (F)0.0) {
temp2 = (F)0.0;
}
else {
temp2 = temp2*MU/temp1;
}
preemphasis(state, res2_pst, temp2, L_SUBFR);
/* filtering through 1/A(z/GAMMA1_PST) */
syn_filt(ap4, res2_pst, &syn_pst[i_subfr], L_SUBFR, state->mem_syn_pst, 1);
/* scale output to input */
agc(state, &syn[i_subfr], &syn_pst[i_subfr], L_SUBFR);
/* update res2[] buffer; shift by L_SUBFR */
copy(&state->res2[L_SUBFR-PIT_MAX], &state->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 pit_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) *
*--------------------------------------------------------------------------*/
static void pit_pst_filt(
FLOAT *signal, /* input : input signal */
int t0_min, /* input : minimum value in the searched range */
int t0_max, /* input : maximum value in the searched range */
int L_subfr, /* input : size of filtering */
FLOAT *signal_pst /* output: harmonically postfiltered signal */
)
{
int i, j;
int t0=0;
FLOAT cor_max;
FLOAT temp, g0, gain;
FLOAT ener, corr, ener0;
FLOAT *p, *p1, *deb_sig;
/*---------------------------------------------------------------------------*
* Compute the correlations for all delays *
* and select the delay which maximizes the correlation *
*---------------------------------------------------------------------------*/
deb_sig = &signal[-t0_min];
cor_max = FLT_MIN_G729;
for (i=t0_min; i<=t0_max; i++)
{
corr = (F)0.0;
p = signal;
p1 = deb_sig;
for (j=0; j<L_subfr; j++)
corr += (*p++) * (*p1++);
if (corr>cor_max)
{
cor_max = corr;
t0 = i;
}
deb_sig--;
}
/* Compute the energy of the signal delayed by t0 */
ener = (F)0.5;
p = signal - t0;
for ( i=0; i<L_subfr ;i++, p++)
ener += (*p) * (*p);
/* Compute the signal energy in the present subframe */
ener0 = (F)0.5;
p = signal;
for ( i=0; i<L_subfr; i++, p++)
ener0 += (*p) * (*p);
if (cor_max < (F)0.0) cor_max = (F)0.0;
/* prediction gain (dB)= -10 log(1-cor_max*cor_max/(ener*ener0)) */
temp = cor_max*cor_max;
if (temp < ener*ener0*0.5) /* if prediction gain < 3 dB */
{ /* switch off pitch postfilter */
for (i = 0; i < L_subfr; i++)
signal_pst[i] = signal[i];
return;
}
if (cor_max > ener) /* if pitch gain > 1 */
{
g0 = INV_GAMMAP;
gain = GAMMAP_2;
}
else
{
cor_max *= GAMMAP;
temp = (F)1.0/(cor_max+ener);
gain = temp * cor_max;
g0 = (F)1.0 - gain;
}
for (i = 0; i < L_subfr; i++)
signal_pst[i] = g0*signal[i] + gain*signal[i-t0];
return;
}
/*---------------------------------------------------------------------*
* routine preemphasis() *
* ~~~~~~~~~~~~~~~~~~~~~ *
* Preemphasis: filtering through 1 - g z^-1 *
*---------------------------------------------------------------------*/
static void preemphasis(
post_filter_state *state,
FLOAT *signal, /* in/out: input signal overwritten by the output */
FLOAT g, /* input : preemphasis coefficient */
int L /* input : size of filtering */
)
{
FLOAT *p1, *p2, temp;
int i;
p1 = signal + L - 1;
p2 = p1 - 1;
temp = *p1;
for (i = 0; i <= L-2; i++) {
*p1 -= g * (*p2--); p1--; }
*p1 = *p1 - g * state->mem_pre;
state->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 *
*----------------------------------------------------------------------*/
static void agc(post_filter_state *state,
FLOAT *sig_in, /* input : postfilter input signal */
FLOAT *sig_out, /* in/out: postfilter output signal */
int l_trm /* input : subframe size */
)
{
int i;
FLOAT gain_in, gain_out;
FLOAT g0, gain;
gain_out = (F)0.;
for(i=0; i<l_trm; i++) {
gain_out += sig_out[i]*sig_out[i];
}
if(gain_out == (F)0.) {
state->past_gain = (F)0.;
return;
}
gain_in = (F)0.;
for(i=0; i<l_trm; i++) {
gain_in += sig_in[i]*sig_in[i];
}
if(gain_in == (F)0.) {
g0 = (F)0.;
}
else {
g0 = (FLOAT)sqrt(gain_in/ gain_out);
g0 *= AGC_FAC1;
}
/* compute gain(n) = AGC_FAC gain(n-1) + (1-AGC_FAC)gain_in/gain_out */
/* sig_out(n) = gain(n) sig_out(n) */
gain = state->past_gain;
for(i=0; i<l_trm; i++) {
gain *= AGC_FAC;
gain += g0;
sig_out[i] *= gain;
}
state->past_gain = gain;
return;
}