STFT/iSTFT in PyTorch
An STFT/iSTFT written up in PyTorch using 1D Convolutions. Requirements are a recent version PyTorch, numpy, and librosa (for loading audio in test_stft.py). Thanks to Shrikant Venkataramani for sharing code this was based off of and Rafael Valle for catching bugs and adding the proper windowing logic. Uses Python 3.
Installation
Install easily with pip:
pip install torch_stft
Usage
import torch
from torch_stft import STFT
import numpy as np
import librosa
import matplotlib.pyplot as plt
audio = librosa.load(librosa.util.example_audio_file(), duration=10.0, offset=30)[0]
device = 'cpu'
filter_length = 1024
hop_length = 256
win_length = 1024 # doesn't need to be specified. if not specified, it's the same as filter_length
window = 'hann'
audio = torch.FloatTensor(audio)
audio = audio.unsqueeze(0)
audio = audio.to(device)
stft = STFT(
filter_length=filter_length,
hop_length=hop_length,
win_length=win_length,
window=window
).to(device)
magnitude, phase = stft.transform(audio)
output = stft.inverse(magnitude, phase)
output = output.cpu().data.numpy()[..., :]
audio = audio.cpu().data.numpy()[..., :]
print(np.mean((output - audio) ** 2)) # on order of 1e-16
Output of compare_stft.py:
Tests
Test it by just cloning this repo and running
pip install -r requirements.txt
python -m pytest .
Unfortunately, since it's implemented with 1D Convolutions, some filter_length/hop_length combinations can result in out of memory errors on your GPU when run on sufficiently large input.
Contributing
Pull requests welcome.
Metadata
Release files for torch-stft 0.1.4
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 |
|---|---|---|---|---|
| torch_stft-0.1.4.tar.gz | 4.8 kB | Without provenance | Details for torch_stft-0.1.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| torch_stft-0.1.4-py3-none-any.whl | Python 3 | none | any | Details for torch_stft-0.1.4-py3-none-any.whl |
Total release size: 11.0 kB
Release files / torch_stft-0.1.4.tar.gz
| Download URL | torch_stft-0.1.4.tar.gz |
|---|---|
| Size | 4.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1d7d1000da567def58de03525824fce0e40702f18555aff1b64d629bd6517217
|
|
BLAKE2b-256 checksum How to use checksums |
c3bdea6bc20ccaf1008c62c3b235eeb76649190e155ad43c0a06ed6bda2afd5f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.24.0 CPython/3.7.2
|
Release files / torch_stft-0.1.4-py3-none-any.whl
| Download URL | torch_stft-0.1.4-py3-none-any.whl |
|---|---|
| Size | 6.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8096b0d74204b9c79439486de8b0c15f465b6dab58653b015691a2c3fef76990
|
|
BLAKE2b-256 checksum How to use checksums |
9a538a0114930b53459bdc6b090515636bbba7e080905284fb83c995a29eb709
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.24.0 CPython/3.7.2
|
Security for torch-stft 0.1.4
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.