SciPy - rfft() Function



The rfft() function in SciPy calculates the 1-dimensional Fast Fourier Transform (FFT) for real-valued input data. It produces the non-redundant positive frequency components. This approach takes advantage of symmetry to reduce computation time and memory usage, which makes it effective for real-world signals.

Rfft() method is widely applied in signal processing audio analysis and data compression. Real-valued datasets have to be understood or manipulated for frequency components in these fields. It will help in tasks like filtering, noise removal, and extracting spectral features.

Users often encounter errors when working with rfft(). Mismatched input dimensions when specifying the axis parameter can lead to ValueError. Using complex input instead of real-valued data might result in unexpected behavior or wrong outputs.

The n parameter has an impact on the FFT's length letting you cut it short or add zeros to pad it. The norm parameter adjusts the scaling ("backward", "forward", "ortho"), and overwrite_x improves speed by using the input array during computation.

Syntax

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

.rfft(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 precomputed FFT plans for performance optimization.

Return Value

Returns a complex-valued array representing the positive frequency components of the input real-valued data.

Example 1

This is the basic example of rfft() method where it computes a complex-valued array from the real-valued data. Following is the code −

import scipy.fft
import numpy as np

# Real-valued input data
x = np.array([1, 2, 3, 4])

# Compute the 1D RFFT
frequency_data = scipy.fft.rfft(x)

print("Frequency Domain Data:", frequency_data)

When we run above program, it produces following result

Frequency Domain Data: [10.+0.j -2.+2.j -2.+0.j]

Example 2

The rfft() method is especially useful for finding out frequencies of real-world data like detection of significant frequencies in a signal from heartbeat for medical application.

This code produces the simulation of a heartbeat signal with an array of the real-valued time domain. It then applies the rfft() to transform this signal into the frequency domain.

import scipy.fft
import numpy as np

heartbeat_signal = np.array([0.2, 0.8, 1.0, 0.6, 0.3, 0.1, 0.05, 0.1])
frequency_data = scipy.fft.rfft(heartbeat_signal)

print("Frequency Components of Heartbeat Signal:", frequency_data)

Following is an output of the above code −

Frequency Components of Heartbeat Signal: [ 3.15      +0.j          0.04142136-1.79852814j -0.55      -0.2j
 -0.24142136+0.10147186j -0.05      +0.j        ]

Example 3

The rfft() expects the input data to be real valued; passing complex-valued data triggers TypeError or produces incorrect results.

This code illustrates an error example in which the input is complex valued while given to rfft(); therefore, checking data consistency prior to using the function is crucial.

import scipy.fft
import numpy as np

# Invalid complex-valued input
invalid_data = np.array([1 + 2j, 3 + 4j, 5 + 6j])

try:
    # Attempt to compute RFFT on invalid input
    frequency_data = scipy.fft.rfft(invalid_data)
except TypeError as e:
    print("Error:", e)

Output of the above code is as follows −

TypeError: x must be a real sequence
scipy_discrete_fourier_transform.htm
Advertisements