
- 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 - hfft() Function
The method Scipy.fft.hfft() is used to calculate the Hermitian FFT. This method transforms Hermitian symmetry data in the frequency domain into real-valued time-domain signals.
When you use hfft, ensure your input array has Hermitian symmetry. If you lose this symmetry, your output might not display a valid real-valued signal. In addition, incorrect specification of the axis can give IndexError messages.
The n parameter in this method handles cutting (n > m) or adding zeros (n < m) to enhance frequency resolution or adapt to specific output lengths where n is output length and m is input length.
The SciPy - hfft() function assists scientists, engineers, and coders that work on real-valued signal rebuilding. It works well with methods like ihfft to check if signal changes can be reversed. This method often useful to work on audio, images, or scientific data to rebuild real-valued signals.
Syntax
The syntax for the SciPy hfft() method is as follows −
.hfft(x, n=None, axis=-1, norm=None, overwrite_x=False, workers=None, *, plan=None)
Parameters
This method accepts the following parameters −
x (array_like) − Input frequency data, assumed Hermitian symmetric for real-valued time-domain results.
n (int, optional) − Output length. Zero-padded if larger than input's length, truncated if smaller. Defaults to 2*(m-1) where m is input length.
axis (int, optional) − Axis for FFT computation. Defaults to -1 (last axis).
norm (backward, ortho, forward), optional − The FFT result is scaled, with options "backward" (default, no scaling), "ortho" (orthonormal scaling), or "forward" (scales the forward transform).
overwrite_x (bool, optional) − Overwrites input to save memory if True. Default is False.
workers (int, optional) − Number of parallel workers. Defaults to single-threaded unless specified.
plan (object, optional) − For advanced optimizations; not widely used in SciPy.
Return Value
The transformed input, truncated or zero-padded as specified, along the indicated axis. The length of the converted axis is defined by n, which defaults to 2*(m - 1).
Example 1: Computing FFT with Hermitian Symmetry
An array with Hermitian symmetry will provide a real-valued output in the frequency spectrum hfft. If an array does not follow Hermitian symmetry, it would give complex values in response or may lead to unexpected results.
The below code is the basic example of scipy.fft.hfft() method where we have created a hermitian array to compute Fast Fourier Transform (FFT) of a signal resulting real-valued frequency spectrum.
import numpy as np from scipy.fft import hfft # Hermitian symmetric input x = np.array([1 + 0j, 2 - 1j, 3 + 0j, 2 + 1j, 1 - 0j]) result = hfft(x) print("Output for Hermitian symmetric input:", result)
When we run above program, it produces following result
Output for Hermitian symmetric input: [16. 0. -8. 0. 0. 0. 0. 0.]
Example 2: Non-Hermitian input in hfft
If the input array does not have Hermitian symmetry, hfft returns complex or inconsistent values compared to the real FFT.
In the code below, we built a non-Hermitian symmetric array and made use of the hfft() method to see how the output deviates from the expected real-valued spectrum showing inconsistency in terms of a lack of symmetry in the input.
import numpy as np from scipy.fft import hfft # Non-Hermitian symmetric input x = np.array([1 + 0j, 2 - 1j, 3 + 0j, 4 + 1j]) result = hfft(x) print("Output for non-Hermitian symmetric input:", result)
Following is an output of the above code
Output for non-Hermitian symmetric input: [15. -5.73205081 -1.73205081 -1. 1.73205081 -2.26794919]
Example 3: Frequency resolution with SciPy hfft
The value of n in hfft defines the length of the output. If n m, the input is padded with zeros (zero-padding), thus increasing the frequency resolution without adding new information.
In the code below, we created an array and used the hfft() method with an output length of (n). For n=3 (that is, n
import numpy as np from scipy.fft import hfft x = np.array([1, 2, 3, 2, 1]) result = hfft(x,n=3) result1 = hfft(x, n=6) print("Truncated result (n < m):", result) print("Zero-padded result (n > m):", result1)
Output of the above code is as follows
Truncated result (n < m): [ 5. -1. -1.] Zero-padded result (n > m): [13. -2. -2. 1. -2. -2.]
Example 4: Axis Mismatch Error in hfft
If the specified axis is greater than the array's dimensions, an IndexError is raised.
The below code attempts to compute hfft along a non-existent axis (axis=2) for a 1-dimensional array, raising an IndexError.
import numpy as np from scipy.fft import hfft x = np.array([4 + 0j, 3 - 2j, 1 + 0j, 3 + 2j, 4 - 0j]) try: result = hfft(x, axis=2) except IndexError as e: print(f"IndexError: {e}")
Output of the above code is as follows
IndexError: tuple index out of range
Example 5: hfft and ihfft reversible transformation
To test the integrity of a signal processing pipeline, scientists often mix hfft() with ihfft() to verify that the transformations are reversible.
This code makes use of hfft to convert a Hermitian symmetric frequency spectrum into a time-domain signal, then of ihfft to retrieve the original frequency spectrum. The transformation is thus reversible.
import numpy as np from scipy.fft import hfft, ihfft # Hermitian symmetric frequency spectrum freq_spectrum = np.array([10 + 0j, 5 - 2j, 3 + 0j, 5 + 2j, 10 - 0j]) # Convert to time-domain signal time_signal = hfft(freq_spectrum) # Reconstruct the frequency spectrum reconstructed_spectrum = ihfft(time_signal) print("Original Frequency Spectrum:", freq_spectrum) print("Reconstructed Frequency Spectrum:", reconstructed_spectrum)
Output of the above code is as follows
Original Frequency Spectrum: [10.+0.j 5.-2.j 3.+0.j 5.+2.j 10.+0.j] Reconstructed Frequency Spectrum: [10.+0.j 5.-2.j 3.+0.j 5.+2.j 10.+0.j]