aboutsummaryrefslogtreecommitdiff
path: root/samples/common/test
diff options
context:
space:
mode:
Diffstat (limited to 'samples/common/test')
-rw-r--r--samples/common/test/Audio/AudioCaptureTest.cpp61
-rw-r--r--samples/common/test/Audio/MathUtilsTest.cpp112
2 files changed, 173 insertions, 0 deletions
diff --git a/samples/common/test/Audio/AudioCaptureTest.cpp b/samples/common/test/Audio/AudioCaptureTest.cpp
new file mode 100644
index 0000000000..b8ea7b285c
--- /dev/null
+++ b/samples/common/test/Audio/AudioCaptureTest.cpp
@@ -0,0 +1,61 @@
+//
+// Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
+// SPDX-License-Identifier: MIT
+//
+
+#define CATCH_CONFIG_MAIN
+#include <catch.hpp>
+#include <limits>
+
+#include "AudioCapture.hpp"
+
+TEST_CASE("Test capture of audio file")
+{
+ std::string testResources = TEST_RESOURCE_DIR;
+ REQUIRE(testResources != "");
+ std::string file = testResources + "/" + "myVoiceIsMyPassportVerifyMe04.wav";
+ audio::AudioCapture capture;
+ std::vector<float> audioData = capture.LoadAudioFile(file);
+ capture.InitSlidingWindow(audioData.data(), audioData.size(), 47712, 16000);
+
+ std::vector<float> firstAudioBlock = capture.Next();
+ float actual1 = firstAudioBlock.at(0);
+ float actual2 = firstAudioBlock.at(47000);
+ CHECK(std::to_string(actual1) == "0.000352");
+ CHECK(std::to_string(actual2) == "-0.056441");
+ CHECK(firstAudioBlock.size() == 47712);
+
+ CHECK(capture.HasNext() == true);
+
+ std::vector<float> secondAudioBlock = capture.Next();
+ float actual3 = secondAudioBlock.at(0);
+ float actual4 = secondAudioBlock.at(47000);
+ CHECK(std::to_string(actual3) == "0.102077");
+ CHECK(std::to_string(actual4) == "0.000194");
+ CHECK(capture.HasNext() == true);
+
+ std::vector<float> thirdAudioBlock = capture.Next();
+ float actual5 = thirdAudioBlock.at(0);
+ float actual6 = thirdAudioBlock.at(33500);
+ float actual7 = thirdAudioBlock.at(33600);
+ CHECK(std::to_string(actual5) == "-0.076416");
+ CHECK(std::to_string(actual6) == "-0.000275");
+ CHECK(std::to_string(actual7) == "0.000000");
+ CHECK(capture.HasNext() == false);
+}
+
+TEST_CASE("Test sliding window of audio capture")
+{
+ std::string testResources = TEST_RESOURCE_DIR;
+ REQUIRE(testResources != "");
+ std::string file = testResources + "/" + "myVoiceIsMyPassportVerifyMe04.wav";
+ audio::AudioCapture capture;
+ std::vector<float> audioData = capture.LoadAudioFile(file);
+ capture.InitSlidingWindow(audioData.data(), audioData.size(), 47712, 16000);
+ capture.Next();
+ capture.Next();
+
+ CHECK(capture.HasNext() == true);
+ capture.Next();
+ CHECK(capture.HasNext() == false);
+}
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 <catch.hpp>
+#include <limits>
+
+#include "MathUtils.hpp"
+#include <iostream>
+#include <numeric>
+
+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<float> input(32, 0);
+ std::vector<float> output(32);
+ std::vector<float> 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<double>(), 0.1));
+
+ transform(expectedResult.begin(), expectedResult.end(), expectedResult.begin(),
+ bind2nd(std::plus<double>(), 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<float> input = { 1, 0.1e-10 };
+ std::vector<float> expectedResult = { 0, -25.328436 };
+ std::vector<float> 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));
+}
+