SciPy DCT

scipy.fftpack provides dct() function to calculate Discrete Cosine Transform on an array. In this tutorial, we shall learn the syntax and the usage of dct() function with SciPy DCT Examples.

Syntax

y = scipy.fftpack.dct(x, type=2, n=None, axis=-1, norm=None, overwrite_x=False)
Parameter Required/ Optional [datatype] Description
xRequired[array] Array on which FFT has to be calculated.
typeOptionalOne of [1, 2, 3] are allowed.
nOptional[int] Length of the Fourier transform. If n < x.shape[axis], x is truncated. If n > x.shape[axis], x is zero-padded. The default results in n = x.shape[axis].
axisOptional[int] Axis along which the fft’s are computed; the default is over the last axis (i.e., axis=-1).
normOptional[None, ‘ortho’] are allowed values. ortho means Orthonormalization.
overwrite_xOptional[boolean] If True, the contents of x can be destroyed; the default is False.
y[Returned value][complex ndarray] Discrete Fourier Transform of x.

Values provided for the optional arguments are default values.

Example 1 – SciPy DCT

scipy-example.py

# import numpy
import numpy as np

# import dct
from scipy.fftpack import dct

# numpy array
x = np.array([1.0, 2.0, 1.0, 2.0, -1.0])
print("x      : ",x)

# apply dct function on array
y = dct(x)
print("dct(x) : ",y)
Try Online

Output

x      :  [ 1.  2.  1.  2. -1.]
dct(x) :  [ 10.           3.80422607  -4.47213595   2.35114101  -4.47213595]