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
/
dtx.c
337 lines (280 loc) · 9.77 KB
/
dtx.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
/*
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.
----------------------------------------------------------------------
*/
/* DTX and Comfort Noise Generator - Encoder part */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "typedef.h"
#include "ld8a.h"
#include "tab_ld8a.h"
#include "vad.h"
#include "dtx.h"
#include "tab_dtx.h"
#include "sid.h"
/* Local functions */
static void calc_pastfilt(enc_cng_state *state, FLOAT *Coeff, FLOAT old_A[], FLOAT old_rc[]);
static void calc_RCoeff(FLOAT *Coeff, FLOAT *RCoeff);
static int cmp_filt(FLOAT *RCoeff, FLOAT *acf, FLOAT alpha, FLOAT Thresh);
static void calc_sum_acf(FLOAT *acf, FLOAT *sum, int nb);
static void update_sumAcf(enc_cng_state *state);
/*-----------------------------------------------------------*
* procedure init_Cod_cng: *
* ~~~~~~~~~~~~ *
* Initialize variables used for dtx at the encoder *
*-----------------------------------------------------------*/
void init_cod_cng(enc_cng_state *state)
{
set_zero(state->sumAcf, SIZ_SUMACF);
set_zero(state->Acf, SIZ_ACF);
set_zero(state->ener, NB_GAIN);
state->cur_gain = 0;
state->fr_cur = 0;
state->flag_chang = 0;
return;
}
/*-----------------------------------------------------------*
* procedure cod_cng: *
* ~~~~~~~~ *
* computes DTX decision *
* encodes SID frames *
* computes CNG excitation for encoder update *
*-----------------------------------------------------------*/
void cod_cng(
enc_cng_state *state,
FLOAT *exc, /* (i/o) : excitation array */
int pastVad, /* (i) : previous VAD decision */
FLOAT *lsp_old_q, /* (i/o) : previous quantized lsp */
FLOAT *old_A, /* (i/o) : last stable filter LPC coefficients */
FLOAT *old_rc, /* (i/o) : last stable filter Reflection coefficients.*/
FLOAT *Aq, /* (o) : set of interpolated LPC coefficients */
int *ana, /* (o) : coded SID parameters */
FLOAT freq_prev[MA_NP][M], /* (i/o) : previous LPS for quantization */
INT16 *seed /* (i/o) : random generator seed */
)
{
int i;
FLOAT curAcf[MP1];
FLOAT bid[MP1];
FLOAT curCoeff[MP1];
FLOAT lsp_new[M];
FLOAT *lpcCoeff;
int cur_igain;
FLOAT energyq;
/* Update Ener */
for(i = NB_GAIN-1; i>=1; i--) {
state->ener[i] = state->ener[i-1];
}
/* Compute current Acfs */
calc_sum_acf(state->Acf, curAcf, NB_CURACF);
/* Compute LPC coefficients and residual energy */
if(curAcf[0] == (F)0.) {
state->ener[0] = (F)0.; /* should not happen */
}
else {
state->ener[0] = levinson(curAcf, curCoeff, bid, old_A, old_rc);
}
/* if first frame of silence => SID frame */
if(pastVad != 0) {
ana[0] = 2;
state->count_fr0 = 0;
state->nb_ener = 1;
qua_Sidgain(state->ener, state->nb_ener, &energyq, &cur_igain);
}
else {
state->nb_ener++;
if(state->nb_ener > NB_GAIN) state->nb_ener = NB_GAIN;
qua_Sidgain(state->ener, state->nb_ener, &energyq, &cur_igain);
/* Compute stationarity of current filter */
/* versus reference filter */
if(cmp_filt(state->RCoeff, curAcf, state->ener[0], THRESH1) != 0) {
state->flag_chang = 1;
}
/* compare energy difference between current frame and last frame */
if( (FLOAT)fabs(state->prev_energy - energyq) > (F)2.0) state->flag_chang = 1;
state->count_fr0++;
if(state->count_fr0 < FR_SID_MIN) {
ana[0] = 0; /* no transmission */
}
else {
if(state->flag_chang != 0) {
ana[0] = 2; /* transmit SID frame */
}
else{
ana[0] = 0;
}
state->count_fr0 = FR_SID_MIN; /* to avoid overflow */
}
}
if(ana[0] == 2) {
/* Reset frame count and change flag */
state->count_fr0 = 0;
state->flag_chang = 0;
/* Compute past average filter */
calc_pastfilt(state, state->pastCoeff, old_A, old_rc);
calc_RCoeff(state->pastCoeff, state->RCoeff);
/* Compute stationarity of current filter */
/* versus past average filter */
/* if stationary */
/* transmit average filter => new ref. filter */
if(cmp_filt(state->RCoeff, curAcf, state->ener[0], THRESH2) == 0) {
lpcCoeff = state->pastCoeff;
}
/* else */
/* transmit current filter => new ref. filter */
else {
lpcCoeff = curCoeff;
calc_RCoeff(curCoeff, state->RCoeff);
}
/* Compute SID frame codes */
az_lsp(lpcCoeff, lsp_new, lsp_old_q); /* From A(z) to lsp */
/* LSP quantization */
lsfq_noise(state->noise_fg, lsp_new, state->lspSid_q, freq_prev, &ana[1]);
state->prev_energy = energyq;
ana[4] = cur_igain;
state->sid_gain = tab_Sidgain[cur_igain];
} /* end of SID frame case */
/* Compute new excitation */
if(pastVad != 0) {
state->cur_gain = state->sid_gain;
}
else {
state->cur_gain *= A_GAIN0;
state->cur_gain += A_GAIN1 * state->sid_gain;
}
calc_exc_rand(state->exc_err, state->cur_gain, exc, seed, FLAG_COD);
int_qlpc(lsp_old_q, state->lspSid_q, Aq);
copy(state->lspSid_q, lsp_old_q, M);
/* Update sumAcf if fr_cur = 0 */
if(state->fr_cur == 0) {
update_sumAcf(state);
}
return;
}
/*-----------------------------------------------------------*
* procedure update_cng: *
* ~~~~~~~~~~ *
* updates autocorrelation arrays *
* used for DTX/CNG *
* If Vad=1 : updating of array sumAcf *
*-----------------------------------------------------------*/
void update_cng(
enc_cng_state *state,
FLOAT *r, /* (i) : frame autocorrelation */
int Vad /* (i) : current Vad decision */
)
{
int i;
FLOAT *ptr1, *ptr2;
/* Update Acf */
ptr1 = state->Acf + SIZ_ACF - 1;
ptr2 = ptr1 - MP1;
for(i=0; i<(SIZ_ACF-MP1); i++) {
*ptr1-- = *ptr2--;
}
/* Save current Acf */
copy(r, state->Acf, MP1);
state->fr_cur++;
if(state->fr_cur == NB_CURACF) {
state->fr_cur = 0;
if(Vad != 0) {
update_sumAcf(state);
}
}
return;
}
/*-----------------------------------------------------------*
* Local procedures *
* ~~~~~~~~~~~~~~~~ *
*-----------------------------------------------------------*/
/* Compute autocorr of LPC coefficients used for Itakura distance */
/******************************************************************/
static void calc_RCoeff(FLOAT *Coeff, FLOAT *RCoeff)
{
int i, j;
FLOAT temp;
/* RCoeff[0] = SUM(j=0->M) Coeff[j] ** 2 */
for(j=0, temp = (F)0.; j <= M; j++) {
temp += Coeff[j] * Coeff[j];
}
RCoeff[0] = temp;
/* RCoeff[i] = SUM(j=0->M-i) Coeff[j] * Coeff[j+i] */
for(i=1; i<=M; i++) {
for(j=0, temp=(F)0.; j<=M-i; j++) {
temp += Coeff[j] * Coeff[j+i];
}
RCoeff[i] = (F)2. * temp;
}
return;
}
/* Compute Itakura distance and compare to threshold */
/*****************************************************/
static int cmp_filt(FLOAT *RCoeff, FLOAT *acf, FLOAT alpha, FLOAT Thresh)
{
FLOAT temp1, temp2;
int i;
int diff;
temp1 = (F)0.;
for(i=0; i <= M; i++) {
temp1 += RCoeff[i] * acf[i];
}
temp2 = alpha * Thresh;
if(temp1 > temp2) diff = 1;
else diff = 0;
return(diff);
}
/* Compute past average filter */
/*******************************/
static void calc_pastfilt(enc_cng_state *state, FLOAT *Coeff, FLOAT old_A[], FLOAT old_rc[])
{
FLOAT s_sumAcf[MP1];
FLOAT bid[M];
calc_sum_acf(state->sumAcf, s_sumAcf, NB_SUMACF);
if(s_sumAcf[0] == (F)0.) {
Coeff[0] = (F)1.;
set_zero(Coeff + 1, M);
return;
}
levinson(s_sumAcf, Coeff, bid, old_A, old_rc);
return;
}
/* Update sumAcf */
/*****************/
static void update_sumAcf(enc_cng_state *state)
{
FLOAT *ptr1, *ptr2;
int i;
/*** Move sumAcf ***/
ptr1 = state->sumAcf + SIZ_SUMACF - 1;
ptr2 = ptr1 - MP1;
for(i=0; i<(SIZ_SUMACF-MP1); i++) {
*ptr1-- = *ptr2--;
}
/* Compute new sumAcf */
calc_sum_acf(state->Acf, state->sumAcf, NB_CURACF);
return;
}
/* Compute sum of acfs (curAcf, sumAcf or s_sumAcf) */
/****************************************************/
static void calc_sum_acf(FLOAT *acf, FLOAT *sum, int nb)
{
FLOAT *ptr1;
int i, j;
set_zero(sum, MP1);
ptr1 = acf;
for(i=0; i<nb; i++) {
for(j=0; j<MP1; j++) {
sum[j] += (*ptr1++);
}
}
return;
}