From 23c26277086c78704a17f0dae86da947816320c0 Mon Sep 17 00:00:00 2001 From: George Gekov Date: Mon, 16 Aug 2021 11:32:10 +0100 Subject: MLECO-2079 Adding the C++ KWS example Signed-off-by: Eanna O Cathain Change-Id: I81899bbfaada32f478c2e2fc6441eabb94d8d0fc --- samples/common/test/Audio/MathUtilsTest.cpp | 112 ++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 samples/common/test/Audio/MathUtilsTest.cpp (limited to 'samples/common/test/Audio/MathUtilsTest.cpp') diff --git a/samples/common/test/Audio/MathUtilsTest.cpp b/samples/common/test/Audio/MathUtilsTest.cpp new file mode 100644 index 0000000000..d7a435db56 --- /dev/null +++ b/samples/common/test/Audio/MathUtilsTest.cpp @@ -0,0 +1,112 @@ +// +// Copyright © 2021 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#include +#include + +#include "MathUtils.hpp" +#include +#include + +TEST_CASE("Test DotProductF32") +{ + // Test Constants: + const int length = 6; + + float inputA[] = { 1, 1, 1, 0, 0, 0 }; + float inputB[] = { 0, 0, 0, 1, 1, 1 }; + + float dot_prod = MathUtils::DotProductF32(inputA, inputB, length); + float expectedResult = 0; + CHECK(dot_prod == expectedResult); +} + +TEST_CASE("Test FFT32") +{ + // Test Constants: + std::vector input(32, 0); + std::vector output(32); + std::vector expectedResult(32, 0); + + MathUtils::FftF32(input, output); + + // To avoid common failed assertions due to rounding of near-zero values a small offset is added + transform(output.begin(), output.end(), output.begin(), + bind2nd(std::plus(), 0.1)); + + transform(expectedResult.begin(), expectedResult.end(), expectedResult.begin(), + bind2nd(std::plus(), 0.1)); + + for (int i = 0; i < output.size(); i++) + { + CHECK (expectedResult[i] == Approx(output[i])); + } +} + +TEST_CASE("Test ComplexMagnitudeSquaredF32") +{ + // Test Constants: + float input[] = { 0.0, 0.0, 0.5, 0.5,1,1 }; + int inputLen = (sizeof(input)/sizeof(*input)); + float expectedResult[] = { 0.0, 0.5, 2 }; + int outputLen = inputLen/2; + float output[outputLen]; + + MathUtils::ComplexMagnitudeSquaredF32(input, inputLen, output, outputLen); + + for (int i = 0; i < outputLen; i++) + { + CHECK (expectedResult[i] == Approx(output[i])); + } +} + +TEST_CASE("Test VecLogarithmF32") +{ + // Test Constants: + + std::vector input = { 1, 0.1e-10 }; + std::vector expectedResult = { 0, -25.328436 }; + std::vector output(input.size()); + MathUtils::VecLogarithmF32(input,output); + + for (int i = 0; i < input.size(); i++) + { + CHECK (expectedResult[i] == Approx(output[i])); + } +} + +TEST_CASE("Test MeanF32") +{ + // Test Constants: + float input[] = { 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 1.000 }; + uint32_t inputLen = (sizeof(input)/sizeof(*input)); + float output; + + // Manually calculated mean of above array + float expectedResult = 0.100; + output = MathUtils::MeanF32(input, inputLen); + + CHECK (expectedResult == Approx(output)); +} + +TEST_CASE("Test StdDevF32") +{ + // Test Constants: + + float input[] = { 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 1.000 }; + + uint32_t inputLen = (sizeof(input)/sizeof(*input)); + + // Calculate mean using std library to avoid dependency on MathUtils::MeanF32 + float mean = (std::accumulate(input, input + inputLen, 0.0f))/float(inputLen); + + float output = MathUtils::StdDevF32(input, inputLen, mean); + + // Manually calculated standard deviation of above array + float expectedResult = 0.300; + + CHECK (expectedResult == Approx(output)); +} + -- cgit v1.2.1