SciPy - hfftn() Function



SciPy's hfftn() function calculates the n-dimensional Hermitian Fast Fourier Transform that transforms real-valued n-dimensional input into a Hermitian symmetric frequency domain representation. It is used to handle multidimensional data, with real input, as efficiently as possible and reduces redundancy in the frequency domain.

The hfftn() finds its applications in scientific computing in multidimensional spectrum analysis, image processing, and data compression.

Errors with hfftn are usually due to incorrect parameter or input data. For instance, specifying axes that are larger than the size of the input array leads to a ValueError, and using non-real input data leads to a TypeError. Make sure Hermitian symmetry and proper axis setup to avoid these problems.

The optional parameters for hfftn are s, axes, and norm. The s parameter specifies the shape of the output. Axes define the dimensions for the transformation. The norm parameter controls normalizing and uniformity in amplitude scaling.

Syntax

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

.hfftn(x, s=None, axes=None, norm=None, overwrite_x=False, workers=None, *, plan=None)

Parameters

This method accepts the following parameters −

  • x (array_like) − Input n-dimensional real-valued data to be transformed.

  • s (sequence of ints, optional) − Shape of the real output. If not provided, the shape of x is used. Data is truncated or zero-padded to match.

  • axes (sequence of ints, optional) − Axes over which the transform is computed. Defaults to all axes.

  • norm ({"backward", "ortho", "forward"}, optional) − Controls the normalization mode. Default is "backward". Use "ortho" for energy-preserving transforms.

  • overwrite_x (bool, optional) − If True, the input array can be overwritten, saving memory.

  • workers (int, optional) − Number of workers for parallel computation. If negative, 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 n-dimensional Hermitian frequency-domain representation of the input array. This is a simplified representation, taking only the unique values to reconstruct the real-valued input.

Example 1

The hfftn() performs the Hermitian FFT on n-dimensional real-valued data with low processing overhead, producing a representation in the frequency domain.

This example demonstrates how hfftn transforms 3D real-valued data into its Hermitian symmetric frequency representation, which is required for spectral analysis in volumetric data.

import numpy as np
import scipy.fft
from scipy.fft import hfftn

# 3D real-valued data
data = np.random.rand(2, 2, 2)

# Compute the 3D Hermitian FFT
freq_data = scipy.fft.hfftn(data)

print("Frequency-Domain Data:\n", freq_data)

When we run above program, it produces following result

Frequency-Domain Data:
 [[[ 3.45881529 -0.19113868]
  [ 0.37285173  0.437461  ]]

 [[-0.95094236 -1.47237476]
  [-1.70602049  0.46563807]]]

Example 2

The following code demonstrates how hfftn() method employs zero-padding with an s option to increase the frequency domain output dimensions. Zero-padded techniques improve the frequency resolution without adding new information. This is actually very helpful for detailed analysis of the spectrum.

import numpy as np
from scipy.fft import hfftn

# 3D real-valued data
data = np.random.rand(3, 3, 3)

# Apply zero-padding
freq_data = hfftn(data, s=(4, 4, 4))

print("Zero-Padded Frequency-Domain Data:\n", freq_data)

Following is an output of the above code −

Zero-Padded Frequency-Domain Data:
 [[[16.15715176 -0.80459333  1.82740225 -0.80459333]
  [ 0.63046021  0.35040934  0.52437894  1.62040128]
  [ 7.92447416 -0.51931855 -3.86529146 -0.51931855]
  [ 0.63046021  1.62040128  0.52437894  0.35040934]]

 [[-0.9179746  -0.75889162  1.83332969  2.08478586]
  [-3.43269106  1.30157545 -3.21867864  0.22135286]
  [-1.33324879 -2.51410327  1.74159842 -0.25501923]
  [-0.49765377  0.86678392 -0.8682348  -0.53698159]]

 [[ 5.79669463  1.79366467 -2.84569992  1.79366467]
  [ 0.4764279  -0.36742833 -2.11364146  0.31797018]
  [ 3.4340916   0.88581347 -3.83750594  0.88581347]
  [ 0.4764279   0.31797018 -2.11364146 -0.36742833]]

 [[-0.9179746   2.08478586  1.83332969 -0.75889162]
  [-0.49765377 -0.53698159 -0.8682348   0.86678392]
  [-1.33324879 -0.25501923  1.74159842 -2.51410327]
  [-3.43269106  0.22135286 -3.21867864  1.30157545]]]

Example 3

By providing axes option, hfftn can perform changes along certain dimensions, allowing for focussed analysis or manipulation of massive datasets.

This example shows that hfftn() transforms only some dimensions of a 3D dataset by using axes= 0,2 (along the first and third axes) which will be useful for cross-sections or filter application selectively on multi-dimensional data.

import numpy as np
from scipy.fft import hfftn

# 3D data for axis-specific transformation
data = np.random.rand(4, 4, 4)

# Apply FFT only along the first and third axes
freq_data = hfftn(data, axes=(0, 2))

# Limit the print size for concise output
np.set_printoptions(precision=2, suppress=True, threshold=10)

print("Transformed Data Along Axes (0, 2):\n", freq_data)

Output of the above code is as follows −

Transformed Data Along Axes (0, 2):
 [[[14.1  -0.72 -0.82  0.19 -0.82 -0.72]
  [13.19  2.96 -0.12 -1.59 -0.12  2.96]
  [11.02  1.08 -0.12 -2.83 -0.12  1.08]
  [10.92  1.    0.98  1.14  0.98  1.  ]]

 [[ 2.03  1.02 -0.95  0.17  2.06 -0.87]
  [-0.21  0.95  1.37 -2.02  0.88 -0.22]
  [-0.42 -0.73  1.48 -1.62  0.9   0.39]
  [-1.31 -0.95  1.88 -2.43 -0.09 -1.31]]

 [[ 1.4  -0.35 -1.08 -1.97 -1.08 -0.35]
  [ 0.58  0.26  1.39  0.94  1.39  0.26]
  [ 1.25 -0.07 -1.54 -3.24 -1.54 -0.07]
  [-0.36 -0.39  0.09 -1.13  0.09 -0.39]]

 [[ 2.03 -0.87  2.06  0.17 -0.95  1.02]
  [-0.21 -0.22  0.88 -2.02  1.37  0.95]
  [-0.42  0.39  0.9  -1.62  1.48 -0.73]
  [-1.31 -1.31 -0.09 -2.43  1.88 -0.95]]]
scipy_discrete_fourier_transform.htm
Advertisements