kaldi-native-fbank C++ API
Kaldi-compatible feature extraction C++ API
Loading...
Searching...
No Matches
mel-computations.h
Go to the documentation of this file.
1
31
32#ifndef KALDI_NATIVE_FBANK_CSRC_MEL_COMPUTATIONS_H_
33#define KALDI_NATIVE_FBANK_CSRC_MEL_COMPUTATIONS_H_
34
35#include <cmath>
36#include <cstdint>
37#include <sstream>
38#include <string>
39#include <utility>
40#include <vector>
41
43
44namespace knf {
46
60 int32_t num_bins = 25;
61
63 float low_freq = 20;
64
67 float high_freq = 0;
68
70 float vtln_low = 100;
71
74 float vtln_high = -500;
75
77 bool debug_mel = false;
78
82 bool htk_mode = false;
83
88 bool is_librosa = false;
89
93 std::string norm = "slaney";
94
97
99 bool floor_to_int_bin = false;
100
102 std::string ToString() const {
103 std::ostringstream os;
104 os << "num_bins: " << num_bins << "\n";
105 os << "low_freq: " << low_freq << "\n";
106 os << "high_freq: " << high_freq << "\n";
107 os << "vtln_low: " << vtln_low << "\n";
108 os << "vtln_high: " << vtln_high << "\n";
109 os << "debug_mel: " << debug_mel << "\n";
110 os << "htk_mode: " << htk_mode << "\n";
111 os << "is_librosa: " << is_librosa << "\n";
112 os << "norm: " << norm << "\n";
113 os << "use_slaney_mel_scale: " << use_slaney_mel_scale << "\n";
114 os << "floor_to_int_bin: " << floor_to_int_bin << "\n";
115 return os.str();
116 }
117};
118
120std::ostream &operator<<(std::ostream &os, const MelBanksOptions &opts);
121
133class MelBanks {
134 public:
138 static inline float InverseMelScale(float mel_freq) {
139 return 700.0f * (expf(mel_freq / 1127.0f) - 1.0f);
140 }
141
145 static inline float MelScale(float freq) {
146 return 1127.0f * logf(1.0f + freq / 700.0f);
147 }
148
155 static inline float InverseMelScaleSlaney(float mel_freq) {
156 if (mel_freq <= 15) {
157 return 200.0f / 3 * mel_freq;
158 }
159
160 // return 1000 * expf((mel_freq - 15) * logf(6.4f) / 27);
161
162 // Note: log(6.4)/27 = 0.06875177742094911
163
164 return 1000 * expf((mel_freq - 15) * 0.06875177742094911f);
165 }
166
173 static inline float MelScaleSlaney(float freq) {
174 if (freq <= 1000) {
175 return freq * 3 / 200.0f;
176 }
177
178 // return 15 + 27 * logf(freq / 1000) / logf(6.4f)
179 //
180 // Note: 27/log(6.4) = 14.545078505785561
181
182 return 15 + 14.545078505785561f * logf(freq / 1000);
183 }
184
238 static float VtlnWarpFreq(
239 float vtln_low_cutoff,
240 float vtln_high_cutoff, // discontinuities in warp func
241 float low_freq,
242 float high_freq, // upper+lower frequency cutoffs in
243 // the mel computation
244 float vtln_warp_factor, float freq);
245
253 static float VtlnWarpMelFreq(float vtln_low_cutoff, float vtln_high_cutoff,
254 float low_freq, float high_freq,
255 float vtln_warp_factor, float mel_freq);
256
272 // TODO(fangjun): Remove vtln_warp_factor
274 const FrameExtractionOptions &frame_opts, float vtln_warp_factor);
275
283 MelBanks(const float *weights, int32_t num_rows, int32_t num_cols);
284
305 void Compute(const float *fft_energies, float *mel_energies_out) const;
306
308 int32_t NumBins() const { return bins_.size(); }
309
312 std::vector<float> GetMatrix() const;
313
314 private:
315 // for kaldi-compatible
316 void InitKaldiMelBanks(const MelBanksOptions &opts,
317 const FrameExtractionOptions &frame_opts,
318 float vtln_warp_factor);
319
320 // for librosa-compatible
321 // See https://librosa.org/doc/main/generated/librosa.filters.mel.html
322 void InitLibrosaMelBanks(const MelBanksOptions &opts,
323 const FrameExtractionOptions &frame_opts,
324 float vtln_warp_factor);
325
326 private:
329 std::vector<std::pair<int32_t, std::vector<float>>> bins_;
330
331 // TODO(fangjun): Remove debug_ and htk_mode_
332 bool debug_ = false;
333 bool htk_mode_ = false;
334 int32_t num_fft_bins_ = -1;
335};
336
356void ComputeLifterCoeffs(float Q, std::vector<float> *coeffs);
357
358} // namespace knf
359
360#endif // KALDI_NATIVE_FBANK_CSRC_MEL_COMPUTATIONS_H_
static float MelScale(float freq)
HTK mel scale: convert Hz to mel frequency.
Definition mel-computations.h:145
MelBanks(const MelBanksOptions &opts, const FrameExtractionOptions &frame_opts, float vtln_warp_factor)
Construct kaldi-compatible mel banks.
std::vector< float > GetMatrix() const
Return the mel filter weights as a flattened 2-d matrix of shape [num_mel_bins, num_fft_bins/2+1],...
static float InverseMelScaleSlaney(float mel_freq)
Slaney mel scale: convert mel frequency to Hz.
Definition mel-computations.h:155
static float MelScaleSlaney(float freq)
Slaney mel scale: convert Hz to mel frequency.
Definition mel-computations.h:173
MelBanks(const float *weights, int32_t num_rows, int32_t num_cols)
Initialize with a 2-d weights matrix.
static float VtlnWarpMelFreq(float vtln_low_cutoff, float vtln_high_cutoff, float low_freq, float high_freq, float vtln_warp_factor, float mel_freq)
Piecewise-linear VTLN warping function in the mel domain.
static float InverseMelScale(float mel_freq)
HTK mel scale: convert mel frequency to Hz.
Definition mel-computations.h:138
int32_t NumBins() const
Return the number of mel bins.
Definition mel-computations.h:308
static float VtlnWarpFreq(float vtln_low_cutoff, float vtln_high_cutoff, float low_freq, float high_freq, float vtln_warp_factor, float freq)
Piecewise-linear VTLN warping function in the frequency domain.
void Compute(const float *fft_energies, float *mel_energies_out) const
Compute mel energies (note: NOT log energies).
Waveform windowing and frame extraction.
This file is part of kaldi-native-fbank.
Definition feature-cmvn.h:42
std::ostream & operator<<(std::ostream &os, const FbankOptions &opts)
Write the options to the stream, one option per line.
void ComputeLifterCoeffs(float Q, std::vector< float > *coeffs)
Compute liftering coefficients (scaling on cepstral coeffs).
Options for extracting frames from a waveform.
Definition feature-window.h:57
Options for constructing triangular mel-filterbank bins.
Definition mel-computations.h:58
bool floor_to_int_bin
Used only when is_librosa is true: floor the bin edges to integers.
Definition mel-computations.h:99
float vtln_low
VTLN lower cutoff of the warping function.
Definition mel-computations.h:70
int32_t num_bins
Number of triangular mel-frequency bins, e.g. 25.
Definition mel-computations.h:60
std::string norm
Used only when is_librosa is true.
Definition mel-computations.h:93
float high_freq
Upper frequency cutoff; 0 means no cutoff, negative values are added to the Nyquist frequency to get ...
Definition mel-computations.h:67
bool htk_mode
htk_mode is a "hidden" config.
Definition mel-computations.h:82
float vtln_high
VTLN upper cutoff of the warping function: if negative, added to the Nyquist frequency to get the cut...
Definition mel-computations.h:74
bool debug_mel
Print out debugging information for mel bin computation.
Definition mel-computations.h:77
std::string ToString() const
Return the options as a string.
Definition mel-computations.h:102
float low_freq
Lower frequency cutoff, e.g. 20.
Definition mel-computations.h:63
bool use_slaney_mel_scale
Used only when is_librosa is true: use the Slaney mel scale.
Definition mel-computations.h:96
bool is_librosa
If true, use librosa-style mel filters instead of kaldi-style ones.
Definition mel-computations.h:88