blob: 0eb152aea3a5d2bfde3ff24f0d1fc75aebae7e36 [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 "Wav2LetterMfcc.hpp"
18
19#include "PlatformMath.hpp"
20
21#include <cfloat>
22
23namespace arm {
24namespace app {
25namespace audio {
26
27 bool Wav2LetterMFCC::ApplyMelFilterBank(
28 std::vector<float>& fftVec,
29 std::vector<std::vector<float>>& melFilterBank,
alexanderc350cdc2021-04-29 20:36:09 +010030 std::vector<uint32_t>& filterBankFilterFirst,
31 std::vector<uint32_t>& filterBankFilterLast,
alexander3c798932021-03-26 21:42:19 +000032 std::vector<float>& melEnergies)
33 {
34 const size_t numBanks = melEnergies.size();
35
36 if (numBanks != filterBankFilterFirst.size() ||
37 numBanks != filterBankFilterLast.size()) {
38 printf_err("Unexpected filter bank lengths\n");
39 return false;
40 }
41
42 for (size_t bin = 0; bin < numBanks; ++bin) {
43 auto filterBankIter = melFilterBank[bin].begin();
alexanderc350cdc2021-04-29 20:36:09 +010044 auto end = melFilterBank[bin].end();
45 /* Avoid log of zero at later stages, same value used in librosa.
46 * The number was used during our default wav2letter model training. */
47 float melEnergy = 1e-10;
48 const uint32_t firstIndex = filterBankFilterFirst[bin];
49 const uint32_t lastIndex = std::min<uint32_t>(filterBankFilterLast[bin], fftVec.size() - 1);
alexander3c798932021-03-26 21:42:19 +000050
alexanderc350cdc2021-04-29 20:36:09 +010051 for (uint32_t i = firstIndex; i <= lastIndex && filterBankIter != end; ++i) {
alexander3c798932021-03-26 21:42:19 +000052 melEnergy += (*filterBankIter++ * fftVec[i]);
53 }
54
55 melEnergies[bin] = melEnergy;
56 }
57
58 return true;
59 }
60
61 void Wav2LetterMFCC::ConvertToLogarithmicScale(
62 std::vector<float>& melEnergies)
63 {
64 float maxMelEnergy = -FLT_MAX;
65
66 /* Container for natural logarithms of mel energies. */
67 std::vector <float> vecLogEnergies(melEnergies.size(), 0.f);
68
69 /* Because we are taking natural logs, we need to multiply by log10(e).
70 * Also, for wav2letter model, we scale our log10 values by 10. */
71 constexpr float multiplier = 10.0 * /* Default scalar. */
72 0.4342944819032518; /* log10f(std::exp(1.0)) */
73
74 /* Take log of the whole vector. */
75 math::MathUtils::VecLogarithmF32(melEnergies, vecLogEnergies);
76
77 /* Scale the log values and get the max. */
78 for (auto iterM = melEnergies.begin(), iterL = vecLogEnergies.begin();
alexanderc350cdc2021-04-29 20:36:09 +010079 iterM != melEnergies.end() && iterL != vecLogEnergies.end(); ++iterM, ++iterL) {
alexander3c798932021-03-26 21:42:19 +000080
81 *iterM = *iterL * multiplier;
82
83 /* Save the max mel energy. */
84 if (*iterM > maxMelEnergy) {
85 maxMelEnergy = *iterM;
86 }
87 }
88
89 /* Clamp the mel energies. */
90 constexpr float maxDb = 80.0;
91 const float clampLevelLowdB = maxMelEnergy - maxDb;
alexanderc350cdc2021-04-29 20:36:09 +010092 for (float& melEnergy : melEnergies) {
93 melEnergy = std::max(melEnergy, clampLevelLowdB);
alexander3c798932021-03-26 21:42:19 +000094 }
95 }
96
97 std::vector<float> Wav2LetterMFCC::CreateDCTMatrix(
98 const int32_t inputLength,
99 const int32_t coefficientCount)
100 {
101 std::vector<float> dctMatix(inputLength * coefficientCount);
102
103 /* Orthonormal normalization. */
104 const float normalizerK0 = 2 * math::MathUtils::SqrtF32(1.0f /
105 static_cast<float>(4*inputLength));
106 const float normalizer = 2 * math::MathUtils::SqrtF32(1.0f /
107 static_cast<float>(2*inputLength));
108
109 const float angleIncr = M_PI / inputLength;
110 float angle = angleIncr; /* We start using it at k = 1 loop. */
111
112 /* First row of DCT will use normalizer K0. */
113 for (int32_t n = 0; n < inputLength; ++n) {
114 dctMatix[n] = normalizerK0 /* cos(0) = 1 */;
115 }
116
117 /* Second row (index = 1) onwards, we use standard normalizer. */
118 for (int32_t k = 1, m = inputLength; k < coefficientCount; ++k, m += inputLength) {
119 for (int32_t n = 0; n < inputLength; ++n) {
120 dctMatix[m+n] = normalizer *
121 math::MathUtils::CosineF32((n + 0.5f) * angle);
122 }
123 angle += angleIncr;
124 }
125 return dctMatix;
126 }
127
128 float Wav2LetterMFCC::GetMelFilterBankNormaliser(
129 const float& leftMel,
130 const float& rightMel,
131 const bool useHTKMethod)
132 {
133 /* Slaney normalization for mel weights. */
134 return (2.0f / (MFCC::InverseMelScale(rightMel, useHTKMethod) -
135 MFCC::InverseMelScale(leftMel, useHTKMethod)));
136 }
137
138} /* namespace audio */
139} /* namespace app */
140} /* namespace arm */