Data holds great importance in this era—different companies, government sectors, and other organizations process a large amount of data daily. According to Seed Scientific, we create almost 2.5 quintillion bytes of data every day. With the increase of IoT devices, this amount can increase exponentially. This shows us how vital compression is in daily usage.
Discrete cosine transform (DCT) is a transform that is mainly used in compression algorithms. It transforms data points in a spatial domain into a frequency domain. This makes it easier to find the repetition of patterns. Like any other transform, it is also invertible. This means we can return the actual data points if the transforms are given.
As DCT was mainly introduced for image compression algorithms, we can try to understand it in terms of images. DCT on an image acts the same way the discrete fourier transform (DFT) works on a bunch of discrete values. It tries to approximate the pixels using cosine waves. This allows us to store it efficiently in a compressed form.
Several formulas define DCT, but the most common one is given below:
Here, the sequence of data is
As mentioned before, DCT does a great job of highlighting the frequencies. Which can further show redundancies. This makes it perfect for compression applications. Some of the applications of DCT are as follows:
Image compression algorithms like JPEG and HEIF.
Audio file formats like MP3, AAC, and more.
Video file formats like MPEG.
Scientists and Engineers also use them for digital signal processing, telecommunications, and more.
Python's SciPy library implements DCT. It can be used by scipy.fft.dct()
function.
from scipy import fftx = [9, -7, 3, 4, 5, 1, -8, 2, 6]y = fft.dct(x)print(y)
Line 3: We create a variable x
to store a list of numbers.
Line 5: We apply DCT and store it in variable y
.
Different DCT definitions are used in SciPy based on the argument type
passed into it. By default, it employs the type II algorithm, which is the same one defined in the equation above. To know more about how SciPy implements these algorithms and information about other parameters, refer to the official documentation.
Free Resources