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