Skip to main content

An STFT/iSTFT for PyTorch

Project description

Build Status

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:

images/stft.png

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

torch_stft-0.1.3.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

torch_stft-0.1.3-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file torch_stft-0.1.3.tar.gz.

File metadata

  • Download URL: torch_stft-0.1.3.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

Hashes for torch_stft-0.1.3.tar.gz
Algorithm Hash digest
SHA256 2619ee97d0b8c1adf79c95f2e6bb1714b6390daae018fe85a9872f1ada92cc98
MD5 0f67a18c618aa029730c6ed6cd7834b1
BLAKE2b-256 e7f78add3fd34d364a72ad38a5f1f5e74684262a91f6a21bd1d97a56471d99c8

See more details on using hashes here.

File details

Details for the file torch_stft-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: torch_stft-0.1.3-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

Hashes for torch_stft-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 22e69e8fa88dad4e0f33356c41b41016615c82efb6bedede4a19b9f7a24e0519
MD5 14113043f91efe534d7c4dfcb80dd0f7
BLAKE2b-256 89e428ba9db521e278d570d5b3b58b5b73332b4410afb7e1ea951ae5a2965c53

See more details on using hashes here.

Supported by

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