blob: 29f2398d9126d1941770ee1d5d3b4e072ea9a04c [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11
12/*
13 * This file contains the function WebRtcSpl_LevinsonDurbin().
14 * The description header can be found in signal_processing_library.h
15 *
16 */
17
pbos@webrtc.orgaa30bb72013-05-27 09:49:58 +000018#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000019
20#define SPL_LEVINSON_MAXORDER 20
21
pbos@webrtc.orgb0913072013-04-09 16:40:28 +000022int16_t WebRtcSpl_LevinsonDurbin(int32_t *R, int16_t *A, int16_t *K,
23 int16_t order)
niklase@google.com470e71d2011-07-07 08:21:25 +000024{
pbos@webrtc.orgb0913072013-04-09 16:40:28 +000025 int16_t i, j;
niklase@google.com470e71d2011-07-07 08:21:25 +000026 // Auto-correlation coefficients in high precision
pbos@webrtc.orgb0913072013-04-09 16:40:28 +000027 int16_t R_hi[SPL_LEVINSON_MAXORDER + 1], R_low[SPL_LEVINSON_MAXORDER + 1];
niklase@google.com470e71d2011-07-07 08:21:25 +000028 // LPC coefficients in high precision
pbos@webrtc.orgb0913072013-04-09 16:40:28 +000029 int16_t A_hi[SPL_LEVINSON_MAXORDER + 1], A_low[SPL_LEVINSON_MAXORDER + 1];
niklase@google.com470e71d2011-07-07 08:21:25 +000030 // LPC coefficients for next iteration
pbos@webrtc.orgb0913072013-04-09 16:40:28 +000031 int16_t A_upd_hi[SPL_LEVINSON_MAXORDER + 1], A_upd_low[SPL_LEVINSON_MAXORDER + 1];
niklase@google.com470e71d2011-07-07 08:21:25 +000032 // Reflection coefficient in high precision
pbos@webrtc.orgb0913072013-04-09 16:40:28 +000033 int16_t K_hi, K_low;
niklase@google.com470e71d2011-07-07 08:21:25 +000034 // Prediction gain Alpha in high precision and with scale factor
pbos@webrtc.orgb0913072013-04-09 16:40:28 +000035 int16_t Alpha_hi, Alpha_low, Alpha_exp;
36 int16_t tmp_hi, tmp_low;
37 int32_t temp1W32, temp2W32, temp3W32;
38 int16_t norm;
niklase@google.com470e71d2011-07-07 08:21:25 +000039
40 // Normalize the autocorrelation R[0]...R[order+1]
41
42 norm = WebRtcSpl_NormW32(R[0]);
43
44 for (i = order; i >= 0; i--)
45 {
46 temp1W32 = WEBRTC_SPL_LSHIFT_W32(R[i], norm);
47 // Put R in hi and low format
bjornv@webrtc.orgf5670952014-10-29 10:29:16 +000048 R_hi[i] = (int16_t)(temp1W32 >> 16);
49 R_low[i] = (int16_t)((temp1W32 - ((int32_t)R_hi[i] << 16)) >> 1);
niklase@google.com470e71d2011-07-07 08:21:25 +000050 }
51
52 // K = A[1] = -R[1] / R[0]
53
pbos@webrtc.orgb0913072013-04-09 16:40:28 +000054 temp2W32 = WEBRTC_SPL_LSHIFT_W32((int32_t)R_hi[1],16)
55 + WEBRTC_SPL_LSHIFT_W32((int32_t)R_low[1],1); // R[1] in Q31
niklase@google.com470e71d2011-07-07 08:21:25 +000056 temp3W32 = WEBRTC_SPL_ABS_W32(temp2W32); // abs R[1]
57 temp1W32 = WebRtcSpl_DivW32HiLow(temp3W32, R_hi[0], R_low[0]); // abs(R[1])/R[0] in Q31
58 // Put back the sign on R[1]
59 if (temp2W32 > 0)
60 {
61 temp1W32 = -temp1W32;
62 }
63
64 // Put K in hi and low format
bjornv@webrtc.orgf5670952014-10-29 10:29:16 +000065 K_hi = (int16_t)(temp1W32 >> 16);
66 K_low = (int16_t)((temp1W32 - ((int32_t)K_hi << 16)) >> 1);
niklase@google.com470e71d2011-07-07 08:21:25 +000067
68 // Store first reflection coefficient
69 K[0] = K_hi;
70
bjornv@webrtc.orgf5670952014-10-29 10:29:16 +000071 temp1W32 >>= 4; // A[1] in Q27.
niklase@google.com470e71d2011-07-07 08:21:25 +000072
73 // Put A[1] in hi and low format
bjornv@webrtc.orgf5670952014-10-29 10:29:16 +000074 A_hi[1] = (int16_t)(temp1W32 >> 16);
75 A_low[1] = (int16_t)((temp1W32 - ((int32_t)A_hi[1] << 16)) >> 1);
niklase@google.com470e71d2011-07-07 08:21:25 +000076
77 // Alpha = R[0] * (1-K^2)
78
79 temp1W32 = (((WEBRTC_SPL_MUL_16_16(K_hi, K_low) >> 14) + WEBRTC_SPL_MUL_16_16(K_hi, K_hi))
80 << 1); // temp1W32 = k^2 in Q31
81
82 temp1W32 = WEBRTC_SPL_ABS_W32(temp1W32); // Guard against <0
pbos@webrtc.orgb0913072013-04-09 16:40:28 +000083 temp1W32 = (int32_t)0x7fffffffL - temp1W32; // temp1W32 = (1 - K[0]*K[0]) in Q31
niklase@google.com470e71d2011-07-07 08:21:25 +000084
85 // Store temp1W32 = 1 - K[0]*K[0] on hi and low format
bjornv@webrtc.orgf5670952014-10-29 10:29:16 +000086 tmp_hi = (int16_t)(temp1W32 >> 16);
87 tmp_low = (int16_t)((temp1W32 - ((int32_t)tmp_hi << 16)) >> 1);
niklase@google.com470e71d2011-07-07 08:21:25 +000088
89 // Calculate Alpha in Q31
90 temp1W32 = ((WEBRTC_SPL_MUL_16_16(R_hi[0], tmp_hi)
91 + (WEBRTC_SPL_MUL_16_16(R_hi[0], tmp_low) >> 15)
92 + (WEBRTC_SPL_MUL_16_16(R_low[0], tmp_hi) >> 15)) << 1);
93
94 // Normalize Alpha and put it in hi and low format
95
96 Alpha_exp = WebRtcSpl_NormW32(temp1W32);
97 temp1W32 = WEBRTC_SPL_LSHIFT_W32(temp1W32, Alpha_exp);
bjornv@webrtc.orgf5670952014-10-29 10:29:16 +000098 Alpha_hi = (int16_t)(temp1W32 >> 16);
99 Alpha_low = (int16_t)((temp1W32 - ((int32_t)Alpha_hi << 16)) >> 1);
niklase@google.com470e71d2011-07-07 08:21:25 +0000100
101 // Perform the iterative calculations in the Levinson-Durbin algorithm
102
103 for (i = 2; i <= order; i++)
104 {
105 /* ----
106 temp1W32 = R[i] + > R[j]*A[i-j]
107 /
108 ----
109 j=1..i-1
110 */
111
112 temp1W32 = 0;
113
114 for (j = 1; j < i; j++)
115 {
116 // temp1W32 is in Q31
117 temp1W32 += ((WEBRTC_SPL_MUL_16_16(R_hi[j], A_hi[i-j]) << 1)
118 + (((WEBRTC_SPL_MUL_16_16(R_hi[j], A_low[i-j]) >> 15)
119 + (WEBRTC_SPL_MUL_16_16(R_low[j], A_hi[i-j]) >> 15)) << 1));
120 }
121
122 temp1W32 = WEBRTC_SPL_LSHIFT_W32(temp1W32, 4);
pbos@webrtc.orgb0913072013-04-09 16:40:28 +0000123 temp1W32 += (WEBRTC_SPL_LSHIFT_W32((int32_t)R_hi[i], 16)
124 + WEBRTC_SPL_LSHIFT_W32((int32_t)R_low[i], 1));
niklase@google.com470e71d2011-07-07 08:21:25 +0000125
126 // K = -temp1W32 / Alpha
127 temp2W32 = WEBRTC_SPL_ABS_W32(temp1W32); // abs(temp1W32)
128 temp3W32 = WebRtcSpl_DivW32HiLow(temp2W32, Alpha_hi, Alpha_low); // abs(temp1W32)/Alpha
129
130 // Put the sign of temp1W32 back again
131 if (temp1W32 > 0)
132 {
133 temp3W32 = -temp3W32;
134 }
135
136 // Use the Alpha shifts from earlier to de-normalize
137 norm = WebRtcSpl_NormW32(temp3W32);
138 if ((Alpha_exp <= norm) || (temp3W32 == 0))
139 {
140 temp3W32 = WEBRTC_SPL_LSHIFT_W32(temp3W32, Alpha_exp);
141 } else
142 {
143 if (temp3W32 > 0)
144 {
pbos@webrtc.orgb0913072013-04-09 16:40:28 +0000145 temp3W32 = (int32_t)0x7fffffffL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000146 } else
147 {
pbos@webrtc.orgb0913072013-04-09 16:40:28 +0000148 temp3W32 = (int32_t)0x80000000L;
niklase@google.com470e71d2011-07-07 08:21:25 +0000149 }
150 }
151
152 // Put K on hi and low format
bjornv@webrtc.orgf5670952014-10-29 10:29:16 +0000153 K_hi = (int16_t)(temp3W32 >> 16);
154 K_low = (int16_t)((temp3W32 - ((int32_t)K_hi << 16)) >> 1);
niklase@google.com470e71d2011-07-07 08:21:25 +0000155
156 // Store Reflection coefficient in Q15
157 K[i - 1] = K_hi;
158
159 // Test for unstable filter.
160 // If unstable return 0 and let the user decide what to do in that case
161
pbos@webrtc.orgb0913072013-04-09 16:40:28 +0000162 if ((int32_t)WEBRTC_SPL_ABS_W16(K_hi) > (int32_t)32750)
niklase@google.com470e71d2011-07-07 08:21:25 +0000163 {
164 return 0; // Unstable filter
165 }
166
167 /*
168 Compute updated LPC coefficient: Anew[i]
169 Anew[j]= A[j] + K*A[i-j] for j=1..i-1
170 Anew[i]= K
171 */
172
173 for (j = 1; j < i; j++)
174 {
175 // temp1W32 = A[j] in Q27
pbos@webrtc.orgb0913072013-04-09 16:40:28 +0000176 temp1W32 = WEBRTC_SPL_LSHIFT_W32((int32_t)A_hi[j],16)
177 + WEBRTC_SPL_LSHIFT_W32((int32_t)A_low[j],1);
niklase@google.com470e71d2011-07-07 08:21:25 +0000178
179 // temp1W32 += K*A[i-j] in Q27
180 temp1W32 += ((WEBRTC_SPL_MUL_16_16(K_hi, A_hi[i-j])
181 + (WEBRTC_SPL_MUL_16_16(K_hi, A_low[i-j]) >> 15)
182 + (WEBRTC_SPL_MUL_16_16(K_low, A_hi[i-j]) >> 15)) << 1);
183
184 // Put Anew in hi and low format
bjornv@webrtc.orgf5670952014-10-29 10:29:16 +0000185 A_upd_hi[j] = (int16_t)(temp1W32 >> 16);
186 A_upd_low[j] = (int16_t)(
187 (temp1W32 - ((int32_t)A_upd_hi[j] << 16)) >> 1);
niklase@google.com470e71d2011-07-07 08:21:25 +0000188 }
189
190 // temp3W32 = K in Q27 (Convert from Q31 to Q27)
bjornv@webrtc.orgf5670952014-10-29 10:29:16 +0000191 temp3W32 >>= 4;
niklase@google.com470e71d2011-07-07 08:21:25 +0000192
193 // Store Anew in hi and low format
bjornv@webrtc.orgf5670952014-10-29 10:29:16 +0000194 A_upd_hi[i] = (int16_t)(temp3W32 >> 16);
195 A_upd_low[i] = (int16_t)(
196 (temp3W32 - ((int32_t)A_upd_hi[i] << 16)) >> 1);
niklase@google.com470e71d2011-07-07 08:21:25 +0000197
198 // Alpha = Alpha * (1-K^2)
199
200 temp1W32 = (((WEBRTC_SPL_MUL_16_16(K_hi, K_low) >> 14)
201 + WEBRTC_SPL_MUL_16_16(K_hi, K_hi)) << 1); // K*K in Q31
202
203 temp1W32 = WEBRTC_SPL_ABS_W32(temp1W32); // Guard against <0
pbos@webrtc.orgb0913072013-04-09 16:40:28 +0000204 temp1W32 = (int32_t)0x7fffffffL - temp1W32; // 1 - K*K in Q31
niklase@google.com470e71d2011-07-07 08:21:25 +0000205
206 // Convert 1- K^2 in hi and low format
bjornv@webrtc.orgf5670952014-10-29 10:29:16 +0000207 tmp_hi = (int16_t)(temp1W32 >> 16);
208 tmp_low = (int16_t)((temp1W32 - ((int32_t)tmp_hi << 16)) >> 1);
niklase@google.com470e71d2011-07-07 08:21:25 +0000209
210 // Calculate Alpha = Alpha * (1-K^2) in Q31
211 temp1W32 = ((WEBRTC_SPL_MUL_16_16(Alpha_hi, tmp_hi)
212 + (WEBRTC_SPL_MUL_16_16(Alpha_hi, tmp_low) >> 15)
213 + (WEBRTC_SPL_MUL_16_16(Alpha_low, tmp_hi) >> 15)) << 1);
214
215 // Normalize Alpha and store it on hi and low format
216
217 norm = WebRtcSpl_NormW32(temp1W32);
218 temp1W32 = WEBRTC_SPL_LSHIFT_W32(temp1W32, norm);
219
bjornv@webrtc.orgf5670952014-10-29 10:29:16 +0000220 Alpha_hi = (int16_t)(temp1W32 >> 16);
221 Alpha_low = (int16_t)((temp1W32 - ((int32_t)Alpha_hi << 16)) >> 1);
niklase@google.com470e71d2011-07-07 08:21:25 +0000222
223 // Update the total normalization of Alpha
224 Alpha_exp = Alpha_exp + norm;
225
226 // Update A[]
227
228 for (j = 1; j <= i; j++)
229 {
230 A_hi[j] = A_upd_hi[j];
231 A_low[j] = A_upd_low[j];
232 }
233 }
234
235 /*
236 Set A[0] to 1.0 and store the A[i] i=1...order in Q12
237 (Convert from Q27 and use rounding)
238 */
239
240 A[0] = 4096;
241
242 for (i = 1; i <= order; i++)
243 {
244 // temp1W32 in Q27
pbos@webrtc.orgb0913072013-04-09 16:40:28 +0000245 temp1W32 = WEBRTC_SPL_LSHIFT_W32((int32_t)A_hi[i], 16)
246 + WEBRTC_SPL_LSHIFT_W32((int32_t)A_low[i], 1);
niklase@google.com470e71d2011-07-07 08:21:25 +0000247 // Round and store upper word
bjornv@webrtc.orgf5670952014-10-29 10:29:16 +0000248 A[i] = (int16_t)(((temp1W32 << 1) + 32768) >> 16);
niklase@google.com470e71d2011-07-07 08:21:25 +0000249 }
250 return 1; // Stable filters
251}