feature-fbank.h (4608B)
1 /** 2 * Copyright (c) 2022 Xiaomi Corporation (authors: Fangjun Kuang) 3 * 4 * See LICENSE for clarification regarding multiple authors 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 // This file is copied/modified from kaldi/src/feat/feature-fbank.h 20 21 #ifndef KALDI_NATIVE_FBANK_CSRC_FEATURE_FBANK_H_ 22 #define KALDI_NATIVE_FBANK_CSRC_FEATURE_FBANK_H_ 23 24 #include <map> 25 #include <string> 26 #include <vector> 27 #include <cstdint> 28 #include <sstream> 29 30 #include "kaldi-native-fbank/csrc/feature-window.h" 31 #include "kaldi-native-fbank/csrc/mel-computations.h" 32 #include "kaldi-native-fbank/csrc/rfft.h" 33 34 namespace knf { 35 36 struct FbankOptions { 37 FrameExtractionOptions frame_opts; 38 MelBanksOptions mel_opts; 39 // append an extra dimension with energy to the filter banks 40 bool use_energy = false; 41 float energy_floor = 0.0f; // active iff use_energy==true 42 43 // If true, compute log_energy before preemphasis and windowing 44 // If false, compute log_energy after preemphasis ans windowing 45 bool raw_energy = true; // active iff use_energy==true 46 47 // If true, put energy last (if using energy) 48 // If false, put energy first 49 bool htk_compat = false; // active iff use_energy==true 50 51 // if true (default), produce log-filterbank, else linear 52 bool use_log_fbank = true; 53 54 // if true (default), use power in filterbank 55 // analysis, else magnitude. 56 bool use_power = true; 57 58 FbankOptions() { mel_opts.num_bins = 23; } 59 60 std::string ToString() const { 61 std::ostringstream os; 62 os << "frame_opts: \n"; 63 os << frame_opts << "\n"; 64 os << "\n"; 65 66 os << "mel_opts: \n"; 67 os << mel_opts << "\n"; 68 69 os << "use_energy: " << use_energy << "\n"; 70 os << "energy_floor: " << energy_floor << "\n"; 71 os << "raw_energy: " << raw_energy << "\n"; 72 os << "htk_compat: " << htk_compat << "\n"; 73 os << "use_log_fbank: " << use_log_fbank << "\n"; 74 os << "use_power: " << use_power << "\n"; 75 return os.str(); 76 } 77 }; 78 79 std::ostream &operator<<(std::ostream &os, const FbankOptions &opts); 80 81 class FbankComputer { 82 public: 83 using Options = FbankOptions; 84 85 explicit FbankComputer(const FbankOptions &opts); 86 ~FbankComputer(); 87 88 int32_t Dim() const { 89 return opts_.mel_opts.num_bins + (opts_.use_energy ? 1 : 0); 90 } 91 92 // if true, compute log_energy_pre_window but after dithering and dc removal 93 bool NeedRawLogEnergy() const { return opts_.use_energy && opts_.raw_energy; } 94 95 const FrameExtractionOptions &GetFrameOptions() const { 96 return opts_.frame_opts; 97 } 98 99 const FbankOptions &GetOptions() const { return opts_; } 100 101 /** 102 Function that computes one frame of features from 103 one frame of signal. 104 105 @param [in] signal_raw_log_energy The log-energy of the frame of the signal 106 prior to windowing and pre-emphasis, or 107 log(numeric_limits<float>::min()), whichever is greater. Must be 108 ignored by this function if this class returns false from 109 this->NeedsRawLogEnergy(). 110 @param [in] vtln_warp The VTLN warping factor that the user wants 111 to be applied when computing features for this utterance. Will 112 normally be 1.0, meaning no warping is to be done. The value will 113 be ignored for feature types that don't support VLTN, such as 114 spectrogram features. 115 @param [in] signal_frame One frame of the signal, 116 as extracted using the function ExtractWindow() using the options 117 returned by this->GetFrameOptions(). The function will use the 118 vector as a workspace, which is why it's a non-const pointer. 119 @param [out] feature Pointer to a vector of size this->Dim(), to which 120 the computed feature will be written. It should be pre-allocated. 121 */ 122 void Compute(float signal_raw_log_energy, float vtln_warp, 123 std::vector<float> *signal_frame, float *feature); 124 125 private: 126 const MelBanks *GetMelBanks(float vtln_warp); 127 128 FbankOptions opts_; 129 float log_energy_floor_; 130 std::map<float, MelBanks *> mel_banks_; // float is VTLN coefficient. 131 Rfft rfft_; 132 }; 133 134 } // namespace knf 135 136 #endif // KALDI_NATIVE_FBANK_CSRC_FEATURE_FBANK_H_