SciPy - idct() Function



The scipy method idct() returns the inverse discrete cosine transform of a given input array. Therefore, it transforms frequency domain data back into the spatial or original domain, so it reverses the process of DCT.

The scipy.fft.idct() method is the inverse DCT that transforms frequency data back into spatial representation. norm="ortho" gives constant scaling and symmetry. IDCT supports types 1, 2, 3, and 4; while 1 and 4 are self-inverse, type 2 Standard IDCT, and type 3 Inverse of DCT Type 2. The most common is IDCT-II (type 2), which is required for accurate reconstruction in applications like image compression and signal recovery.

IDCT is very much widely used in applications such as the reconstruction of signals and decompression of images, in which a data compressed or analyzed using the frequency domain needs to recover to its original state. In combination with DCT, it makes it recoverable without losing the information.

Errors occur if input data is in a form that is not supported like complex numbers or non-numeric types or the parameter type, n, or axis is incorrect. Misuse can lead to wrong reconstruction and runtime exceptions. Proper input validation gives it a smooth functioning.

Syntax

The syntax for the Scipy idct() method is as follows −

.idct(x, type=2, n=None, axis=-1, norm=None, overwrite_x=False, workers=None, orthogonalize=None)

Parameters

This method accepts the following parameters −

  • x − Input frequency-domain data.

  • type − Type of IDCT (1, 2, 3, or 4; default is 2). Matches the type of the DCT used.

  • n − Length of the transform; pads or truncates input data if specified.

  • axis − Axis for the IDCT in multi-dimensional arrays (default: last axis).

  • norm − The normalization mode allows control over scaling: "backward" (default, no scaling), "ortho" (unitary scaling for energy preservation), and "forward" (scales the output by 1/n, where n is the transform length).

  • overwrite_x − Modifies input to save memory if True. Fact: IDCT, when paired with DCT, provides perfect reconstruction of the original data if no coefficients are modified.

  • workers − Number of threads to use for parallel computation. Default is 1.

  • orthogonalize − Allows explicit orthogonalization for advanced use cases. Default is None.

Return Value

idct (ndarray of real) − The IDCT-transformed array in the spatial domain.

Example 1: Basic IDCT Transformation

This example shows how the IDCT converts frequency-domain data back to its original spatial format. It reverses the DCT transformation to retrieve the input array.

import numpy as np
import scipy.fft
from scipy.fft import idct

freq_data = [20, -6.30864406, 0, -0.44834153]
result = scipy.fft.idct(freq_data)
print("Reconstructed data using IDCT:", result)

When we run above program, it produces following result

Reconstructed data using IDCT: [1. 2. 3. 4.]

Example 2: IDCT on 2D Arrays

This demonstrates how IDCT handles multi-dimensional data by making the transformation row-wise onto a 2D array by using the axis parameter. Each row is mapped back into its spatial format.

The idct() method is used on each row of a 2D array, with an axis=-1 parameter to make the reconstruction of a 2D array so that each row is again mapped back into its original form.

import numpy as np
from scipy.fft import idct

freq_data = np.array([[12, -3.46410162, -4.44089210e-16], [30, -3.46410162, -4.44089210e-16]])
result = idct(freq_data, axis=-1)
print("Reconstructed 2D data using IDCT:\n", result)

Output of the above code is as follows −

Reconstructed 2D data using IDCT:
 [[1. 2. 3.]
 [4. 5. 6.]]

Example 3: Handling Errors with Unsupported Data Types in IDCT

IDCT takes real numbers; either integers or floats are acceptable. Any other type of data, such as a string or complex integer, will result in a TypeError.

In the below code, idct() method is given a string array, and an error is returned. The IDCT can be correctly calculated using proper input of real values.

from scipy.fft import idct
import numpy as np

# Correct input: real numbers
freq_data = np.array([1, 2, 3], dtype=float)  # Ensure numerical input
result = idct(freq_data)
print("Corrected IDCT of data:", result)

# Incorrect input: array of strings
data = ["a", "b", "c"]  # Invalid input
try:
   data_array = np.array(data, dtype=float)  # Attempt conversion to NumPy float array
   result = idct(data_array)
   print("IDCT of data:", result)
except ValueError as e:  # ValueError occurs when converting strings to float
   print("Error:", e)  # Expected error due to invalid input type

Output of the above code is as follows −

Corrected IDCT of data: [ 1.24401694 -0.83333333  0.0893164 ]
ValueError: could not convert string to float: 'a'

Example 4: Combining DCT and IDCT for Reconstruction

This example shows how combining DCT with IDCT leads to perfect reconstruction of the original data.

In the below example, the original data is altered by dct() and then rebuilt with idct. The final output is the original data, which proves the lossless nature of these procedures.

from scipy.fft import dct, idct

original_data = [1, 2, 3, 4]
freq_data = dct(original_data, norm='ortho')
reconstructed_data = idct(freq_data, norm='ortho')

print("Original Data:", original_data)
print("Frequency Data (DCT):", freq_data)
print("Reconstructed Data (IDCT):", reconstructed_data)

Output of the above code is as follows −

Original Data: [1, 2, 3, 4]
Frequency Data (DCT): [ 5.         -2.2304425   0.         -0.15851267]
Reconstructed Data (IDCT): [1. 2. 3. 4.]
scipy_discrete_fourier_transform.htm
Advertisements