-
Notifications
You must be signed in to change notification settings - Fork 0
/
lsf.c
276 lines (192 loc) · 7.48 KB
/
lsf.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
/******************************************************************
iLBC Speech Coder ANSI-C Source Code
lsf.c
Copyright (C) The Internet Society (2004).
All Rights Reserved.
******************************************************************/
#include <string.h>
#include <math.h>
#include "iLBC_define.h"
/*----------------------------------------------------------------*
* conversion from lpc coefficients to lsf coefficients
*---------------------------------------------------------------*/
void a2lsf(
float *freq,/* (o) lsf coefficients */
float *a /* (i) lpc coefficients */
){
float steps[LSF_NUMBER_OF_STEPS] =
{(float)0.00635, (float)0.003175, (float)0.0015875,
(float)0.00079375};
float step;
int step_idx;
int lsp_index;
float p[LPC_HALFORDER];
float q[LPC_HALFORDER];
float p_pre[LPC_HALFORDER];
float q_pre[LPC_HALFORDER];
float old_p, old_q, *old;
float *pq_coef;
float omega, old_omega;
int i;
float hlp, hlp1, hlp2, hlp3, hlp4, hlp5;
for (i=0; i<LPC_HALFORDER; i++) {
p[i] = (float)-1.0 * (a[i + 1] + a[LPC_FILTERORDER - i]);
q[i] = a[LPC_FILTERORDER - i] - a[i + 1];
}
p_pre[0] = (float)-1.0 - p[0];
p_pre[1] = - p_pre[0] - p[1];
p_pre[2] = - p_pre[1] - p[2];
p_pre[3] = - p_pre[2] - p[3];
p_pre[4] = - p_pre[3] - p[4];
p_pre[4] = p_pre[4] / 2;
q_pre[0] = (float)1.0 - q[0];
q_pre[1] = q_pre[0] - q[1];
q_pre[2] = q_pre[1] - q[2];
q_pre[3] = q_pre[2] - q[3];
q_pre[4] = q_pre[3] - q[4];
q_pre[4] = q_pre[4] / 2;
omega = 0.0;
old_omega = 0.0;
old_p = FLOAT_MAX;
old_q = FLOAT_MAX;
/* Here we loop through lsp_index to find all the
LPC_FILTERORDER roots for omega. */
for (lsp_index = 0; lsp_index<LPC_FILTERORDER; lsp_index++) {
/* Depending on lsp_index being even or odd, we
alternatively solve the roots for the two LSP equations. */
if ((lsp_index & 0x1) == 0) {
pq_coef = p_pre;
old = &old_p;
} else {
pq_coef = q_pre;
old = &old_q;
}
/* Start with low resolution grid */
for (step_idx = 0, step = steps[step_idx];
step_idx < LSF_NUMBER_OF_STEPS;){
/* cos(10piw) + pq(0)cos(8piw) + pq(1)cos(6piw) +
pq(2)cos(4piw) + pq(3)cod(2piw) + pq(4) */
hlp = (float)cos(omega * TWO_PI);
hlp1 = (float)2.0 * hlp + pq_coef[0];
hlp2 = (float)2.0 * hlp * hlp1 - (float)1.0 +
pq_coef[1];
hlp3 = (float)2.0 * hlp * hlp2 - hlp1 + pq_coef[2];
hlp4 = (float)2.0 * hlp * hlp3 - hlp2 + pq_coef[3];
hlp5 = hlp * hlp4 - hlp3 + pq_coef[4];
if (((hlp5 * (*old)) <= 0.0) || (omega >= 0.5)){
if (step_idx == (LSF_NUMBER_OF_STEPS - 1)){
if (fabs(hlp5) >= fabs(*old)) {
freq[lsp_index] = omega - step;
} else {
freq[lsp_index] = omega;
}
if ((*old) >= 0.0){
*old = (float)-1.0 * FLOAT_MAX;
} else {
*old = FLOAT_MAX;
}
omega = old_omega;
step_idx = 0;
step_idx = LSF_NUMBER_OF_STEPS;
} else {
if (step_idx == 0) {
old_omega = omega;
}
step_idx++;
omega -= steps[step_idx];
/* Go back one grid step */
step = steps[step_idx];
}
} else {
/* increment omega until they are of different sign,
and we know there is at least one root between omega
and old_omega */
*old = hlp5;
omega += step;
}
}
}
for (i = 0; i<LPC_FILTERORDER; i++) {
freq[i] = freq[i] * TWO_PI;
}
}
/*----------------------------------------------------------------*
* conversion from lsf coefficients to lpc coefficients
*---------------------------------------------------------------*/
void lsf2a(
float *a_coef, /* (o) lpc coefficients */
float *freq /* (i) lsf coefficients */
){
int i, j;
float hlp;
float p[LPC_HALFORDER], q[LPC_HALFORDER];
float a[LPC_HALFORDER + 1], a1[LPC_HALFORDER],
a2[LPC_HALFORDER];
float b[LPC_HALFORDER + 1], b1[LPC_HALFORDER],
b2[LPC_HALFORDER];
for (i=0; i<LPC_FILTERORDER; i++) {
freq[i] = freq[i] * PI2;
}
/* Check input for ill-conditioned cases. This part is not
found in the TIA standard. It involves the following 2 IF
blocks. If "freq" is judged ill-conditioned, then we first
modify freq[0] and freq[LPC_HALFORDER-1] (normally
LPC_HALFORDER = 10 for LPC applications), then we adjust
the other "freq" values slightly */
if ((freq[0] <= 0.0) || (freq[LPC_FILTERORDER - 1] >= 0.5)){
if (freq[0] <= 0.0) {
freq[0] = (float)0.022;
}
if (freq[LPC_FILTERORDER - 1] >= 0.5) {
freq[LPC_FILTERORDER - 1] = (float)0.499;
}
hlp = (freq[LPC_FILTERORDER - 1] - freq[0]) /
(float) (LPC_FILTERORDER - 1);
for (i=1; i<LPC_FILTERORDER; i++) {
freq[i] = freq[i - 1] + hlp;
}
}
memset(a1, 0, LPC_HALFORDER*sizeof(float));
memset(a2, 0, LPC_HALFORDER*sizeof(float));
memset(b1, 0, LPC_HALFORDER*sizeof(float));
memset(b2, 0, LPC_HALFORDER*sizeof(float));
memset(a, 0, (LPC_HALFORDER+1)*sizeof(float));
memset(b, 0, (LPC_HALFORDER+1)*sizeof(float));
/* p[i] and q[i] compute cos(2*pi*omega_{2j}) and
cos(2*pi*omega_{2j-1} in eqs. 4.2.2.2-1 and 4.2.2.2-2.
Note that for this code p[i] specifies the coefficients
used in .Q_A(z) while q[i] specifies the coefficients used
in .P_A(z) */
for (i=0; i<LPC_HALFORDER; i++) {
p[i] = (float)cos(TWO_PI * freq[2 * i]);
q[i] = (float)cos(TWO_PI * freq[2 * i + 1]);
}
a[0] = 0.25;
b[0] = 0.25;
for (i= 0; i<LPC_HALFORDER; i++) {
a[i + 1] = a[i] - 2 * p[i] * a1[i] + a2[i];
b[i + 1] = b[i] - 2 * q[i] * b1[i] + b2[i];
a2[i] = a1[i];
a1[i] = a[i];
b2[i] = b1[i];
b1[i] = b[i];
}
for (j=0; j<LPC_FILTERORDER; j++) {
if (j == 0) {
a[0] = 0.25;
b[0] = -0.25;
} else {
a[0] = b[0] = 0.0;
}
for (i=0; i<LPC_HALFORDER; i++) {
a[i + 1] = a[i] - 2 * p[i] * a1[i] + a2[i];
b[i + 1] = b[i] - 2 * q[i] * b1[i] + b2[i];
a2[i] = a1[i];
a1[i] = a[i];
b2[i] = b1[i];
b1[i] = b[i];
}
a_coef[j + 1] = 2 * (a[LPC_HALFORDER] + b[LPC_HALFORDER]);
}
a_coef[0] = 1.0;
}