SciPy - ihfft2() Function



SciPy's ihfft2 calculates the 2D inverse Hermitian Fast Fourier Transform that reconstructs real-valued spatial data from a Hermitian symmetric representation of its frequency domain.This approach is developed specifically to be used with the Hermitian symmetry, which then leads to proper and efficient transformation back into the spatial domain.

The ihfft2 is a useful tool for such image processing, medical imaging and physics simulation that needs some switching from spatial to the frequency and vice versa.

ihfft2 is often used along with methods like hfft2, fftshift and ifftshift to create signal and image processing workflows all together. For instance, hfft2 generates the data in the frequency domain, fftshift rearranges it for viewing or modifications, and ihfft2 reconstructs the original data. Such combinations make ihfft2 useful and efficient for applications involving both frequency-domain analysis as well as spatial reconstruction.

Syntax

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

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

Parameters

This method accepts the following parameters −

  • x (array_like) − Input array representing the Hermitian symmetric frequency domain data.

  • s (sequence of ints, optional) − Shape of the real input to the inverse FFT. If not specified, the shape of x is used.

  • axes (sequence of ints, optional)− Axes over which to compute the inverse FFT. Defaults to the last two axes.

  • norm ({"backward", "ortho", "forward"}, optional) − Normalization mode. Defaults to "backward".

  • overwrite_x (bool, optional) − If True, allows the contents of x to be destroyed to save memory. Defaults to False.

  • workers (int, optional) − Maximum number of workers to use for parallel computation. If negative, the value wraps around from os.cpu_count().

  • plan (object, optional) − Reserved for passing in a precomputed plan provided by downstream FFT vendors. Currently not used in SciPy.

Return Value

out (ndarray) − The result of the inverse real 2-D FFT, representing the reconstructed spatial domain data.

Example 1: Reconstructing 2D Spatial Data Using ihfft2

The ihfft2() reconstructs a real-valued 2D spatial domain signal from its Hermitian symmetric frequency domain form.

This example makes use of hfft2() to calculate the Hermitian symmetric frequency representation of a 2D real-valued dataset and ihfft2() to reconstruct the original data, demonstrating the feasibility of both transformations.

import numpy as np
from scipy.fft import hfft2, ihfft2

# Original 2D real-valued data
original_data = np.random.rand(2, 2)

# Forward transform to Hermitian symmetric frequency domain
freq_data = hfft2(original_data)

# Inverse transform to reconstruct the original spatial data
reconstructed_data = ihfft2(freq_data)

print("Original Data:\n", original_data)
print("Reconstructed Data:\n", reconstructed_data)

When we run above program, it produces following result

Original Data:
 [[0.38437874 0.61130806]
 [0.8485624  0.18304901]]
Reconstructed Data:
 [[0.38437874+0.j 0.61130806+0.j]
 [0.8485624 +0.j 0.18304901+0.j]]

Example 2: Handling Non-Hermitian Input Errors in ihfft2

If the input to ihfft2() method is not a real Hermitian symmetric array, it raises a TypeError and highlights the need for correct inputs.

The following code uses incorrect frequency data to help illustrate the TypeError raised when ihfft2 finds that the input is not Hermitian symmetric, which may help users determine input limitations.

import numpy as np
from scipy.fft import ihfft2

# Non-Hermitian frequency data
freq_data = np.array([[10 + 0j, 5 - 2j], [3 + 1j, 4 - 3j]])

# Attempt to use ihfft2 on invalid input
try:
    spatial_data = ihfft2(freq_data)
except TypeError as e:
    print(f"TypeError: {e}")

Following is an output of the above code −

TypeError: x must be a real sequence

Example 3: Combining ihfft2 with fftshift and ifftshift

Combining ihfft2 with fftshift and ifftshift allows to visualize and manipulate in frequency domain by centering zero-frequency component and then undoing it for exact reconstruction.

The following example illustrates how fftshift centers the frequency spectrum for easier interpretation and then ifftshift reverses it back to its original state before using ihfft2 to reconstruct the spatial data. This process is essential for applications like filtering and frequency-domain image processing.

import numpy as np
from scipy.fft import hfft2, ihfft2, fftshift, ifftshift

# 2D real-valued data
data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# Forward transform with hfft2
freq_data = hfft2(data)

# Shift zero-frequency component to the center
shifted_freq = fftshift(freq_data)

# Undo the shift and reconstruct the data with ihfft2
reconstructed_data = ihfft2(ifftshift(shifted_freq))

print("Original Data:\n", data)
print("Shifted Frequency Spectrum:\n", shifted_freq)
print("Reconstructed Data:\n", reconstructed_data)

Output of the above code is as follows −

Original Data:
 [[1 2 3]
 [4 5 6]
 [7 8 9]]
Shifted Frequency Spectrum:
 [[  0.           5.19615242 -18.          -5.19615242]
 [  0.          -6.          60.          -6.        ]
 [  0.          -5.19615242 -18.           5.19615242]]
Reconstructed Data:
 [[1. +0.j 2. +0.j 3. +0.j]
 [5.5+0.j 5. +0.j 7.5+0.j]
 [5.5+0.j 8. -0.j 7.5+0.j]]
scipy_discrete_fourier_transform.htm
Advertisements