SciPy - ifft2() Function



The scipy.fft.ifft2() method performs 2D inverse discrete Fourier transform (IDFT) for any given 2D array, using the FFT method. It transforms data from the frequency domain to a spatial or time domain.

The following is an example that uses any input array, x. ifft2(fft2(x)) restores the original array and in numerical accuracy. In other words, this is multidimensional data, maximized in performance and accuracy.

The ifft2 function is widely used in any application that requires the analysis and reconstruction of 2D signals such as image processing, filtering, and audio spectrograms.

The ifft2() method returns the inverse FFT for 2D arrays, making it appropriate for images and 2D signals. In contrast, the ifft() method can use 1D arrays, for example time series or audio signals, to carry out analogous transformations in one dimension.

In the real-world scenario, this method proved to be effective in getting rid of high-frequency noises by using FFT for analysis, and IFFT for the reconstruction process, as well as compressing images that reject less relevant frequencies in frequency domain and restore the data using IFFT.

Syntax

The syntax for the SciPy ifft2() method is as follows −

.ifft2(x, s=None, axes=(-2, -1), norm=None, overwrite_x=False, workers=None, *, plan=None)

Parameters

This method accepts the following parameters −

  • x − Input 2D array (can be complex) representing frequency domain data.Shape of the output along each axis. Pads with zeros or truncates the input. If not specified, the input shape is used.

  • s − Shape of the output along each axis. Pads with zeros or truncates the input. If not specified, the input shape is used.

  • axes − Axes over which to compute the inverse FFT. Defaults to the last two axes.

  • norm − The normalization mode for IFFT 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 − If True, allows modifying the input array for memory efficiency. Default is False.

  • workers − Number of workers for parallel computation. Defaults to using all available cores.

  • plan − A precomputed plan for FFT, used to optimize repetitive computations. Not actively used in standard SciPy FFT.

Return Value

out(complex ndarray) The reconstructed 2D array in the spatial domain (real values represent the signal).

Example 1

This is the basic example of ifft2() method where we compute the inverse FFT for identity matrix. Following is the code −

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

# Create a 2D identity matrix
x = 4 * np.eye(4)

# Compute the 2D Inverse FFT
result = scipy.fft.ifft2(x)
print("Inverse FFT Result:\n", result)

When we run above program, it produces following result

Inverse FFT Result:
 [[1.-0.j 0.+0.j 0.-0.j 0.-0.j]
 [0.+0.j 0.+0.j 0.+0.j 1.-0.j]
 [0.-0.j 0.-0.j 1.-0.j 0.+0.j]
 [0.-0.j 1.+0.j 0.-0.j 0.-0.j]]

Example 2

This code demonstrates the application of a low-pass filter to a 2D signal, retaining only the low-frequency components and setting the rest to zero.

In below code, freq_signal[1:, 1:] = 0. ensures that only the first frequency (low-frequency corner) is retained and higher frequencies are deleted. The filtered signal is then reconstructed by the inverse FFT ifft2, showing how removing high frequencies smooths down the data.

import numpy as np
from scipy.fft import fft2, ifft2

# Create a simple 2D signal
signal = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]])

# Transform to frequency domain
freq_signal = fft2(signal)

# Apply a low-pass filter (keep only low frequencies)
freq_signal[1:, 1:] = 0

# Reconstruct the filtered signal
reconstructed = ifft2(freq_signal).real
print("Filtered Signal:\n", reconstructed)

Following is an output of the above code −

Filtered Signal:
 [[ 1.  2.  3.  4.]
 [ 5.  6.  7.  8.]
 [ 9. 10. 11. 12.]
 [13. 14. 15. 16.]]

Example 3

This code shows the compression of an image by retaining only the low-frequency components of a two-dimensional image in the frequency domain. It sets the higher frequency components (responsible for finer details) to zero.

The image is then reconstituted using ifft2(), which displays how much of the original image can be represented by only the low frequencies.

import numpy as np
from scipy.fft import fft2, ifft2

# Create a small 2D image
image = np.random.rand(4,4)

# Transform to frequency domain
freq_image = fft2(image)

# Keep only low frequencies (compress)
compressed_freq = np.zeros_like(freq_image)
compressed_freq[:4, :4] = freq_image[:4, :4]

# Reconstruct the compressed image
compressed_image = ifft2(compressed_freq).real
print("Compressed Image:\n", compressed_image)

Output of the above code is as follows

Compressed Image:
 [[0.90369036 0.12522491 0.31535467 0.98059885]
 [0.28058477 0.74990898 0.79490161 0.35299132]
 [0.47120366 0.35896801 0.21072606 0.08047562]
 [0.18525763 0.72736845 0.68369767 0.47155035]]
scipy_discrete_fourier_transform.htm
Advertisements