SciPy - irfft() Function



SciPy's irfft() method calculates the Inverse Real Fast Fourier Transform that converts frequency-domain data into the real-valued time or spatial domain. It does this in an efficient manner, eliminating the unnecessary negative frequency terms. This makes it ideal for processing real-valued datasets in numerous scientific and technical applications.

If the given input carries negative frequencies, then the irfft algorithm will compute them as conjugate versions of positive frequencies that appear like a real-valued signal. This assumption ensuresconsistency for real inputs derived from rfft.

This method is more commonly used for recovery from frequency-domain filtering or operations in signal processing, imaging reconstruction, and spectrum analysis.

The norm option controls scaling, for example, "forward" for proportional scaling, overwrite_x allows you to overwrite the input data to have better performance in large scales.

Syntax

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

.irfft(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 array representing real-valued frequency-domain data.

  • n (int, optional) − Length of the inverse FFT output. If greater than the input size, the data is padded; if smaller, it is truncated.

  • axis (int, optional) − Axis along which to compute the inverse FFT. Defaults to the last axis (-1).

  • norm (backward, ortho, forward), 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 (object, optional) − Precomputed plan for FFT optimization (advanced use). Default is None.

Return Value

out ndarray − Returns a real-valued time or spatial domain signal reconstructed from the input frequency-domain data.

Example 1: irfft() with 'n' parameter

This is the basic example of SciPy irfft() which computes the inverse of the real Fast Fourier Transform (RFFT) from frequency domain to time or spatial domain, with the n parameter which specifies the output length.

import scipy.fft
import numpy as np

# Frequency-domain data (result of rfft)
frequency_data = np.array([8 + 0j, 0 + 0j, 0 + 0j])

# Reconstruct the original time-domain signal
time_data = scipy.fft.irfft(frequency_data, n=4)

print("Reconstructed Signal:", time_data)

When we run above program, it produces following result

Reconstructed Signal: [2. 2. 2. 2.]

Example 2: Comparison of ifft() and irfft()

SciPy's ifft reconstructs a signal from a full complex input including positive and negative frequencies and returns as a complex output. Instead, irfft takes as input a real-valued input from rfft, which does not include the negative frequencies and returns purely real output.

In the above code, ifft works on the entire frequency data, whereas irfft focuses on the real inputs, which reduces redundant calculations and improves efficiency for real-valued datasets.

import scipy.fft

# Using ifft for full inverse FFT on a complex input
ifft_result = scipy.fft.ifft([2, -2j, -2, 2j])

# Using irfft for inverse FFT on real-valued frequency data
irfft_result = scipy.fft.irfft([2, -2j, -2])

print("IFFT Result:", ifft_result)
print("IRFFT Result:", irfft_result)

Following is an output of the above code −

IFFT Result: [0.+0.j 2.+0.j 0.+0.j 0.+0.j]
IRFFT Result: [0. 2. 0. 0.]

Example 3: Handling Invalid Input in irfft()

Input should be numeric; otherwise, an Value error will be generated when using irfft() method.

The following code illustrates why data type validation of input values is important when calling the irfft() method.

import scipy.fft

# Invalid input with non-numeric values
frequency_data = [10, 'a', -5]

try:
    scipy.fft.irfft(frequency_data)
except ValueError as e:
    print("Error:", e)

Output of the above code is as follows −

ValueError: could not convert string to float: 'a'

Example 4: Using norm="forward" for Proper Amplitude Scaling

The norm="forward" argument in irfft from SciPy ensures that the amplitude of the reconstructed signal directly maps to the frequency domain with proportional energy. This is an important consideration for any application requiring proper amplitude scaling.

The below code, reconstructs a real-valued signal from frequency data using irfft() and forward normalization, which makes it ideal for the accurate signal processing task.

import scipy.fft
import numpy as np

a = [20, -10+5j, -5]
reconstructed_signal = scipy.fft.irfft(a, norm="forward")
print("Reconstructed Signal with Forward Norm:", reconstructed_signal)

Output of the above code is as follows −

Reconstructed Signal with Forward Norm: [-5. 15. 35. 35.]

Example 5: irfft with overwrite_x = True parameter

The overwrite_x = True parameter of irfft() in SciPy instructs the function to reuse the memory of the input array for the computation, which improves performance by reducing memory usage. This is useful for large datasets and performance-critical applications.

The method makes use of irfft to reconstruct a real-valued signal from frequency data, maximizing memory efficiency by allowing input overwriting throughout the transformation.

import scipy.fft
import numpy as np

data = np.array([10, -4+2j, -6], dtype=complex)
reconstructed_signal = scipy.fft.irfft(data, overwrite_x=True)
print("Reconstructed Signal:", reconstructed_signal)

Output of the above code is as follows −

Reconstructed Signal: [-1.  3.  3.  5.]
scipy_discrete_fourier_transform.htm
Advertisements