SciPy - fft() Function



The scipy.fft.fft() function calculates the frequency parts of a signal. It does this by changing a signal from the time domain, which shows how a signal changes over time, to the frequency domain, which shows how much of each frequency is in the signal. The FFT is a quick way to look at and understand the many frequencies in a signal.

This method is useful to study and work with frequency data in signal processing audio analysis, and image processing. In science and engineering, it's also useful for convolutions solving complex math problems, and looking at patterns that repeat.

This fft() method is very efficient since it drastically lowers computational complexity as compared to naive DFT. It is flexible to use for many applications as it supports configurable lengths, axes, and multi-dimensional arrays.

Syntax

The syntax for the Scipy fft() method is as follows −

.fft(x, n=None, axis=-1, norm=None, overwrite_x=False, plan=None)

Parameters

This method accepts the following parameters −

  • x (array-like) − Input array (signal data).

  • n (int, optional) − Length of the output transform. Pads with zeros or truncates if needed.

  • axis (int, optional) − Axis along which to compute the FFT (default is the last axis).

  • norm (str, optional) − Normalization mode ("backward", "forward" or "ortho").

  • overwrite_x (bool, optional) − If True, allows modifying input x to save memory.

  • plan (plan, optional) − Reserved for pre-computed FFT plans for performance optimization.

Return Value

out(complex ndarray) − The DFT of the input array x, containing complex values that represent the frequency components of the signal.

Example 1: Basic FFT Calculation

The FFT gives the frequency components of a signal, showing the proportion of each frequency in the input array.

In the below code we calculates the FFT of a basic signal, shows the real and imaginary parts of the frequency components.

import numpy as np
from scipy.fft import fft

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

# Compute FFT
result = fft(x)
print("FFT Result:", result)

When we run above program, it produces following result −

FFT Result: [10.-0.j -2.+2.j -2.-0.j -2.-2.j]

Example 2: Zero-Padding for Higher Resolution

Zero-padding allows the FFT output to have finer frequency resolution by lengthening the input signal.

In this example, Zero-padding is used to construct a signal's FFT, which will produce more precise frequency data in the output array.

import numpy as np
from scipy.fft import fft

x = np.array([1, 2, 3, 4])
fft_x_padded = fft(x, n=8)
print("FFT with zero-padding:\n", fft_x_padded)

Following is an output of the above code −

FFT with zero-padding:
 [10.        -0.j         -0.41421356-7.24264069j -2.        +2.j
  2.41421356-1.24264069j -2.        -0.j          2.41421356+1.24264069j
 -2.        -2.j         -0.41421356+7.24264069j]

Example 3: FFT on Multi-Dimensional Arrays

In multi-dimensional arrays, FFT can be used along particular axes to alter signals one dimension at a time.

This example shows how to calculate the FFT along a 2D array's rows axis=1, individually converting each row into the frequency domain.

import numpy as np
from scipy.fft import fft

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

# Compute FFT along rows
fft_x_rows = fft(x, axis=1)
print("FFT along rows:\n", fft_x_rows)

Output of the above code is as follows

FFT along rows:
 [[ 6. -0.j        -1.5+0.8660254j -1.5-0.8660254j]
 [15. -0.j        -1.5+0.8660254j -1.5-0.8660254j]]

Example 4: Overwriting Input for Performance

Enabling overwrite_x can improve performance by reusing memory making it suitable for large datasets.

This example optimizes memory usage and processing speed for a large input array by computing the FFT with overwrite_x=True.

import numpy as np
from scipy.fft import fft

x = np.random.rand(1000)
fft_x = fft(x, overwrite_x=True)
print("FFT computed with overwrite_x=True.")

Output of the above code is as follows −

FFT computed with overwrite_x=True.
scipy_discrete_fourier_transform.htm
Advertisements