summaryrefslogtreecommitdiff
path: root/tests/use_case/asr/MfccTests.cc
blob: c70e53e23e8fa5866df28f83d9392f1f3bddb417 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * Copyright (c) 2021 Arm Limited. All rights reserved.
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "Wav2LetterMfcc.hpp"

#include <algorithm>
#include <catch.hpp>
#include <limits>

/* First 512 samples from itellyou.wav. */
const std::vector<int16_t> testWav1 = std::vector<int16_t> {
    -3,0,1,-1,2,3,-2,2,
    1,-2,0,3,-1,8,3,2,
    -1,-1,2,7,3,5,6,6,
    6,12,5,6,3,3,5,4,
    4,6,7,7,7,3,7,2,
    8,4,4,2,-4,-1,-1,-4,
    2,1,-1,-4,0,-7,-6,-2,
    -5,1,-5,-1,-7,-3,-3,-7,
    0,-3,3,-5,0,1,-2,-2,
    -3,-3,-7,-3,-2,-6,-5,-8,
    -2,-8,4,-9,-4,-9,-5,-5,
    -3,-9,-3,-9,-1,-7,-4,1,
    -3,2,-8,-4,-4,-5,1,-3,
    -1,0,-1,-2,-3,-2,-4,-1,
    1,-1,3,0,3,2,0,0,
    0,-3,1,1,0,8,3,4,
    1,5,6,4,7,3,3,0,
    3,6,7,6,4,5,9,9,
    5,5,8,1,6,9,6,6,
    7,1,8,1,5,0,5,5,
    0,3,2,7,2,-3,3,0,
    3,0,0,0,2,0,-1,-1,
    -2,-3,-8,0,1,0,-3,-3,
    -3,-2,-3,-3,-4,-6,-2,-8,
    -9,-4,-1,-5,-3,-3,-4,-3,
    -6,3,0,-1,-2,-9,-4,-2,
    2,-1,3,-5,-5,-2,0,-2,
    0,-1,-3,1,-2,9,4,5,
    2,2,1,0,-6,-2,0,0,
    0,-1,4,-4,3,-7,-1,5,
    -6,-1,-5,4,3,9,-2,1,
    3,0,0,-2,1,2,1,1,
    0,3,2,-1,3,-3,7,0,
    0,3,2,2,-2,3,-2,2,
    -3,4,-1,-1,-5,-1,-3,-2,
    1,-1,3,2,4,1,2,-2,
    0,2,7,0,8,-3,6,-3,
    6,1,2,-3,-1,-1,-1,1,
    -2,2,1,2,0,-2,3,-2,
    3,-2,1,0,-3,-1,-2,-4,
    -6,-5,-8,-1,-4,0,-3,-1,
    -1,-1,0,-2,-3,-7,-1,0,
    1,5,0,5,1,1,-3,0,
    -6,3,-8,4,-8,6,-6,1,
    -6,-2,-5,-6,0,-5,4,-1,
    4,-2,1,2,1,0,-2,0,
    0,2,-2,2,-5,2,0,-2,
    1,-2,0,5,1,0,1,5,
    0,8,3,2,2,0,5,-2,
    3,1,0,1,0,-2,-1,-3,
    1,-1,3,0,3,0,-2,-1,
    -4,-4,-4,-1,-4,-4,-3,-6,
    -3,-7,-3,-1,-2,0,-5,-4,
    -7,-3,-2,-2,1,2,2,8,
    5,4,2,4,3,5,0,3,
    3,6,4,2,2,-2,4,-2,
    3,3,2,1,1,4,-5,2,
    -3,0,-1,1,-2,2,5,1,
    4,2,3,1,-1,1,0,6,
    0,-2,-1,1,-1,2,-5,-1,
    -5,-1,-6,-3,-3,2,4,0,
    -1,-5,3,-4,-1,-3,-4,1,
    -4,1,-1,-1,0,-5,-4,-2,
    -1,-1,-3,-7,-3,-3,4,4,
};

const std::vector<int16_t> testWav2 = std::vector<int16_t> (512, 0);

/* Golden mfcc output for testwav1. */
const std::vector<float> golden_mfcc_output_testWav1 {
    -835.24603, 21.010452, 18.699404, 7.4338417, 19.028961, -5.401735, 6.4761047, -11.400679,
    8.392709, 12.202361, 8.403276, -13.508412, -18.307348
};

/* Golden mfcc output for the all zero wav. */
const std::vector<float> golden_mfcc_output_testWav2 {
    -1131.37085, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};


arm::app::audio::Wav2LetterMFCC GetMFCCInstance()
{
    const auto sampFreq = arm::app::audio::Wav2LetterMFCC::ms_defaultSamplingFreq;
    const auto frameLenMs = 32;
    const auto numMfccFeats = 13;
    const auto frameLenSamples = sampFreq * frameLenMs * 0.001;
    return arm::app::audio::Wav2LetterMFCC(numMfccFeats, frameLenSamples);
}

template <class T>
void TestQuantisedMFCC()
{
    const auto quantScale = 0.1410219967365265;
    const auto quantOffset = 11;
    std::vector<T> mfccOutput = GetMFCCInstance().MfccComputeQuant<T>(testWav1, quantScale, quantOffset);

    long min_val = std::numeric_limits<T>::min();
    long max_val = std::numeric_limits<T>::max();

    for (size_t i = 0; i < golden_mfcc_output_testWav1.size(); i++){
        long TestWavMfcc = (std::lround((golden_mfcc_output_testWav1[i] / quantScale) + quantOffset));
        T quantizedTestWavMfcc = static_cast<T>(std::max(min_val, std::min(TestWavMfcc, max_val)));

        REQUIRE(quantizedTestWavMfcc  == Approx(mfccOutput[i]).margin(2));
    }
}

template void TestQuantisedMFCC<int8_t>();
template void TestQuantisedMFCC<uint8_t>();
template void TestQuantisedMFCC<int16_t>();

TEST_CASE("MFCC calculation")
{
    hal_platform    platform;
    data_acq_module dataAcq;
    data_psn_module dataPsn;
    platform_timer  timer;

    /* Initialise the HAL and platform. */
    hal_init(&platform, &dataAcq, &dataPsn, &timer);
    hal_platform_init(&platform);

    SECTION("FP32")
    {
        auto mfccOutput = GetMFCCInstance().MfccCompute(testWav1);
        REQUIRE_THAT( mfccOutput, Catch::Approx( golden_mfcc_output_testWav1 ).margin(0.3) );

        auto mfccOutput2 = GetMFCCInstance().MfccCompute(testWav2);
        REQUIRE_THAT( mfccOutput2, Catch::Approx( golden_mfcc_output_testWav2 ).margin(0.001) );
    }

    SECTION("int8_t")
    {
        TestQuantisedMFCC<int8_t>();
    }

    SECTION("uint8_t")
    {
        TestQuantisedMFCC<uint8_t>();
    }

    SECTION("int16_t")
    {
        TestQuantisedMFCC<int16_t>();
    }
}