STFT / iSTFT
Short-time Fourier transform and its inverse (kaldi-native-fbank/csrc/stft.h, kaldi-native-fbank/csrc/istft.h).
import kaldi_native_fbank as knf
config = knf.StftConfig(...)
stft = knf.Stft(config)
spec = stft(samples) # complex spectra
istft = knf.IStft(config)
wave = istft(spec)
Note: these are low-level signal-processing building blocks (as used inside kaldi's pitch tracker etc.); there is no direct kaldi binary counterpart — kaldi implements STFT inside feature extraction.
API reference
- class kaldi_native_fbank.StftConfig(self: kaldi_native_fbank.StftConfig, n_fft: SupportsInt, hop_length: SupportsInt, win_length: SupportsInt, window_type: str = '', center: bool = True, pad_mode: str = 'reflect', normalized: bool = False, window: collections.abc.Sequence[SupportsFloat] = [])
Configuration for
StftandIStft.Construct with the given parameters.
All fields are optional keyword arguments; the defaults are shown in the signature.
- Parameters:
n_fft -- FFT size; should be a power of two.
hop_length -- Number of samples between successive frames.
win_length -- Window size in samples (must be <= n_fft).
window_type -- Window type, e.g. "hann".
center -- If True, pad by n_fft/2 and center the frames.
pad_mode -- Padding mode when center is True ("reflect").
normalized -- If True, scale the FFT by 1/sqrt(n_fft).
window -- Explicit window coefficients (overrides window_type).
- __init__(self: kaldi_native_fbank.StftConfig, n_fft: SupportsInt, hop_length: SupportsInt, win_length: SupportsInt, window_type: str = '', center: bool = True, pad_mode: str = 'reflect', normalized: bool = False, window: collections.abc.Sequence[SupportsFloat] = []) None
Construct with the given parameters.
All fields are optional keyword arguments; the defaults are shown in the signature.
- Parameters:
n_fft -- FFT size; should be a power of two.
hop_length -- Number of samples between successive frames.
win_length -- Window size in samples (must be <= n_fft).
window_type -- Window type, e.g. "hann".
center -- If True, pad by n_fft/2 and center the frames.
pad_mode -- Padding mode when center is True ("reflect").
normalized -- If True, scale the FFT by 1/sqrt(n_fft).
window -- Explicit window coefficients (overrides window_type).
- property center
If True, pad the signal by
n_fft // 2on both sides (usingpad_mode) and center the frames.
- property hop_length
Number of samples between successive frames.
- property n_fft
FFT size; should be a power of two.
- property normalized
If True, scale the FFT by
1 / sqrt(n_fft).
- property pad_mode
Padding mode used when
centeris True; currently only "reflect".
- property win_length
Window size in samples; must be <= n_fft. The window is zero-padded to n_fft.
- property window_type
Window type, e.g. "hann"; ignored if explicit window coefficients are given.
- class kaldi_native_fbank.StftResult(self: kaldi_native_fbank.StftResult, real: collections.abc.Sequence[SupportsFloat], imag: collections.abc.Sequence[SupportsFloat], num_frames: SupportsInt)
Result of
Stft.compute(): the complex spectra of all frames.Construct a StftResult.
- Parameters:
real -- Real parts, shape (num_frames, n_fft // 2 + 1), flattened row-major.
imag -- Imaginary parts, same shape.
num_frames -- Number of frames in real/imag.
- __init__(self: kaldi_native_fbank.StftResult, real: collections.abc.Sequence[SupportsFloat], imag: collections.abc.Sequence[SupportsFloat], num_frames: SupportsInt) None
Construct a StftResult.
- Parameters:
real -- Real parts, shape (num_frames, n_fft // 2 + 1), flattened row-major.
imag -- Imaginary parts, same shape.
num_frames -- Number of frames in real/imag.
- property imag
Imaginary parts of the spectra, shape (num_frames, n_fft // 2 + 1), flattened row-major.
- property num_frames
Number of frames in real/imag.
- property real
Real parts of the spectra, shape (num_frames, n_fft // 2 + 1), flattened row-major.
- class kaldi_native_fbank.Stft(self: kaldi_native_fbank.Stft, config: kaldi_native_fbank.StftConfig)
Compute the short-time Fourier transform of a waveform.
See also
IStftThe inverse transform.
StftConfigConfiguration.
Construct with the given config.
- Parameters:
config -- STFT configuration; see
StftConfig.
- __init__(self: kaldi_native_fbank.Stft, config: kaldi_native_fbank.StftConfig) None
Construct with the given config.
- Parameters:
config -- STFT configuration; see
StftConfig.
- compute(self: kaldi_native_fbank.Stft, input: collections.abc.Sequence[SupportsFloat]) kaldi_native_fbank.StftResult
Compute the STFT of a waveform.
- Parameters:
data -- 1-D float32 samples.
- Returns:
A
StftResultwith the complex spectra of all frames.
- class kaldi_native_fbank.IStft(self: kaldi_native_fbank.IStft, config: kaldi_native_fbank.StftConfig)
Reconstruct a waveform from
Stftresults.This is the inverse of
Stft(compatible with librosa's istft()): the frames are inverse-transformed, windowed, overlap-added, and divided by the summed window envelope.See also
Construct with the same config that was used for the forward
Stft.- Parameters:
config -- The config used for the forward transform.
- __init__(self: kaldi_native_fbank.IStft, config: kaldi_native_fbank.StftConfig) None
Construct with the same config that was used for the forward
Stft.- Parameters:
config -- The config used for the forward transform.
- compute(self: kaldi_native_fbank.IStft, stft_result: kaldi_native_fbank.StftResult) list[float]
Reconstruct the waveform.
- Parameters:
stft_result -- Output of
Stft.compute().- Returns:
1-D list of samples. If
config.centeris True, then_fft // 2padding samples at both ends are removed.
- class kaldi_native_fbank.Rfft(self: kaldi_native_fbank.Rfft, n: SupportsInt, inverse: bool = False)
n-point real discrete Fourier transform, where n is even.
Forward transform:
\[ \begin{align}\begin{aligned}R[k] = \sum_{j=0}^{n-1} \mathrm{in}[j] \cos(2\pi j k / n), \quad 0 \le k \le n/2\\I[k] = \sum_{j=0}^{n-1} \mathrm{in}[j] \sin(2\pi j k / n), \quad 0 < k < n/2\end{aligned}\end{align} \]See also
Construct for an n-point transform.
- Parameters:
n -- Number of fft points; must be even and >= 2.
inverse -- If True,
compute()performs the inverse transform.
- __init__(self: kaldi_native_fbank.Rfft, n: SupportsInt, inverse: bool = False) None
Construct for an n-point transform.
- Parameters:
n -- Number of fft points; must be even and >= 2.
inverse -- If True,
compute()performs the inverse transform.
- compute(self: kaldi_native_fbank.Rfft, arg0: collections.abc.Sequence[SupportsFloat]) list[float]
Transform in place and return the packed spectrum.
On return of the forward transform:
out[0] = R[0]out[1] = R[n/2]for
1 < k < n/2:out[2*k] = R[k],out[2*k+1] = I[k]
- Parameters:
d -- 1-D list of n floats.
- Returns:
The packed spectrum, a 1-D list of n floats.