Short-Time Fourier Transform (STFT)
This module implements Forward and inverse Short-Time Fourier Transform (STFT) in Python using MLX.
It is designed to inherit from the nn.Module, which allows it to be used either as a standalone module or as part of a larger neural network architecture.
Installation
You can install mlx_stft directly from pypi:
pip install mlx_stft
Or you can install it directly from the source code:
git clone https://github.com/nuniz/mlx_stft.git
cd mlx_stft
python setup.py install
Features
- Multiple Backends: FFT-based or convolution-based implementations
- JIT Compilation: Compiled versions (
CompiledSTFT,CompiledISTFT) - Spectrum Modes: Support for both one-sided and dual-sided spectrums
Usage
from mlx_stft import STFT, ISTFT
# Forward transform
stft = STFT(n_fft=1024, hop_length=256, onesided=True)
spectrum = stft(x)
# Inverse transform
istft = ISTFT(n_fft=1024, hop_length=256, onesided=True)
reconstructed = istft(spectrum, length=x.shape[-1])
Compiled (JIT) Versions
from mlx_stft import CompiledSTFT, CompiledISTFT
# Use compiled versions for better performance
stft = CompiledSTFT(n_fft=1024, hop_length=256, onesided=True)
istft = CompiledISTFT(n_fft=1024, hop_length=256, onesided=True)
Arguments
STFT
n_fft: Number of Fourier transform points.
win_length: Length of the STFT window (default: same as n_fft).
hop_length: Number of audio samples between adjacent STFT columns.
window: Type of window function to apply (default is "hann").
onesided: Whether to return only the non-redundant part of the spectrum (default is False).
return_db: Whether to return the result in decibels (default is False).
use_fft: Force FFT-based computation. If None, auto-selects based on n_fft
ISTFT
n_fft: Number of Fourier transform points.
win_length: Length of the STFT window (default: same as n_fft).
hop_length: Number of audio samples between adjacent STFT columns.
window: Type of window function to apply (default is "hann").
onesided: Whether the input spectrum is one-sided (default is False).
use_fft: Force FFT-based computation. If None, auto-selects based on n_fft.
center: Whether to center the frames (default is True).
Performance
Benchmarks on Apple M4 (16GB RAM) with n_fft=1024, signal_length=16000, batch_size=1:
| Transform | Compiled | Onesided | Time (ms) | Throughput |
|---|---|---|---|---|
| STFT | No | Yes | 0.64 ± 0.07 | 25.0 M samples/s |
| STFT | Yes | Yes | 0.44 ± 0.03 | 36.6 M samples/s |
| iSTFT | No | Yes | 0.86 ± 0.13 | 18.7 M samples/s |
| iSTFT | Yes | Yes | 0.82 ± 0.12 | 19.5 M samples/s |
| Roundtrip | Yes | Yes | 0.83 ± 0.07 | 19.2 M samples/s |
Run your benchmarks:
python scripts/benchmark.py --quick # Quick benchmark
python scripts/benchmark.py # Full benchmark with multiple configurations
Example
The STFT is illustrated in the picture below using the NOIZEUS dataset's sp09.wav file.
One-Sided STFT
Dual-Sided STFT
Metadata
Release files for mlx-stft 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| mlx_stft-0.1.2.tar.gz | 13.6 kB | Without provenance | Details for mlx_stft-0.1.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| mlx_stft-0.1.2-py3-none-any.whl | Python 3 | none | any | Details for mlx_stft-0.1.2-py3-none-any.whl |
Total release size: 25.8 kB
Release files / mlx_stft-0.1.2.tar.gz
| Download URL | mlx_stft-0.1.2.tar.gz |
|---|---|
| Size | 13.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fa879a3b1274860812adb880f49bcf23302cba8038f31d1929b07490dc2b48d3
|
|
BLAKE2b-256 checksum How to use checksums |
1e6112c40ca7f684dc94a34c9cbd694fde09e2d4f601981eb164ce543e16a315
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.25
|
Release files / mlx_stft-0.1.2-py3-none-any.whl
| Download URL | mlx_stft-0.1.2-py3-none-any.whl |
|---|---|
| Size | 12.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6ced0034db07e3d5693d724a40a60869f3ee7d5de3e3e558926d421b0ae83295
|
|
BLAKE2b-256 checksum How to use checksums |
5567b6e3322dc36ad04da33cd7b1af22f0599ca234cd4f452e25b0b371076bfb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.25
|
Security for mlx-stft 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.