alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
Richard Burton | f32a86a | 2022-11-15 11:46:11 +0000 | [diff] [blame^] | 2 | * SPDX-FileCopyrightText: Copyright 2021 Arm Limited and/or its affiliates <open-source-office@arm.com> |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 3 | * SPDX-License-Identifier: Apache-2.0 |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | #include "Wav2LetterMfcc.hpp" |
| 18 | |
| 19 | #include <algorithm> |
| 20 | #include <catch.hpp> |
| 21 | #include <limits> |
| 22 | |
| 23 | /* First 512 samples from itellyou.wav. */ |
| 24 | const std::vector<int16_t> testWav1 = std::vector<int16_t> { |
| 25 | -3,0,1,-1,2,3,-2,2, |
| 26 | 1,-2,0,3,-1,8,3,2, |
| 27 | -1,-1,2,7,3,5,6,6, |
| 28 | 6,12,5,6,3,3,5,4, |
| 29 | 4,6,7,7,7,3,7,2, |
| 30 | 8,4,4,2,-4,-1,-1,-4, |
| 31 | 2,1,-1,-4,0,-7,-6,-2, |
| 32 | -5,1,-5,-1,-7,-3,-3,-7, |
| 33 | 0,-3,3,-5,0,1,-2,-2, |
| 34 | -3,-3,-7,-3,-2,-6,-5,-8, |
| 35 | -2,-8,4,-9,-4,-9,-5,-5, |
| 36 | -3,-9,-3,-9,-1,-7,-4,1, |
| 37 | -3,2,-8,-4,-4,-5,1,-3, |
| 38 | -1,0,-1,-2,-3,-2,-4,-1, |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| 77 | -3,-7,-3,-1,-2,0,-5,-4, |
| 78 | -7,-3,-2,-2,1,2,2,8, |
| 79 | 5,4,2,4,3,5,0,3, |
| 80 | 3,6,4,2,2,-2,4,-2, |
| 81 | 3,3,2,1,1,4,-5,2, |
| 82 | -3,0,-1,1,-2,2,5,1, |
| 83 | 4,2,3,1,-1,1,0,6, |
| 84 | 0,-2,-1,1,-1,2,-5,-1, |
| 85 | -5,-1,-6,-3,-3,2,4,0, |
| 86 | -1,-5,3,-4,-1,-3,-4,1, |
| 87 | -4,1,-1,-1,0,-5,-4,-2, |
| 88 | -1,-1,-3,-7,-3,-3,4,4, |
| 89 | }; |
| 90 | |
| 91 | const std::vector<int16_t> testWav2 = std::vector<int16_t> (512, 0); |
| 92 | |
| 93 | /* Golden mfcc output for testwav1. */ |
| 94 | const std::vector<float> golden_mfcc_output_testWav1 { |
| 95 | -835.24603, 21.010452, 18.699404, 7.4338417, 19.028961, -5.401735, 6.4761047, -11.400679, |
| 96 | 8.392709, 12.202361, 8.403276, -13.508412, -18.307348 |
| 97 | }; |
| 98 | |
| 99 | /* Golden mfcc output for the all zero wav. */ |
| 100 | const std::vector<float> golden_mfcc_output_testWav2 { |
| 101 | -1131.37085, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
| 102 | }; |
| 103 | |
| 104 | |
| 105 | arm::app::audio::Wav2LetterMFCC GetMFCCInstance() |
| 106 | { |
| 107 | const auto sampFreq = arm::app::audio::Wav2LetterMFCC::ms_defaultSamplingFreq; |
| 108 | const auto frameLenMs = 32; |
| 109 | const auto numMfccFeats = 13; |
| 110 | const auto frameLenSamples = sampFreq * frameLenMs * 0.001; |
| 111 | return arm::app::audio::Wav2LetterMFCC(numMfccFeats, frameLenSamples); |
| 112 | } |
| 113 | |
| 114 | template <class T> |
| 115 | void TestQuantisedMFCC() |
| 116 | { |
| 117 | const auto quantScale = 0.1410219967365265; |
| 118 | const auto quantOffset = 11; |
| 119 | std::vector<T> mfccOutput = GetMFCCInstance().MfccComputeQuant<T>(testWav1, quantScale, quantOffset); |
| 120 | |
| 121 | long min_val = std::numeric_limits<T>::min(); |
| 122 | long max_val = std::numeric_limits<T>::max(); |
| 123 | |
| 124 | for (size_t i = 0; i < golden_mfcc_output_testWav1.size(); i++){ |
| 125 | long TestWavMfcc = (std::lround((golden_mfcc_output_testWav1[i] / quantScale) + quantOffset)); |
| 126 | T quantizedTestWavMfcc = static_cast<T>(std::max(min_val, std::min(TestWavMfcc, max_val))); |
| 127 | |
| 128 | REQUIRE(quantizedTestWavMfcc == Approx(mfccOutput[i]).margin(2)); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | template void TestQuantisedMFCC<int8_t>(); |
| 133 | template void TestQuantisedMFCC<uint8_t>(); |
| 134 | template void TestQuantisedMFCC<int16_t>(); |
| 135 | |
| 136 | TEST_CASE("MFCC calculation") |
| 137 | { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 138 | SECTION("FP32") |
| 139 | { |
| 140 | auto mfccOutput = GetMFCCInstance().MfccCompute(testWav1); |
| 141 | REQUIRE_THAT( mfccOutput, Catch::Approx( golden_mfcc_output_testWav1 ).margin(0.3) ); |
| 142 | |
| 143 | auto mfccOutput2 = GetMFCCInstance().MfccCompute(testWav2); |
| 144 | REQUIRE_THAT( mfccOutput2, Catch::Approx( golden_mfcc_output_testWav2 ).margin(0.001) ); |
| 145 | } |
| 146 | |
| 147 | SECTION("int8_t") |
| 148 | { |
| 149 | TestQuantisedMFCC<int8_t>(); |
| 150 | } |
| 151 | |
| 152 | SECTION("uint8_t") |
| 153 | { |
| 154 | TestQuantisedMFCC<uint8_t>(); |
| 155 | } |
| 156 | |
| 157 | SECTION("int16_t") |
| 158 | { |
| 159 | TestQuantisedMFCC<int16_t>(); |
| 160 | } |
| 161 | } |