-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlpcfunc.c
311 lines (248 loc) · 10.1 KB
/
lpcfunc.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
/*
ITU-T G.729A Speech Coder ANSI-C Source Code
Version 1.1 Last modified: September 1996
Copyright (c) 1996,
AT&T, France Telecom, NTT, Universite de Sherbrooke, Lucent Technologies
All rights reserved.
*/
/*-------------------------------------------------------------*
* Procedure Lsp_Az: *
* ~~~~~~ *
* Compute the LPC coefficients from lsp (order=10) *
*-------------------------------------------------------------*/
#include "typedef.h"
#include "basic_op.h"
#include "oper_32b.h"
#include "ld8a.h"
#include "tab_ld8a.h"
/* local function */
static void Get_lsp_pol(int16_t *lsp, int32_t *f);
void Lsp_Az(
int16_t lsp[], /* (i) Q15 : line spectral frequencies */
int16_t a[] /* (o) Q12 : predictor coefficients (order = 10) */
)
{
int16_t i, j;
int32_t f1[6], f2[6];
int32_t t0;
Get_lsp_pol(&lsp[0],f1);
Get_lsp_pol(&lsp[1],f2);
for (i = 5; i > 0; i--)
{
f1[i] = L_add(f1[i], f1[i-1]); /* f1[i] += f1[i-1]; */
f2[i] = L_sub(f2[i], f2[i-1]); /* f2[i] -= f2[i-1]; */
}
a[0] = 4096;
for (i = 1, j = 10; i <= 5; i++, j--)
{
t0 = L_add(f1[i], f2[i]); /* f1[i] + f2[i] */
a[i] = extract_l( L_shr_r(t0, 13) ); /* from Q24 to Q12 and * 0.5 */
t0 = L_sub(f1[i], f2[i]); /* f1[i] - f2[i] */
a[j] = extract_l( L_shr_r(t0, 13) ); /* from Q24 to Q12 and * 0.5 */
}
return;
}
/*-----------------------------------------------------------*
* procedure Get_lsp_pol: *
* ~~~~~~~~~~~ *
* Find the polynomial F1(z) or F2(z) from the LSPs *
*-----------------------------------------------------------*
* *
* Parameters: *
* lsp[] : line spectral freq. (cosine domain) in Q15 *
* f[] : the coefficients of F1 or F2 in Q24 *
*-----------------------------------------------------------*/
static void Get_lsp_pol(int16_t *lsp, int32_t *f)
{
int16_t i,j, hi, lo;
int32_t t0;
/* All computation in Q24 */
*f = L_mult(4096, 2048); /* f[0] = 1.0; in Q24 */
f++;
*f = L_msu((int32_t)0, *lsp, 512); /* f[1] = -2.0 * lsp[0]; in Q24 */
f++;
lsp += 2; /* Advance lsp pointer */
for(i=2; i<=5; i++)
{
*f = f[-2];
for(j=1; j<i; j++, f--)
{
L_Extract(f[-1] ,&hi, &lo);
t0 = Mpy_32_16(hi, lo, *lsp); /* t0 = f[-1] * lsp */
t0 = L_shl(t0, 1);
*f = L_add(*f, f[-2]); /* *f += f[-2] */
*f = L_sub(*f, t0); /* *f -= t0 */
}
*f = L_msu(*f, *lsp, 512); /* *f -= lsp<<9 */
f += i; /* Advance f pointer */
lsp += 2; /* Advance lsp pointer */
}
return;
}
/*___________________________________________________________________________
| |
| Functions : Lsp_lsf and Lsf_lsp |
| |
| Lsp_lsf Transformation lsp to lsf |
| Lsf_lsp Transformation lsf to lsp |
|---------------------------------------------------------------------------|
| Algorithm: |
| |
| The transformation from lsp[i] to lsf[i] and lsf[i] to lsp[i] are |
| approximated by a look-up table and interpolation. |
|___________________________________________________________________________|
*/
void Lsf_lsp(
int16_t lsf[], /* (i) Q15 : lsf[m] normalized (range: 0.0<=val<=0.5) */
int16_t lsp[], /* (o) Q15 : lsp[m] (range: -1<=val<1) */
int16_t m /* (i) : LPC order */
)
{
int16_t i, ind, offset;
int32_t L_tmp;
for(i=0; i<m; i++)
{
ind = shr(lsf[i], 8); /* ind = b8-b15 of lsf[i] */
offset = lsf[i] & (int16_t)0x00ff; /* offset = b0-b7 of lsf[i] */
/* lsp[i] = table[ind]+ ((table[ind+1]-table[ind])*offset) / 256 */
L_tmp = L_mult(sub(table[ind+1], table[ind]), offset);
lsp[i] = add(table[ind], extract_l(L_shr(L_tmp, 9)));
}
return;
}
void Lsp_lsf(
int16_t lsp[], /* (i) Q15 : lsp[m] (range: -1<=val<1) */
int16_t lsf[], /* (o) Q15 : lsf[m] normalized (range: 0.0<=val<=0.5) */
int16_t m /* (i) : LPC order */
)
{
int16_t i, ind, tmp;
int32_t L_tmp;
ind = 63; /* begin at end of table -1 */
for(i= m-(int16_t)1; i >= 0; i--)
{
/* find value in table that is just greater than lsp[i] */
while( sub(table[ind], lsp[i]) < 0 )
{
ind = sub(ind,1);
}
/* acos(lsp[i])= ind*256 + ( ( lsp[i]-table[ind] ) * slope[ind] )/4096 */
L_tmp = L_mult( sub(lsp[i], table[ind]) , slope[ind] );
tmp = _round(L_shl(L_tmp, 3)); /*(lsp[i]-table[ind])*slope[ind])>>12*/
lsf[i] = add(tmp, shl(ind, 8));
}
return;
}
/*___________________________________________________________________________
| |
| Functions : Lsp_lsf and Lsf_lsp |
| |
| Lsp_lsf Transformation lsp to lsf |
| Lsf_lsp Transformation lsf to lsp |
|---------------------------------------------------------------------------|
| Algorithm: |
| |
| The transformation from lsp[i] to lsf[i] and lsf[i] to lsp[i] are |
| approximated by a look-up table and interpolation. |
|___________________________________________________________________________|
*/
void Lsf_lsp2(
int16_t lsf[], /* (i) Q13 : lsf[m] (range: 0.0<=val<PI) */
int16_t lsp[], /* (o) Q15 : lsp[m] (range: -1<=val<1) */
int16_t m /* (i) : LPC order */
)
{
int16_t i, ind;
int16_t offset; /* in Q8 */
int16_t freq; /* normalized frequency in Q15 */
int32_t L_tmp;
for(i=0; i<m; i++)
{
/* freq = abs_s(freq);*/
freq = mult(lsf[i], 20861); /* 20861: 1.0/(2.0*PI) in Q17 */
ind = shr(freq, 8); /* ind = b8-b15 of freq */
offset = freq & (int16_t)0x00ff; /* offset = b0-b7 of freq */
if ( sub(ind, 63)>0 ){
ind = 63; /* 0 <= ind <= 63 */
}
/* lsp[i] = table2[ind]+ (slope_cos[ind]*offset >> 12) */
L_tmp = L_mult(slope_cos[ind], offset); /* L_tmp in Q28 */
lsp[i] = add(table2[ind], extract_l(L_shr(L_tmp, 13)));
}
return;
}
void Lsp_lsf2(
int16_t lsp[], /* (i) Q15 : lsp[m] (range: -1<=val<1) */
int16_t lsf[], /* (o) Q13 : lsf[m] (range: 0.0<=val<PI) */
int16_t m /* (i) : LPC order */
)
{
int16_t i, ind;
int16_t offset; /* in Q15 */
int16_t freq; /* normalized frequency in Q16 */
int32_t L_tmp;
ind = 63; /* begin at end of table2 -1 */
for(i= m-(int16_t)1; i >= 0; i--)
{
/* find value in table2 that is just greater than lsp[i] */
while( sub(table2[ind], lsp[i]) < 0 )
{
ind = sub(ind,1);
if ( ind <= 0 )
break;
}
offset = sub(lsp[i], table2[ind]);
/* acos(lsp[i])= ind*512 + (slope_acos[ind]*offset >> 11) */
L_tmp = L_mult( slope_acos[ind], offset ); /* L_tmp in Q28 */
freq = add(shl(ind, 9), extract_l(L_shr(L_tmp, 12)));
lsf[i] = mult(freq, 25736); /* 25736: 2.0*PI in Q12 */
}
return;
}
/*-------------------------------------------------------------*
* procedure Weight_Az *
* ~~~~~~~~~ *
* Weighting of LPC coefficients. *
* ap[i] = a[i] * (gamma ** i) *
* *
*-------------------------------------------------------------*/
void Weight_Az(
int16_t a[], /* (i) Q12 : a[m+1] LPC coefficients */
int16_t gamma, /* (i) Q15 : Spectral expansion factor. */
int16_t m, /* (i) : LPC order. */
int16_t ap[] /* (o) Q12 : Spectral expanded LPC coefficients */
)
{
int16_t i, fac;
ap[0] = a[0];
fac = gamma;
for(i=1; i<m; i++)
{
ap[i] = _round( L_mult(a[i], fac) );
fac = _round( L_mult(fac, gamma) );
}
ap[m] = _round( L_mult(a[m], fac) );
return;
}
/*----------------------------------------------------------------------*
* Function Int_qlpc() *
* ~~~~~~~~~~~~~~~~~~~ *
* Interpolation of the LPC parameters. *
*----------------------------------------------------------------------*/
/* Interpolation of the quantized LSP's */
void Int_qlpc(
int16_t lsp_old[], /* input : LSP vector of past frame */
int16_t lsp_new[], /* input : LSP vector of present frame */
int16_t Az[] /* output: interpolated Az() for the 2 subframes */
)
{
int16_t i;
int16_t lsp[M];
/* lsp[i] = lsp_new[i] * 0.5 + lsp_old[i] * 0.5 */
for (i = 0; i < M; i++) {
lsp[i] = add(shr(lsp_new[i], 1), shr(lsp_old[i], 1));
}
Lsp_Az(lsp, Az); /* Subframe 1 */
Lsp_Az(lsp_new, &Az[MP1]); /* Subframe 2 */
return;
}