blob: 59a0b9792558324d2d5e4631bec824d18854c893 [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
18#include "AdMelSpectrogram.hpp"
19#include <limits>
20#include <algorithm>
21#include <catch.hpp>
22
23/* First 1024 samples from test wav. */
24const std::vector<int16_t> testWav1 = std::vector<int16_t>{
25 490,495,445,403,390,259,126,146,
26 175,134,232,243,166,145,123,33,
27 -61,-4,8,-115,-281,-292,-210,-133,
28 -98,-142,-229,-356,-415,-438,-443,-396,
29 -377,-297,-85,122,172,16,-197,-351,
30 -484,-408,-378,-405,-399,-335,-180,-141,
31 -124,-108,-46,37,141,234,264,218,
32 147,164,132,111,125,73,2,36,
33 107,113,93,6,-40,-153,-273,-282,
34 -291,-298,-389,-446,-394,-324,-333,-385,
35 -485,-548,-690,-718,-660,-704,-690,-601,
36 -549,-641,-637,-513,-469,-366,-227,-269,
37 -348,-408,-486,-570,-638,-666,-730,-746,
38 -710,-634,-543,-461,-281,-156,-130,-126,
39 -144,-118,-23,103,132,37,-69,-86,
40 -234,-360,-366,-330,-248,-268,-282,-169,
41 -190,-152,-151,-145,-133,-205,-263,-397,
42 -558,-656,-668,-718,-779,-828,-856,-817,
43 -761,-759,-722,-772,-873,-983,-962,-897,
44 -843,-788,-750,-677,-555,-447,-373,-218,
45 -182,-230,-204,-174,-144,-127,-231,-199,
46 -127,-194,-250,-183,-189,-254,-249,-337,
47 -417,-459,-513,-505,-481,-402,-344,-284,
48 -281,-441,-450,-423,-327,-119,102,197,
49 208,173,102,103,165,131,15,75,
50 283,365,322,391,303,287,372,406,
51 493,577,640,681,577,498,524,511,
52 476,425,380,315,337,339,408,603,
53 749,745,672,654,588,520,523,544,
54 557,632,636,565,491,413,368,252,
55 136,33,1,-26,-152,-258,-98,18,
56 1,-18,-99,-117,-109,-228,-295,-349,
57 -334,-337,-441,-373,-279,-202,-204,-219,
58 -119,149,410,489,564,623,683,642,
59 707,872,932,862,833,862,894,784,
60 637,559,507,394,306,420,510,484,
61 519,526,599,789,959,1052,1063,1030,
62 860,697,603,530,475,463,468,461,
63 609,641,534,482,435,329,239,216,
64 185,82,88,106,60,26,-43,-127,
65 -220,-262,-317,-259,-172,-175,-271,-217,
66 -196,-164,8,144,150,134,60,13,
67 57,-58,-115,-171,-282,-310,-298,-106,
68 42,-101,-172,-181,-249,-326,-262,-132,
69 -56,-82,-71,-88,-196,-325,-426,-413,
70 -411,-317,-191,-172,-195,-292,-328,-191,
71 -88,-60,21,-63,-175,-135,-64,-83,
72 -163,-279,-440,-536,-403,-308,-236,-132,
73 -95,-69,-73,-21,13,133,185,251,
74 238,88,-66,-134,-175,-231,-219,-151,
75 -213,-328,-340,-374,-459,-601,-556,-395,
76 -248,-205,-174,-227,-402,-493,-464,-483,
77 -588,-564,-463,-493,-505,-416,-378,-313,
78 -215,-192,-192,-59,18,-40,-66,-60,
79 -143,-263,-213,-224,-265,-249,-237,-227,
80 -418,-504,-573,-699,-679,-577,-500,-570,
81 -538,-416,-444,-415,-294,-300,-427,-423,
82 -299,-279,-279,-187,-137,-123,60,230,
83 227,277,356,413,440,418,477,594,
84 697,729,586,561,653,570,590,628,
85 497,357,366,470,591,576,458,439,
86 417,431,447,349,304,241,294,406,
87 484,516,587,598,566,465,380,347,
88 316,391,429,409,216,69,57,76,
89 150,101,93,113,90,41,-28,-15,
90 -2,47,208,261,333,362,239,301,
91 422,431,426,434,482,510,480,407,
92 244,53,-108,-234,-275,-302,-304,-207,
93 -117,-181,-214,-248,-203,-52,5,-14,
94 24,-9,-154,-186,-82,-23,-62,-165,
95 -174,-190,-368,-414,-316,-301,-180,41,
96 116,214,319,408,416,157,-100,-40,
97 118,248,310,301,302,387,458,414,
98 301,261,233,111,33,39,65,56,
99 9,-92,-87,-98,-172,-196,-186,-18,
100 -14,-57,-111,-178,-278,-304,-358,-359,
101 -362,-464,-528,-400,-355,-284,-189,-240,
102 -253,-216,-319,-490,-621,-684,-758,-860,
103 -883,-877,-847,-787,-766,-852,-727,-481,
104 -339,-282,-266,-405,-414,-286,-225,-204,
105 -330,-488,-412,-292,-254,-290,-372,-436,
106 -545,-564,-413,-360,-344,-389,-430,-340,
107 -248,-271,-343,-383,-414,-409,-272,-223,
108 -215,-123,-10,-4,-6,-27,-11,78,
109 169,226,139,-19,16,100,54,-75,
110 -117,-103,-77,-277,-598,-644,-602,-509,
111 -396,-232,-227,-208,-153,-146,-205,-223,
112 -108,-55,-26,-8,-42,-178,-298,-320,
113 -254,-146,-135,-262,-370,-331,-337,-394,
114 -265,-53,136,309,354,312,345,303,
115 275,338,287,269,346,329,319,327,
116 199,118,251,296,243,111,90,150,
117 104,163,274,278,242,135,93,138,
118 5,-154,-206,-270,-334,-356,-251,-96,
119 -78,-123,-80,-93,-160,-217,-214,-154,
120 -42,128,228,243,307,465,492,425,
121 381,382,425,530,518,484,560,654,
122 659,663,723,717,672,652,542,507,
123 471,468,579,573,459,313,262,310,
124 284,235,331,361,275,207,104,35,
125 35,89,136,192,218,161,89,64,
126 116,175,159,95,96,242,350,248,
127 170,64,-35,-136,-202,-271,-307,-290,
128 -257,-219,-206,-185,-216,-213,-184,-135,
129 -165,-141,-25,-31,-28,-98,-247,-162,
130 10,35,-16,-113,-139,-127,-58,-100,
131 -166,-320,-406,-462,-604,-594,-650,-538,
132 -427,-365,-196,-117,-120,-102,-66,-122,
133 -211,-235,-202,-135,-40,-10,-38,-150,
134 -286,-223,-50,93,149,86,184,128,
135 113,163,13,-53,-135,-100,-72,-75,
136 -73,-118,-150,-197,-224,-131,-59,-109,
137 -92,-129,-189,-220,-166,-173,-114,-8,
138 26,-27,-38,50,109,143,161,209,
139 266,289,384,397,312,203,5,-64,
140 -14,6,56,67,19,-43,-112,-46,
141 -74,-101,-83,-115,-142,-207,-274,-292,
142 -299,-236,-181,-188,-48,60,6,-76,
143 -8,115,188,260,236,143,44,-30,
144 -17,31,37,-16,-28,87,210,276,
145 372,365,302,270,137,-8,-142,-246,
146 -279,-259,-203,-241,-278,-254,-245,-177,
147 -77,-8,-47,-159,-295,-412,-414,-414,
148 -566,-533,-255,-82,-10,222,358,336,
149 355,360,303,237,267,224,244,434,
150 422,372,404,464,559,538,446,294,
151 217,60,-82,-150,-144,-162,-250,-263,
152 -222,-148,-81,-134,-134,-106,-27,-71,
153};
154
155/* Golden log mel spec output for test wav. */
156const std::vector<float> testWavMelSpec {
157 -8.601085, -10.563560, -13.791912, -12.356619, -16.892878,
158 -16.913876, -15.695299, -21.848980, -21.193371, -18.772688,
159 -21.795116, -20.008236, -22.413673, -25.162649, -24.091856,
160 -24.936411, -19.341146, -23.534576, -29.052885, -26.562546,
161 -25.046455, -29.586889, -30.115177, -32.281334, -29.806450,
162 -30.398304, -26.682615, -27.397421, -31.224312, -31.033779,
163 -36.314369, -29.530331, -28.428139, -30.097546, -34.101303,
164 -32.660480, -34.229076, -34.668293, -35.140759, -34.104649,
165 -34.141472, -36.514408, -37.655891, -33.590931, -40.532566,
166 -39.105091, -39.600319, -40.239834, -41.356224, -41.103714,
167 -39.861557, -41.827553, -41.275696, -42.203575, -42.689217,
168 -46.495552, -46.704731, -45.560322, -47.423828, -50.672031,
169 -51.387669, -53.410839, -54.899536, -55.807552,
170};
171
172
173arm::app::audio::AdMelSpectrogram GetMelSpecInstance() {
174 int frameLenSamples = 1024;
175 return arm::app::audio::AdMelSpectrogram(frameLenSamples);
176}
177
178template <class T>
179void TestQuntisedMelSpec() {
180 float quantScale = 0.1410219967365265;
181 int quantOffset = 11;
182 std::vector<T> melSpecOutput = GetMelSpecInstance().MelSpecComputeQuant<T>(testWav1, quantScale, quantOffset);
183
184 long min_val = std::numeric_limits<T>::min();
185 long max_val = std::numeric_limits<T>::max();
186
187 for (size_t i = 0; i < testWavMelSpec.size(); i++){
188 long TestWavMelSpec = (std::lround((testWavMelSpec[i] / quantScale) + quantOffset));
189 T quantizedTestWavMelSpec = static_cast<T>(std::max(min_val, std::min(TestWavMelSpec, max_val)));
190
191 REQUIRE(quantizedTestWavMelSpec == Approx(melSpecOutput[i]).margin(1));
192 }
193}
194
195template void TestQuntisedMelSpec<int8_t>();
196template void TestQuntisedMelSpec<uint8_t>();
197template void TestQuntisedMelSpec<int16_t>();
198
199TEST_CASE("Mel Spec calculation") {
200
alexander3c798932021-03-26 21:42:19 +0000201 SECTION("FP32") {
202 auto melSpecOutput = GetMelSpecInstance().ComputeMelSpec(testWav1);
203 REQUIRE_THAT( melSpecOutput, Catch::Approx( testWavMelSpec ).margin(0.1) );
204 }
205
206 SECTION("int8_t") {
207 TestQuntisedMelSpec<int8_t>();
208 }
209
210 SECTION("uint8_t") {
211 TestQuntisedMelSpec<uint8_t>();
212 }
213
214 SECTION("int16_t") {
215 TestQuntisedMelSpec<int16_t>();
216 }
217}