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
/
lspgetq.c
231 lines (198 loc) · 6.1 KB
/
lspgetq.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
/*
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 "typedef.h"
#include "ld8a.h"
/*----------------------------------------------------------------------------
* lsp_get_quant - reconstruct quantized LSP parameter and check the stabilty
*----------------------------------------------------------------------------
*/
void lsp_get_quant(
FLOAT lspcb1[][M], /*input : first stage LSP codebook */
FLOAT lspcb2[][M], /*input : Second stage LSP codebook */
int code0, /*input : selected code of first stage */
int code1, /*input : selected code of second stage*/
int code2, /*input : selected code of second stage*/
FLOAT fg[][M], /*input : MA prediction coef. */
FLOAT freq_prev[][M], /*input : previous LSP vector */
FLOAT lspq[], /*output: quantized LSP parameters */
FLOAT fg_sum[] /*input : present MA prediction coef. */
)
{
int j;
FLOAT buf[M];
for(j=0; j<NC; j++)
buf[j] = lspcb1[code0][j] + lspcb2[code1][j];
for(j=NC; j<M; j++)
buf[j] = lspcb1[code0][j] + lspcb2[code2][j];
/* check */
lsp_expand_1_2(buf, GAP1);
lsp_expand_1_2(buf, GAP2);
/* reconstruct quantized LSP parameters */
lsp_prev_compose(buf, lspq, fg, freq_prev, fg_sum);
lsp_prev_update(buf, freq_prev);
lsp_stability( lspq ); /* check the stabilty */
return;
}
/*----------------------------------------------------------------------------
* lsp_expand_1 - check for lower (0-4)
*----------------------------------------------------------------------------
*/
void lsp_expand_1(
FLOAT buf[], /* in/out: lsp vectors */
FLOAT gap
)
{
int j;
FLOAT diff, tmp;
for(j=1; j<NC; j++) {
diff = buf[j-1] - buf[j];
tmp = (diff + gap) * (F)0.5;
if(tmp > 0) {
buf[j-1] -= tmp;
buf[j] += tmp;
}
}
return;
}
/*----------------------------------------------------------------------------
* lsp_expand_2 - check for higher (5-9)
*----------------------------------------------------------------------------
*/
void lsp_expand_2(
FLOAT buf[], /*in/out: lsp vectors */
FLOAT gap
)
{
int j;
FLOAT diff, tmp;
for(j=NC; j<M; j++) {
diff = buf[j-1] - buf[j];
tmp = (diff + gap) * (F)0.5;
if(tmp > 0) {
buf[j-1] -= tmp;
buf[j] += tmp;
}
}
return;
}
/*----------------------------------------------------------------------------
* lsp_expand_1_2 - ..
*----------------------------------------------------------------------------
*/
void lsp_expand_1_2(
FLOAT buf[], /*in/out: LSP parameters */
FLOAT gap /*input */
)
{
int j;
FLOAT diff, tmp;
for(j=1; j<M; j++) {
diff = buf[j-1] - buf[j];
tmp = (diff + gap) * (F)0.5;
if(tmp > 0) {
buf[j-1] -= tmp;
buf[j] += tmp;
}
}
return;
}
/*
Functions which use previous LSP parameter (freq_prev).
*/
/*
compose LSP parameter from elementary LSP with previous LSP.
*/
void lsp_prev_compose(
FLOAT lsp_ele[], /* (i) Q13 : LSP vectors */
FLOAT lsp[], /* (o) Q13 : quantized LSP parameters */
FLOAT fg[][M], /* (i) Q15 : MA prediction coef. */
FLOAT freq_prev[][M], /* (i) Q13 : previous LSP vector */
FLOAT fg_sum[] /* (i) Q15 : present MA prediction coef. */
)
{
int j, k;
for(j=0; j<M; j++) {
lsp[j] = lsp_ele[j] * fg_sum[j];
for(k=0; k<MA_NP; k++) lsp[j] += freq_prev[k][j]*fg[k][j];
}
return;
}
/*
extract elementary LSP from composed LSP with previous LSP
*/
void lsp_prev_extract(
FLOAT lsp[M], /* (i) Q13 : unquantized LSP parameters */
FLOAT lsp_ele[M], /* (o) Q13 : target vector */
FLOAT fg[MA_NP][M], /* (i) Q15 : MA prediction coef. */
FLOAT freq_prev[MA_NP][M], /* (i) Q13 : previous LSP vector */
FLOAT fg_sum_inv[M] /* (i) Q12 : inverse previous LSP vector */
)
{
int j, k;
/*----- compute target vectors for each MA coef.-----*/
for( j = 0 ; j < M ; j++ ) {
lsp_ele[j]=lsp[j];
for ( k = 0 ; k < MA_NP ; k++ )
lsp_ele[j] -= freq_prev[k][j] * fg[k][j];
lsp_ele[j] *= fg_sum_inv[j];
}
return;
}
/*
update previous LSP parameter
*/
void lsp_prev_update(
FLOAT lsp_ele[M], /* input : LSP vectors */
FLOAT freq_prev[MA_NP][M] /* input/output: previous LSP vectors */
)
{
int k;
for ( k = MA_NP-1 ; k > 0 ; k-- )
copy(freq_prev[k-1], freq_prev[k], M);
copy(lsp_ele, freq_prev[0], M);
return;
}
/*----------------------------------------------------------------------------
* lsp_stability - check stability of lsp coefficients
*----------------------------------------------------------------------------
*/
void lsp_stability(
FLOAT buf[] /*in/out: LSP parameters */
)
{
int j;
FLOAT diff, tmp;
for(j=0; j<M-1; j++) {
diff = buf[j+1] - buf[j];
if( diff < (F)0. ) {
tmp = buf[j+1];
buf[j+1] = buf[j];
buf[j] = tmp;
}
}
if( buf[0] < L_LIMIT ) {
buf[0] = L_LIMIT;
//printf("warning LSP Low \n");
}
for(j=0; j<M-1; j++) {
diff = buf[j+1] - buf[j];
if( diff < GAP3 ) {
buf[j+1] = buf[j]+ GAP3;
}
}
if( buf[M-1] > M_LIMIT ) {
buf[M-1] = M_LIMIT;
//printf("warning LSP High \n");
}
return;
}