| 283 | } |
| 284 | |
| 285 | std::vector<std::vector<float>> MFCC::CreateMelFilterBank() |
| 286 | { |
| 287 | size_t numFftBins = this->m_params.m_frameLenPadded / 2; |
| 288 | float fftBinWidth = static_cast<float>(this->m_params.m_samplingFreq) / this->m_params.m_frameLenPadded; |
| 289 | |
| 290 | float melLowFreq = MFCC::MelScale(this->m_params.m_melLoFreq, |
| 291 | this->m_params.m_useHtkMethod); |
| 292 | float melHighFreq = MFCC::MelScale(this->m_params.m_melHiFreq, |
| 293 | this->m_params.m_useHtkMethod); |
| 294 | float melFreqDelta = (melHighFreq - melLowFreq) / (this->m_params.m_numFbankBins + 1); |
| 295 | |
| 296 | std::vector<float> thisBin = std::vector<float>(numFftBins); |
| 297 | std::vector<std::vector<float>> melFilterBank( |
| 298 | this->m_params.m_numFbankBins); |
| 299 | this->m_filterBankFilterFirst = |
| 300 | std::vector<uint32_t>(this->m_params.m_numFbankBins); |
| 301 | this->m_filterBankFilterLast = |
| 302 | std::vector<uint32_t>(this->m_params.m_numFbankBins); |
| 303 | |
| 304 | for (size_t bin = 0; bin < this->m_params.m_numFbankBins; bin++) |
| 305 | { |
| 306 | float leftMel = melLowFreq + bin * melFreqDelta; |
| 307 | float centerMel = melLowFreq + (bin + 1) * melFreqDelta; |
| 308 | float rightMel = melLowFreq + (bin + 2) * melFreqDelta; |
| 309 | |
| 310 | uint32_t firstIndex = 0; |
| 311 | uint32_t lastIndex = 0; |
| 312 | bool firstIndexFound = false; |
| 313 | const float normaliser = this->GetMelFilterBankNormaliser(leftMel, rightMel, this->m_params.m_useHtkMethod); |
| 314 | |
| 315 | for (size_t i = 0; i < numFftBins; i++) |
| 316 | { |
| 317 | float freq = (fftBinWidth * i); /* Center freq of this fft bin. */ |
| 318 | float mel = MFCC::MelScale(freq, this->m_params.m_useHtkMethod); |
| 319 | thisBin[i] = 0.0; |
| 320 | |
| 321 | if (mel > leftMel && mel < rightMel) |
| 322 | { |
| 323 | float weight; |
| 324 | if (mel <= centerMel) |
| 325 | { |
| 326 | weight = (mel - leftMel) / (centerMel - leftMel); |
| 327 | } |
| 328 | else |
| 329 | { |
| 330 | weight = (rightMel - mel) / (rightMel - centerMel); |
| 331 | } |
| 332 | |
| 333 | thisBin[i] = weight * normaliser; |
| 334 | if (!firstIndexFound) |
| 335 | { |
| 336 | firstIndex = i; |
| 337 | firstIndexFound = true; |
| 338 | } |
| 339 | lastIndex = i; |
| 340 | } |
| 341 | } |
| 342 |
no test coverage detected