A fast and clean implementation of the Modified Discrete Cosine Transform (MDCT) algorithm in PyTorch.
pip install torch_mdct
import torchaudio
from torch_mdct import MDCT, IMDCT
# Load a sample waveform
waveform, sample_rate = torchaudio.load("/path/to/audio.file")
# Initialize the mdct and imdct transforms
mdct = MDCT(win_length=2048)
imdct = IMDCT(win_length=2048)
# Transform waveform into mdct spectrogram
spectrogram = mdct(waveform)
# Transform spectrogram back to audio
reconst_waveform = imdct(spectrogram)
# Compute the differences
print(f"L1: {(waveform - reconst_waveform).abs().mean()}")
[1] Zaf-Python: Zafar's Audio Functions in Python for audio signal analysis.
[2] MDCT: A fast MDCT implementation using SciPy and FFTs.