blob: 9401f40a3eab620f9fb0fb808e3309f5d1661ca6 [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:
28 TestPreprocess()
29 : arm::app::audio::asr::Preprocess(0,0,0,0)
30 {}
31
32 bool ComputeDeltas(arm::app::Array2d<float>& mfcc,
33 arm::app::Array2d<float>& delta1,
34 arm::app::Array2d<float>& delta2)
35 {
36 return this->_ComputeDeltas(mfcc, delta1, delta2);
37 }
38
39 float GetMean(arm::app::Array2d<float>& vec)
40 {
41 return this->_GetMean(vec);
42 }
43
44 float GetStdDev(arm::app::Array2d<float>& vec, const float mean)
45 {
46 return this->_GetStdDev(vec, mean);
47 }
48
49 void NormaliseVec(arm::app::Array2d<float>& vec)
50 {
51 return this->_NormaliseVec(vec);
52 }
53};
54
55template<class T>
56void CheckOutputs(const std::vector<T> goldenOutput, std::vector<T> output)
57{
58 const size_t goldenSize = goldenOutput.size();
59 const size_t realSize = output.size();
60
61 REQUIRE(realSize == goldenSize);
62 REQUIRE_THAT(output, Catch::Approx( goldenOutput ).margin(0.0001));
63}
64template void CheckOutputs<float>(const std::vector<float> goldenOutput, std::vector<float> output);
65
66void populateBuffer(const float* input, size_t size, size_t numMfccFeats, std::vector<std::vector<float>>& buf)
67{
68 size_t time = 0;
69 for (size_t i = 0; i < size; ++i) {
70 if (i > 0 && i % numMfccFeats == 0) {
71 ++time;
72 }
73 float featureValue = *(input + i);
74 buf[i % numMfccFeats][time] = featureValue;
75 }
76}
77
78void populateArray2dWithVectorOfVector(std::vector<std::vector<float>> vec, arm::app::Array2d<float>& buf)
79{
80 for (size_t i = 0; i < vec.size(); ++i) {
81 for (size_t j = 0; j < vec[i].size(); ++j) {
82 buf(i, j) = vec[i][j];
83 }
84 }
85}
86
87TEST_CASE("Floating point asr features calculation", "[ASR]")
88{
89 TestPreprocess tp;
90
91 SECTION("First and second diff")
92 {
93 constexpr uint32_t numMfccFeats = 13;
94 constexpr uint32_t numFeatVectors = 296;
95
96 arm::app::Array2d<float> mfccBuf(numMfccFeats, numFeatVectors);
97 arm::app::Array2d<float> delta1Buf(numMfccFeats, numFeatVectors);
98 arm::app::Array2d<float> delta2Buf(numMfccFeats, numFeatVectors);
99
100 std::vector<std::vector<float>> goldenMfccBuf(numMfccFeats, std::vector<float>(numFeatVectors));
101 std::vector<std::vector<float>> goldenDelta1Buf(numMfccFeats, std::vector<float>(numFeatVectors));
102 std::vector<std::vector<float>> goldenDelta2Buf(numMfccFeats, std::vector<float>(numFeatVectors));
103
104 populateBuffer(golden_asr_mfcc, golden_asr_mfcc_len, numMfccFeats, goldenMfccBuf);
105 populateBuffer(golden_diff1_features, golden_diff1_len, numMfccFeats, goldenDelta1Buf);
106 populateBuffer(golden_diff2_features, golden_diff2_len, numMfccFeats, goldenDelta2Buf);
107
108 populateArray2dWithVectorOfVector(goldenMfccBuf, mfccBuf);
109 std::fill(delta1Buf.begin(), delta1Buf.end(), 0.f);
110 std::fill(delta2Buf.begin(), delta2Buf.end(), 0.f);
111
112 tp.ComputeDeltas(mfccBuf, delta1Buf, delta2Buf);
113
114 /* First 4 and last 4 values are different because we pad AFTER diff calculated. */
115 for (size_t i = 0; i < numMfccFeats; ++i) {
116 const float* start_goldenDelta1Buf = goldenDelta1Buf[i].data() + 4;
117 const float* start_delta1 = delta1Buf.begin() + i * delta1Buf.size(1) + 4;
118 std::vector<float> goldenDataDelta1(start_goldenDelta1Buf, start_goldenDelta1Buf + numFeatVectors - 8);
119 std::vector<float> tensorDataDelta1(start_delta1, start_delta1 + numFeatVectors - 8);
120
121 CheckOutputs<float>(goldenDataDelta1,tensorDataDelta1);
122
123 const float* start_goldenDelta2Buf = goldenDelta2Buf[i].data() + 4;
124 const float* start_delta2 = delta2Buf.begin() + i * delta2Buf.size(1) + 4;
125 std::vector<float> goldenDataDelta2(start_goldenDelta2Buf, start_goldenDelta2Buf + numFeatVectors - 8);
126 std::vector<float> tensorDataDelta2(start_delta2, start_delta2 + numFeatVectors - 8);
127
128 CheckOutputs<float>(goldenDataDelta2,tensorDataDelta2);
129 }
130
131 }
132
133 SECTION("Mean")
134 {
135 std::vector<std::vector<float>> mean1vec{{1, 2},
136 {-1, -2}};
137 arm::app::Array2d<float> mean1(2,2); /* {{1, 2},{-1, -2}} */
138 populateArray2dWithVectorOfVector(mean1vec, mean1);
139 REQUIRE(0 == Approx(tp.GetMean(mean1)));
140
141 arm::app::Array2d<float> mean2(2, 2);
142 std::fill(mean2.begin(), mean2.end(), 0.f);
143 REQUIRE(0 == Approx(tp.GetMean(mean2)));
144
145 arm::app::Array2d<float> mean3(3,3);
146 std::fill(mean3.begin(), mean3.end(), 1.f);
147 REQUIRE(1 == Approx(tp.GetMean(mean3)));
148 }
149
150 SECTION("Std")
151 {
152 arm::app::Array2d<float> std1(2, 2);
153 std::fill(std1.begin(), std1.end(), 0.f); /* {{0, 0}, {0, 0}} */
154 REQUIRE(0 == Approx(tp.GetStdDev(std1, 0)));
155
156 std::vector<std::vector<float>> std2vec{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 0}};
157 arm::app::Array2d<float> std2(2,5);
158 populateArray2dWithVectorOfVector(std2vec, std2);
159 const float mean = tp.GetMean(std2);
160 REQUIRE(2.872281323 == Approx(tp.GetStdDev(std2, mean)));
161
162 arm::app::Array2d<float> std3(2,2);
163 std::fill(std3.begin(), std3.end(), 1.f); /* std3{{1, 1}, {1, 1}}; */
164 REQUIRE(0 == Approx(tp.GetStdDev(std3, 1)));
165 }
166
167 SECTION("Norm") {
168 auto checker = [&](arm::app::Array2d<float>& d, std::vector<float>& g) {
169 tp.NormaliseVec(d);
170 std::vector<float> d_vec(d.begin(), d.end());
171 REQUIRE_THAT(g, Catch::Approx(d_vec));
172 };
173
174 std::vector<std::vector<float>> norm0vec{{1, 1}, {1, 1}};
175 std::vector<float> goldenNorm0 {0, 0, 0, 0};
176 arm::app::Array2d<float> norm0(2, 2);
177 populateArray2dWithVectorOfVector(norm0vec, norm0);
178 checker(norm0, goldenNorm0);
179
180 std::vector<std::vector<float>> norm1vec{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 0}};
181 std::vector<float> goldenNorm1 {
182 -1.218543592, -0.87038828, -0.522232968, -0.174077656, 0.174077656,
183 0.522232968, 0.87038828, 1.218543592, 1.566698904, -1.566698904};
184 arm::app::Array2d<float> norm1(2, 5);
185 populateArray2dWithVectorOfVector(norm1vec, norm1);
186 checker(norm1, goldenNorm1);
187 }
188}