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, nm, zero-padding enhances the frequency resolution of the FFT without adding new information into the signal.

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]
scipy_discrete_fourier_transform.htm
Advertisements