https://github.com/jonashaag/pydct
Short-Time Discrete Cosine Transform (DCT) for Python. SciPy, TensorFlow and PyTorch implementations.
https://github.com/jonashaag/pydct
dct discrete-cosine-transform python pytorch scipy tensorflow
Last synced: 19 days ago
JSON representation
Short-Time Discrete Cosine Transform (DCT) for Python. SciPy, TensorFlow and PyTorch implementations.
- Host: GitHub
- URL: https://github.com/jonashaag/pydct
- Owner: jonashaag
- License: isc
- Created: 2020-06-05T15:59:12.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2021-02-11T15:50:35.000Z (about 4 years ago)
- Last Synced: 2025-04-05T00:44:48.449Z (25 days ago)
- Topics: dct, discrete-cosine-transform, python, pytorch, scipy, tensorflow
- Language: Jupyter Notebook
- Homepage:
- Size: 150 KB
- Stars: 27
- Watchers: 2
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PyDCT
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](https://github.com/zh217/torch-dct).
## Usage
See also: [Example notebook](./example.ipynb).
```py
# 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',
)
```## Differences between SciPy and TensorFlow implementations
### Batching
#### SciPy
No batch support.
#### TensorFlow
Supports batching:
```py
example_audio_batch.shape # (32, ...)
spectrogram_tf_batch = pydct.tf.sdct_tf(example_audio_batch, ...)
spectrogram_tf_batch.shape # TensorShape([32, ..., ...])
```#### PyTorch
Supports batching plus arbitrary number of additional dimensions:
- `pydct.torch.sdct_torch: (..., time) -> (..., frequencies, time)`
- `pydct.torch.isdct_torch: (..., frequencies, time) -> (..., time)`### Order of dimensions
#### SciPy
Dimension order is "F-T", identical to `librosa.core.stft`: `pydct.scipy.sdct(...) -> (frequencies, time)`
#### TensorFlow
Dimension order is "T-F", identical to `tf.signal.stft`: `pydct.tf.sdct_tf(...) -> (batch, time, frequencies)`
#### PyTorch
Dimension order is "F-T", identical to `torch.stft`: `pydct.torch.sdct_torch(...) -> (..., frequencies, time)`