SciPy - hfft2() Function



SciPy has a function named hfft2(), that calculates the 2D Hermitian Fast Fourier Transform, transforming a Hermitian symmetric complex array into a real-valued 2D array in the frequency domain.

hfft2 approach is very efficient when applied to image processing, medical imaging applications like MRI, and other physics simulations, where data is often Hermitian symmetric. It efficiently manipulates 2D arrays, which makes it very suitable for spatial pattern studies, spectrum filtering, and reconstructing real-valued signals from their frequency-domain representations. hfft2 is normally combined with ihfft2() to have an exact translation between the time and frequency domains. It may further be used in combination with fftshift and ifftshift such that the output may be reshuffled for better display and handling.
The hfft() calculates the 1-D Hermitian FFT on real-valued inputs, in contrast hfft2 extends similar functionality to two-dimensional datasets such as pictures, where one can get fast multidimensional data processing.

Syntax

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

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

Parameters

This method accepts the following parameters −

  • x (array) − Input array, assumed to be Hermitian complex.

  • s (sequence of ints, optional) − Shape of the real output (optional).

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

  • norm {backward, ortho, forward}, optional − Normalization mode; options are "backward", "ortho", or "forward" (optional).

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

  • workers (int, optional) − Maximum number of workers for parallel computation (optional).

  • plan (object, optional) − Reserved for passing a precomputed plan provided by downstream FFT vendors (optional).

Return Value

A real-valued ndarray representing the result of the 2-D Hermitian complex to real FFT.

Example 1: Computing 2D Hermitian FFT Using hfft2

The below example demonstrates the basic usage of hfft2() method where it computes the 2D FFT of Hermitian symmetric complex array. Following is the code −

import numpy as np
from scipy.fft import hfft2

hermitian_array = np.array([[1 + 0j, 2 - 1j], [2 + 1j, 1 - 0j]])
real_output = hfft2(hermitian_array)

print("Real-valued output from hfft2:", real_output)

When we run above program, it produces following result

Real-valued output from hfft2: [[ 6.  0.]
 [ 0. -2.]]

Example 2: Transformation with hfft2 and ihfft2

hfft2 transforms the 2D real-valued array to the frequency domain, while ihfft2 reconstructs it back into the spatial domain preserving its original structure.

The below example shows how hfft2() takes a 2D real-valued array and makes a Hermitian symmetric representation in the frequency domain and ihfft2 reconstructs the array back into the spatial domain.

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

data = np.array([[1, 2], [3, 4]])
freq_data = hfft2(data)

# Reconstruct the original 2D data
reconstructed_data = ihfft2(freq_data)

print("Original Data:\n", data)
print("Frequency Domain Data:\n", freq_data)
print("Reconstructed Data:\n", reconstructed_data)

Following is an output of the above code

Original Data:
 [[1 2]
 [3 4]]
Frequency Domain Data:
 [[10. -2.]
 [-4.  0.]]
Reconstructed Data:
 [[1.+0.j 2.+0.j]
 [3.+0.j 4.+0.j]]

Example 3: Mismatched Axes

When we provide axes parameter that are larger than the dimensions of the input array, hfft2 raises an value error, ensuring that computation is restricted to valid axes only within the structure of the array.

In the below code, we present valid and invalid axes for hfft2. Valid axis work on the input array successfully, while invalid axes raise a ValueError, highlighting the importance of matching the axes argument to the dimensionality of the array. Following is the code −

import numpy as np
from scipy.fft import hfft2

data = np.random.rand(2, 2)

# Specify valid axes for hfft2

result = hfft2(data, axes=(0, 1))
print("Result:\n", result)

# Specify an invalid axis for hfft2
try:
    result = hfft2(data, axes=(2, 3))  # Invalid axes
except IndexError as e:
    print(f"valueError: {e}")

Following is an output of the above code

Result:
 [[ 1.91341583  0.96181243]
 [-0.56504988  0.19047427]]
--------------------------------

ValueError: axes exceeds dimensionality of input

Example 4: Invalid Shape or Mismatch

The s parameter in the hfft2() method automatically handles mismatched output shapes internally by reducing the information if dimensions are smaller or zero-padding if the dimensions are large.

In the below code we have created a 2D array with rand function then applied hfft2() along with s parameter with larger dimensions from the input array (i.e, s = 6,4) here the data will be zero-padded to match the specified shape. Following is the code −

import numpy as np
from scipy.fft import hfft2

# Create a 2D array
data = np.random.rand(5, 5)

# Specify an output shape that doesn't match the input
result = hfft2(data, s=(6, 4))  # Shape mismatch is handled internally
print("Result Shape:", result.shape)
print("Result:\n", result)

Following is an output of the above code

Result Shape: (6, 4)
Result:
 [[ 6.93250933e+00 -5.35237642e-01  1.87303292e+00 -5.35237642e-01]
 [-1.83510022e+00 -4.13356553e-01 -2.95109771e-03  1.86774305e-01]
 [-1.00352642e+00 -1.19234833e+00  5.38727120e-01 -1.25334602e+00]
 [ 3.92572643e+00 -3.64064124e-02  8.99516265e-02 -3.64064124e-02]
 [-1.00352642e+00 -1.25334602e+00  5.38727120e-01 -1.19234833e+00]
 [-1.83510022e+00  1.86774305e-01 -2.95109771e-03 -4.13356553e-01]]
scipy_discrete_fourier_transform.htm
Advertisements