Skip to main content

An implementation of STFT and Inverse STFT in mlx

Project description

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

one-sided stft

Dual-Sided STFT

dual-sided stft

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mlx_stft-0.1.2.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mlx_stft-0.1.2-py3-none-any.whl (12.2 kB view details)

Uploaded Python 3

File details

Details for the file mlx_stft-0.1.2.tar.gz.

File metadata

  • Download URL: mlx_stft-0.1.2.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for mlx_stft-0.1.2.tar.gz
Algorithm Hash digest
SHA256 fa879a3b1274860812adb880f49bcf23302cba8038f31d1929b07490dc2b48d3
MD5 754c489226df9dc88770adcb2e5f07c1
BLAKE2b-256 1e6112c40ca7f684dc94a34c9cbd694fde09e2d4f601981eb164ce543e16a315

See more details on using hashes here.

File details

Details for the file mlx_stft-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: mlx_stft-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 12.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for mlx_stft-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6ced0034db07e3d5693d724a40a60869f3ee7d5de3e3e558926d421b0ae83295
MD5 1cbcee3c35214124c482802b6a05c57e
BLAKE2b-256 5567b6e3322dc36ad04da33cd7b1af22f0599ca234cd4f452e25b0b371076bfb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page