Short-Time Discrete Cosine Transform (DCT) for Python. SciPy, TensorFlow and PyTorch implementations.
This similar to librosa.core.{stft,istft}
, tf.signal.{stft,inverse_stft}
, torch.{stft,istft}
but using DCT.
Note that the PyTorch implementation requires torch-dct.
See also: Example notebook.
# Short-Time DCT
spectrogram = pydct.scipy.sdct(example_audio, frame_length=1024, frame_step=256)
spectrogram_tf = pydct.tf.sdct_tf(example_audio, frame_length=1024, frame_step=256)
spectrogram_torch = pydct.torch.sdct_torch(torch.from_numpy(example_audio), frame_length=1024, frame_step=256)
# Inverse Short-Time DCT
example_audio_2 = pydct.scipy.isdct(spectrogram, frame_step=256)
example_audio_2_tf = pydct.tf.isdct_tf(spectrogram_tf, frame_step=256)
example_audio_2_torch = pydct.torch.isdct_torch(spectrogram_torch, frame_step=256)
# Plot with librosa
librosa.display.specshow(
librosa.core.amplitude_to_db(spectrogram),
y_axis='log',
)
No batch support.
Supports batching:
example_audio_batch.shape # (32, ...)
spectrogram_tf_batch = pydct.tf.sdct_tf(example_audio_batch, ...)
spectrogram_tf_batch.shape # TensorShape([32, ..., ...])
Supports batching plus arbitrary number of additional dimensions:
pydct.torch.sdct_torch: (..., time) -> (..., frequencies, time)
pydct.torch.isdct_torch: (..., frequencies, time) -> (..., time)
Dimension order is "F-T", identical to librosa.core.stft
: pydct.scipy.sdct(...) -> (frequencies, time)
Dimension order is "T-F", identical to tf.signal.stft
: pydct.tf.sdct_tf(...) -> (batch, time, frequencies)
Dimension order is "F-T", identical to torch.stft
: pydct.torch.sdct_torch(...) -> (..., frequencies, time)