Discrete Cosine Transform (DCT) for pytorch
Project description
DCT (Discrete Cosine Transform) for pytorch
This library implements DCT in terms of the built-in FFT operations in pytorch so that back propagation works through it, on both CPU and GPU. For more information on DCT and the algorithms used here, see Wikipedia and the paper by J. Makhoul. This StackExchange article might also be helpful.
The following are currently implemented:
- 1-D DCT-I and its inverse (which is a scaled DCT-I)
- 1-D DCT-II and its inverse (which is a scaled DCT-III)
- 2-D DCT-II and its inverse (which is a scaled DCT-III)
- 3-D DCT-II and its inverse (which is a scaled DCT-III)
Install
pip install torch-dct
Requires torch>=0.4.1
(lower versions are probably OK but I haven't tested them).
You can run test by getting the source and run pytest
. To run the test you also
need scipy
installed.
Usage
import numpy as np
import torch
import torch_dct as dct
x = torch.tensor(np.random.normal(size=(1, 200)))
X = dct.dct(x) # DCT-II done through the last dimension
y = dct.idct(X) # scaled DCT-III done through the last dimension
assert (torch.abs(x - y)).sum() < 1e-10 # x == y within numerical tolerance
dct.dct1
and dct.idct1
are for DCT-I and its inverse. The usage is the same.
Just replace dct
and idct
by dct_2d
, dct_3d
, idct_2d
, idct_3d
, etc
to get the multidimensional versions.
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
Built Distribution
Hashes for torch_dct-0.1.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b80f1e18a21716228c73eb52583da007a7f0eead4f10f2654d8a2673d17f6b7c |
|
MD5 | 6606b3e6102c164a4e357d3f33cdf2c1 |
|
BLAKE2b-256 | a29fd6d213562a3a6420824e41189d367a9e4e20138788674559b06519be2ac3 |