0
0
SciPydata~5 mins

Real FFT (rfft) in SciPy

Choose your learning style9 modes available
Introduction

Real FFT helps us find the frequency parts of a signal that has only real numbers. It is faster and simpler than the full FFT when the input is real.

Analyzing sound waves recorded as real numbers to find their main tones.
Checking vibrations in machines using sensor data that is real-valued.
Studying heartbeats from real-valued ECG signals to find patterns.
Finding repeating patterns in temperature data collected over time.
Syntax
SciPy
scipy.fft.rfft(x, n=None, axis=-1, norm=None, workers=None, overwrite_x=False)

x is the input array of real numbers.

n is the length of the FFT. If not given, it uses the length of x.

Examples
Basic use of rfft on a small real array.
SciPy
from scipy.fft import rfft
import numpy as np
x = np.array([1, 2, 3, 4])
result = rfft(x)
print(result)
Using rfft on a sine wave to see the main frequency components.
SciPy
from scipy.fft import rfft
import numpy as np
x = np.sin(2 * np.pi * 5 * np.linspace(0, 1, 100))
result = rfft(x)
print(result[:5])
Sample Program

This program creates a signal with two sine waves at 3 Hz and 7 Hz. It uses rfft to find the frequency parts and prints their amplitudes.

SciPy
from scipy.fft import rfft, rfftfreq
import numpy as np
import matplotlib.pyplot as plt

# Create a sample signal: 3 Hz and 7 Hz sine waves added
sampling_rate = 50  # samples per second
T = 1 / sampling_rate
t = np.linspace(0, 1, sampling_rate, endpoint=False)
signal = 0.7 * np.sin(2 * np.pi * 3 * t) + 0.3 * np.sin(2 * np.pi * 7 * t)

# Compute the real FFT
fft_result = rfft(signal)

# Get the frequencies for the FFT result
freqs = rfftfreq(len(signal), T)

# Print the frequencies and their amplitudes
for f, amp in zip(freqs, np.abs(fft_result)):
    print(f"Frequency: {f:.1f} Hz, Amplitude: {amp:.2f}")
OutputSuccess
Important Notes

rfft returns only the positive frequency parts because the input is real, so the negative frequencies are mirror images.

The output is a complex array showing amplitude and phase for each frequency.

Use rfftfreq to get the frequency values that match the rfft output.

Summary

Real FFT (rfft) finds frequency parts of real-valued signals efficiently.

It returns only half the spectrum because real signals have mirrored negative frequencies.

Use it to analyze real-world signals like sound, vibrations, or sensor data.