blob: 75d75da91d4357d52c388be777de3f2df0f7e489 [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#ifndef KWS_ASR_WAV2LET_MFCC_HPP
18#define KWS_ASR_WAV2LET_MFCC_HPP
19
20#include "Mfcc.hpp"
21
22namespace arm {
23namespace app {
24namespace audio {
25
26 /* Class to provide Wav2Letter specific MFCC calculation requirements. */
27 class Wav2LetterMFCC : public MFCC {
28
29 public:
30 static constexpr uint32_t ms_defaultSamplingFreq = 16000;
31 static constexpr uint32_t ms_defaultNumFbankBins = 128;
32 static constexpr uint32_t ms_defaultMelLoFreq = 0;
33 static constexpr uint32_t ms_defaultMelHiFreq = 8000;
34 static constexpr bool ms_defaultUseHtkMethod = false;
35
36 explicit Wav2LetterMFCC(const size_t numFeats, const size_t frameLen)
37 : MFCC(MfccParams(
38 ms_defaultSamplingFreq, ms_defaultNumFbankBins,
39 ms_defaultMelLoFreq, ms_defaultMelHiFreq,
40 numFeats, frameLen, ms_defaultUseHtkMethod))
41 {}
42
43 Wav2LetterMFCC() = delete;
44 ~Wav2LetterMFCC() = default;
45
46 protected:
47
48 /**
49 * @brief Overrides base class implementation of this function.
50 * @param[in] fftVec Vector populated with FFT magnitudes.
51 * @param[in] melFilterBank 2D Vector with filter bank weights.
52 * @param[in] filterBankFilterFirst Vector containing the first indices of filter bank
53 * to be used for each bin.
54 * @param[in] filterBankFilterLast Vector containing the last indices of filter bank
55 * to be used for each bin.
56 * @param[out] melEnergies Pre-allocated vector of MEL energies to be
57 * populated.
58 * @return true if successful, false otherwise.
59 */
60 bool ApplyMelFilterBank(
61 std::vector<float>& fftVec,
62 std::vector<std::vector<float>>& melFilterBank,
alexanderc350cdc2021-04-29 20:36:09 +010063 std::vector<uint32_t>& filterBankFilterFirst,
64 std::vector<uint32_t>& filterBankFilterLast,
alexander3c798932021-03-26 21:42:19 +000065 std::vector<float>& melEnergies) override;
66
67 /**
68 * @brief Override for the base class implementation convert mel
69 * energies to logarithmic scale. The difference from
70 * default behaviour is that the power is converted to dB
71 * and subsequently clamped.
72 * @param[in,out] melEnergies 1D vector of Mel energies.
73 **/
74 void ConvertToLogarithmicScale(
75 std::vector<float>& melEnergies) override;
76
77 /**
78 * @brief Create a matrix used to calculate Discrete Cosine
79 * Transform. Override for the base class' default
80 * implementation as the first and last elements
81 * use a different normaliser.
82 * @param[in] inputLength Input length of the buffer on which
83 * DCT will be performed.
84 * @param[in] coefficientCount Total coefficients per input length.
85 * @return 1D vector with inputLength x coefficientCount elements
86 * populated with DCT coefficients.
87 */
88 std::vector<float> CreateDCTMatrix(
89 int32_t inputLength,
90 int32_t coefficientCount) override;
91
92 /**
93 * @brief Given the low and high Mel values, get the normaliser
94 * for weights to be applied when populating the filter
95 * bank. Override for the base class implementation.
96 * @param[in] leftMel Low Mel frequency value.
97 * @param[in] rightMel High Mel frequency value.
98 * @param[in] useHTKMethod Bool to signal if HTK method is to be
99 * used for calculation.
100 * @return Value to use for normalising.
101 */
102 float GetMelFilterBankNormaliser(
103 const float& leftMel,
104 const float& rightMel,
105 bool useHTKMethod) override;
alexanderc350cdc2021-04-29 20:36:09 +0100106
alexander3c798932021-03-26 21:42:19 +0000107 };
108
109} /* namespace audio */
110} /* namespace app */
111} /* namespace arm */
112
113#endif /* KWS_ASR_WAV2LET_MFCC_HPP */