1 /* 2 * Copyright (c) 2012 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 #include "testing/gtest/include/gtest/gtest.h" 12 #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h" 13 14 static const size_t kVector16Size = 9; 15 static const int16_t vector16[kVector16Size] = {1, -15511, 4323, 1963, 16 WEBRTC_SPL_WORD16_MAX, 0, WEBRTC_SPL_WORD16_MIN + 5, -3333, 345}; 17 18 class SplTest : public testing::Test { 19 protected: 20 SplTest() { 21 WebRtcSpl_Init(); 22 } 23 virtual ~SplTest() { 24 } 25 }; 26 27 TEST_F(SplTest, MacroTest) { 28 // Macros with inputs. 29 int A = 10; 30 int B = 21; 31 int a = -3; 32 int b = WEBRTC_SPL_WORD32_MAX; 33 34 EXPECT_EQ(10, WEBRTC_SPL_MIN(A, B)); 35 EXPECT_EQ(21, WEBRTC_SPL_MAX(A, B)); 36 37 EXPECT_EQ(3, WEBRTC_SPL_ABS_W16(a)); 38 EXPECT_EQ(3, WEBRTC_SPL_ABS_W32(a)); 39 40 EXPECT_EQ(-63, WEBRTC_SPL_MUL(a, B)); 41 EXPECT_EQ(-2147483645, WEBRTC_SPL_MUL(a, b)); 42 EXPECT_EQ(2147483651u, WEBRTC_SPL_UMUL(a, b)); 43 b = WEBRTC_SPL_WORD16_MAX >> 1; 44 EXPECT_EQ(4294918147u, WEBRTC_SPL_UMUL_32_16(a, b)); 45 EXPECT_EQ(-49149, WEBRTC_SPL_MUL_16_U16(a, b)); 46 47 a = b; 48 b = -3; 49 50 EXPECT_EQ(-1, WEBRTC_SPL_MUL_16_32_RSFT16(a, b)); 51 EXPECT_EQ(-1, WEBRTC_SPL_MUL_16_32_RSFT15(a, b)); 52 EXPECT_EQ(-3, WEBRTC_SPL_MUL_16_32_RSFT14(a, b)); 53 EXPECT_EQ(-24, WEBRTC_SPL_MUL_16_32_RSFT11(a, b)); 54 55 EXPECT_EQ(-12288, WEBRTC_SPL_MUL_16_16_RSFT(a, b, 2)); 56 EXPECT_EQ(-12287, WEBRTC_SPL_MUL_16_16_RSFT_WITH_ROUND(a, b, 2)); 57 58 EXPECT_EQ(21, WEBRTC_SPL_SAT(a, A, B)); 59 EXPECT_EQ(21, WEBRTC_SPL_SAT(a, B, A)); 60 61 // Shifting with negative numbers allowed 62 int shift_amount = 1; // Workaround compiler warning using variable here. 63 // Positive means left shift 64 EXPECT_EQ(32766, WEBRTC_SPL_SHIFT_W32(a, shift_amount)); 65 66 // Shifting with negative numbers not allowed 67 // We cannot do casting here due to signed/unsigned problem 68 EXPECT_EQ(32766, WEBRTC_SPL_LSHIFT_W32(a, 1)); 69 70 EXPECT_EQ(8191u, WEBRTC_SPL_RSHIFT_U32(a, 1)); 71 72 EXPECT_EQ(1470, WEBRTC_SPL_RAND(A)); 73 74 EXPECT_EQ(-49149, WEBRTC_SPL_MUL_16_16(a, b)); 75 EXPECT_EQ(1073676289, WEBRTC_SPL_MUL_16_16(WEBRTC_SPL_WORD16_MAX, 76 WEBRTC_SPL_WORD16_MAX)); 77 EXPECT_EQ(1073709055, WEBRTC_SPL_MUL_16_32_RSFT16(WEBRTC_SPL_WORD16_MAX, 78 WEBRTC_SPL_WORD32_MAX)); 79 EXPECT_EQ(1073741824, WEBRTC_SPL_MUL_16_32_RSFT16(WEBRTC_SPL_WORD16_MIN, 80 WEBRTC_SPL_WORD32_MIN)); 81 #ifdef WEBRTC_ARCH_ARM_V7 82 EXPECT_EQ(-1073741824, 83 WEBRTC_SPL_MUL_16_32_RSFT16(WEBRTC_SPL_WORD16_MIN, 84 WEBRTC_SPL_WORD32_MAX)); 85 #else 86 EXPECT_EQ(-1073741823, 87 WEBRTC_SPL_MUL_16_32_RSFT16(WEBRTC_SPL_WORD16_MIN, 88 WEBRTC_SPL_WORD32_MAX)); 89 #endif 90 } 91 92 TEST_F(SplTest, InlineTest) { 93 int16_t a16 = 121; 94 int16_t b16 = -17; 95 int32_t a32 = 111121; 96 int32_t b32 = -1711; 97 98 EXPECT_EQ(17, WebRtcSpl_GetSizeInBits(a32)); 99 100 EXPECT_EQ(0, WebRtcSpl_NormW32(0)); 101 EXPECT_EQ(31, WebRtcSpl_NormW32(-1)); 102 EXPECT_EQ(0, WebRtcSpl_NormW32(WEBRTC_SPL_WORD32_MIN)); 103 EXPECT_EQ(14, WebRtcSpl_NormW32(a32)); 104 105 EXPECT_EQ(0, WebRtcSpl_NormW16(0)); 106 EXPECT_EQ(15, WebRtcSpl_NormW16(-1)); 107 EXPECT_EQ(0, WebRtcSpl_NormW16(WEBRTC_SPL_WORD16_MIN)); 108 EXPECT_EQ(4, WebRtcSpl_NormW16(b32)); 109 for (int ii = 0; ii < 15; ++ii) { 110 int16_t value = 1 << ii; 111 EXPECT_EQ(14 - ii, WebRtcSpl_NormW16(value)); 112 EXPECT_EQ(15 - ii, WebRtcSpl_NormW16(-value)); 113 } 114 115 EXPECT_EQ(0, WebRtcSpl_NormU32(0u)); 116 EXPECT_EQ(0, WebRtcSpl_NormU32(0xffffffff)); 117 EXPECT_EQ(15, WebRtcSpl_NormU32(static_cast<uint32_t>(a32))); 118 119 EXPECT_EQ(104, WebRtcSpl_AddSatW16(a16, b16)); 120 EXPECT_EQ(138, WebRtcSpl_SubSatW16(a16, b16)); 121 122 EXPECT_EQ(109410, WebRtcSpl_AddSatW32(a32, b32)); 123 EXPECT_EQ(112832, WebRtcSpl_SubSatW32(a32, b32)); 124 125 a32 = 0x80000000; 126 b32 = 0x80000000; 127 // Cast to signed int to avoid compiler complaint on gtest.h. 128 EXPECT_EQ(static_cast<int>(0x80000000), WebRtcSpl_AddSatW32(a32, b32)); 129 a32 = 0x7fffffff; 130 b32 = 0x7fffffff; 131 EXPECT_EQ(0x7fffffff, WebRtcSpl_AddSatW32(a32, b32)); 132 a32 = 0; 133 b32 = 0x80000000; 134 EXPECT_EQ(0x7fffffff, WebRtcSpl_SubSatW32(a32, b32)); 135 a32 = 0x7fffffff; 136 b32 = 0x80000000; 137 EXPECT_EQ(0x7fffffff, WebRtcSpl_SubSatW32(a32, b32)); 138 a32 = 0x80000000; 139 b32 = 0x7fffffff; 140 EXPECT_EQ(static_cast<int>(0x80000000), WebRtcSpl_SubSatW32(a32, b32)); 141 } 142 143 TEST_F(SplTest, MathOperationsTest) { 144 int A = 1134567892; 145 int32_t num = 117; 146 int32_t den = -5; 147 uint16_t denU = 5; 148 EXPECT_EQ(33700, WebRtcSpl_Sqrt(A)); 149 EXPECT_EQ(33683, WebRtcSpl_SqrtFloor(A)); 150 151 152 EXPECT_EQ(-91772805, WebRtcSpl_DivResultInQ31(den, num)); 153 EXPECT_EQ(-23, WebRtcSpl_DivW32W16ResW16(num, (int16_t)den)); 154 EXPECT_EQ(-23, WebRtcSpl_DivW32W16(num, (int16_t)den)); 155 EXPECT_EQ(23u, WebRtcSpl_DivU32U16(num, denU)); 156 EXPECT_EQ(0, WebRtcSpl_DivW32HiLow(128, 0, 256)); 157 } 158 159 TEST_F(SplTest, BasicArrayOperationsTest) { 160 const size_t kVectorSize = 4; 161 int B[] = {4, 12, 133, 1100}; 162 int16_t b16[kVectorSize]; 163 int32_t b32[kVectorSize]; 164 165 int16_t bTmp16[kVectorSize]; 166 int32_t bTmp32[kVectorSize]; 167 168 WebRtcSpl_MemSetW16(b16, 3, kVectorSize); 169 for (size_t kk = 0; kk < kVectorSize; ++kk) { 170 EXPECT_EQ(3, b16[kk]); 171 } 172 WebRtcSpl_ZerosArrayW16(b16, kVectorSize); 173 for (size_t kk = 0; kk < kVectorSize; ++kk) { 174 EXPECT_EQ(0, b16[kk]); 175 } 176 WebRtcSpl_MemSetW32(b32, 3, kVectorSize); 177 for (size_t kk = 0; kk < kVectorSize; ++kk) { 178 EXPECT_EQ(3, b32[kk]); 179 } 180 WebRtcSpl_ZerosArrayW32(b32, kVectorSize); 181 for (size_t kk = 0; kk < kVectorSize; ++kk) { 182 EXPECT_EQ(0, b32[kk]); 183 } 184 for (size_t kk = 0; kk < kVectorSize; ++kk) { 185 bTmp16[kk] = (int16_t)kk; 186 bTmp32[kk] = (int32_t)kk; 187 } 188 WEBRTC_SPL_MEMCPY_W16(b16, bTmp16, kVectorSize); 189 for (size_t kk = 0; kk < kVectorSize; ++kk) { 190 EXPECT_EQ(b16[kk], bTmp16[kk]); 191 } 192 // WEBRTC_SPL_MEMCPY_W32(b32, bTmp32, kVectorSize); 193 // for (int kk = 0; kk < kVectorSize; ++kk) { 194 // EXPECT_EQ(b32[kk], bTmp32[kk]); 195 // } 196 WebRtcSpl_CopyFromEndW16(b16, kVectorSize, 2, bTmp16); 197 for (size_t kk = 0; kk < 2; ++kk) { 198 EXPECT_EQ(static_cast<int16_t>(kk+2), bTmp16[kk]); 199 } 200 201 for (size_t kk = 0; kk < kVectorSize; ++kk) { 202 b32[kk] = B[kk]; 203 b16[kk] = (int16_t)B[kk]; 204 } 205 WebRtcSpl_VectorBitShiftW32ToW16(bTmp16, kVectorSize, b32, 1); 206 for (size_t kk = 0; kk < kVectorSize; ++kk) { 207 EXPECT_EQ((B[kk]>>1), bTmp16[kk]); 208 } 209 WebRtcSpl_VectorBitShiftW16(bTmp16, kVectorSize, b16, 1); 210 for (size_t kk = 0; kk < kVectorSize; ++kk) { 211 EXPECT_EQ((B[kk]>>1), bTmp16[kk]); 212 } 213 WebRtcSpl_VectorBitShiftW32(bTmp32, kVectorSize, b32, 1); 214 for (size_t kk = 0; kk < kVectorSize; ++kk) { 215 EXPECT_EQ((B[kk]>>1), bTmp32[kk]); 216 } 217 218 WebRtcSpl_MemCpyReversedOrder(&bTmp16[3], b16, kVectorSize); 219 for (size_t kk = 0; kk < kVectorSize; ++kk) { 220 EXPECT_EQ(b16[3-kk], bTmp16[kk]); 221 } 222 } 223 224 TEST_F(SplTest, MinMaxOperationsTest) { 225 const size_t kVectorSize = 17; 226 227 // Vectors to test the cases where minimum values have to be caught 228 // outside of the unrolled loops in ARM-Neon. 229 int16_t vector16[kVectorSize] = {-1, 7485, 0, 3333, 230 -18283, 0, 12334, -29871, 988, -3333, 231 345, -456, 222, 999, 888, 8774, WEBRTC_SPL_WORD16_MIN}; 232 int32_t vector32[kVectorSize] = {-1, 0, 283211, 3333, 233 8712345, 0, -3333, 89345, -374585456, 222, 999, 122345334, 234 -12389756, -987329871, 888, -2, WEBRTC_SPL_WORD32_MIN}; 235 236 EXPECT_EQ(WEBRTC_SPL_WORD16_MIN, 237 WebRtcSpl_MinValueW16(vector16, kVectorSize)); 238 EXPECT_EQ(WEBRTC_SPL_WORD32_MIN, 239 WebRtcSpl_MinValueW32(vector32, kVectorSize)); 240 EXPECT_EQ(kVectorSize - 1, WebRtcSpl_MinIndexW16(vector16, kVectorSize)); 241 EXPECT_EQ(kVectorSize - 1, WebRtcSpl_MinIndexW32(vector32, kVectorSize)); 242 243 // Test the cases where maximum values have to be caught 244 // outside of the unrolled loops in ARM-Neon. 245 vector16[kVectorSize - 1] = WEBRTC_SPL_WORD16_MAX; 246 vector32[kVectorSize - 1] = WEBRTC_SPL_WORD32_MAX; 247 248 EXPECT_EQ(WEBRTC_SPL_WORD16_MAX, 249 WebRtcSpl_MaxAbsValueW16(vector16, kVectorSize)); 250 EXPECT_EQ(WEBRTC_SPL_WORD16_MAX, 251 WebRtcSpl_MaxValueW16(vector16, kVectorSize)); 252 EXPECT_EQ(WEBRTC_SPL_WORD32_MAX, 253 WebRtcSpl_MaxAbsValueW32(vector32, kVectorSize)); 254 EXPECT_EQ(WEBRTC_SPL_WORD32_MAX, 255 WebRtcSpl_MaxValueW32(vector32, kVectorSize)); 256 EXPECT_EQ(kVectorSize - 1, WebRtcSpl_MaxAbsIndexW16(vector16, kVectorSize)); 257 EXPECT_EQ(kVectorSize - 1, WebRtcSpl_MaxIndexW16(vector16, kVectorSize)); 258 EXPECT_EQ(kVectorSize - 1, WebRtcSpl_MaxIndexW32(vector32, kVectorSize)); 259 260 // Test the cases where multiple maximum and minimum values are present. 261 vector16[1] = WEBRTC_SPL_WORD16_MAX; 262 vector16[6] = WEBRTC_SPL_WORD16_MIN; 263 vector16[11] = WEBRTC_SPL_WORD16_MIN; 264 vector32[1] = WEBRTC_SPL_WORD32_MAX; 265 vector32[6] = WEBRTC_SPL_WORD32_MIN; 266 vector32[11] = WEBRTC_SPL_WORD32_MIN; 267 268 EXPECT_EQ(WEBRTC_SPL_WORD16_MAX, 269 WebRtcSpl_MaxAbsValueW16(vector16, kVectorSize)); 270 EXPECT_EQ(WEBRTC_SPL_WORD16_MAX, 271 WebRtcSpl_MaxValueW16(vector16, kVectorSize)); 272 EXPECT_EQ(WEBRTC_SPL_WORD16_MIN, 273 WebRtcSpl_MinValueW16(vector16, kVectorSize)); 274 EXPECT_EQ(WEBRTC_SPL_WORD32_MAX, 275 WebRtcSpl_MaxAbsValueW32(vector32, kVectorSize)); 276 EXPECT_EQ(WEBRTC_SPL_WORD32_MAX, 277 WebRtcSpl_MaxValueW32(vector32, kVectorSize)); 278 EXPECT_EQ(WEBRTC_SPL_WORD32_MIN, 279 WebRtcSpl_MinValueW32(vector32, kVectorSize)); 280 EXPECT_EQ(6u, WebRtcSpl_MaxAbsIndexW16(vector16, kVectorSize)); 281 EXPECT_EQ(1u, WebRtcSpl_MaxIndexW16(vector16, kVectorSize)); 282 EXPECT_EQ(1u, WebRtcSpl_MaxIndexW32(vector32, kVectorSize)); 283 EXPECT_EQ(6u, WebRtcSpl_MinIndexW16(vector16, kVectorSize)); 284 EXPECT_EQ(6u, WebRtcSpl_MinIndexW32(vector32, kVectorSize)); 285 } 286 287 TEST_F(SplTest, VectorOperationsTest) { 288 const size_t kVectorSize = 4; 289 int B[] = {4, 12, 133, 1100}; 290 int16_t a16[kVectorSize]; 291 int16_t b16[kVectorSize]; 292 int16_t bTmp16[kVectorSize]; 293 294 for (size_t kk = 0; kk < kVectorSize; ++kk) { 295 a16[kk] = B[kk]; 296 b16[kk] = B[kk]; 297 } 298 299 WebRtcSpl_AffineTransformVector(bTmp16, b16, 3, 7, 2, kVectorSize); 300 for (size_t kk = 0; kk < kVectorSize; ++kk) { 301 EXPECT_EQ((B[kk]*3+7)>>2, bTmp16[kk]); 302 } 303 WebRtcSpl_ScaleAndAddVectorsWithRound(b16, 3, b16, 2, 2, bTmp16, kVectorSize); 304 for (size_t kk = 0; kk < kVectorSize; ++kk) { 305 EXPECT_EQ((B[kk]*3+B[kk]*2+2)>>2, bTmp16[kk]); 306 } 307 308 WebRtcSpl_AddAffineVectorToVector(bTmp16, b16, 3, 7, 2, kVectorSize); 309 for (size_t kk = 0; kk < kVectorSize; ++kk) { 310 EXPECT_EQ(((B[kk]*3+B[kk]*2+2)>>2)+((b16[kk]*3+7)>>2), bTmp16[kk]); 311 } 312 313 WebRtcSpl_ScaleVector(b16, bTmp16, 13, kVectorSize, 2); 314 for (size_t kk = 0; kk < kVectorSize; ++kk) { 315 EXPECT_EQ((b16[kk]*13)>>2, bTmp16[kk]); 316 } 317 WebRtcSpl_ScaleVectorWithSat(b16, bTmp16, 13, kVectorSize, 2); 318 for (size_t kk = 0; kk < kVectorSize; ++kk) { 319 EXPECT_EQ((b16[kk]*13)>>2, bTmp16[kk]); 320 } 321 WebRtcSpl_ScaleAndAddVectors(a16, 13, 2, b16, 7, 2, bTmp16, kVectorSize); 322 for (size_t kk = 0; kk < kVectorSize; ++kk) { 323 EXPECT_EQ(((a16[kk]*13)>>2)+((b16[kk]*7)>>2), bTmp16[kk]); 324 } 325 326 WebRtcSpl_AddVectorsAndShift(bTmp16, a16, b16, kVectorSize, 2); 327 for (size_t kk = 0; kk < kVectorSize; ++kk) { 328 EXPECT_EQ(B[kk] >> 1, bTmp16[kk]); 329 } 330 WebRtcSpl_ReverseOrderMultArrayElements(bTmp16, a16, &b16[3], kVectorSize, 2); 331 for (size_t kk = 0; kk < kVectorSize; ++kk) { 332 EXPECT_EQ((a16[kk]*b16[3-kk])>>2, bTmp16[kk]); 333 } 334 WebRtcSpl_ElementwiseVectorMult(bTmp16, a16, b16, kVectorSize, 6); 335 for (size_t kk = 0; kk < kVectorSize; ++kk) { 336 EXPECT_EQ((a16[kk]*b16[kk])>>6, bTmp16[kk]); 337 } 338 339 WebRtcSpl_SqrtOfOneMinusXSquared(b16, kVectorSize, bTmp16); 340 for (size_t kk = 0; kk < kVectorSize - 1; ++kk) { 341 EXPECT_EQ(32767, bTmp16[kk]); 342 } 343 EXPECT_EQ(32749, bTmp16[kVectorSize - 1]); 344 345 EXPECT_EQ(0, WebRtcSpl_GetScalingSquare(b16, kVectorSize, 1)); 346 } 347 348 TEST_F(SplTest, EstimatorsTest) { 349 const size_t kOrder = 2; 350 const int32_t unstable_filter[] = { 4, 12, 133, 1100 }; 351 const int32_t stable_filter[] = { 1100, 133, 12, 4 }; 352 int16_t lpc[kOrder + 2] = { 0 }; 353 int16_t refl[kOrder + 2] = { 0 }; 354 int16_t lpc_result[] = { 4096, -497, 15, 0 }; 355 int16_t refl_result[] = { -3962, 123, 0, 0 }; 356 357 EXPECT_EQ(0, WebRtcSpl_LevinsonDurbin(unstable_filter, lpc, refl, kOrder)); 358 EXPECT_EQ(1, WebRtcSpl_LevinsonDurbin(stable_filter, lpc, refl, kOrder)); 359 for (size_t i = 0; i < kOrder + 2; ++i) { 360 EXPECT_EQ(lpc_result[i], lpc[i]); 361 EXPECT_EQ(refl_result[i], refl[i]); 362 } 363 } 364 365 TEST_F(SplTest, FilterTest) { 366 const size_t kVectorSize = 4; 367 const size_t kFilterOrder = 3; 368 int16_t A[] = {1, 2, 33, 100}; 369 int16_t A5[] = {1, 2, 33, 100, -5}; 370 int16_t B[] = {4, 12, 133, 110}; 371 int16_t data_in[kVectorSize]; 372 int16_t data_out[kVectorSize]; 373 int16_t bTmp16Low[kVectorSize]; 374 int16_t bState[kVectorSize]; 375 int16_t bStateLow[kVectorSize]; 376 377 WebRtcSpl_ZerosArrayW16(bState, kVectorSize); 378 WebRtcSpl_ZerosArrayW16(bStateLow, kVectorSize); 379 380 for (size_t kk = 0; kk < kVectorSize; ++kk) { 381 data_in[kk] = A[kk]; 382 data_out[kk] = 0; 383 } 384 385 // MA filters. 386 // Note that the input data has |kFilterOrder| states before the actual 387 // data (one sample). 388 WebRtcSpl_FilterMAFastQ12(&data_in[kFilterOrder], data_out, B, 389 kFilterOrder + 1, 1); 390 EXPECT_EQ(0, data_out[0]); 391 // AR filters. 392 // Note that the output data has |kFilterOrder| states before the actual 393 // data (one sample). 394 WebRtcSpl_FilterARFastQ12(data_in, &data_out[kFilterOrder], A, 395 kFilterOrder + 1, 1); 396 EXPECT_EQ(0, data_out[kFilterOrder]); 397 398 EXPECT_EQ(kVectorSize, WebRtcSpl_FilterAR(A5, 399 5, 400 data_in, 401 kVectorSize, 402 bState, 403 kVectorSize, 404 bStateLow, 405 kVectorSize, 406 data_out, 407 bTmp16Low, 408 kVectorSize)); 409 } 410 411 TEST_F(SplTest, RandTest) { 412 const int kVectorSize = 4; 413 int16_t BU[] = {3653, 12446, 8525, 30691}; 414 int16_t b16[kVectorSize]; 415 uint32_t bSeed = 100000; 416 417 EXPECT_EQ(7086, WebRtcSpl_RandU(&bSeed)); 418 EXPECT_EQ(31565, WebRtcSpl_RandU(&bSeed)); 419 EXPECT_EQ(-9786, WebRtcSpl_RandN(&bSeed)); 420 EXPECT_EQ(kVectorSize, WebRtcSpl_RandUArray(b16, kVectorSize, &bSeed)); 421 for (int kk = 0; kk < kVectorSize; ++kk) { 422 EXPECT_EQ(BU[kk], b16[kk]); 423 } 424 } 425 426 TEST_F(SplTest, DotProductWithScaleTest) { 427 EXPECT_EQ(605362796, WebRtcSpl_DotProductWithScale(vector16, 428 vector16, kVector16Size, 2)); 429 } 430 431 TEST_F(SplTest, CrossCorrelationTest) { 432 // Note the function arguments relation specificed by API. 433 const size_t kCrossCorrelationDimension = 3; 434 const int kShift = 2; 435 const int kStep = 1; 436 const size_t kSeqDimension = 6; 437 438 const int16_t kVector16[kVector16Size] = {1, 4323, 1963, 439 WEBRTC_SPL_WORD16_MAX, WEBRTC_SPL_WORD16_MIN + 5, -3333, -876, 8483, 142}; 440 int32_t vector32[kCrossCorrelationDimension] = {0}; 441 442 WebRtcSpl_CrossCorrelation(vector32, vector16, kVector16, kSeqDimension, 443 kCrossCorrelationDimension, kShift, kStep); 444 445 // WebRtcSpl_CrossCorrelationC() and WebRtcSpl_CrossCorrelationNeon() 446 // are not bit-exact. 447 const int32_t kExpected[kCrossCorrelationDimension] = 448 {-266947903, -15579555, -171282001}; 449 const int32_t* expected = kExpected; 450 #if !defined(MIPS32_LE) 451 const int32_t kExpectedNeon[kCrossCorrelationDimension] = 452 {-266947901, -15579553, -171281999}; 453 if (WebRtcSpl_CrossCorrelation != WebRtcSpl_CrossCorrelationC) { 454 expected = kExpectedNeon; 455 } 456 #endif 457 for (size_t i = 0; i < kCrossCorrelationDimension; ++i) { 458 EXPECT_EQ(expected[i], vector32[i]); 459 } 460 } 461 462 TEST_F(SplTest, AutoCorrelationTest) { 463 int scale = 0; 464 int32_t vector32[kVector16Size]; 465 const int32_t expected[kVector16Size] = {302681398, 14223410, -121705063, 466 -85221647, -17104971, 61806945, 6644603, -669329, 43}; 467 468 EXPECT_EQ(kVector16Size, 469 WebRtcSpl_AutoCorrelation(vector16, kVector16Size, 470 kVector16Size - 1, vector32, &scale)); 471 EXPECT_EQ(3, scale); 472 for (size_t i = 0; i < kVector16Size; ++i) { 473 EXPECT_EQ(expected[i], vector32[i]); 474 } 475 } 476 477 TEST_F(SplTest, SignalProcessingTest) { 478 const size_t kVectorSize = 4; 479 int A[] = {1, 2, 33, 100}; 480 const int16_t kHanning[4] = { 2399, 8192, 13985, 16384 }; 481 int16_t b16[kVectorSize]; 482 483 int16_t bTmp16[kVectorSize]; 484 485 int bScale = 0; 486 487 for (size_t kk = 0; kk < kVectorSize; ++kk) { 488 b16[kk] = A[kk]; 489 } 490 491 // TODO(bjornv): Activate the Reflection Coefficient tests when refactoring. 492 // WebRtcSpl_ReflCoefToLpc(b16, kVectorSize, bTmp16); 493 //// for (int kk = 0; kk < kVectorSize; ++kk) { 494 //// EXPECT_EQ(aTmp16[kk], bTmp16[kk]); 495 //// } 496 // WebRtcSpl_LpcToReflCoef(bTmp16, kVectorSize, b16); 497 //// for (int kk = 0; kk < kVectorSize; ++kk) { 498 //// EXPECT_EQ(a16[kk], b16[kk]); 499 //// } 500 // WebRtcSpl_AutoCorrToReflCoef(b32, kVectorSize, bTmp16); 501 //// for (int kk = 0; kk < kVectorSize; ++kk) { 502 //// EXPECT_EQ(aTmp16[kk], bTmp16[kk]); 503 //// } 504 505 WebRtcSpl_GetHanningWindow(bTmp16, kVectorSize); 506 for (size_t kk = 0; kk < kVectorSize; ++kk) { 507 EXPECT_EQ(kHanning[kk], bTmp16[kk]); 508 } 509 510 for (size_t kk = 0; kk < kVectorSize; ++kk) { 511 b16[kk] = A[kk]; 512 } 513 EXPECT_EQ(11094 , WebRtcSpl_Energy(b16, kVectorSize, &bScale)); 514 EXPECT_EQ(0, bScale); 515 } 516 517 TEST_F(SplTest, FFTTest) { 518 int16_t B[] = {1, 2, 33, 100, 519 2, 3, 34, 101, 520 3, 4, 35, 102, 521 4, 5, 36, 103}; 522 523 EXPECT_EQ(0, WebRtcSpl_ComplexFFT(B, 3, 1)); 524 // for (int kk = 0; kk < 16; ++kk) { 525 // EXPECT_EQ(A[kk], B[kk]); 526 // } 527 EXPECT_EQ(0, WebRtcSpl_ComplexIFFT(B, 3, 1)); 528 // for (int kk = 0; kk < 16; ++kk) { 529 // EXPECT_EQ(A[kk], B[kk]); 530 // } 531 WebRtcSpl_ComplexBitReverse(B, 3); 532 for (int kk = 0; kk < 16; ++kk) { 533 //EXPECT_EQ(A[kk], B[kk]); 534 } 535 } 536 537 TEST_F(SplTest, Resample48WithSaturationTest) { 538 // The test resamples 3*kBlockSize number of samples to 2*kBlockSize number 539 // of samples. 540 const size_t kBlockSize = 16; 541 542 // Saturated input vector of 48 samples. 543 const int32_t kVectorSaturated[3 * kBlockSize + 7] = { 544 -32768, -32768, -32768, -32768, -32768, -32768, -32768, -32768, 545 -32768, -32768, -32768, -32768, -32768, -32768, -32768, -32768, 546 -32768, -32768, -32768, -32768, -32768, -32768, -32768, -32768, 547 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 548 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 549 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 550 32767, 32767, 32767, 32767, 32767, 32767, 32767 551 }; 552 553 // All values in |out_vector| should be |kRefValue32kHz|. 554 const int32_t kRefValue32kHz1 = -1077493760; 555 const int32_t kRefValue32kHz2 = 1077493645; 556 557 // After bit shift with saturation, |out_vector_w16| is saturated. 558 559 const int16_t kRefValue16kHz1 = -32768; 560 const int16_t kRefValue16kHz2 = 32767; 561 // Vector for storing output. 562 int32_t out_vector[2 * kBlockSize]; 563 int16_t out_vector_w16[2 * kBlockSize]; 564 565 WebRtcSpl_Resample48khzTo32khz(kVectorSaturated, out_vector, kBlockSize); 566 WebRtcSpl_VectorBitShiftW32ToW16(out_vector_w16, 2 * kBlockSize, out_vector, 567 15); 568 569 // Comparing output values against references. The values at position 570 // 12-15 are skipped to account for the filter lag. 571 for (size_t i = 0; i < 12; ++i) { 572 EXPECT_EQ(kRefValue32kHz1, out_vector[i]); 573 EXPECT_EQ(kRefValue16kHz1, out_vector_w16[i]); 574 } 575 for (size_t i = 16; i < 2 * kBlockSize; ++i) { 576 EXPECT_EQ(kRefValue32kHz2, out_vector[i]); 577 EXPECT_EQ(kRefValue16kHz2, out_vector_w16[i]); 578 } 579 } 580