Mel filter banks

Triangular mel-frequency filter banks that project FFT bin energies onto mel bins (kaldi-native-fbank/csrc/mel-computations.h). kaldi counterpart: MelBanksOptions.

Both kaldi-style and librosa-style filters are supported (is_librosa=True). The MFCC/fbank extractors use these internally via mel_opts; you can also use them directly:

import numpy as np
import kaldi_native_fbank as knf

mel_opts = knf.MelBanksOptions(num_bins=23, low_freq=20, high_freq=7800)
frame_opts = knf.FrameExtractionOptions()
banks = knf.MelBanks(mel_opts, frame_opts)

weights = banks.get_matrix()          # (23, num_fft_bins // 2 + 1)
mel = banks.compute(power_spectrum)   # (23,) mel energies, not log

API reference

class kaldi_native_fbank.MelBanksOptions(self: kaldi_native_fbank.MelBanksOptions, num_bins: SupportsInt = 25, low_freq: SupportsFloat = 20, high_freq: SupportsFloat = 0, vtln_low: SupportsFloat = 100, vtln_high: SupportsFloat = -500, debug_mel: bool = False, htk_mode: bool = False, is_librosa: bool = False, norm: str = 'slaney', use_slaney_mel_scale: bool = True, floor_to_int_bin: bool = False)

Options for constructing triangular mel-filterbank bins.

The kaldi-compatible fields correspond to kaldi's MelBanksOptions; the is_librosa-related fields select librosa-compatible filters instead.

See also

MelBanks

Construct with the given options.

All fields are optional keyword arguments; the defaults are shown in the signature.

Parameters:
  • num_bins -- Number of triangular mel-frequency bins.

  • low_freq -- Lower frequency cutoff.

  • high_freq -- Upper cutoff; 0 means none, negative values are offsets from Nyquist.

  • vtln_low -- VTLN lower cutoff of the warping function.

  • vtln_high -- VTLN upper cutoff of the warping function; negative values are offsets from Nyquist.

  • debug_mel -- Print debugging information for mel bin computation.

  • htk_mode -- Exact HTK compatibility (affects mel-energy flooring).

  • is_librosa -- Use librosa-style mel filters.

  • norm -- Librosa filter normalization ("" or "slaney").

  • use_slaney_mel_scale -- Use the Slaney mel scale (librosa mode only).

  • floor_to_int_bin -- Floor the bin edges to integers (librosa mode only).

__init__(self: kaldi_native_fbank.MelBanksOptions, num_bins: SupportsInt = 25, low_freq: SupportsFloat = 20, high_freq: SupportsFloat = 0, vtln_low: SupportsFloat = 100, vtln_high: SupportsFloat = -500, debug_mel: bool = False, htk_mode: bool = False, is_librosa: bool = False, norm: str = 'slaney', use_slaney_mel_scale: bool = True, floor_to_int_bin: bool = False) → None

Construct with the given options.

All fields are optional keyword arguments; the defaults are shown in the signature.

Parameters:
  • num_bins -- Number of triangular mel-frequency bins.

  • low_freq -- Lower frequency cutoff.

  • high_freq -- Upper cutoff; 0 means none, negative values are offsets from Nyquist.

  • vtln_low -- VTLN lower cutoff of the warping function.

  • vtln_high -- VTLN upper cutoff of the warping function; negative values are offsets from Nyquist.

  • debug_mel -- Print debugging information for mel bin computation.

  • htk_mode -- Exact HTK compatibility (affects mel-energy flooring).

  • is_librosa -- Use librosa-style mel filters.

  • norm -- Librosa filter normalization ("" or "slaney").

  • use_slaney_mel_scale -- Use the Slaney mel scale (librosa mode only).

  • floor_to_int_bin -- Floor the bin edges to integers (librosa mode only).

as_dict(self: kaldi_native_fbank.MelBanksOptions) → dict

Return the options as a dict.

property debug_mel

Print debugging information for mel bin computation.

property floor_to_int_bin

Floor the bin edges to integers (used only when is_librosa=True).

static from_dict(arg0: dict) → kaldi_native_fbank.MelBanksOptions

Build options from a dict.

property high_freq

Upper frequency cutoff; 0 means no cutoff, negative values are offsets from Nyquist.

property htk_mode

Enable exact HTK compatibility (affects mel-energy flooring).

property is_librosa

Use librosa-style mel filters.

If True, you probably want low_freq to be 0. See the librosa mel filter documentation.

property low_freq

Lower frequency cutoff of the mel bins.

See also high_freq.

property norm

Librosa filter normalization: "" or "slaney" (used only when is_librosa=True).

property num_bins

Number of triangular mel-frequency bins.

property use_slaney_mel_scale

Use the Slaney mel scale (used only when is_librosa=True).

property vtln_high

VTLN upper cutoff of the warping function; negative values are offsets from Nyquist.

property vtln_low

VTLN lower cutoff of the warping function.

class kaldi_native_fbank.MelBanks(self: kaldi_native_fbank.MelBanks, opts: kaldi_native_fbank.MelBanksOptions = MelBanksOptions(), frame_opts: kaldi_native_fbank.FrameExtractionOptions = FrameExtractionOptions(), vtln_warp_factor: SupportsFloat = 1.0)

Triangular mel filter banks.

A MelBanks object holds the weights of a set of triangular mel-frequency bins and can project FFT bin energies onto them.

See also

MelBanksOptions

Construct kaldi-compatible (or librosa-style) 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. If opts.is_librosa is true, librosa-style filters are computed instead.

Parameters:
  • opts -- Mel-bin configuration; see MelBanksOptions.

  • frame_opts -- Frame-extraction configuration (provides samp_freq and the FFT size).

  • vtln_warp_factor -- VTLN warping factor (1.0 means no warping).

__init__(self: kaldi_native_fbank.MelBanks, opts: kaldi_native_fbank.MelBanksOptions = MelBanksOptions(), frame_opts: kaldi_native_fbank.FrameExtractionOptions = FrameExtractionOptions(), vtln_warp_factor: SupportsFloat = 1.0) → None

Construct kaldi-compatible (or librosa-style) 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. If opts.is_librosa is true, librosa-style filters are computed instead.

Parameters:
  • opts -- Mel-bin configuration; see MelBanksOptions.

  • frame_opts -- Frame-extraction configuration (provides samp_freq and the FFT size).

  • vtln_warp_factor -- VTLN warping factor (1.0 means no warping).

compute(self: kaldi_native_fbank.MelBanks, fft_energies: Annotated[numpy.typing.ArrayLike, numpy.float32]) → numpy.typing.NDArray[numpy.float32]

Compute mel energies (note: NOT log) from a power spectrum.

For each mel bin \(i\), the output is the weighted sum of the FFT bin energies that fall into that bin:

\[\mathrm{mel}[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:

fft_energies -- 1-D float32 array of size num_fft_bins // 2 + 1 holding the power spectrum (not log). Must be C-contiguous.

Returns:

1-D float32 array of size num_mel_bins with the mel energies.

get_matrix(self: kaldi_native_fbank.MelBanks) → numpy.typing.NDArray[numpy.float32]

Return the mel filter weights.

Returns:

A float32 array of shape (num_mel_bins, num_fft_bins // 2 + 1); weights[i, k] is the weight of fft bin \(k\) for mel bin \(i\).

static inverse_mel_scale(mel: SupportsFloat) → float

HTK mel scale: convert mel frequency to Hz.

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

mel -- Mel frequency.

Returns:

The frequency in Hz.

static inverse_mel_scale_slaney(mel: SupportsFloat) → float

Slaney mel scale: convert mel frequency to Hz.

\[\begin{split}\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.\end{split}\]
Parameters:

mel -- Mel frequency (Slaney scale).

Returns:

The frequency in Hz.

static mel_scale(hz: SupportsFloat) → float

HTK mel scale: convert Hz to mel frequency.

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

hz -- The frequency in Hz.

Returns:

The mel frequency.

static mel_scale_slaney(hz: SupportsFloat) → float

Slaney mel scale: convert Hz to mel frequency.

\[\begin{split}\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.\end{split}\]
Parameters:

hz -- The frequency in Hz.

Returns:

The mel frequency (Slaney scale).