SciPy - fftn() Function



The scipy.fft.fftn() function calculates the N-dimensional Fast Fourier Transform (FFT) of an input array. This expands the FFT to higher dimensions enabling frequency analysis and manipulation of data with multiple dimensions.

This function proves valuable to process or analyze applications that need multidimensional data, like 3D images in medical imaging and CT scans multidimensional simulations, or 3D spectrograms in audio and signal processing.

Syntax

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

.fftn(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 array containing the data to be transformed.

  • s (sequence of ints, optional) − Shape of the output along each transformed axis. Pads or truncates the input array accordingly.

  • axes (sequence of ints, optional) − Axes over which to compute the FFT. Defaults to all axes.

  • norm (str, optional) − The normalization mode control over scaling: "backward" (default, no scaling), "ortho" (unitary scaling for energy preservation), and "forward" (scales the output by 1/n, where n is the transform length).

  • overwrite_x (bool, optional) − If True, allows the input array x to be overwritten for better performance. Default is False.

  • workers (int, optional) − Number of parallel workers to use for computation. Default is None, meaning a single thread is used.

  • plan (optional) − A precomputed plan for optimized repeated FFT computations (not commonly used).

Return Value

out(complex ndarray) − The N-dimensional FFT of the input array, containing complex numbers representing frequency components.

Example 1

This example shows how the fftn() function determines the N-dimensional Fast Fourier Transform of a basic 2D array.

The result is a complex array where each element represents a frequency's magnitude and phase in the frequency domain.

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

# Define a 2D input array
x = np.array([[1, 2], [3, 4]])

# Compute the N-Dimensional FFT
result = scipy.fft.fftn(x)
print("N-Dimensional FFT Result:\n", result)

When we run above program, it produces following result −

N-Dimensional FFT Result:
 [[10.-0.j -2.-0.j]
 [-4.-0.j  0.-0.j]]

Example 2

This example shows how fftn() with zero-padding increases the input array size to a required shape.

The output gains finer frequency resolution due to padding the array. The reason is that this step is essential for analyzing finer details in the frequency domain of signals or images.

import numpy as np
from scipy.fft import fftn

x = np.array([[1, 2], [3, 4]])

result = fftn(x, s=(4, 4))
print("FFT with Zero-Padding:\n", result)

Following is an output of the above code

FFT with Zero-Padding:
 [[10.-0.j  4.-6.j -2.-0.j  4.+6.j] 
 [ 3.-7.j -3.-5.j -1.+1.j  5.-1.j]
 [-4.-0.j -2.+2.j  0.-0.j -2.-2.j]
 [ 3.+7.j  5.+1.j -1.-1.j -3.+5.j]]

Example 3

This example demonstrates how to use fftn() to examine 3D medical images such as CT and MRI scans. Scientists can use the FFT to identify frequency components from 3D data, which can aid in spotting repeating structures or filtering out noise to improve display.

The function takes a 3D dataset, such as a medical scan, and uses fftn() to convert it to the frequency domain.

The frequency components from FFT can be used to eliminate strong high-frequency noise so the images are cleaner and diagnosed more accurately. It even helps in the identification of repeated structures and highlights different frequencies.

import numpy as np
from scipy.fft import fftn

# Create a 3D synthetic dataset (representing a medical scan)
medical_scan = np.random.rand(64, 64, 64)

# Compute the N-Dimensional FFT
freq_components = fftn(medical_scan)

print("Frequency components of the 3D medical scan computed.")

Output of the above code is as follows −

Frequency components of the 3D medical scan computed.
scipy_discrete_fourier_transform.htm
Advertisements