blob: e6b336ec985c14ec46ab428616352ebf714e54d8 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
Richard Burtonf32a86a2022-11-15 11:46:11 +00002 * SPDX-FileCopyrightText: Copyright 2021-2022 Arm Limited and/or its affiliates <open-source-office@arm.com>
alexander3c798932021-03-26 21:42:19 +00003 * 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
alexander3c798932021-03-26 21:42:19 +000019#include <limits>
Richard Burton4e002792022-05-04 09:45:02 +010020#include <catch.hpp>
alexander3c798932021-03-26 21:42:19 +000021
22constexpr uint32_t numMfccFeatures = 13;
23constexpr uint32_t numMfccVectors = 10;
24
25/* Test vector output: generated using test-asr-preprocessing.py. */
Richard Burton4e002792022-05-04 09:45:02 +010026int8_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 /* Feature vec 1. */
32 {-31, 4, -9, -8, -10, -10, -11, -11, -11, -11, -12, -11, -11,
33 -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11,
34 -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10},
35 /* Feature vec 2. */
36 {-31, 4, -9, -9, -10, -10, -11, -11, -11, -11, -12, -12, -12,
37 -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11,
38 -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10},
39 /* Feature vec 3. */
40 {-31, 4, -9, -9, -10, -10, -11, -11, -11, -11, -11, -12, -12,
41 -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11,
42 -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10},
43 /* Feature vec 4 : this should have valid delta 1 and delta 2. */
44 {-31, 4, -9, -9, -10, -10, -11, -11, -11, -11, -11, -12, -12,
45 -38, -29, -9, 1, -2, -7, -8, -8, -12, -16, -14, -5, 5,
46 -68, -50, -13, 5, 0, -9, -9, -8, -13, -20, -19, -3, 15},
47 /* Feature vec 5 : this should have valid delta 1 and delta 2. */
48 {-31, 4, -9, -8, -10, -10, -11, -11, -11, -11, -11, -12, -12,
49 -62, -45, -11, 5, 0, -8, -9, -8, -12, -19, -17, -3, 13,
50 -27, -22, -13, -9, -11, -12, -12, -11, -11, -13, -13, -10, -6},
51 /* Feature vec 6. */
52 {-31, 4, -9, -8, -10, -10, -11, -11, -11, -11, -12, -11, -11,
53 -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11,
54 -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10},
55 /* Feature vec 7. */
56 {-32, 4, -9, -8, -10, -10, -11, -11, -11, -12, -12, -11, -11,
57 -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11,
58 -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10},
59 /* Feature vec 8. */
60 {-32, 4, -9, -8, -10, -10, -11, -11, -11, -12, -12, -11, -11,
61 -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11,
62 -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10},
63 /* Feature vec 9. */
64 {-31, 4, -9, -8, -10, -10, -11, -11, -11, -11, -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}
alexander3c798932021-03-26 21:42:19 +000067};
68
69void PopulateTestWavVector(std::vector<int16_t>& vec)
70{
71 constexpr int int16max = std::numeric_limits<int16_t>::max();
72 int val = 0;
73 for (size_t i = 0; i < vec.size(); ++i, ++val) {
74
75 /* We want a differential filter response from both - order 1
76 * and 2 => Don't have a linear signal here - we use a signal
77 * using squares for example. Alternate sign flips might work
78 * just as well and will be computationally less work! */
79 int valsq = val * val;
80 if (valsq > int16max) {
81 val = 0;
82 valsq = 0;
83 }
84 vec[i] = valsq;
85 }
86}
87
88TEST_CASE("Preprocessing calculation INT8")
89{
alexander3c798932021-03-26 21:42:19 +000090 /* Constants. */
Richard Burton4e002792022-05-04 09:45:02 +010091 const uint32_t mfccWindowLen = 512;
92 const uint32_t mfccWindowStride = 160;
93 int dimArray[] = {3, 1, numMfccFeatures * 3, numMfccVectors};
94 const float quantScale = 0.1410219967365265;
95 const int quantOffset = -11;
alexander3c798932021-03-26 21:42:19 +000096
97 /* Test wav memory. */
Richard Burton4e002792022-05-04 09:45:02 +010098 std::vector<int16_t> testWav((mfccWindowStride * numMfccVectors) +
99 (mfccWindowLen - mfccWindowStride)
100 );
alexander3c798932021-03-26 21:42:19 +0000101
102 /* Populate with dummy input. */
103 PopulateTestWavVector(testWav);
104
105 /* Allocate mem for tensor. */
106 std::vector<int8_t> tensorVec(dimArray[1]*dimArray[2]*dimArray[3]);
107
108 /* Initialise dimensions and the test tensor. */
109 TfLiteIntArray* dims= tflite::testing::IntArrayFromInts(dimArray);
Richard Burton4e002792022-05-04 09:45:02 +0100110 TfLiteTensor inputTensor = tflite::testing::CreateQuantizedTensor(
111 tensorVec.data(), dims, quantScale, quantOffset, "preprocessedInput");
alexander3c798932021-03-26 21:42:19 +0000112
113 /* Initialise pre-processing module. */
Richard Burton4e002792022-05-04 09:45:02 +0100114 arm::app::AsrPreProcess prep{&inputTensor,
115 numMfccFeatures, numMfccVectors, mfccWindowLen, mfccWindowStride};
alexander3c798932021-03-26 21:42:19 +0000116
117 /* Invoke pre-processing. */
Richard Burton4e002792022-05-04 09:45:02 +0100118 REQUIRE(prep.DoPreProcess(testWav.data(), testWav.size()));
alexander3c798932021-03-26 21:42:19 +0000119
120 /* Wrap the tensor with a std::vector for ease. */
Richard Burton4e002792022-05-04 09:45:02 +0100121 auto* tensorData = tflite::GetTensorData<int8_t>(&inputTensor);
alexander3c798932021-03-26 21:42:19 +0000122 std::vector <int8_t> vecResults =
Richard Burton4e002792022-05-04 09:45:02 +0100123 std::vector<int8_t>(tensorData, tensorData + inputTensor.bytes);
alexander3c798932021-03-26 21:42:19 +0000124
125 /* Check sizes. */
126 REQUIRE(vecResults.size() == sizeof(expectedResult));
127
128 /* Check that the elements have been calculated correctly. */
129 for (uint32_t j = 0; j < numMfccVectors; ++j) {
130 for (uint32_t i = 0; i < numMfccFeatures * 3; ++i) {
131 size_t tensorIdx = (j * numMfccFeatures * 3) + i;
132 CHECK(vecResults[tensorIdx] == expectedResult[j][i]);
133 }
134 }
135}