/src/pjsip/third_party/ilbc/lsf.c
Line | Count | Source |
1 | | |
2 | | /****************************************************************** |
3 | | |
4 | | iLBC Speech Coder ANSI-C Source Code |
5 | | |
6 | | lsf.c |
7 | | |
8 | | Copyright (C) The Internet Society (2004). |
9 | | All Rights Reserved. |
10 | | |
11 | | ******************************************************************/ |
12 | | |
13 | | #include <string.h> |
14 | | |
15 | | |
16 | | |
17 | | |
18 | | |
19 | | #include <math.h> |
20 | | |
21 | | #include "iLBC_define.h" |
22 | | |
23 | | /*----------------------------------------------------------------* |
24 | | * conversion from lpc coefficients to lsf coefficients |
25 | | *---------------------------------------------------------------*/ |
26 | | |
27 | | void a2lsf( |
28 | | float *freq,/* (o) lsf coefficients */ |
29 | | float *a /* (i) lpc coefficients */ |
30 | 0 | ){ |
31 | 0 | float steps[LSF_NUMBER_OF_STEPS] = |
32 | 0 | {(float)0.00635, (float)0.003175, (float)0.0015875, |
33 | 0 | (float)0.00079375}; |
34 | 0 | float step; |
35 | 0 | int step_idx; |
36 | 0 | int lsp_index; |
37 | 0 | float p[LPC_HALFORDER]; |
38 | 0 | float q[LPC_HALFORDER]; |
39 | 0 | float p_pre[LPC_HALFORDER]; |
40 | 0 | float q_pre[LPC_HALFORDER]; |
41 | 0 | float old_p, old_q, *old; |
42 | 0 | float *pq_coef; |
43 | 0 | float omega, old_omega; |
44 | 0 | int i; |
45 | 0 | float hlp, hlp1, hlp2, hlp3, hlp4, hlp5; |
46 | |
|
47 | 0 | for (i=0; i<LPC_HALFORDER; i++) { |
48 | 0 | p[i] = (float)-1.0 * (a[i + 1] + a[LPC_FILTERORDER - i]); |
49 | 0 | q[i] = a[LPC_FILTERORDER - i] - a[i + 1]; |
50 | 0 | } |
51 | |
|
52 | 0 | p_pre[0] = (float)-1.0 - p[0]; |
53 | 0 | p_pre[1] = - p_pre[0] - p[1]; |
54 | 0 | p_pre[2] = - p_pre[1] - p[2]; |
55 | 0 | p_pre[3] = - p_pre[2] - p[3]; |
56 | 0 | p_pre[4] = - p_pre[3] - p[4]; |
57 | 0 | p_pre[4] = p_pre[4] / 2; |
58 | |
|
59 | 0 | q_pre[0] = (float)1.0 - q[0]; |
60 | 0 | q_pre[1] = q_pre[0] - q[1]; |
61 | 0 | q_pre[2] = q_pre[1] - q[2]; |
62 | 0 | q_pre[3] = q_pre[2] - q[3]; |
63 | 0 | q_pre[4] = q_pre[3] - q[4]; |
64 | 0 | q_pre[4] = q_pre[4] / 2; |
65 | |
|
66 | 0 | omega = 0.0; |
67 | | |
68 | | |
69 | | |
70 | | |
71 | |
|
72 | 0 | old_omega = 0.0; |
73 | |
|
74 | 0 | old_p = FLOAT_MAX; |
75 | 0 | old_q = FLOAT_MAX; |
76 | | |
77 | | /* Here we loop through lsp_index to find all the |
78 | | LPC_FILTERORDER roots for omega. */ |
79 | |
|
80 | 0 | for (lsp_index = 0; lsp_index<LPC_FILTERORDER; lsp_index++) { |
81 | | |
82 | | /* Depending on lsp_index being even or odd, we |
83 | | alternatively solve the roots for the two LSP equations. */ |
84 | | |
85 | |
|
86 | 0 | if ((lsp_index & 0x1) == 0) { |
87 | 0 | pq_coef = p_pre; |
88 | 0 | old = &old_p; |
89 | 0 | } else { |
90 | 0 | pq_coef = q_pre; |
91 | 0 | old = &old_q; |
92 | 0 | } |
93 | | |
94 | | /* Start with low resolution grid */ |
95 | |
|
96 | 0 | for (step_idx = 0, step = steps[step_idx]; |
97 | 0 | step_idx < LSF_NUMBER_OF_STEPS;){ |
98 | | |
99 | | /* cos(10piw) + pq(0)cos(8piw) + pq(1)cos(6piw) + |
100 | | pq(2)cos(4piw) + pq(3)cod(2piw) + pq(4) */ |
101 | |
|
102 | 0 | hlp = (float)cos(omega * TWO_PI); |
103 | 0 | hlp1 = (float)2.0 * hlp + pq_coef[0]; |
104 | 0 | hlp2 = (float)2.0 * hlp * hlp1 - (float)1.0 + |
105 | 0 | pq_coef[1]; |
106 | 0 | hlp3 = (float)2.0 * hlp * hlp2 - hlp1 + pq_coef[2]; |
107 | 0 | hlp4 = (float)2.0 * hlp * hlp3 - hlp2 + pq_coef[3]; |
108 | 0 | hlp5 = hlp * hlp4 - hlp3 + pq_coef[4]; |
109 | | |
110 | |
|
111 | 0 | if (((hlp5 * (*old)) <= 0.0) || (omega >= 0.5)){ |
112 | |
|
113 | 0 | if (step_idx == (LSF_NUMBER_OF_STEPS - 1)){ |
114 | |
|
115 | 0 | if (fabs(hlp5) >= fabs(*old)) { |
116 | 0 | freq[lsp_index] = omega - step; |
117 | 0 | } else { |
118 | 0 | freq[lsp_index] = omega; |
119 | 0 | } |
120 | | |
121 | | |
122 | | |
123 | | |
124 | | |
125 | | |
126 | |
|
127 | 0 | if ((*old) >= 0.0){ |
128 | 0 | *old = (float)-1.0 * FLOAT_MAX; |
129 | 0 | } else { |
130 | 0 | *old = FLOAT_MAX; |
131 | 0 | } |
132 | |
|
133 | 0 | omega = old_omega; |
134 | 0 | step_idx = 0; |
135 | |
|
136 | 0 | step_idx = LSF_NUMBER_OF_STEPS; |
137 | 0 | } else { |
138 | |
|
139 | 0 | if (step_idx == 0) { |
140 | 0 | old_omega = omega; |
141 | 0 | } |
142 | |
|
143 | 0 | step_idx++; |
144 | 0 | omega -= steps[step_idx]; |
145 | | |
146 | | /* Go back one grid step */ |
147 | |
|
148 | 0 | step = steps[step_idx]; |
149 | 0 | } |
150 | 0 | } else { |
151 | | |
152 | | /* increment omega until they are of different sign, |
153 | | and we know there is at least one root between omega |
154 | | and old_omega */ |
155 | 0 | *old = hlp5; |
156 | 0 | omega += step; |
157 | 0 | } |
158 | 0 | } |
159 | 0 | } |
160 | |
|
161 | 0 | for (i = 0; i<LPC_FILTERORDER; i++) { |
162 | 0 | freq[i] = freq[i] * TWO_PI; |
163 | 0 | } |
164 | 0 | } |
165 | | |
166 | | /*----------------------------------------------------------------* |
167 | | * conversion from lsf coefficients to lpc coefficients |
168 | | *---------------------------------------------------------------*/ |
169 | | |
170 | | void lsf2a( |
171 | | float *a_coef, /* (o) lpc coefficients */ |
172 | | float *freq /* (i) lsf coefficients */ |
173 | | |
174 | | |
175 | | |
176 | | |
177 | | |
178 | 0 | ){ |
179 | 0 | int i, j; |
180 | 0 | float hlp; |
181 | 0 | float p[LPC_HALFORDER], q[LPC_HALFORDER]; |
182 | 0 | float a[LPC_HALFORDER + 1], a1[LPC_HALFORDER], |
183 | 0 | a2[LPC_HALFORDER]; |
184 | 0 | float b[LPC_HALFORDER + 1], b1[LPC_HALFORDER], |
185 | 0 | b2[LPC_HALFORDER]; |
186 | |
|
187 | 0 | for (i=0; i<LPC_FILTERORDER; i++) { |
188 | 0 | freq[i] = freq[i] * PI2; |
189 | 0 | } |
190 | | |
191 | | /* Check input for ill-conditioned cases. This part is not |
192 | | found in the TIA standard. It involves the following 2 IF |
193 | | blocks. If "freq" is judged ill-conditioned, then we first |
194 | | modify freq[0] and freq[LPC_HALFORDER-1] (normally |
195 | | LPC_HALFORDER = 10 for LPC applications), then we adjust |
196 | | the other "freq" values slightly */ |
197 | | |
198 | |
|
199 | 0 | if ((freq[0] <= 0.0) || (freq[LPC_FILTERORDER - 1] >= 0.5)){ |
200 | | |
201 | |
|
202 | 0 | if (freq[0] <= 0.0) { |
203 | 0 | freq[0] = (float)0.022; |
204 | 0 | } |
205 | | |
206 | |
|
207 | 0 | if (freq[LPC_FILTERORDER - 1] >= 0.5) { |
208 | 0 | freq[LPC_FILTERORDER - 1] = (float)0.499; |
209 | 0 | } |
210 | |
|
211 | 0 | hlp = (freq[LPC_FILTERORDER - 1] - freq[0]) / |
212 | 0 | (float) (LPC_FILTERORDER - 1); |
213 | |
|
214 | 0 | for (i=1; i<LPC_FILTERORDER; i++) { |
215 | 0 | freq[i] = freq[i - 1] + hlp; |
216 | 0 | } |
217 | 0 | } |
218 | |
|
219 | 0 | memset(a1, 0, LPC_HALFORDER*sizeof(float)); |
220 | 0 | memset(a2, 0, LPC_HALFORDER*sizeof(float)); |
221 | 0 | memset(b1, 0, LPC_HALFORDER*sizeof(float)); |
222 | 0 | memset(b2, 0, LPC_HALFORDER*sizeof(float)); |
223 | 0 | memset(a, 0, (LPC_HALFORDER+1)*sizeof(float)); |
224 | 0 | memset(b, 0, (LPC_HALFORDER+1)*sizeof(float)); |
225 | | |
226 | | |
227 | | |
228 | | |
229 | | |
230 | | |
231 | | /* p[i] and q[i] compute cos(2*pi*omega_{2j}) and |
232 | | cos(2*pi*omega_{2j-1} in eqs. 4.2.2.2-1 and 4.2.2.2-2. |
233 | | Note that for this code p[i] specifies the coefficients |
234 | | used in .Q_A(z) while q[i] specifies the coefficients used |
235 | | in .P_A(z) */ |
236 | |
|
237 | 0 | for (i=0; i<LPC_HALFORDER; i++) { |
238 | 0 | p[i] = (float)cos(TWO_PI * freq[2 * i]); |
239 | 0 | q[i] = (float)cos(TWO_PI * freq[2 * i + 1]); |
240 | 0 | } |
241 | |
|
242 | 0 | a[0] = 0.25; |
243 | 0 | b[0] = 0.25; |
244 | |
|
245 | 0 | for (i= 0; i<LPC_HALFORDER; i++) { |
246 | 0 | a[i + 1] = a[i] - 2 * p[i] * a1[i] + a2[i]; |
247 | 0 | b[i + 1] = b[i] - 2 * q[i] * b1[i] + b2[i]; |
248 | 0 | a2[i] = a1[i]; |
249 | 0 | a1[i] = a[i]; |
250 | 0 | b2[i] = b1[i]; |
251 | 0 | b1[i] = b[i]; |
252 | 0 | } |
253 | |
|
254 | 0 | for (j=0; j<LPC_FILTERORDER; j++) { |
255 | |
|
256 | 0 | if (j == 0) { |
257 | 0 | a[0] = 0.25; |
258 | 0 | b[0] = -0.25; |
259 | 0 | } else { |
260 | 0 | a[0] = b[0] = 0.0; |
261 | 0 | } |
262 | |
|
263 | 0 | for (i=0; i<LPC_HALFORDER; i++) { |
264 | 0 | a[i + 1] = a[i] - 2 * p[i] * a1[i] + a2[i]; |
265 | 0 | b[i + 1] = b[i] - 2 * q[i] * b1[i] + b2[i]; |
266 | 0 | a2[i] = a1[i]; |
267 | 0 | a1[i] = a[i]; |
268 | 0 | b2[i] = b1[i]; |
269 | 0 | b1[i] = b[i]; |
270 | 0 | } |
271 | |
|
272 | 0 | a_coef[j + 1] = 2 * (a[LPC_HALFORDER] + b[LPC_HALFORDER]); |
273 | 0 | } |
274 | |
|
275 | 0 | a_coef[0] = 1.0; |
276 | 0 | } |
277 | | |
278 | | |
279 | | |
280 | | |
281 | | |
282 | | |
283 | | |