kaldi-native-fbank C++ API
Kaldi-compatible feature extraction C++ API
Loading...
Searching...
No Matches
knf::MelBanks Class Reference

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.

Detailed Description

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

Used by:
FbankComputer, MfccComputer, WhisperFeatureComputer, and OnlineNemoFbank.
See also
MelBanksOptions, FbankComputer, MfccComputer, ComputeLifterCoeffs

Constructor & Destructor Documentation

◆ MelBanks() [1/2]

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.

Parameters
[in]optsMel-bin configuration; see MelBanksOptions.
[in]frame_optsFrame-extraction configuration (provides samp_freq and the FFT size).
[in]vtln_warp_factorVTLN warping factor (1.0 means no warping); the bin edges are warped with VtlnWarpMelFreq() before the triangles are computed.

◆ MelBanks() [2/2]

knf::MelBanks::MelBanks ( const float * weights,
int32_t num_rows,
int32_t num_cols )

Initialize with a 2-d weights matrix.

Parameters
[in]weightsPointer 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_rowsIt equls to number of mel bins
[in]num_colsIt equals to (number of fft bins)/2+1

Member Function Documentation

◆ Compute()

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.

Parameters
[in]fft_energies1-D array of size num_fft_bins/2+1, holding the power spectrum (not log).
[out]mel_energies_out1-D array of size num_mel_bins; the caller must pre-allocate it. On return it holds the mel energies (not log).

◆ GetMatrix()

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.

◆ InverseMelScale()

float knf::MelBanks::InverseMelScale ( float mel_freq)
inlinestatic

HTK mel scale: convert mel frequency to Hz.

\[ \mathrm{Hz} = 700 \left( \exp(\mathrm{mel} / 1127) - 1 \right) \]

◆ InverseMelScaleSlaney()

float knf::MelBanks::InverseMelScaleSlaney ( float mel_freq)
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. \]

◆ MelScale()

float knf::MelBanks::MelScale ( float freq)
inlinestatic

HTK mel scale: convert Hz to mel frequency.

\[ \mathrm{mel} = 1127 \log(1 + \mathrm{Hz} / 700) \]

◆ MelScaleSlaney()

float knf::MelBanks::MelScaleSlaney ( float freq)
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. \]

◆ NumBins()

int32_t knf::MelBanks::NumBins ( ) const
inline

Return the number of mel bins.

◆ VtlnWarpFreq()

float knf::MelBanks::VtlnWarpFreq ( float vtln_low_cutoff,
float vtln_high_cutoff,
float low_freq,
float high_freq,
float vtln_warp_factor,
float freq )
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:

  • \( F(\mathrm{low\_freq}) = \mathrm{low\_freq} \)
  • \( F(\mathrm{high\_freq}) = \mathrm{high\_freq} \)

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}). \]

Parameters
[in]vtln_low_cutoffLower discontinuity of the warp function; must be > low_freq.
[in]vtln_high_cutoffUpper discontinuity of the warp function; must be < high_freq.
[in]low_freqLower frequency cutoff of the mel computation.
[in]high_freqUpper frequency cutoff of the mel computation.
[in]vtln_warp_factorVTLN warping factor (1.0 means no warping).
[in]freqThe frequency to warp.
Returns
The warped frequency; freq itself if it lies outside [low_freq, high_freq].

◆ VtlnWarpMelFreq()

float knf::MelBanks::VtlnWarpMelFreq ( float vtln_low_cutoff,
float vtln_high_cutoff,
float low_freq,
float high_freq,
float vtln_warp_factor,
float mel_freq )
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.


The documentation for this class was generated from the following file:
  • /home/runner/work/kaldi-native-fbank/kaldi-native-fbank/kaldi-native-fbank/csrc/mel-computations.h