Window & frame extraction
Splitting a waveform into overlapping frames and pre-processing each frame (dithering, DC offset removal, preemphasis, windowing) before the FFT (kaldi-native-fbank/csrc/feature-window.h). kaldi counterpart: FrameExtractionOptions. See Python API reference for the full field-by-field mapping to kaldi options.
Every feature extractor accepts these settings via its frame_opts field.
The window formulas for each window_type are documented on
kaldi_native_fbank.FrameExtractionOptions() (and in the C++ docs).
import kaldi_native_fbank as knf
opts = knf.FrameExtractionOptions()
opts.samp_freq = 16000
opts.dither = 0 # deterministic output
window = knf.FeatureWindowFunction(opts)
frame = [0.0] * len(window.window) # one frame of window_size
windowed = window.apply(frame)
API reference
- class kaldi_native_fbank.FrameExtractionOptions(self: kaldi_native_fbank.FrameExtractionOptions, samp_freq: SupportsFloat = 16000.0, frame_shift_ms: SupportsFloat = 10.0, frame_length_ms: SupportsFloat = 25.0, dither: SupportsFloat = 0.00003, preemph_coeff: SupportsFloat = 0.97, remove_dc_offset: bool = True, window_type: str = 'povey', round_to_power_of_two: bool = True, blackman_coeff: SupportsFloat = 0.42, snip_edges: bool = True)
Options for extracting frames from a waveform.
This is the equivalent of kaldi's FrameExtractionOptions. It controls how a waveform is split into overlapping frames and how each frame is pre-processed (dithering, DC offset removal, preemphasis, windowing) before the FFT.
See also
FeatureWindowFunctionThe windowing function implied by these options.
kaldi_native_fbank.OnlineFbankStreaming extractors that accept these options via
frame_opts.
Construct with the given options.
All fields are optional keyword arguments; the defaults are shown in the signature.
- Parameters:
samp_freq -- Waveform sample frequency in Hz (must match the waveform).
frame_shift_ms -- Frame shift in milliseconds.
frame_length_ms -- Frame length in milliseconds.
dither -- Amount of dithering; 0.0 disables it. The default 0.00003 is equivalent to kaldi's 1.0.
preemph_coeff -- Preemphasis coefficient.
remove_dc_offset -- Subtract the mean of the waveform before the FFT.
window_type -- Window type, e.g. "povey"; see
window_typefor the formulas of each type.round_to_power_of_two -- If True, round the FFT window size up to a power of two.
blackman_coeff -- Constant coefficient for the generalized Blackman window.
snip_edges -- If True, emit only frames that completely fit in the file; if False, reflect at the edges.
- __init__(self: kaldi_native_fbank.FrameExtractionOptions, samp_freq: SupportsFloat = 16000.0, frame_shift_ms: SupportsFloat = 10.0, frame_length_ms: SupportsFloat = 25.0, dither: SupportsFloat = 0.00003, preemph_coeff: SupportsFloat = 0.97, remove_dc_offset: bool = True, window_type: str = 'povey', round_to_power_of_two: bool = True, blackman_coeff: SupportsFloat = 0.42, snip_edges: bool = True) None
Construct with the given options.
All fields are optional keyword arguments; the defaults are shown in the signature.
- Parameters:
samp_freq -- Waveform sample frequency in Hz (must match the waveform).
frame_shift_ms -- Frame shift in milliseconds.
frame_length_ms -- Frame length in milliseconds.
dither -- Amount of dithering; 0.0 disables it. The default 0.00003 is equivalent to kaldi's 1.0.
preemph_coeff -- Preemphasis coefficient.
remove_dc_offset -- Subtract the mean of the waveform before the FFT.
window_type -- Window type, e.g. "povey"; see
window_typefor the formulas of each type.round_to_power_of_two -- If True, round the FFT window size up to a power of two.
blackman_coeff -- Constant coefficient for the generalized Blackman window.
snip_edges -- If True, emit only frames that completely fit in the file; if False, reflect at the edges.
- as_dict(self: kaldi_native_fbank.FrameExtractionOptions) dict
Return the options as a dict.
- property blackman_coeff
Constant coefficient for the generalized Blackman window.
- property dither
Amount of dithering; 0.0 means no dither. The default 0.00003 is equivalent to dither=1.0 in kaldi.
- property frame_length_ms
Frame length in milliseconds.
- property frame_shift_ms
Frame shift in milliseconds.
- static from_dict(arg0: dict) kaldi_native_fbank.FrameExtractionOptions
Build options from a dict.
- property preemph_coeff
Preemphasis coefficient.
- property remove_dc_offset
Subtract mean of the waveform before the FFT.
- property round_to_power_of_two
If True, round the window size to a power of two by zero-padding the FFT input.
- property samp_freq
Waveform data sample frequency (must match the waveform).
- property snip_edges
If True, output only frames that completely fit in the file (the frame count depends on the frame length). If False, the frame count depends only on the frame shift and the data is reflected at the edges.
- property window_type
Window type: one of
"hanning","hann","sine","hamming","povey","rectangular"or"blackman".Let \(N\) be the window size, \(n = 0, \ldots, N-1\) the sample index, and \(a = 2\pi/(N-1)\). The formulas are:
"hanning": \(w[n] = 0.5 - 0.5 \cos(a n)\)"hann": \(w[n] = 0.5 - 0.5 \cos(a n)\) with \(a = 2\pi/N\) (periodic variant; see the hann_window docs of PyTorch)"sine": \(w[n] = \sin(0.5\,a\,n) = \sin(\pi n/(N-1))\)"hamming": \(w[n] = 0.54 - 0.46 \cos(a n)\)"povey": \(w[n] = (0.5 - 0.5 \cos(a n))^{0.85}\) (like hamming but goes to zero at the edges)"rectangular": \(w[n] = 1\)"blackman": \(w[n] = c - 0.5 \cos(a n) + (0.5 - c) \cos(2 a n)\) where \(c\) isblackman_coeff.
- class kaldi_native_fbank.FeatureWindowFunction(self: kaldi_native_fbank.FeatureWindowFunction, opts: kaldi_native_fbank.FrameExtractionOptions)
Stores the windowing coefficients and applies them to a frame.
Construct the windowing function implied by
opts.window_type.The window has
opts.WindowSize()samples.- Parameters:
opts -- Frame-extraction options selecting the window type and size.
- __init__(self: kaldi_native_fbank.FeatureWindowFunction, opts: kaldi_native_fbank.FrameExtractionOptions) None
Construct the windowing function implied by
opts.window_type.The window has
opts.WindowSize()samples.- Parameters:
opts -- Frame-extraction options selecting the window type and size.
- apply(self: kaldi_native_fbank.FeatureWindowFunction, arg0: collections.abc.Sequence[SupportsFloat]) list[float]
Apply the window to a frame, in place.
wave[i]becomeswave[i] * window[i].- Parameters:
wave -- One frame of
window_sizesamples.- Returns:
The windowed frame (the same list, modified in place).
- property window
The window coefficients, a list of
window_sizefloats.