xref: /aosp_15_r20/external/armnn/samples/SpeechRecognition/test/PreprocessTest.cpp (revision 89c4ff92f2867872bb9e2354d150bf0c8c502810)
1 //
2 // Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include <catch.hpp>
7 #include <limits>
8 
9 #include "DataStructures.hpp"
10 #include "Wav2LetterPreprocessor.hpp"
11 
PopulateTestWavVector(std::vector<int16_t> & vec)12 void PopulateTestWavVector(std::vector<int16_t>& vec)
13 {
14     constexpr int int16max = std::numeric_limits<int16_t>::max();
15     int val = 0;
16     for (size_t i = 0; i < vec.size(); ++i, ++val)
17     {
18 
19         /* We want a differential filter response from both - order 1
20          * and 2 => Don't have a linear signal here - we use a signal
21          * using squares for example. Alternate sign flips might work
22          * just as well and will be computationally less work! */
23         int valsq = val * val;
24         if (valsq > int16max)
25         {
26             val = 0;
27             valsq = 0;
28         }
29         vec[i] = valsq;
30     }
31 }
32 
33 TEST_CASE("Preprocessing calculation INT8")
34 {
35     /*Test  Constants: */
36     const uint32_t  windowLen             = 512;
37     const uint32_t  windowStride          = 160;
38     const float     quantScale            = 0.1410219967365265;
39     const int       quantOffset           = -11;
40     int             numMfccVectors        = 10;
41     const int       sampFreq              = 16000;
42     const int       frameLenMs            = 32;
43     const int       frameLenSamples       = sampFreq * frameLenMs * 0.001;
44     const int       numMfccFeats          = 13;
45     const int       audioDataToPreProcess = 512 + ((numMfccVectors -1) * windowStride);
46     int             outputBufferSize = numMfccVectors * numMfccFeats * 3;
47 
48     /* Test wav memory */
49     std::vector <int16_t> testWav1((windowStride * numMfccVectors) +
50                               (windowLen - windowStride));
51     /* Populate with dummy input */
52     PopulateTestWavVector(testWav1);
53 
54     MfccParams mfccParams(sampFreq, 128, 0, 8000, numMfccFeats,
55                           frameLenSamples, false, numMfccVectors);
56 
57     std::unique_ptr<Wav2LetterMFCC> mfccInst = std::make_unique<Wav2LetterMFCC>(mfccParams);
58 
59     std::vector<float> fullAudioData;
60 
61     for(int i = 0; i < 4; ++i)
62     {
63         for (auto f : testWav1)
64         {
65             fullAudioData.emplace_back(static_cast<float>(f) / (1<<15));
66         }
67     }
68 
69     Wav2LetterPreprocessor prep(frameLenSamples, windowStride, std::move(mfccInst));
70 
71     std::vector<int8_t> outputBuffer(outputBufferSize);
72 
73     prep.Invoke(fullAudioData.data(), audioDataToPreProcess, outputBuffer, quantOffset, quantScale);
74 
75     int8_t expectedResult[numMfccVectors][numMfccFeats*3] =
76     {
77             /* Feature vec 0 */
78             -32, 4, -9, -8, -10, -10, -11, -11, -11, -11, -12, -11, -11,    /* MFCCs   */
79             -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11,    /* Delta 1 */
80             -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10,    /* Delta 2 */
81 
82             /* Feature vec 1 */
83             -31, 4, -9, -8, -10, -10, -11, -11, -11, -11, -12, -11, -11,
84             -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11,
85             -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10,
86 
87             /* Feature vec 2 */
88             -31, 4, -9, -9, -10, -10, -11, -11, -11, -11, -12, -12, -12,
89             -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11,
90             -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10,
91 
92             /* Feature vec 3 */
93             -31, 4, -9, -9, -10, -10, -11, -11, -11, -11, -11, -12, -12,
94             -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11,
95             -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10,
96 
97             /* Feature vec 4 : this should have valid delta 1 and delta 2 */
98             -31, 4, -9, -9, -10, -10, -11, -11, -11, -11, -11, -12, -12,
99             -38, -29, -9, 1, -2, -7, -8, -8, -12, -16, -14, -5, 5,
100             -68, -50, -13, 5, 0, -9, -9, -8, -13, -20, -19, -3, 15,
101 
102             /* Feature vec 5 : this should have valid delta 1 and delta 2 */
103             -31, 4, -9, -8, -10, -10, -11, -11, -11, -11, -11, -12, -12,
104             -62, -45, -11, 5, 0, -8, -9, -8, -12, -19, -17, -3, 13,
105             -27, -22, -13, -9, -11, -12, -12, -11, -11, -13, -13, -10, -6,
106 
107             /* Feature vec 6 */
108             -31, 4, -9, -8, -10, -10, -11, -11, -11, -11, -12, -11, -11,
109             -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11,
110             -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10,
111 
112             /* Feature vec 7 */
113             -32, 4, -9, -8, -10, -10, -11, -11, -11, -12, -12, -11, -11,
114             -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11,
115             -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10,
116 
117             /* Feature vec 8 */
118             -32, 4, -9, -8, -10, -10, -11, -11, -11, -12, -12, -11, -11,
119             -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11,
120             -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10,
121 
122             /* Feature vec 9 */
123             -31, 4, -9, -8, -10, -10, -11, -11, -11, -11, -12, -11, -11,
124             -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11,
125             -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10
126     };
127 
128     /* Check that the elements have been calculated correctly */
129     for (uint32_t j = 0; j < numMfccVectors; ++j)
130     {
131         for (uint32_t i = 0; i < numMfccFeats * 3; ++i)
132         {
133             size_t tensorIdx = (j * numMfccFeats * 3) + i;
134             CHECK(static_cast<int16_t>(outputBuffer.at(tensorIdx) == static_cast<int16_t>(expectedResult[j][i])));
135         }
136     }
137 }
138