
- SciPy - Home
- SciPy - Introduction
- SciPy - Environment Setup
- SciPy - Basic Functionality
- SciPy - Relationship with NumPy
- SciPy Clusters
- SciPy - Clusters
- SciPy - Hierarchical Clustering
- SciPy - K-means Clustering
- SciPy - Distance Metrics
- SciPy Constants
- SciPy - Constants
- SciPy - Mathematical Constants
- SciPy - Physical Constants
- SciPy - Unit Conversion
- SciPy - Astronomical Constants
- SciPy - Fourier Transforms
- SciPy - FFTpack
- SciPy - Discrete Fourier Transform (DFT)
- SciPy - Fast Fourier Transform (FFT)
- SciPy Integration Equations
- SciPy - Integrate Module
- SciPy - Single Integration
- SciPy - Double Integration
- SciPy - Triple Integration
- SciPy - Multiple Integration
- SciPy Differential Equations
- SciPy - Differential Equations
- SciPy - Integration of Stochastic Differential Equations
- SciPy - Integration of Ordinary Differential Equations
- SciPy - Discontinuous Functions
- SciPy - Oscillatory Functions
- SciPy - Partial Differential Equations
- SciPy Interpolation
- SciPy - Interpolate
- SciPy - Linear 1-D Interpolation
- SciPy - Polynomial 1-D Interpolation
- SciPy - Spline 1-D Interpolation
- SciPy - Grid Data Multi-Dimensional Interpolation
- SciPy - RBF Multi-Dimensional Interpolation
- SciPy - Polynomial & Spline Interpolation
- SciPy Curve Fitting
- SciPy - Curve Fitting
- SciPy - Linear Curve Fitting
- SciPy - Non-Linear Curve Fitting
- SciPy - Input & Output
- SciPy - Input & Output
- SciPy - Reading & Writing Files
- SciPy - Working with Different File Formats
- SciPy - Efficient Data Storage with HDF5
- SciPy - Data Serialization
- SciPy Linear Algebra
- SciPy - Linalg
- SciPy - Matrix Creation & Basic Operations
- SciPy - Matrix LU Decomposition
- SciPy - Matrix QU Decomposition
- SciPy - Singular Value Decomposition
- SciPy - Cholesky Decomposition
- SciPy - Solving Linear Systems
- SciPy - Eigenvalues & Eigenvectors
- SciPy Image Processing
- SciPy - Ndimage
- SciPy - Reading & Writing Images
- SciPy - Image Transformation
- SciPy - Filtering & Edge Detection
- SciPy - Top Hat Filters
- SciPy - Morphological Filters
- SciPy - Low Pass Filters
- SciPy - High Pass Filters
- SciPy - Bilateral Filter
- SciPy - Median Filter
- SciPy - Non - Linear Filters in Image Processing
- SciPy - High Boost Filter
- SciPy - Laplacian Filter
- SciPy - Morphological Operations
- SciPy - Image Segmentation
- SciPy - Thresholding in Image Segmentation
- SciPy - Region-Based Segmentation
- SciPy - Connected Component Labeling
- SciPy Optimize
- SciPy - Optimize
- SciPy - Special Matrices & Functions
- SciPy - Unconstrained Optimization
- SciPy - Constrained Optimization
- SciPy - Matrix Norms
- SciPy - Sparse Matrix
- SciPy - Frobenius Norm
- SciPy - Spectral Norm
- SciPy Condition Numbers
- SciPy - Condition Numbers
- SciPy - Linear Least Squares
- SciPy - Non-Linear Least Squares
- SciPy - Finding Roots of Scalar Functions
- SciPy - Finding Roots of Multivariate Functions
- SciPy - Signal Processing
- SciPy - Signal Filtering & Smoothing
- SciPy - Short-Time Fourier Transform
- SciPy - Wavelet Transform
- SciPy - Continuous Wavelet Transform
- SciPy - Discrete Wavelet Transform
- SciPy - Wavelet Packet Transform
- SciPy - Multi-Resolution Analysis
- SciPy - Stationary Wavelet Transform
- SciPy - Statistical Functions
- SciPy - Stats
- SciPy - Descriptive Statistics
- SciPy - Continuous Probability Distributions
- SciPy - Discrete Probability Distributions
- SciPy - Statistical Tests & Inference
- SciPy - Generating Random Samples
- SciPy - Kaplan-Meier Estimator Survival Analysis
- SciPy - Cox Proportional Hazards Model Survival Analysis
- SciPy Spatial Data
- SciPy - Spatial
- SciPy - Special Functions
- SciPy - Special Package
- SciPy Advanced Topics
- SciPy - CSGraph
- SciPy - ODR
- SciPy Useful Resources
- SciPy - Reference
- SciPy - Quick Guide
- SciPy - Cheatsheet
- SciPy - Useful Resources
- SciPy - Discussion
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]]