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.
Real FFT (rfft) in 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.
rfft on a small real array.from scipy.fft import rfft import numpy as np x = np.array([1, 2, 3, 4]) result = rfft(x) print(result)
rfft on a sine wave to see the main frequency components.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])
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.
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}")
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.
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.
Practice
scipy.fft.rfft function compute when applied to a real-valued signal?Solution
Step 1: Understand the input type
rfftis designed for real-valued input signals.Step 2: Analyze the output of
It returns only the positive frequency components because the negative frequencies are mirror images for real signals.rfftFinal Answer:
The positive frequency components of the signal's Fourier transform -> Option BQuick Check:
Real input -> half spectrum output [OK]
- Confusing rfft with full fft output
- Thinking rfft returns time-domain data
- Assuming rfft returns negative frequencies
rfft from SciPy's FFT module?Solution
Step 1: Recall correct import syntax
Therfftfunction is inscipy.fft, so import it directly from there.Step 2: Check usage
Callingrfft(signal)after import is the correct usage.Final Answer:
from scipy.fft import rfft\nresult = rfft(signal) -> Option CQuick Check:
Correct import and call syntax [OK]
- Trying to import rfft from scipy directly
- Using wrong module names
- Calling rfft as a method of scipy.fft without import
import numpy as np from scipy.fft import rfft signal = np.array([1, 0, -1, 0]) result = rfft(signal) print(np.round(result, 2))
Solution
Step 1: Understand the input signal
The signal is [1, 0, -1, 0], a simple 4-point real array.Step 2: Compute rfft manually or recall pattern
rfft returns N/2+1 = 3 complex values for length 4 input. The values correspond to DC, positive frequency, and Nyquist frequency components. The result is approximately [0.+0.j, 2.+0.j, 0.+0.j].Final Answer:
[0.+0.j 2.+0.j 0.+0.j] -> Option DQuick Check:
rfft length 4 -> 3 outputs, middle value 2 [OK]
- Expecting output length same as input
- Ignoring complex number format
- Misreading the signal values
rfft and fix it:import numpy as np from scipy.fft import rfft signal = [1, 2, 3, 4] result = rfft(signal, n=2) print(result)
Solution
Step 1: Check the
Thenparameter usagenparameter sets the length of the FFT. Here,n=2is less than the signal length 4, so the signal is truncated.Step 2: Fix the parameter to match or exceed signal length
Usingn=4ensures the full signal is transformed without truncation.Final Answer:
The parameternis smaller than signal length; usen=4instead -> Option AQuick Check:
FFT length must cover signal length [OK]
- Passing list instead of array (allowed but less efficient)
- Ignoring effect of n parameter truncation
- Wrong import module for rfft
rfft. Which code snippet correctly computes this frequency?Solution
Step 1: Use rfft and rfftfreq correctly
For real signals,rfftcomputes the positive frequencies, andrfftfreqgives the corresponding frequency bins. The sampling interval is 1/100 seconds.Step 2: Find the peak frequency
Calculate the absolute values of FFT coefficients, find the index of the max, then map to frequency usingfreqs.Final Answer:
Code snippet A correctly computes the peak frequency -> Option AQuick Check:
Use rfft + rfftfreq with correct sampling interval [OK]
- Using fft and fftfreq for real signals (less efficient)
- Wrong sampling interval in rfftfreq
- Not mapping peak index to frequency
Ready to prove it?
Pass the test and this pattern moves unknown
