blob: 26ddb245dcd592836c7b7d281ef24a9ef493574b [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 "Wav2LetterPreprocess.hpp"
18
19#include <algorithm>
20#include <catch.hpp>
21#include <limits>
22
23constexpr uint32_t numMfccFeatures = 13;
24constexpr uint32_t numMfccVectors = 10;
25
26/* Test vector output: generated using test-asr-preprocessing.py. */
27int8_t expectedResult[numMfccVectors][numMfccFeatures*3] = {
28 /* Feature vec 0. */
29 -32, 4, -9, -8, -10, -10, -11, -11, -11, -11, -12, -11, -11, /* MFCCs. */
30 -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, /* Delta 1. */
31 -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, /* Delta 2. */
32
33 /* Feature vec 1. */
34 -31, 4, -9, -8, -10, -10, -11, -11, -11, -11, -12, -11, -11,
35 -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11,
36 -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10,
37
38 /* Feature vec 2. */
39 -31, 4, -9, -9, -10, -10, -11, -11, -11, -11, -12, -12, -12,
40 -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11,
41 -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10,
42
43 /* Feature vec 3. */
44 -31, 4, -9, -9, -10, -10, -11, -11, -11, -11, -11, -12, -12,
45 -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11,
46 -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10,
47
48 /* Feature vec 4 : this should have valid delta 1 and delta 2. */
49 -31, 4, -9, -9, -10, -10, -11, -11, -11, -11, -11, -12, -12,
50 -38, -29, -9, 1, -2, -7, -8, -8, -12, -16, -14, -5, 5,
51 -68, -50, -13, 5, 0, -9, -9, -8, -13, -20, -19, -3, 15,
52
53 /* Feature vec 5 : this should have valid delta 1 and delta 2. */
54 -31, 4, -9, -8, -10, -10, -11, -11, -11, -11, -11, -12, -12,
55 -62, -45, -11, 5, 0, -8, -9, -8, -12, -19, -17, -3, 13,
56 -27, -22, -13, -9, -11, -12, -12, -11, -11, -13, -13, -10, -6,
57
58 /* Feature vec 6. */
59 -31, 4, -9, -8, -10, -10, -11, -11, -11, -11, -12, -11, -11,
60 -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11,
61 -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10,
62
63 /* Feature vec 7. */
64 -32, 4, -9, -8, -10, -10, -11, -11, -11, -12, -12, -11, -11,
65 -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11,
66 -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10,
67
68 /* Feature vec 8. */
69 -32, 4, -9, -8, -10, -10, -11, -11, -11, -12, -12, -11, -11,
70 -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11,
71 -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10,
72
73 /* Feature vec 9. */
74 -31, 4, -9, -8, -10, -10, -11, -11, -11, -11, -12, -11, -11,
75 -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11,
76 -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10
77};
78
79void PopulateTestWavVector(std::vector<int16_t>& vec)
80{
81 constexpr int int16max = std::numeric_limits<int16_t>::max();
82 int val = 0;
83 for (size_t i = 0; i < vec.size(); ++i, ++val) {
84
85 /* We want a differential filter response from both - order 1
86 * and 2 => Don't have a linear signal here - we use a signal
87 * using squares for example. Alternate sign flips might work
88 * just as well and will be computationally less work! */
89 int valsq = val * val;
90 if (valsq > int16max) {
91 val = 0;
92 valsq = 0;
93 }
94 vec[i] = valsq;
95 }
96}
97
98TEST_CASE("Preprocessing calculation INT8")
99{
alexander3c798932021-03-26 21:42:19 +0000100
101 /* Constants. */
102 const uint32_t windowLen = 512;
103 const uint32_t windowStride = 160;
Richard Burton0d110592021-08-12 17:26:30 +0100104 int dimArray[] = {3, 1, numMfccFeatures * 3, numMfccVectors};
alexander3c798932021-03-26 21:42:19 +0000105 const float quantScale = 0.1410219967365265;
106 const int quantOffset = -11;
107
108 /* Test wav memory. */
109 std::vector <int16_t> testWav((windowStride * numMfccVectors) +
110 (windowLen - windowStride));
111
112 /* Populate with dummy input. */
113 PopulateTestWavVector(testWav);
114
115 /* Allocate mem for tensor. */
116 std::vector<int8_t> tensorVec(dimArray[1]*dimArray[2]*dimArray[3]);
117
118 /* Initialise dimensions and the test tensor. */
119 TfLiteIntArray* dims= tflite::testing::IntArrayFromInts(dimArray);
120 TfLiteTensor tensor = tflite::testing::CreateQuantizedTensor(
121 tensorVec.data(), dims, quantScale, quantOffset, "preprocessedInput");
122
123 /* Initialise pre-processing module. */
124 arm::app::audio::asr::Preprocess prep{
125 numMfccFeatures, windowLen, windowStride, numMfccVectors};
126
127 /* Invoke pre-processing. */
128 REQUIRE(prep.Invoke(testWav.data(), testWav.size(), &tensor));
129
130 /* Wrap the tensor with a std::vector for ease. */
131 int8_t * tensorData = tflite::GetTensorData<int8_t>(&tensor);
132 std::vector <int8_t> vecResults =
133 std::vector<int8_t>(tensorData, tensorData + tensor.bytes);
134
135 /* Check sizes. */
136 REQUIRE(vecResults.size() == sizeof(expectedResult));
137
138 /* Check that the elements have been calculated correctly. */
139 for (uint32_t j = 0; j < numMfccVectors; ++j) {
140 for (uint32_t i = 0; i < numMfccFeatures * 3; ++i) {
141 size_t tensorIdx = (j * numMfccFeatures * 3) + i;
142 CHECK(vecResults[tensorIdx] == expectedResult[j][i]);
143 }
144 }
145}