MFCC
Mel-frequency cepstral coefficients, kaldi-compatible (kaldi-native-fbank/csrc/feature-mfcc.h). Numerically equal to kaldi's compute-mfcc-feats for identical parameters.
Usage
import numpy as np
import kaldi_native_fbank as knf
samples = ... # 1-D float32 waveform, raw int16 amplitude scale
sample_rate = 16000
opts = knf.MfccOptions()
opts.frame_opts.samp_freq = sample_rate
opts.frame_opts.snip_edges = True
opts.frame_opts.dither = 0.0 # use 0 for deterministic output
opts.mel_opts.num_bins = 23
opts.mel_opts.low_freq = 20
opts.mel_opts.high_freq = 7800
opts.num_ceps = 13
opts.use_energy = False
opts.cepstral_lifter = 22
mfcc = knf.OnlineMfcc(opts)
mfcc.accept_waveform(sample_rate, samples)
mfcc.input_finished()
num_frames = mfcc.num_frames_ready
frame = mfcc.get_frame(0) # list of floats, dim = num_ceps
Important: feed samples at raw int16 amplitude (do NOT divide by 32768). kaldi's wav reader keeps raw magnitudes; normalizing scales the energy by 32768^2 and shifts C0.
Equivalent in kaldi
# kaldi (dither off for deterministic output)
compute-mfcc-feats --dither=0.0 --use-energy=false --num-ceps=13 \\
--num-mel-bins=23 --cepstral-lifter=22 --low-freq=20 --high-freq=7800 \\
--sample-frequency=16000 --snip-edges=true scp:wav.scp ark:mfcc.ark
Mapping
kaldi option |
python API |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
With dither=0 and identical parameters the output matches kaldi
value-by-value (verified against compute-mfcc-feats output; residual
diff ~1e-4 at float rounding level).
API reference
- class kaldi_native_fbank.MfccOptions(self: kaldi_native_fbank.MfccOptions, frame_opts: kaldi_native_fbank.FrameExtractionOptions = FrameExtractionOptions(), mel_opts: kaldi_native_fbank.MelBanksOptions = MelBanksOptions(num_bins=23), num_ceps: SupportsInt = 13, use_energy: bool = True, energy_floor: SupportsFloat = 0, raw_energy: bool = True, cepstral_lifter: SupportsFloat = 22, htk_compat: bool = False)
Options for computing MFCC features.
This is kaldi's MfccOptions.
See also
FrameExtractionOptionsFrame extraction settings (
frame_opts).MelBanksOptionsMel filter-bank settings (
mel_opts).kaldi_native_fbank.OnlineMfccThe streaming extractor that consumes these options.
Construct with the given options.
All fields are optional keyword arguments; the defaults are shown in the signature.
- Parameters:
frame_opts -- Frame-extraction options; see
FrameExtractionOptions.mel_opts -- Mel filter-bank options (default 23 bins); see
MelBanksOptions.num_ceps -- Number of cepstra in the MFCC computation (including C0).
use_energy -- Use energy instead of C0.
energy_floor -- Absolute floor on energy (only with use_energy; needed when dither is 0).
raw_energy -- Compute energy before preemphasis/windowing.
cepstral_lifter -- Liftering constant; 0 disables liftering.
htk_compat -- Put energy/C0 last and use a factor of sqrt(2) on C0.
- __init__(self: kaldi_native_fbank.MfccOptions, frame_opts: kaldi_native_fbank.FrameExtractionOptions = FrameExtractionOptions(), mel_opts: kaldi_native_fbank.MelBanksOptions = MelBanksOptions(num_bins=23), num_ceps: SupportsInt = 13, use_energy: bool = True, energy_floor: SupportsFloat = 0, raw_energy: bool = True, cepstral_lifter: SupportsFloat = 22, htk_compat: bool = False) None
Construct with the given options.
All fields are optional keyword arguments; the defaults are shown in the signature.
- Parameters:
frame_opts -- Frame-extraction options; see
FrameExtractionOptions.mel_opts -- Mel filter-bank options (default 23 bins); see
MelBanksOptions.num_ceps -- Number of cepstra in the MFCC computation (including C0).
use_energy -- Use energy instead of C0.
energy_floor -- Absolute floor on energy (only with use_energy; needed when dither is 0).
raw_energy -- Compute energy before preemphasis/windowing.
cepstral_lifter -- Liftering constant; 0 disables liftering.
htk_compat -- Put energy/C0 last and use a factor of sqrt(2) on C0.
- as_dict(self: kaldi_native_fbank.MfccOptions) dict
Return the options as a dict.
- property cepstral_lifter
Constant that controls scaling of the MFCCs (liftering). If 0.0, no liftering is done.
- property energy_floor
Floor on energy (absolute, not relative). Only makes a difference if
use_energyis True; only necessary ifframe_opts.ditheris 0. Suggested values: 0.1 or 1.0.
- property frame_opts
Frame-extraction options; see
FrameExtractionOptions.
- static from_dict(arg0: dict) kaldi_native_fbank.MfccOptions
Build options from a dict.
- property htk_compat
If True, put energy/C0 last and use a factor of sqrt(2) on C0. Warning: not sufficient for HTK-compatible features (other parameters must change too).
- property mel_opts
Mel filter-bank options; see
MelBanksOptions. The default has 23 mel bins.
- property num_ceps
Number of cepstra in the MFCC computation (including C0).
- property raw_energy
If True, compute energy before preemphasis and windowing.
- property use_energy
Use energy (not C0) in the MFCC computation.
- class kaldi_native_fbank.OnlineMfcc(self: kaldi_native_fbank.OnlineMfcc, opts: kaldi_native_fbank.MfccOptions)
Construct from the options of the wrapped feature computer.
- Parameters:
opts -- Options of the computer, e.g.
FbankOptionsforkaldi_native_fbank.OnlineFbank.
- __init__(self: kaldi_native_fbank.OnlineMfcc, opts: kaldi_native_fbank.MfccOptions) None
Construct from the options of the wrapped feature computer.
- Parameters:
opts -- Options of the computer, e.g.
FbankOptionsforkaldi_native_fbank.OnlineFbank.
- accept_waveform(self: kaldi_native_fbank.OnlineMfcc, sampling_rate: SupportsFloat, waveform: collections.abc.Sequence[SupportsFloat]) None
Add more waveform samples.
- Parameters:
sampling_rate -- The sampling rate of
waveform; must match the rate in the options.waveform -- 1-D float32 samples.
- property dim
The dimension of the output features.
- property frame_shift_in_seconds
The frame shift in seconds.
- get_frame(self: object, frame: SupportsInt) numpy.typing.NDArray[numpy.float32]
Return the
frame-th feature vector.- Parameters:
frame -- Frame index.
- Returns:
np.ndarray of shape
(dim,)and dtype float32. The buffer is owned by the extractor.
- input_finished(self: kaldi_native_fbank.OnlineMfcc) None
Tell the extractor that no more waveform will be added.
This flushes out the last frame or two when
snip_edgesis False and also affects the return value ofis_last_frame().
- is_last_frame(self: kaldi_native_fbank.OnlineMfcc, frame: SupportsInt) bool
Return true if
frameis the last frame.Note: this can only return true after
input_finished()has been called.- Parameters:
frame -- Frame index.
- Returns:
True if the frame is the last one.
- property num_frames_ready
The number of frames that have already been computed.
- pop(self: kaldi_native_fbank.OnlineMfcc, n: SupportsInt) None
Discard the first
nframes.- Parameters:
n -- Number of frames to discard.