blob: 183c05cc96808e14542647f9cd0736dae1128922 [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 "AdMelSpectrogram.hpp"
18
19#include "PlatformMath.hpp"
20
21namespace arm {
22namespace app {
23namespace audio {
24
25 bool AdMelSpectrogram::ApplyMelFilterBank(
26 std::vector<float>& fftVec,
27 std::vector<std::vector<float>>& melFilterBank,
28 std::vector<int32_t>& filterBankFilterFirst,
29 std::vector<int32_t>& filterBankFilterLast,
30 std::vector<float>& melEnergies)
31 {
32 const size_t numBanks = melEnergies.size();
33
34 if (numBanks != filterBankFilterFirst.size() ||
35 numBanks != filterBankFilterLast.size()) {
36 printf_err("unexpected filter bank lengths\n");
37 return false;
38 }
39
40 for (size_t bin = 0; bin < numBanks; ++bin) {
41 auto filterBankIter = melFilterBank[bin].begin();
42 float melEnergy = 1e-10; /* Avoid log of zero at later stages. */
43 const int32_t firstIndex = filterBankFilterFirst[bin];
44 const int32_t lastIndex = filterBankFilterLast[bin];
45
46 for (int32_t i = firstIndex; i <= lastIndex; ++i) {
47 melEnergy += (*filterBankIter++ * fftVec[i]);
48 }
49
50 melEnergies[bin] = melEnergy;
51 }
52
53 return true;
54 }
55
56 void AdMelSpectrogram::ConvertToLogarithmicScale(
57 std::vector<float>& melEnergies)
58 {
59 /* Container for natural logarithms of mel energies */
60 std::vector <float> vecLogEnergies(melEnergies.size(), 0.f);
61
62 /* Because we are taking natural logs, we need to multiply by log10(e).
63 * Also, for wav2letter model, we scale our log10 values by 10 */
64 constexpr float multiplier = 10.0 * /* default scalar */
65 0.4342944819032518; /* log10f(std::exp(1.0))*/
66
67 /* Take log of the whole vector */
68 math::MathUtils::VecLogarithmF32(melEnergies, vecLogEnergies);
69
70 /* Scale the log values. */
71 for (auto iterM = melEnergies.begin(), iterL = vecLogEnergies.begin();
72 iterM != melEnergies.end(); ++iterM, ++iterL) {
73
74 *iterM = *iterL * multiplier;
75 }
76 }
77
78 float AdMelSpectrogram::GetMelFilterBankNormaliser(
79 const float& leftMel,
80 const float& rightMel,
81 const bool useHTKMethod)
82 {
83 /* Slaney normalization for mel weights. */
84 return (2.0f / (AdMelSpectrogram::InverseMelScale(rightMel, useHTKMethod) -
85 AdMelSpectrogram::InverseMelScale(leftMel, useHTKMethod)));
86 }
87
88} /* namespace audio */
89} /* namespace app */
90} /* namespace arm */