alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
Richard Burton | f32a86a | 2022-11-15 11:46:11 +0000 | [diff] [blame] | 2 | * SPDX-FileCopyrightText: Copyright 2021 Arm Limited and/or its affiliates <open-source-office@arm.com> |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 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 "Mfcc.hpp" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 18 | #include "PlatformMath.hpp" |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 19 | #include "log_macros.h" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 20 | |
| 21 | #include <cfloat> |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 22 | #include <cinttypes> |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 23 | |
| 24 | namespace arm { |
| 25 | namespace app { |
| 26 | namespace audio { |
| 27 | |
| 28 | MfccParams::MfccParams( |
| 29 | const float samplingFreq, |
| 30 | const uint32_t numFbankBins, |
| 31 | const float melLoFreq, |
| 32 | const float melHiFreq, |
| 33 | const uint32_t numMfccFeats, |
| 34 | const uint32_t frameLen, |
| 35 | const bool useHtkMethod): |
| 36 | m_samplingFreq(samplingFreq), |
| 37 | m_numFbankBins(numFbankBins), |
| 38 | m_melLoFreq(melLoFreq), |
| 39 | m_melHiFreq(melHiFreq), |
| 40 | m_numMfccFeatures(numMfccFeats), |
| 41 | m_frameLen(frameLen), |
| 42 | |
| 43 | /* Smallest power of 2 >= frame length. */ |
| 44 | m_frameLenPadded(pow(2, ceil((log(frameLen)/log(2))))), |
| 45 | m_useHtkMethod(useHtkMethod) |
| 46 | {} |
| 47 | |
Kshitij Sisodia | 3c8256d | 2021-05-24 16:12:40 +0100 | [diff] [blame] | 48 | void MfccParams::Log() const |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 49 | { |
Kshitij Sisodia | 3c8256d | 2021-05-24 16:12:40 +0100 | [diff] [blame] | 50 | debug("MFCC parameters:\n"); |
| 51 | debug("\t Sampling frequency: %f\n", this->m_samplingFreq); |
| 52 | debug("\t Number of filter banks: %" PRIu32 "\n", this->m_numFbankBins); |
| 53 | debug("\t Mel frequency limit (low): %f\n", this->m_melLoFreq); |
| 54 | debug("\t Mel frequency limit (high): %f\n", this->m_melHiFreq); |
| 55 | debug("\t Number of MFCC features: %" PRIu32 "\n", this->m_numMfccFeatures); |
| 56 | debug("\t Frame length: %" PRIu32 "\n", this->m_frameLen); |
| 57 | debug("\t Padded frame length: %" PRIu32 "\n", this->m_frameLenPadded); |
| 58 | debug("\t Using HTK for Mel scale: %s\n", this->m_useHtkMethod ? "yes" : "no"); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | MFCC::MFCC(const MfccParams& params): |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 62 | m_params(params), |
| 63 | m_filterBankInitialised(false) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 64 | { |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 65 | this->m_buffer = std::vector<float>( |
| 66 | this->m_params.m_frameLenPadded, 0.0); |
| 67 | this->m_frame = std::vector<float>( |
| 68 | this->m_params.m_frameLenPadded, 0.0); |
| 69 | this->m_melEnergies = std::vector<float>( |
| 70 | this->m_params.m_numFbankBins, 0.0); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 71 | |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 72 | this->m_windowFunc = std::vector<float>(this->m_params.m_frameLen); |
| 73 | const auto multiplier = static_cast<float>(2 * M_PI / this->m_params.m_frameLen); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 74 | |
| 75 | /* Create window function. */ |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 76 | for (size_t i = 0; i < this->m_params.m_frameLen; i++) { |
| 77 | this->m_windowFunc[i] = (0.5 - (0.5 * |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 78 | math::MathUtils::CosineF32(static_cast<float>(i) * multiplier))); |
| 79 | } |
| 80 | |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 81 | math::MathUtils::FftInitF32(this->m_params.m_frameLenPadded, this->m_fftInstance); |
Kshitij Sisodia | 3c8256d | 2021-05-24 16:12:40 +0100 | [diff] [blame] | 82 | this->m_params.Log(); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void MFCC::Init() |
| 86 | { |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 87 | this->InitMelFilterBank(); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | float MFCC::MelScale(const float freq, const bool useHTKMethod) |
| 91 | { |
| 92 | if (useHTKMethod) { |
| 93 | return 1127.0f * logf (1.0f + freq / 700.0f); |
| 94 | } else { |
| 95 | /* Slaney formula for mel scale. */ |
| 96 | |
| 97 | float mel = freq / ms_freqStep; |
| 98 | |
| 99 | if (freq >= ms_minLogHz) { |
| 100 | mel = ms_minLogMel + logf(freq / ms_minLogHz) / ms_logStep; |
| 101 | } |
| 102 | return mel; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | float MFCC::InverseMelScale(const float melFreq, const bool useHTKMethod) |
| 107 | { |
| 108 | if (useHTKMethod) { |
| 109 | return 700.0f * (expf (melFreq / 1127.0f) - 1.0f); |
| 110 | } else { |
| 111 | /* Slaney formula for mel scale. */ |
| 112 | float freq = ms_freqStep * melFreq; |
| 113 | |
| 114 | if (melFreq >= ms_minLogMel) { |
| 115 | freq = ms_minLogHz * expf(ms_logStep * (melFreq - ms_minLogMel)); |
| 116 | } |
| 117 | return freq; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | |
| 122 | bool MFCC::ApplyMelFilterBank( |
| 123 | std::vector<float>& fftVec, |
| 124 | std::vector<std::vector<float>>& melFilterBank, |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 125 | std::vector<uint32_t>& filterBankFilterFirst, |
| 126 | std::vector<uint32_t>& filterBankFilterLast, |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 127 | std::vector<float>& melEnergies) |
| 128 | { |
| 129 | const size_t numBanks = melEnergies.size(); |
| 130 | |
| 131 | if (numBanks != filterBankFilterFirst.size() || |
| 132 | numBanks != filterBankFilterLast.size()) { |
| 133 | printf_err("unexpected filter bank lengths\n"); |
| 134 | return false; |
| 135 | } |
| 136 | |
| 137 | for (size_t bin = 0; bin < numBanks; ++bin) { |
| 138 | auto filterBankIter = melFilterBank[bin].begin(); |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 139 | auto end = melFilterBank[bin].end(); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 140 | float melEnergy = FLT_MIN; /* Avoid log of zero at later stages */ |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 141 | const uint32_t firstIndex = filterBankFilterFirst[bin]; |
| 142 | const uint32_t lastIndex = std::min<uint32_t>(filterBankFilterLast[bin], fftVec.size() - 1); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 143 | |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 144 | for (uint32_t i = firstIndex; i <= lastIndex && filterBankIter != end; i++) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 145 | float energyRep = math::MathUtils::SqrtF32(fftVec[i]); |
| 146 | melEnergy += (*filterBankIter++ * energyRep); |
| 147 | } |
| 148 | |
| 149 | melEnergies[bin] = melEnergy; |
| 150 | } |
| 151 | |
| 152 | return true; |
| 153 | } |
| 154 | |
| 155 | void MFCC::ConvertToLogarithmicScale(std::vector<float>& melEnergies) |
| 156 | { |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 157 | for (float& melEnergy : melEnergies) { |
| 158 | melEnergy = logf(melEnergy); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 162 | void MFCC::ConvertToPowerSpectrum() |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 163 | { |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 164 | const uint32_t halfDim = this->m_buffer.size() / 2; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 165 | |
| 166 | /* Handle this special case. */ |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 167 | float firstEnergy = this->m_buffer[0] * this->m_buffer[0]; |
| 168 | float lastEnergy = this->m_buffer[1] * this->m_buffer[1]; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 169 | |
| 170 | math::MathUtils::ComplexMagnitudeSquaredF32( |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 171 | this->m_buffer.data(), |
| 172 | this->m_buffer.size(), |
| 173 | this->m_buffer.data(), |
| 174 | this->m_buffer.size()/2); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 175 | |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 176 | this->m_buffer[0] = firstEnergy; |
| 177 | this->m_buffer[halfDim] = lastEnergy; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | std::vector<float> MFCC::CreateDCTMatrix( |
| 181 | const int32_t inputLength, |
| 182 | const int32_t coefficientCount) |
| 183 | { |
| 184 | std::vector<float> dctMatix(inputLength * coefficientCount); |
| 185 | |
| 186 | const float normalizer = math::MathUtils::SqrtF32(2.0f/inputLength); |
| 187 | const float angleIncr = M_PI/inputLength; |
| 188 | float angle = 0; |
| 189 | |
| 190 | for (int32_t k = 0, m = 0; k < coefficientCount; k++, m += inputLength) { |
| 191 | for (int32_t n = 0; n < inputLength; n++) { |
| 192 | dctMatix[m+n] = normalizer * |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 193 | math::MathUtils::CosineF32((n + 0.5f) * angle); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 194 | } |
| 195 | angle += angleIncr; |
| 196 | } |
| 197 | |
| 198 | return dctMatix; |
| 199 | } |
| 200 | |
| 201 | float MFCC::GetMelFilterBankNormaliser( |
| 202 | const float& leftMel, |
| 203 | const float& rightMel, |
| 204 | const bool useHTKMethod) |
| 205 | { |
| 206 | UNUSED(leftMel); |
| 207 | UNUSED(rightMel); |
| 208 | UNUSED(useHTKMethod); |
| 209 | |
| 210 | /* By default, no normalisation => return 1 */ |
| 211 | return 1.f; |
| 212 | } |
| 213 | |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 214 | void MFCC::InitMelFilterBank() |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 215 | { |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 216 | if (!this->IsMelFilterBankInited()) { |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 217 | this->m_melFilterBank = this->CreateMelFilterBank(); |
| 218 | this->m_dctMatrix = this->CreateDCTMatrix( |
| 219 | this->m_params.m_numFbankBins, |
| 220 | this->m_params.m_numMfccFeatures); |
| 221 | this->m_filterBankInitialised = true; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 225 | bool MFCC::IsMelFilterBankInited() const |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 226 | { |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 227 | return this->m_filterBankInitialised; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 228 | } |
| 229 | |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 230 | void MFCC::MfccComputePreFeature(const std::vector<int16_t>& audioData) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 231 | { |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 232 | this->InitMelFilterBank(); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 233 | |
| 234 | /* TensorFlow way of normalizing .wav data to (-1, 1). */ |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 235 | constexpr float normaliser = 1.0/(1u<<15u); |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 236 | for (size_t i = 0; i < this->m_params.m_frameLen; i++) { |
| 237 | this->m_frame[i] = static_cast<float>(audioData[i]) * normaliser; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | /* Apply window function to input frame. */ |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 241 | for(size_t i = 0; i < this->m_params.m_frameLen; i++) { |
| 242 | this->m_frame[i] *= this->m_windowFunc[i]; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | /* Set remaining frame values to 0. */ |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 246 | std::fill(this->m_frame.begin() + this->m_params.m_frameLen,this->m_frame.end(), 0); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 247 | |
| 248 | /* Compute FFT. */ |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 249 | math::MathUtils::FftF32(this->m_frame, this->m_buffer, this->m_fftInstance); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 250 | |
| 251 | /* Convert to power spectrum. */ |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 252 | this->ConvertToPowerSpectrum(); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 253 | |
| 254 | /* Apply mel filterbanks. */ |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 255 | if (!this->ApplyMelFilterBank(this->m_buffer, |
| 256 | this->m_melFilterBank, |
| 257 | this->m_filterBankFilterFirst, |
| 258 | this->m_filterBankFilterLast, |
| 259 | this->m_melEnergies)) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 260 | printf_err("Failed to apply MEL filter banks\n"); |
| 261 | } |
| 262 | |
| 263 | /* Convert to logarithmic scale. */ |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 264 | this->ConvertToLogarithmicScale(this->m_melEnergies); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | std::vector<float> MFCC::MfccCompute(const std::vector<int16_t>& audioData) |
| 268 | { |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 269 | this->MfccComputePreFeature(audioData); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 270 | |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 271 | std::vector<float> mfccOut(this->m_params.m_numMfccFeatures); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 272 | |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 273 | float * ptrMel = this->m_melEnergies.data(); |
| 274 | float * ptrDct = this->m_dctMatrix.data(); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 275 | float * ptrMfcc = mfccOut.data(); |
| 276 | |
| 277 | /* Take DCT. Uses matrix mul. */ |
| 278 | for (size_t i = 0, j = 0; i < mfccOut.size(); |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 279 | ++i, j += this->m_params.m_numFbankBins) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 280 | *ptrMfcc++ = math::MathUtils::DotProductF32( |
| 281 | ptrDct + j, |
| 282 | ptrMel, |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 283 | this->m_params.m_numFbankBins); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 284 | } |
| 285 | return mfccOut; |
| 286 | } |
| 287 | |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 288 | std::vector<std::vector<float>> MFCC::CreateMelFilterBank() |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 289 | { |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 290 | size_t numFftBins = this->m_params.m_frameLenPadded / 2; |
| 291 | float fftBinWidth = static_cast<float>(this->m_params.m_samplingFreq) / this->m_params.m_frameLenPadded; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 292 | |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 293 | float melLowFreq = MFCC::MelScale(this->m_params.m_melLoFreq, |
| 294 | this->m_params.m_useHtkMethod); |
| 295 | float melHighFreq = MFCC::MelScale(this->m_params.m_melHiFreq, |
| 296 | this->m_params.m_useHtkMethod); |
| 297 | float melFreqDelta = (melHighFreq - melLowFreq) / (this->m_params.m_numFbankBins + 1); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 298 | |
| 299 | std::vector<float> thisBin = std::vector<float>(numFftBins); |
| 300 | std::vector<std::vector<float>> melFilterBank( |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 301 | this->m_params.m_numFbankBins); |
| 302 | this->m_filterBankFilterFirst = |
| 303 | std::vector<uint32_t>(this->m_params.m_numFbankBins); |
| 304 | this->m_filterBankFilterLast = |
| 305 | std::vector<uint32_t>(this->m_params.m_numFbankBins); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 306 | |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 307 | for (size_t bin = 0; bin < this->m_params.m_numFbankBins; bin++) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 308 | float leftMel = melLowFreq + bin * melFreqDelta; |
| 309 | float centerMel = melLowFreq + (bin + 1) * melFreqDelta; |
| 310 | float rightMel = melLowFreq + (bin + 2) * melFreqDelta; |
| 311 | |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 312 | uint32_t firstIndex = 0; |
| 313 | uint32_t lastIndex = 0; |
| 314 | bool firstIndexFound = false; |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 315 | const float normaliser = this->GetMelFilterBankNormaliser(leftMel, rightMel, this->m_params.m_useHtkMethod); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 316 | |
| 317 | for (size_t i = 0; i < numFftBins; i++) { |
| 318 | float freq = (fftBinWidth * i); /* Center freq of this fft bin. */ |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 319 | float mel = MFCC::MelScale(freq, this->m_params.m_useHtkMethod); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 320 | thisBin[i] = 0.0; |
| 321 | |
| 322 | if (mel > leftMel && mel < rightMel) { |
| 323 | float weight; |
| 324 | if (mel <= centerMel) { |
| 325 | weight = (mel - leftMel) / (centerMel - leftMel); |
| 326 | } else { |
| 327 | weight = (rightMel - mel) / (rightMel - centerMel); |
| 328 | } |
| 329 | |
| 330 | thisBin[i] = weight * normaliser; |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 331 | if (!firstIndexFound) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 332 | firstIndex = i; |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 333 | firstIndexFound = true; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 334 | } |
| 335 | lastIndex = i; |
| 336 | } |
| 337 | } |
| 338 | |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 339 | this->m_filterBankFilterFirst[bin] = firstIndex; |
| 340 | this->m_filterBankFilterLast[bin] = lastIndex; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 341 | |
| 342 | /* Copy the part we care about. */ |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 343 | for (uint32_t i = firstIndex; i <= lastIndex; i++) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 344 | melFilterBank[bin].push_back(thisBin[i]); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | return melFilterBank; |
| 349 | } |
| 350 | |
| 351 | } /* namespace audio */ |
| 352 | } /* namespace app */ |
| 353 | } /* namespace arm */ |