
- 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 - 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]]