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
/
qsidlsf.c
295 lines (257 loc) · 9.95 KB
/
qsidlsf.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
/*
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 <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "typedef.h"
#include "ld8a.h"
#include "tab_ld8a.h"
#include "sid.h"
#include "vad.h"
#include "dtx.h"
#include "tab_dtx.h"
/* local functions */
static void Qnt_e(
FLOAT *errlsf, /* (i) : error lsf vector */
FLOAT *weight, /* (i) : weighting vector */
int DIn, /* (i) : number of input candidates */
FLOAT *qlsf, /* (o) : quantized error lsf vector */
int *Pptr, /* (o) : predictor index */
int DOut, /* (i) : number of quantized vectors */
int *cluster, /* (o) : quantizer indices */
int *MS /* (i) : size of the quantizers */
);
static void New_ML_search_1(
FLOAT *d_data, /* (i) : error vector */
int J, /* (i) : number of input vectors */
FLOAT *new_d_data,/* (o) : output vector */
int K, /* (i) : number of candidates */
int *best_indx, /* (o) : best indices */
int *ptr_back, /* (o) : pointer for backtracking */
int *PtrTab, /* (i) : quantizer table */
int MQ /* (i) : size of quantizer */
);
static void New_ML_search_2(
FLOAT *d_data, /* (i) : error vector */
FLOAT *weight, /* (i) : weighting vector */
int J, /* (i) : number of input vectors */
FLOAT *new_d_data,/* (o) : output vector */
int K, /* (i) : number of candidates */
int *best_indx, /* (o) : best indices */
int *ptr_prd, /* (i) : pointer for backtracking */
int *ptr_back, /* (o) : pointer for backtracking */
int PtrTab[2][16],/* (i) : quantizer table */
int MQ /* (i) : size of quantizer */
);
/*-----------------------------------------------------------------*
* Functions lsfq_noise *
* ~~~~~~~~~~ *
* Input: *
* lsp[] : unquantized lsp vector *
* freq_prev[][] : memory of the lsf predictor *
* *
* Output: *
* *
* lspq[] : quantized lsp vector *
* ana[] : indices *
* *
*-----------------------------------------------------------------*/
void lsfq_noise(FLOAT noise_fg[MODE][MA_NP][M], FLOAT *lsp, FLOAT *lspq, FLOAT freq_prev[MA_NP][M], int *ana)
{
int i, MS[MODE]={32, 16}, Clust[MODE], mode;
FLOAT lsf[M], lsfq[M], weight[M], tmpbuf[M], errlsf[M*MODE];
/* convert lsp to lsf */
lsp_lsf(lsp, lsf, M);
/* spacing to ~100Hz */
if (lsf[0] < L_LIMIT)
lsf[0] = L_LIMIT;
for (i=0 ; i < M-1 ; i++) {
if (lsf[i+1]- lsf[i] < 2*GAP3) lsf[i+1] = lsf[i]+ 2*GAP3;
}
if (lsf[M-1] > M_LIMIT)
lsf[M-1] = M_LIMIT;
if (lsf[M-1] < lsf[M-2])
lsf[M-2] = lsf[M-1]- GAP3;
/* get the lsf weighting */
get_wegt(lsf, weight);
/**********************/
/* quantize the lsf's */
/**********************/
/* get the prediction error vector */
for (mode=0; mode<MODE; mode++)
lsp_prev_extract(lsf, errlsf+mode*M, noise_fg[mode], freq_prev,
noise_fg_sum_inv[mode]);
/* quantize the lsf and get the corresponding indices */
Qnt_e(errlsf, weight, MODE, tmpbuf, &mode, 1, Clust, MS);
ana[0] = (int) mode;
ana[1] = (int) Clust[0];
ana[2] = (int) Clust[1];
/* guarantee minimum distance of 0.0012 between tmpbuf[j]
and tmpbuf[j+1] */
lsp_expand_1_2(tmpbuf, (F)0.0012);
/* compute the quantized lsf vector */
lsp_prev_compose(tmpbuf, lsfq, noise_fg[mode], freq_prev,
noise_fg_sum[mode]);
/* update the prediction memory */
lsp_prev_update(tmpbuf, freq_prev);
/* lsf stability check */
lsp_stability(lsfq);
/* convert lsf to lsp */
lsf_lsp(lsfq, lspq, M);
return;
}
static void Qnt_e(
FLOAT *errlsf, /* (i) : error lsf vector */
FLOAT *weight, /* (i) : weighting vector */
int DIn, /* (i) : number of input candidates */
FLOAT *qlsf, /* (o) : quantized error lsf vector */
int *Pptr, /* (o) : predictor index */
int DOut, /* (i) : number of quantized vectors */
int *cluster, /* (o) : quantizer indices */
int *MS /* (i) : size of the quantizers */
)
{
int best_indx[2][R_LSFQ];
int ptr_back[2][R_LSFQ], ptr, i;
FLOAT d_data[2][R_LSFQ*M];
New_ML_search_1(errlsf, DIn, d_data[0], 4, best_indx[0], ptr_back[0],
PtrTab_1, MS[0]);
New_ML_search_2(d_data[0], weight, 4, d_data[1], DOut, best_indx[1],
ptr_back[0], ptr_back[1], PtrTab_2, MS[1]);
/* backward path for the indices */
cluster[1] = best_indx[1][0];
ptr = ptr_back[1][0];
cluster[0] = best_indx[0][ptr];
/* this is the pointer to the best predictor */
*Pptr = ptr_back[0][ptr];
/* generating the quantized vector */
copy(lspcb1[PtrTab_1[cluster[0]]], qlsf, M);
for (i=0; i<M/2; i++)
qlsf[i] = qlsf[i]+ lspcb2[PtrTab_2[0][cluster[1]]][i];
for (i=M/2; i<M; i++)
qlsf[i] = qlsf[i]+ lspcb2[PtrTab_2[1][cluster[1]]][i];
}
static void New_ML_search_1(
FLOAT *d_data, /* (i) : error vector */
int J, /* (i) : number of input vectors */
FLOAT *new_d_data,/* (o) : output vector */
int K, /* (i) : number of candidates */
int *best_indx, /* (o) : best indices */
int *ptr_back, /* (o) : pointer for backtracking */
int *PtrTab, /* (i) : quantizer table */
int MQ /* (i) : size of quantizer */
)
{
int m, l, p, q, min_indx_p[R_LSFQ], min_indx_m[R_LSFQ];
FLOAT sum[R_LSFQ*R_LSFQ], min[R_LSFQ];
for (q=0 ; q < K ; q++) {
min[q] = FLT_MAX_G729;
}
/* compute the errors */
for (p=0 ; p < J ; p++) {
for(m=0 ; m < MQ ; m++) {
sum[p*MQ+m] = (F)0.0;
for (l=0 ; l < M ; l++) {
sum[p*MQ+m] +=
sqr(d_data[p*M+l]-lspcb1[PtrTab[m]][l]);
}
sum[p*MQ+m] *= Mp[p];
}
}
/* select the candidates */
for (q=0 ; q < K ; q++) {
for (p=0 ; p < J ; p++) {
for (m=0 ; m < MQ ; m++) {
if (sum[p*MQ+m] < min[q]) {
min[q] = sum[p*MQ+m];
min_indx_p[q] = p;
min_indx_m[q] = m;
}
}
}
sum[min_indx_p[q]*MQ+min_indx_m[q]] = FLT_MAX_G729;
}
for (q=0 ; q < K ; q++) {
for (l=0 ; l < M ; l++) {
new_d_data[q*M+l] =
d_data[min_indx_p[q]*M+l]-lspcb1[PtrTab[min_indx_m[q]]][l];
}
ptr_back[q] = min_indx_p[q];
best_indx[q] = min_indx_m[q];
}
return;
}
static void New_ML_search_2(
FLOAT *d_data, /* (i) : error vector */
FLOAT *weight, /* (i) : weighting vector */
int J, /* (i) : number of input vectors */
FLOAT *new_d_data,/* (o) : output vector */
int K, /* (i) : number of candidates */
int *best_indx, /* (o) : best indices */
int *ptr_prd, /* (i) : pointer for backtracking */
int *ptr_back, /* (o) : pointer for backtracking */
int PtrTab[2][16],/* (i) : quantizer table */
int MQ /* (i) : size of quantizer */
)
{
int m,l,p,q;
int min_indx_p[R_LSFQ];
int min_indx_m[R_LSFQ];
FLOAT sum[R_LSFQ*R_LSFQ];
FLOAT min[R_LSFQ];
for (q=0; q<K; q++)
min[q] = FLT_MAX_G729;
/* compute the errors */
for (p=0 ; p < J ; p++) {
for(m=0 ; m < MQ ; m++) {
sum[p*MQ+m] = (F)0.0;
for (l=0 ; l < M/2 ; l++) {
sum[p*MQ+m] +=
weight[l]*sqr(noise_fg_sum[ptr_prd[p]][l])*sqr(d_data[p*M+l]-lspcb2[PtrTab[0][m]][l]);
}
for (l=M/2 ; l < M ; l++) {
sum[p*MQ+m] +=
weight[l]*sqr(noise_fg_sum[ptr_prd[p]][l])*sqr(d_data[p*M+l]-lspcb2[PtrTab[1][m]][l]);
}
}
}
/* select the candidates */
for (q=0 ; q < K ; q++) {
min_indx_p[q] = 0;
min_indx_m[q] = 0;
for (p=0 ; p < J ; p++) {
for (m=0 ; m < MQ ; m++) {
if (sum[p*MQ+m] < min[q]) {
min[q] = sum[p*MQ+m];
min_indx_p[q] = p;
min_indx_m[q] = m;
}
}
}
sum[min_indx_p[q]*MQ+min_indx_m[q]] = FLT_MAX_G729;
}
/* compute the candidates */
for (q=0 ; q < K ; q++) {
for (l=0 ; l < M/2 ; l++) {
new_d_data[q*M+l] =
d_data[min_indx_p[q]*M+l]-lspcb2[PtrTab[0][min_indx_m[q]]][l];
}
for (l=M/2 ; l < M ; l++) {
new_d_data[q*M+l] = d_data[min_indx_p[q]*M+l]-
lspcb2[PtrTab[1][min_indx_m[q]]][l];
}
ptr_back[q] = min_indx_p[q];
best_indx[q] = min_indx_m[q];
}
return;
}