blob: 59fe29bb036065201fbfca663b6d67fff9dcc7d1 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
2 * Copyright (c) 2021 Arm Limited. All rights reserved.
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 "DataStructures.hpp"
18#include "AsrGoldenFeatures.hpp"
19#include "hal.h"
20#include "TensorFlowLiteMicro.hpp"
21#include "Wav2LetterPreprocess.hpp"
22
23#include <catch.hpp>
24#include <random>
25
26class TestPreprocess : public arm::app::audio::asr::Preprocess {
27public:
alexander3c798932021-03-26 21:42:19 +000028
alexanderc350cdc2021-04-29 20:36:09 +010029 static bool ComputeDeltas(arm::app::Array2d<float>& mfcc,
alexander3c798932021-03-26 21:42:19 +000030 arm::app::Array2d<float>& delta1,
31 arm::app::Array2d<float>& delta2)
32 {
alexanderc350cdc2021-04-29 20:36:09 +010033 return Preprocess::ComputeDeltas(mfcc, delta1, delta2);
alexander3c798932021-03-26 21:42:19 +000034 }
35
alexanderc350cdc2021-04-29 20:36:09 +010036 static float GetMean(arm::app::Array2d<float>& vec)
alexander3c798932021-03-26 21:42:19 +000037 {
alexanderc350cdc2021-04-29 20:36:09 +010038 return Preprocess::GetMean(vec);
alexander3c798932021-03-26 21:42:19 +000039 }
40
alexanderc350cdc2021-04-29 20:36:09 +010041 static float GetStdDev(arm::app::Array2d<float>& vec, const float mean)
alexander3c798932021-03-26 21:42:19 +000042 {
alexanderc350cdc2021-04-29 20:36:09 +010043 return Preprocess::GetStdDev(vec, mean);
alexander3c798932021-03-26 21:42:19 +000044 }
45
alexanderc350cdc2021-04-29 20:36:09 +010046 static void NormaliseVec(arm::app::Array2d<float>& vec)
alexander3c798932021-03-26 21:42:19 +000047 {
alexanderc350cdc2021-04-29 20:36:09 +010048 return Preprocess::NormaliseVec(vec);
alexander3c798932021-03-26 21:42:19 +000049 }
50};
51
52template<class T>
53void CheckOutputs(const std::vector<T> goldenOutput, std::vector<T> output)
54{
55 const size_t goldenSize = goldenOutput.size();
56 const size_t realSize = output.size();
57
58 REQUIRE(realSize == goldenSize);
59 REQUIRE_THAT(output, Catch::Approx( goldenOutput ).margin(0.0001));
60}
61template void CheckOutputs<float>(const std::vector<float> goldenOutput, std::vector<float> output);
62
63void populateBuffer(const float* input, size_t size, size_t numMfccFeats, std::vector<std::vector<float>>& buf)
64{
65 size_t time = 0;
66 for (size_t i = 0; i < size; ++i) {
67 if (i > 0 && i % numMfccFeats == 0) {
68 ++time;
69 }
70 float featureValue = *(input + i);
71 buf[i % numMfccFeats][time] = featureValue;
72 }
73}
74
75void populateArray2dWithVectorOfVector(std::vector<std::vector<float>> vec, arm::app::Array2d<float>& buf)
76{
77 for (size_t i = 0; i < vec.size(); ++i) {
78 for (size_t j = 0; j < vec[i].size(); ++j) {
79 buf(i, j) = vec[i][j];
80 }
81 }
82}
83
84TEST_CASE("Floating point asr features calculation", "[ASR]")
85{
alexander3c798932021-03-26 21:42:19 +000086
87 SECTION("First and second diff")
88 {
89 constexpr uint32_t numMfccFeats = 13;
90 constexpr uint32_t numFeatVectors = 296;
91
92 arm::app::Array2d<float> mfccBuf(numMfccFeats, numFeatVectors);
93 arm::app::Array2d<float> delta1Buf(numMfccFeats, numFeatVectors);
94 arm::app::Array2d<float> delta2Buf(numMfccFeats, numFeatVectors);
95
96 std::vector<std::vector<float>> goldenMfccBuf(numMfccFeats, std::vector<float>(numFeatVectors));
97 std::vector<std::vector<float>> goldenDelta1Buf(numMfccFeats, std::vector<float>(numFeatVectors));
98 std::vector<std::vector<float>> goldenDelta2Buf(numMfccFeats, std::vector<float>(numFeatVectors));
99
100 populateBuffer(golden_asr_mfcc, golden_asr_mfcc_len, numMfccFeats, goldenMfccBuf);
101 populateBuffer(golden_diff1_features, golden_diff1_len, numMfccFeats, goldenDelta1Buf);
102 populateBuffer(golden_diff2_features, golden_diff2_len, numMfccFeats, goldenDelta2Buf);
103
104 populateArray2dWithVectorOfVector(goldenMfccBuf, mfccBuf);
105 std::fill(delta1Buf.begin(), delta1Buf.end(), 0.f);
106 std::fill(delta2Buf.begin(), delta2Buf.end(), 0.f);
107
alexanderc350cdc2021-04-29 20:36:09 +0100108 TestPreprocess::ComputeDeltas(mfccBuf, delta1Buf, delta2Buf);
alexander3c798932021-03-26 21:42:19 +0000109
110 /* First 4 and last 4 values are different because we pad AFTER diff calculated. */
111 for (size_t i = 0; i < numMfccFeats; ++i) {
112 const float* start_goldenDelta1Buf = goldenDelta1Buf[i].data() + 4;
113 const float* start_delta1 = delta1Buf.begin() + i * delta1Buf.size(1) + 4;
114 std::vector<float> goldenDataDelta1(start_goldenDelta1Buf, start_goldenDelta1Buf + numFeatVectors - 8);
115 std::vector<float> tensorDataDelta1(start_delta1, start_delta1 + numFeatVectors - 8);
116
117 CheckOutputs<float>(goldenDataDelta1,tensorDataDelta1);
118
119 const float* start_goldenDelta2Buf = goldenDelta2Buf[i].data() + 4;
120 const float* start_delta2 = delta2Buf.begin() + i * delta2Buf.size(1) + 4;
121 std::vector<float> goldenDataDelta2(start_goldenDelta2Buf, start_goldenDelta2Buf + numFeatVectors - 8);
122 std::vector<float> tensorDataDelta2(start_delta2, start_delta2 + numFeatVectors - 8);
123
124 CheckOutputs<float>(goldenDataDelta2,tensorDataDelta2);
125 }
126
127 }
128
129 SECTION("Mean")
130 {
131 std::vector<std::vector<float>> mean1vec{{1, 2},
132 {-1, -2}};
133 arm::app::Array2d<float> mean1(2,2); /* {{1, 2},{-1, -2}} */
134 populateArray2dWithVectorOfVector(mean1vec, mean1);
alexanderc350cdc2021-04-29 20:36:09 +0100135 REQUIRE(0 == Approx(TestPreprocess::GetMean(mean1)));
alexander3c798932021-03-26 21:42:19 +0000136
137 arm::app::Array2d<float> mean2(2, 2);
138 std::fill(mean2.begin(), mean2.end(), 0.f);
alexanderc350cdc2021-04-29 20:36:09 +0100139 REQUIRE(0 == Approx(TestPreprocess::GetMean(mean2)));
alexander3c798932021-03-26 21:42:19 +0000140
141 arm::app::Array2d<float> mean3(3,3);
142 std::fill(mean3.begin(), mean3.end(), 1.f);
alexanderc350cdc2021-04-29 20:36:09 +0100143 REQUIRE(1 == Approx(TestPreprocess::GetMean(mean3)));
alexander3c798932021-03-26 21:42:19 +0000144 }
145
146 SECTION("Std")
147 {
148 arm::app::Array2d<float> std1(2, 2);
149 std::fill(std1.begin(), std1.end(), 0.f); /* {{0, 0}, {0, 0}} */
alexanderc350cdc2021-04-29 20:36:09 +0100150 REQUIRE(0 == Approx(TestPreprocess::GetStdDev(std1, 0)));
alexander3c798932021-03-26 21:42:19 +0000151
152 std::vector<std::vector<float>> std2vec{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 0}};
153 arm::app::Array2d<float> std2(2,5);
154 populateArray2dWithVectorOfVector(std2vec, std2);
alexanderc350cdc2021-04-29 20:36:09 +0100155 const float mean = TestPreprocess::GetMean(std2);
156 REQUIRE(2.872281323 == Approx(TestPreprocess::GetStdDev(std2, mean)));
alexander3c798932021-03-26 21:42:19 +0000157
158 arm::app::Array2d<float> std3(2,2);
159 std::fill(std3.begin(), std3.end(), 1.f); /* std3{{1, 1}, {1, 1}}; */
alexanderc350cdc2021-04-29 20:36:09 +0100160 REQUIRE(0 == Approx(TestPreprocess::GetStdDev(std3, 1)));
alexander3c798932021-03-26 21:42:19 +0000161 }
162
163 SECTION("Norm") {
164 auto checker = [&](arm::app::Array2d<float>& d, std::vector<float>& g) {
alexanderc350cdc2021-04-29 20:36:09 +0100165 TestPreprocess::NormaliseVec(d);
alexander3c798932021-03-26 21:42:19 +0000166 std::vector<float> d_vec(d.begin(), d.end());
167 REQUIRE_THAT(g, Catch::Approx(d_vec));
168 };
169
170 std::vector<std::vector<float>> norm0vec{{1, 1}, {1, 1}};
171 std::vector<float> goldenNorm0 {0, 0, 0, 0};
172 arm::app::Array2d<float> norm0(2, 2);
173 populateArray2dWithVectorOfVector(norm0vec, norm0);
174 checker(norm0, goldenNorm0);
175
176 std::vector<std::vector<float>> norm1vec{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 0}};
177 std::vector<float> goldenNorm1 {
178 -1.218543592, -0.87038828, -0.522232968, -0.174077656, 0.174077656,
179 0.522232968, 0.87038828, 1.218543592, 1.566698904, -1.566698904};
180 arm::app::Array2d<float> norm1(2, 5);
181 populateArray2dWithVectorOfVector(norm1vec, norm1);
182 checker(norm1, goldenNorm1);
183 }
184}