|
kaldi-native-fbank C++ API
Kaldi-compatible feature extraction C++ API
|
Triangular mel-filterbank bins. More...
#include <mel-computations.h>
Public Member Functions | |
| MelBanks (const MelBanksOptions &opts, const FrameExtractionOptions &frame_opts, float vtln_warp_factor) | |
| Construct kaldi-compatible mel banks. | |
| MelBanks (const float *weights, int32_t num_rows, int32_t num_cols) | |
| Initialize with a 2-d weights matrix. | |
| void | Compute (const float *fft_energies, float *mel_energies_out) const |
| Compute mel energies (note: NOT log energies). | |
| int32_t | NumBins () const |
| Return the number of mel bins. | |
| 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], in row major. | |
Static Public Member Functions | |
| static float | InverseMelScale (float mel_freq) |
| HTK mel scale: convert mel frequency to Hz. | |
| static float | MelScale (float freq) |
| HTK mel scale: convert Hz to mel frequency. | |
| static float | InverseMelScaleSlaney (float mel_freq) |
| Slaney mel scale: convert mel frequency to Hz. | |
| static float | MelScaleSlaney (float freq) |
| Slaney mel scale: convert Hz to mel frequency. | |
| 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. | |
| 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. | |
Triangular mel-filterbank bins.
A MelBanks object holds the weights of a set of triangular mel-frequency bins and can project FFT bin energies onto them. See also https://en.wikipedia.org/wiki/Mel_scale
| knf::MelBanks::MelBanks | ( | const MelBanksOptions & | opts, |
| const FrameExtractionOptions & | frame_opts, | ||
| float | vtln_warp_factor ) |
Construct kaldi-compatible mel banks.
The mel bin edges are placed uniformly on the mel scale between opts.low_freq and opts.high_freq; each bin is a triangle in the frequency domain, defined by a start fft bin and per-bin weights (see GetBins()/GetMatrix()). If opts.is_librosa is true, librosa-style filters are computed instead.
| [in] | opts | Mel-bin configuration; see MelBanksOptions. |
| [in] | frame_opts | Frame-extraction configuration (provides samp_freq and the FFT size). |
| [in] | vtln_warp_factor | VTLN warping factor (1.0 means no warping); the bin edges are warped with VtlnWarpMelFreq() before the triangles are computed. |
| knf::MelBanks::MelBanks | ( | const float * | weights, |
| int32_t | num_rows, | ||
| int32_t | num_cols ) |
Initialize with a 2-d weights matrix.
| [in] | weights | Pointer to the start address of the matrix, in row major; element (i, j) is the weight of fft bin j for mel bin i. |
| [in] | num_rows | It equls to number of mel bins |
| [in] | num_cols | It equals to (number of fft bins)/2+1 |
| void knf::MelBanks::Compute | ( | const float * | fft_energies, |
| float * | mel_energies_out ) const |
Compute mel energies (note: NOT log energies).
At input, "fft_energies" contains the FFT energies (not log). For each mel bin \( i \), the output is the weighted sum of the FFT bin energies that fall into that bin:
\[ \mathrm{mel\_energies\_out}[i] = \sum_{k} w[i][k] \cdot \mathrm{fft\_energies}[\mathrm{offset}[i] + k] \]
where \( \mathrm{offset}[i] \) is the first nonzero fft bin of mel bin \( i \) and \( w[i] \) are its triangle weights.
| [in] | fft_energies | 1-D array of size num_fft_bins/2+1, holding the power spectrum (not log). |
| [out] | mel_energies_out | 1-D array of size num_mel_bins; the caller must pre-allocate it. On return it holds the mel energies (not log). |
| std::vector< float > knf::MelBanks::GetMatrix | ( | ) | const |
Return the mel filter weights as a flattened 2-d matrix of shape [num_mel_bins, num_fft_bins/2+1], in row major.
|
inlinestatic |
HTK mel scale: convert mel frequency to Hz.
\[ \mathrm{Hz} = 700 \left( \exp(\mathrm{mel} / 1127) - 1 \right) \]
|
inlinestatic |
Slaney mel scale: convert mel frequency to Hz.
\[ \mathrm{Hz} = \left\{ \begin{array}{ll} (200/3)\,\mathrm{mel} & \mathrm{mel} \le 15 \\ 1000 \exp((\mathrm{mel} - 15) \log(6.4) / 27) & \mathrm{otherwise} \end{array} \right. \]
|
inlinestatic |
HTK mel scale: convert Hz to mel frequency.
\[ \mathrm{mel} = 1127 \log(1 + \mathrm{Hz} / 700) \]
|
inlinestatic |
Slaney mel scale: convert Hz to mel frequency.
\[ \mathrm{mel} = \left\{ \begin{array}{ll} (3/200)\,\mathrm{Hz} & \mathrm{Hz} \le 1000 \\ 15 + 27 \log(\mathrm{Hz} / 1000) / \log(6.4) & \mathrm{otherwise} \end{array} \right. \]
|
inline |
Return the number of mel bins.
|
static |
Piecewise-linear VTLN warping function in the frequency domain.
This computes a VTLN warping function that is not the same as HTK's one, but has similar inputs (this function has the advantage of never producing empty bins).
This function computes a warp function \( F(\mathrm{freq}) \), defined between low_freq and high_freq inclusive, with the following properties:
The function is continuous and piecewise linear with two inflection points. The lower inflection point (measured in terms of the unwarped frequency) is at frequency \( l \); the higher inflection point is at a frequency \( h \). If \( l \le f \le h \), then \( F(f) = f/\mathrm{vtln\_warp\_factor} \).
If the higher inflection point (measured in terms of the unwarped frequency) is at \( h \), then \( \max(h, F(h)) = \mathrm{vtln\_high\_cutoff} \). Since \( F(h) = h/\mathrm{vtln\_warp\_factor} \), then \( \max(h, h/\mathrm{vtln\_warp\_factor}) = \mathrm{vtln\_high\_cutoff} \), so
\[ h = \frac{\mathrm{vtln\_high\_cutoff}} {\max(1,\, 1/\mathrm{vtln\_warp\_factor})} = \mathrm{vtln\_high\_cutoff} \cdot \min(1,\, \mathrm{vtln\_warp\_factor}). \]
If the lower inflection point (measured in terms of the unwarped frequency) is at \( l \), then \( \min(l, F(l)) = \mathrm{vtln\_low\_cutoff} \). This implies that
\[ l = \frac{\mathrm{vtln\_low\_cutoff}} {\min(1,\, 1/\mathrm{vtln\_warp\_factor})} = \mathrm{vtln\_low\_cutoff} \cdot \max(1,\, \mathrm{vtln\_warp\_factor}). \]
| [in] | vtln_low_cutoff | Lower discontinuity of the warp function; must be > low_freq. |
| [in] | vtln_high_cutoff | Upper discontinuity of the warp function; must be < high_freq. |
| [in] | low_freq | Lower frequency cutoff of the mel computation. |
| [in] | high_freq | Upper frequency cutoff of the mel computation. |
| [in] | vtln_warp_factor | VTLN warping factor (1.0 means no warping). |
| [in] | freq | The frequency to warp. |
|
static |
Piecewise-linear VTLN warping function in the mel domain.
Same as VtlnWarpFreq(), but the input and output are on the mel scale: it warps mel_freq by first converting it to Hz, applying VtlnWarpFreq(), and converting the result back to mel.