feature-mfcc.h (5156B)
1 /** 2 * Copyright 2009-2011 Karel Vesely; Petr Motlicek; Saarland University 3 * 2014-2016 Johns Hopkins University (author: Daniel Povey) 4 * Copyright 2024 Xiaomi Corporation (authors: Fangjun Kuang) 5 * 6 * See LICENSE for clarification regarding multiple authors 7 * 8 * Licensed under the Apache License, Version 2.0 (the "License"); 9 * you may not use this file except in compliance with the License. 10 * You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 21 // This file is copied/modified from kaldi/src/feat/feature-mfcc.h 22 23 #ifndef KALDI_NATIVE_FBANK_CSRC_FEATURE_MFCC_H_ 24 #define KALDI_NATIVE_FBANK_CSRC_FEATURE_MFCC_H_ 25 26 #include <cstdint> 27 #include <map> 28 #include <sstream> 29 #include <string> 30 #include <vector> 31 32 #include "kaldi-native-fbank/csrc/feature-window.h" 33 #include "kaldi-native-fbank/csrc/mel-computations.h" 34 #include "kaldi-native-fbank/csrc/rfft.h" 35 36 namespace knf { 37 38 /// MfccOptions contains basic options for computing MFCC features. 39 // (this class is copied from kaldi) 40 struct MfccOptions { 41 FrameExtractionOptions frame_opts; 42 MelBanksOptions mel_opts; 43 44 // Number of cepstra in MFCC computation (including C0) 45 int32_t num_ceps = 13; 46 47 // Use energy (not C0) in MFCC computation 48 bool use_energy = true; 49 50 // Floor on energy (absolute, not relative) in MFCC 51 // computation. Only makes a difference if use_energy=true; 52 // only necessary if dither=0.0. 53 // Suggested values: 0.1 or 1.0 54 float energy_floor = 0.0; 55 56 // If true, compute energy before preemphasis and windowing 57 bool raw_energy = true; 58 59 // Constant that controls scaling of MFCCs 60 float cepstral_lifter = 22.0; 61 62 // If true, put energy or C0 last and use a factor of 63 // sqrt(2) on C0. 64 // Warning: not sufficient to get HTK compatible features 65 // (need to change other parameters) 66 bool htk_compat = false; 67 68 MfccOptions() { mel_opts.num_bins = 23; } 69 70 std::string ToString() const { 71 std::ostringstream os; 72 os << "MfccOptions("; 73 os << "frame_opts=" << frame_opts.ToString() << ", "; 74 os << "mel_opts=" << mel_opts.ToString() << ", "; 75 76 os << "num_ceps=" << num_ceps << ", "; 77 os << "use_energy=" << (use_energy ? "True" : "False") << ", "; 78 os << "energy_floor=" << energy_floor << ", "; 79 os << "raw_energy=" << (raw_energy ? "True" : "False") << ", "; 80 os << "cepstral_lifter=" << cepstral_lifter << ", "; 81 os << "htk_compat=" << (htk_compat ? "True" : "False") << ")"; 82 83 return os.str(); 84 } 85 }; 86 87 std::ostream &operator<<(std::ostream &os, const MfccOptions &opts); 88 89 class MfccComputer { 90 public: 91 using Options = MfccOptions; 92 93 explicit MfccComputer(const MfccOptions &opts); 94 ~MfccComputer(); 95 96 int32_t Dim() const { return opts_.num_ceps; } 97 98 // if true, compute log_energy_pre_window but after dithering and dc removal 99 bool NeedRawLogEnergy() const { return opts_.use_energy && opts_.raw_energy; } 100 101 const FrameExtractionOptions &GetFrameOptions() const { 102 return opts_.frame_opts; 103 } 104 105 const MfccOptions &GetOptions() const { return opts_; } 106 107 /** 108 Function that computes one frame of features from 109 one frame of signal. 110 111 @param [in] signal_raw_log_energy The log-energy of the frame of the signal 112 prior to windowing and pre-emphasis, or 113 log(numeric_limits<float>::min()), whichever is greater. Must be 114 ignored by this function if this class returns false from 115 this->NeedsRawLogEnergy(). 116 @param [in] vtln_warp The VTLN warping factor that the user wants 117 to be applied when computing features for this utterance. Will 118 normally be 1.0, meaning no warping is to be done. The value will 119 be ignored for feature types that don't support VLTN, such as 120 spectrogram features. 121 @param [in] signal_frame One frame of the signal, 122 as extracted using the function ExtractWindow() using the options 123 returned by this->GetFrameOptions(). The function will use the 124 vector as a workspace, which is why it's a non-const pointer. 125 @param [out] feature Pointer to a vector of size this->Dim(), to which 126 the computed feature will be written. It should be pre-allocated. 127 */ 128 void Compute(float signal_raw_log_energy, float vtln_warp, 129 std::vector<float> *signal_frame, float *feature); 130 131 private: 132 const MelBanks *GetMelBanks(float vtln_warp); 133 134 MfccOptions opts_; 135 float log_energy_floor_; 136 std::map<float, MelBanks *> mel_banks_; // float is VTLN coefficient. 137 Rfft rfft_; 138 139 // temp buffer of size num_mel_bins = opts.mel_opts.num_bins 140 std::vector<float> mel_energies_; 141 142 // opts_.num_ceps 143 std::vector<float> lifter_coeffs_; 144 145 // [num_ceps][num_mel_bins] 146 std::vector<float> dct_matrix_; 147 }; 148 149 } // namespace knf 150 151 #endif // KALDI_NATIVE_FBANK_CSRC_FEATURE_MFCC_H_