An open API service indexing awesome lists of open source software.

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.

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)`