SciPy - rfft2() Function



The two-dimensional Fast Fourier Transform function rfft2() in the SciPy library takes input data as real numbers, returning only non-redundant positive frequency components. Thus, it efficiently translates the 2D data to their frequency domain, lowering down the computation time and required memory, especially for most datasets in real-world applications.

In image processing, 2D signal analysis, scientific computing, frequency-domain filtering, noise removal, and the finding pattern are some of the numerous applications of rfft2.

The norm parameter of rfft2 defines the scaling of the output by transformation. "Forward" scales the output to the input's energy, "backward" (default) keeps values unnormalized hence, contains raw results, while "ortho" scales for both forward and inverse transform such that the transforms are orthonormal. This offers the option of flexibility based on what one needs for analysis or processing of signals.

Syntax

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

.rfft2(x, s=None, axes=(-2, -1), norm=None, overwrite_x=False, workers=None, *, plan=None)

Parameters

This method accepts the following parameters −

  • x (array_like) − Input 2D array containing real-valued spatial-domain data.

  • s (sequence of ints, optional) − Shape of the output along each transformed axis. Pads or truncates the input data to match the specified shape.

  • axes sequence of ints, optional − Axes over which to compute the FFT. Defaults to the last two axes ((-2, -1)).

  • norm (str, optional) − The normalization mode allows 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 (plan, optional) − Reserved for precomputed FFT plans for performance optimization.

Return Value

out (ndarray) − The rfft2 method returns an array of complex values holding the two-dimensional Fourier Transform of the input real-valued data. This output contains only the non-redundant positive frequency terms for the last two axes, leveraging the symmetry of real-valued signals.

Example 1

This is the basic example of rfft2() method that returns the array of complex values in 2D of the real-valued data.

import scipy.fft
import numpy as np

# 2D input data
data = np.array([[1, 2], [3, 4]])
fft_result = scipy.fft.rfft2(data)
print("2D FFT Result:", fft_result)

When we run above program, it produces following result

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

Example 2

The axes parameter specifies the dimensions to be used in the FFT computation. If non-existent axes are provided, a ValueError is raised.

The following example demonstrates that using incorrect axes in rfft2() leads to an error, highlighting the importance of matching the axes with the data dimensions.

import scipy.fft
import numpy as np

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

# computing FFT on non-existent axes
try:
    fft_result = scipy.fft.rfft2(data, axes=(0, 2))
except ValueError as e:
    print("Error:", e)

Following is an output of the above code −

Error: axes exceeds dimensionality of input

Example 3

The norm = "backward" option does not scale during the transformation and returns the result unnormalized.

This example illustrates the fact that applying rfft2() using norm="backward" preserves the raw FFT results, without any amplitude change, that is useful for mathematical exact calculations.

import scipy.fft
import numpy as np

data = np.array([[1, 2], [3, 4]])
fft_result = scipy.fft.rfft2(data, norm="backward")
print("FFT Result with Backward Norm:", fft_result)

Output of the above code is as follows −

FFT Result with Backward Norm: [[10.+0.j -2.+0.j]
 [-4.+0.j  0.+0.j]]
scipy_discrete_fourier_transform.htm
Advertisements