niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
| 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.org | aa30bb7 | 2013-05-27 09:49:58 +0000 | [diff] [blame] | 18 | #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 19 | |
| 20 | #define SPL_LEVINSON_MAXORDER 20 |
| 21 | |
bjornv@webrtc.org | 88a4298 | 2015-01-12 05:53:43 +0000 | [diff] [blame] | 22 | int16_t WebRtcSpl_LevinsonDurbin(const int32_t* R, int16_t* A, int16_t* K, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 23 | size_t order) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 24 | { |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 25 | size_t i, j; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 26 | // Auto-correlation coefficients in high precision |
pbos@webrtc.org | b091307 | 2013-04-09 16:40:28 +0000 | [diff] [blame] | 27 | int16_t R_hi[SPL_LEVINSON_MAXORDER + 1], R_low[SPL_LEVINSON_MAXORDER + 1]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 28 | // LPC coefficients in high precision |
pbos@webrtc.org | b091307 | 2013-04-09 16:40:28 +0000 | [diff] [blame] | 29 | int16_t A_hi[SPL_LEVINSON_MAXORDER + 1], A_low[SPL_LEVINSON_MAXORDER + 1]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 30 | // LPC coefficients for next iteration |
pbos@webrtc.org | b091307 | 2013-04-09 16:40:28 +0000 | [diff] [blame] | 31 | int16_t A_upd_hi[SPL_LEVINSON_MAXORDER + 1], A_upd_low[SPL_LEVINSON_MAXORDER + 1]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 32 | // Reflection coefficient in high precision |
pbos@webrtc.org | b091307 | 2013-04-09 16:40:28 +0000 | [diff] [blame] | 33 | int16_t K_hi, K_low; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 34 | // Prediction gain Alpha in high precision and with scale factor |
pbos@webrtc.org | b091307 | 2013-04-09 16:40:28 +0000 | [diff] [blame] | 35 | 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 39 | |
| 40 | // Normalize the autocorrelation R[0]...R[order+1] |
| 41 | |
| 42 | norm = WebRtcSpl_NormW32(R[0]); |
| 43 | |
Peter Kasting | f045e4d | 2015-06-10 21:15:38 -0700 | [diff] [blame] | 44 | for (i = 0; i <= order; ++i) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 45 | { |
henrik.lundin | 79dfdad | 2016-11-15 01:45:53 -0800 | [diff] [blame] | 46 | temp1W32 = R[i] * (1 << norm); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 47 | // Put R in hi and low format |
bjornv@webrtc.org | f567095 | 2014-10-29 10:29:16 +0000 | [diff] [blame] | 48 | R_hi[i] = (int16_t)(temp1W32 >> 16); |
henrik.lundin | 79dfdad | 2016-11-15 01:45:53 -0800 | [diff] [blame] | 49 | R_low[i] = (int16_t)((temp1W32 - ((int32_t)R_hi[i] * 65536)) >> 1); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | // K = A[1] = -R[1] / R[0] |
| 53 | |
ivoc | 7b25166 | 2016-12-14 01:59:59 -0800 | [diff] [blame] | 54 | temp2W32 = R[1] * (1 << norm); // R[1] in Q31 |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 55 | temp3W32 = WEBRTC_SPL_ABS_W32(temp2W32); // abs R[1] |
| 56 | temp1W32 = WebRtcSpl_DivW32HiLow(temp3W32, R_hi[0], R_low[0]); // abs(R[1])/R[0] in Q31 |
| 57 | // Put back the sign on R[1] |
| 58 | if (temp2W32 > 0) |
| 59 | { |
| 60 | temp1W32 = -temp1W32; |
| 61 | } |
| 62 | |
| 63 | // Put K in hi and low format |
bjornv@webrtc.org | f567095 | 2014-10-29 10:29:16 +0000 | [diff] [blame] | 64 | K_hi = (int16_t)(temp1W32 >> 16); |
henrik.lundin | 79dfdad | 2016-11-15 01:45:53 -0800 | [diff] [blame] | 65 | K_low = (int16_t)((temp1W32 - ((int32_t)K_hi * 65536)) >> 1); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 66 | |
| 67 | // Store first reflection coefficient |
| 68 | K[0] = K_hi; |
| 69 | |
bjornv@webrtc.org | f567095 | 2014-10-29 10:29:16 +0000 | [diff] [blame] | 70 | temp1W32 >>= 4; // A[1] in Q27. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 71 | |
| 72 | // Put A[1] in hi and low format |
bjornv@webrtc.org | f567095 | 2014-10-29 10:29:16 +0000 | [diff] [blame] | 73 | A_hi[1] = (int16_t)(temp1W32 >> 16); |
henrik.lundin | 79dfdad | 2016-11-15 01:45:53 -0800 | [diff] [blame] | 74 | A_low[1] = (int16_t)((temp1W32 - ((int32_t)A_hi[1] * 65536)) >> 1); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 75 | |
| 76 | // Alpha = R[0] * (1-K^2) |
| 77 | |
Bjorn Volcker | affcfb2 | 2015-04-24 08:12:07 +0200 | [diff] [blame] | 78 | temp1W32 = ((K_hi * K_low >> 14) + K_hi * K_hi) << 1; // = k^2 in Q31 |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 79 | |
| 80 | temp1W32 = WEBRTC_SPL_ABS_W32(temp1W32); // Guard against <0 |
pbos@webrtc.org | b091307 | 2013-04-09 16:40:28 +0000 | [diff] [blame] | 81 | temp1W32 = (int32_t)0x7fffffffL - temp1W32; // temp1W32 = (1 - K[0]*K[0]) in Q31 |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 82 | |
| 83 | // Store temp1W32 = 1 - K[0]*K[0] on hi and low format |
bjornv@webrtc.org | f567095 | 2014-10-29 10:29:16 +0000 | [diff] [blame] | 84 | tmp_hi = (int16_t)(temp1W32 >> 16); |
| 85 | tmp_low = (int16_t)((temp1W32 - ((int32_t)tmp_hi << 16)) >> 1); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 86 | |
| 87 | // Calculate Alpha in Q31 |
Bjorn Volcker | affcfb2 | 2015-04-24 08:12:07 +0200 | [diff] [blame] | 88 | temp1W32 = (R_hi[0] * tmp_hi + (R_hi[0] * tmp_low >> 15) + |
| 89 | (R_low[0] * tmp_hi >> 15)) << 1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 90 | |
| 91 | // Normalize Alpha and put it in hi and low format |
| 92 | |
| 93 | Alpha_exp = WebRtcSpl_NormW32(temp1W32); |
| 94 | temp1W32 = WEBRTC_SPL_LSHIFT_W32(temp1W32, Alpha_exp); |
bjornv@webrtc.org | f567095 | 2014-10-29 10:29:16 +0000 | [diff] [blame] | 95 | Alpha_hi = (int16_t)(temp1W32 >> 16); |
| 96 | Alpha_low = (int16_t)((temp1W32 - ((int32_t)Alpha_hi << 16)) >> 1); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 97 | |
| 98 | // Perform the iterative calculations in the Levinson-Durbin algorithm |
| 99 | |
| 100 | for (i = 2; i <= order; i++) |
| 101 | { |
| 102 | /* ---- |
| 103 | temp1W32 = R[i] + > R[j]*A[i-j] |
| 104 | / |
| 105 | ---- |
| 106 | j=1..i-1 |
| 107 | */ |
| 108 | |
| 109 | temp1W32 = 0; |
| 110 | |
| 111 | for (j = 1; j < i; j++) |
| 112 | { |
Bjorn Volcker | affcfb2 | 2015-04-24 08:12:07 +0200 | [diff] [blame] | 113 | // temp1W32 is in Q31 |
henrik.lundin | 79dfdad | 2016-11-15 01:45:53 -0800 | [diff] [blame] | 114 | temp1W32 += (R_hi[j] * A_hi[i - j] * 2) + |
Bjorn Volcker | affcfb2 | 2015-04-24 08:12:07 +0200 | [diff] [blame] | 115 | (((R_hi[j] * A_low[i - j] >> 15) + |
henrik.lundin | 79dfdad | 2016-11-15 01:45:53 -0800 | [diff] [blame] | 116 | (R_low[j] * A_hi[i - j] >> 15)) * 2); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 117 | } |
| 118 | |
henrik.lundin | 79dfdad | 2016-11-15 01:45:53 -0800 | [diff] [blame] | 119 | temp1W32 = temp1W32 * 16; |
| 120 | temp1W32 += ((int32_t)R_hi[i] * 65536) |
| 121 | + WEBRTC_SPL_LSHIFT_W32((int32_t)R_low[i], 1); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 122 | |
| 123 | // K = -temp1W32 / Alpha |
| 124 | temp2W32 = WEBRTC_SPL_ABS_W32(temp1W32); // abs(temp1W32) |
| 125 | temp3W32 = WebRtcSpl_DivW32HiLow(temp2W32, Alpha_hi, Alpha_low); // abs(temp1W32)/Alpha |
| 126 | |
| 127 | // Put the sign of temp1W32 back again |
| 128 | if (temp1W32 > 0) |
| 129 | { |
| 130 | temp3W32 = -temp3W32; |
| 131 | } |
| 132 | |
| 133 | // Use the Alpha shifts from earlier to de-normalize |
| 134 | norm = WebRtcSpl_NormW32(temp3W32); |
| 135 | if ((Alpha_exp <= norm) || (temp3W32 == 0)) |
| 136 | { |
henrik.lundin | 79dfdad | 2016-11-15 01:45:53 -0800 | [diff] [blame] | 137 | temp3W32 = temp3W32 * (1 << Alpha_exp); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 138 | } else |
| 139 | { |
| 140 | if (temp3W32 > 0) |
| 141 | { |
pbos@webrtc.org | b091307 | 2013-04-09 16:40:28 +0000 | [diff] [blame] | 142 | temp3W32 = (int32_t)0x7fffffffL; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 143 | } else |
| 144 | { |
pbos@webrtc.org | b091307 | 2013-04-09 16:40:28 +0000 | [diff] [blame] | 145 | temp3W32 = (int32_t)0x80000000L; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| 149 | // Put K on hi and low format |
bjornv@webrtc.org | f567095 | 2014-10-29 10:29:16 +0000 | [diff] [blame] | 150 | K_hi = (int16_t)(temp3W32 >> 16); |
henrik.lundin | 79dfdad | 2016-11-15 01:45:53 -0800 | [diff] [blame] | 151 | K_low = (int16_t)((temp3W32 - ((int32_t)K_hi * 65536)) >> 1); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 152 | |
| 153 | // Store Reflection coefficient in Q15 |
| 154 | K[i - 1] = K_hi; |
| 155 | |
| 156 | // Test for unstable filter. |
| 157 | // If unstable return 0 and let the user decide what to do in that case |
| 158 | |
pbos@webrtc.org | b091307 | 2013-04-09 16:40:28 +0000 | [diff] [blame] | 159 | if ((int32_t)WEBRTC_SPL_ABS_W16(K_hi) > (int32_t)32750) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 160 | { |
| 161 | return 0; // Unstable filter |
| 162 | } |
| 163 | |
| 164 | /* |
| 165 | Compute updated LPC coefficient: Anew[i] |
| 166 | Anew[j]= A[j] + K*A[i-j] for j=1..i-1 |
| 167 | Anew[i]= K |
| 168 | */ |
| 169 | |
| 170 | for (j = 1; j < i; j++) |
| 171 | { |
| 172 | // temp1W32 = A[j] in Q27 |
henrik.lundin | 79dfdad | 2016-11-15 01:45:53 -0800 | [diff] [blame] | 173 | temp1W32 = (int32_t)A_hi[j] * 65536 |
pbos@webrtc.org | b091307 | 2013-04-09 16:40:28 +0000 | [diff] [blame] | 174 | + WEBRTC_SPL_LSHIFT_W32((int32_t)A_low[j],1); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 175 | |
| 176 | // temp1W32 += K*A[i-j] in Q27 |
Bjorn Volcker | affcfb2 | 2015-04-24 08:12:07 +0200 | [diff] [blame] | 177 | temp1W32 += (K_hi * A_hi[i - j] + (K_hi * A_low[i - j] >> 15) + |
henrik.lundin | 79dfdad | 2016-11-15 01:45:53 -0800 | [diff] [blame] | 178 | (K_low * A_hi[i - j] >> 15)) * 2; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 179 | |
| 180 | // Put Anew in hi and low format |
bjornv@webrtc.org | f567095 | 2014-10-29 10:29:16 +0000 | [diff] [blame] | 181 | A_upd_hi[j] = (int16_t)(temp1W32 >> 16); |
| 182 | A_upd_low[j] = (int16_t)( |
henrik.lundin | 79dfdad | 2016-11-15 01:45:53 -0800 | [diff] [blame] | 183 | (temp1W32 - ((int32_t)A_upd_hi[j] * 65536)) >> 1); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | // temp3W32 = K in Q27 (Convert from Q31 to Q27) |
bjornv@webrtc.org | f567095 | 2014-10-29 10:29:16 +0000 | [diff] [blame] | 187 | temp3W32 >>= 4; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 188 | |
| 189 | // Store Anew in hi and low format |
bjornv@webrtc.org | f567095 | 2014-10-29 10:29:16 +0000 | [diff] [blame] | 190 | A_upd_hi[i] = (int16_t)(temp3W32 >> 16); |
| 191 | A_upd_low[i] = (int16_t)( |
henrik.lundin | 79dfdad | 2016-11-15 01:45:53 -0800 | [diff] [blame] | 192 | (temp3W32 - ((int32_t)A_upd_hi[i] * 65536)) >> 1); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 193 | |
| 194 | // Alpha = Alpha * (1-K^2) |
| 195 | |
ivoc | abf1752 | 2017-01-10 03:37:20 -0800 | [diff] [blame^] | 196 | temp1W32 = ((K_hi * K_low >> 14) + K_hi * K_hi) * 2; // K*K in Q31 |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 197 | |
| 198 | temp1W32 = WEBRTC_SPL_ABS_W32(temp1W32); // Guard against <0 |
pbos@webrtc.org | b091307 | 2013-04-09 16:40:28 +0000 | [diff] [blame] | 199 | temp1W32 = (int32_t)0x7fffffffL - temp1W32; // 1 - K*K in Q31 |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 200 | |
| 201 | // Convert 1- K^2 in hi and low format |
bjornv@webrtc.org | f567095 | 2014-10-29 10:29:16 +0000 | [diff] [blame] | 202 | tmp_hi = (int16_t)(temp1W32 >> 16); |
| 203 | tmp_low = (int16_t)((temp1W32 - ((int32_t)tmp_hi << 16)) >> 1); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 204 | |
| 205 | // Calculate Alpha = Alpha * (1-K^2) in Q31 |
Bjorn Volcker | affcfb2 | 2015-04-24 08:12:07 +0200 | [diff] [blame] | 206 | temp1W32 = (Alpha_hi * tmp_hi + (Alpha_hi * tmp_low >> 15) + |
| 207 | (Alpha_low * tmp_hi >> 15)) << 1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 208 | |
| 209 | // Normalize Alpha and store it on hi and low format |
| 210 | |
| 211 | norm = WebRtcSpl_NormW32(temp1W32); |
| 212 | temp1W32 = WEBRTC_SPL_LSHIFT_W32(temp1W32, norm); |
| 213 | |
bjornv@webrtc.org | f567095 | 2014-10-29 10:29:16 +0000 | [diff] [blame] | 214 | Alpha_hi = (int16_t)(temp1W32 >> 16); |
| 215 | Alpha_low = (int16_t)((temp1W32 - ((int32_t)Alpha_hi << 16)) >> 1); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 216 | |
| 217 | // Update the total normalization of Alpha |
| 218 | Alpha_exp = Alpha_exp + norm; |
| 219 | |
| 220 | // Update A[] |
| 221 | |
| 222 | for (j = 1; j <= i; j++) |
| 223 | { |
| 224 | A_hi[j] = A_upd_hi[j]; |
| 225 | A_low[j] = A_upd_low[j]; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | /* |
| 230 | Set A[0] to 1.0 and store the A[i] i=1...order in Q12 |
| 231 | (Convert from Q27 and use rounding) |
| 232 | */ |
| 233 | |
| 234 | A[0] = 4096; |
| 235 | |
| 236 | for (i = 1; i <= order; i++) |
| 237 | { |
| 238 | // temp1W32 in Q27 |
henrik.lundin | 79dfdad | 2016-11-15 01:45:53 -0800 | [diff] [blame] | 239 | temp1W32 = (int32_t)A_hi[i] * 65536 |
pbos@webrtc.org | b091307 | 2013-04-09 16:40:28 +0000 | [diff] [blame] | 240 | + WEBRTC_SPL_LSHIFT_W32((int32_t)A_low[i], 1); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 241 | // Round and store upper word |
henrik.lundin | 79dfdad | 2016-11-15 01:45:53 -0800 | [diff] [blame] | 242 | A[i] = (int16_t)(((temp1W32 * 2) + 32768) >> 16); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 243 | } |
| 244 | return 1; // Stable filters |
| 245 | } |