Discrete Cosine Transform
Details
The DCT definitions here are based on the python scipy.fft.dct
definitions.
Specifically this use:
When norm_forward = TRUE
$$ y_k = \frac{1}{zN} \sum_{j=0}^{N-1}x_j\cos\left(\frac{\pi k(2j+1)}{2N}\right) $$
$$ z = \begin{cases} \sqrt{2}& \text{for }k=0\\ 1 & \text{for }k>0 \end{cases} $$
When norm_forward = FALSE
$$
y_k = \frac{2}{z} \sum_{j=0}^{N-1}x_j\cos\left(\frac{\pi k(2j+1)}{2N}\right)
$$
$$ z = \begin{cases} \sqrt{2}& \text{for }k=0\\ 1 & \text{for }k>0 \end{cases} $$
This second formulation is primarily to be able to generate the DCT basis functions like so
For the Inverse Discrete Cosine Transform, see idct.
Examples
x <- seq(0,1, length = 10)
y <- 5 + x + (2 * (x^2)) + (-2 * (x^4))
dct_coefs <- dct(y)