An STFT/iSTFT for PyTorch
Project description
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.
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
Built Distribution
File details
Details for the file torch_stft-0.1.4.tar.gz
.
File metadata
- Download URL: torch_stft-0.1.4.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1d7d1000da567def58de03525824fce0e40702f18555aff1b64d629bd6517217 |
|
MD5 | ddd4600243c92ef8c95c92facd636a08 |
|
BLAKE2b-256 | c3bdea6bc20ccaf1008c62c3b235eeb76649190e155ad43c0a06ed6bda2afd5f |
File details
Details for the file torch_stft-0.1.4-py3-none-any.whl
.
File metadata
- Download URL: torch_stft-0.1.4-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8096b0d74204b9c79439486de8b0c15f465b6dab58653b015691a2c3fef76990 |
|
MD5 | 4e57cdc6fd8208803481495f52a54b32 |
|
BLAKE2b-256 | 9a538a0114930b53459bdc6b090515636bbba7e080905284fb83c995a29eb709 |