Bird
Raised Fist0
SciPydata~20 mins

Real FFT (rfft) in SciPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Real FFT Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of rfft on a simple signal
What is the output of the following code that uses scipy.fft.rfft on a simple signal array?
SciPy
import numpy as np
from scipy.fft import rfft
signal = np.array([1, 2, 3, 4])
result = rfft(signal)
print(result)
A[10.+0.j -2.+2.j -2.+0.j]
B[10.+0.j 2.-2.j 2.+0.j]
C[10.+0.j -2.-2.j -2.+0.j]
D[10.+0.j 2.+2.j 2.+0.j]
Attempts:
2 left
💡 Hint
Remember that rfft returns the positive frequency terms including zero frequency.
❓ data_output
intermediate
1:30remaining
Length of rfft output array
Given a real input array of length 10, what is the length of the output array from scipy.fft.rfft?
SciPy
import numpy as np
from scipy.fft import rfft
signal = np.arange(10)
result = rfft(signal)
print(len(result))
A6
B9
C5
D10
Attempts:
2 left
💡 Hint
The length of rfft output is N//2 + 1 for input length N.
❓ visualization
advanced
3:00remaining
Visualizing magnitude spectrum from rfft
Which code snippet correctly plots the magnitude spectrum of a real signal using scipy.fft.rfft and matplotlib?
A
import numpy as np
import matplotlib.pyplot as plt
from scipy.fft import rfft
signal = np.sin(2*np.pi*5*np.linspace(0,1,100))
freqs = np.fft.fftfreq(len(signal))
spectrum = np.abs(rfft(signal))
plt.plot(freqs, spectrum)
plt.xlabel('Frequency (Hz)')
plt.ylabel('Magnitude')
plt.show()
B
import numpy as np
import matplotlib.pyplot as plt
from scipy.fft import rfft
signal = np.sin(2*np.pi*5*np.linspace(0,1,100))
freqs = np.fft.fftfreq(len(signal), 1/100)
spectrum = np.abs(rfft(signal))
plt.plot(freqs, spectrum)
plt.xlabel('Frequency (Hz)')
plt.ylabel('Magnitude')
plt.show()
C
import numpy as np
import matplotlib.pyplot as plt
from scipy.fft import rfft
signal = np.sin(2*np.pi*5*np.linspace(0,1,100))
freqs = np.fft.rfftfreq(len(signal))
spectrum = rfft(signal)
plt.plot(freqs, spectrum)
plt.xlabel('Frequency (Hz)')
plt.ylabel('Magnitude')
plt.show()
D
import numpy as np
import matplotlib.pyplot as plt
from scipy.fft import rfft
signal = np.sin(2*np.pi*5*np.linspace(0,1,100))
freqs = np.fft.rfftfreq(len(signal), 1/100)
spectrum = np.abs(rfft(signal))
plt.plot(freqs, spectrum)
plt.xlabel('Frequency (Hz)')
plt.ylabel('Magnitude')
plt.show()
Attempts:
2 left
💡 Hint
Use rfftfreq with the correct sample spacing and plot the magnitude (absolute value) of rfft output.
🔧 Debug
advanced
1:30remaining
Error raised by incorrect rfft usage
What error will this code raise when run?
SciPy
from scipy.fft import rfft
signal = 'not an array'
result = rfft(signal)
AValueError: object too deep for desired array
BTypeError: unsupported operand type(s) for *: 'str' and 'int'
CTypeError: loop of ufunc does not support argument 0 of type str which has no callable rfft method
DTypeError: float() argument must be a string or a number, not 'str'
Attempts:
2 left
💡 Hint
rfft expects a numeric array, not a string.
🧠 Conceptual
expert
2:00remaining
Understanding output symmetry of rfft
Which statement correctly describes the output of scipy.fft.rfft compared to the full FFT of a real-valued input?
Arfft returns only the negative frequency terms excluding zero frequency, halving the output size.
Brfft returns the full complex FFT output including negative frequencies, doubling the output size.
Crfft returns only the positive frequency terms including zero and Nyquist frequency, exploiting symmetry to reduce output size.
Drfft returns the magnitude spectrum only, discarding phase information.
Attempts:
2 left
💡 Hint
Think about how real input signals produce symmetric FFT outputs.

Practice

(1/5)
1. What does the scipy.fft.rfft function compute when applied to a real-valued signal?
easy
A. The full complex Fourier transform including negative frequencies
B. The positive frequency components of the signal's Fourier transform
C. The inverse Fourier transform of the signal
D. The time-domain signal filtered by a low-pass filter

Solution

  1. Step 1: Understand the input type

    rfft is designed for real-valued input signals.
  2. Step 2: Analyze the output of rfft

    It returns only the positive frequency components because the negative frequencies are mirror images for real signals.
  3. Final Answer:

    The positive frequency components of the signal's Fourier transform -> Option B
  4. Quick Check:

    Real input -> half spectrum output [OK]
Hint: Real signals -> use rfft for positive frequencies only [OK]
Common Mistakes:
  • Confusing rfft with full fft output
  • Thinking rfft returns time-domain data
  • Assuming rfft returns negative frequencies
2. Which of the following is the correct way to import and use rfft from SciPy's FFT module?
easy
A. import scipy.fft as rfft result = rfft.rfft(signal)
B. import scipy.rfft result = scipy.rfft(signal)
C. from scipy.fft import rfft result = rfft(signal)
D. from scipy import rfft result = rfft.fft(signal)

Solution

  1. Step 1: Recall correct import syntax

    The rfft function is in scipy.fft, so import it directly from there.
  2. Step 2: Check usage

    Calling rfft(signal) after import is the correct usage.
  3. Final Answer:

    from scipy.fft import rfft\nresult = rfft(signal) -> Option C
  4. Quick Check:

    Correct import and call syntax [OK]
Hint: Import rfft directly from scipy.fft for simple use [OK]
Common Mistakes:
  • Trying to import rfft from scipy directly
  • Using wrong module names
  • Calling rfft as a method of scipy.fft without import
3. What is the output of the following code?
import numpy as np
from scipy.fft import rfft
signal = np.array([1, 0, -1, 0])
result = rfft(signal)
print(np.round(result, 2))
medium
A. [0.+0.j 1.+0.j 0.+0.j]
B. [0.+0.j 0.+0.j 0.+0.j]
C. [0.+0.j -2.+0.j 0.+0.j]
D. [0.+0.j 2.+0.j 0.+0.j]

Solution

  1. Step 1: Understand the input signal

    The signal is [1, 0, -1, 0], a simple 4-point real array.
  2. 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].
  3. Final Answer:

    [0.+0.j 2.+0.j 0.+0.j] -> Option D
  4. Quick Check:

    rfft length 4 -> 3 outputs, middle value 2 [OK]
Hint: rfft output length = input//2 + 1 [OK]
Common Mistakes:
  • Expecting output length same as input
  • Ignoring complex number format
  • Misreading the signal values
4. Identify the error in this code snippet using 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)
medium
A. The parameter n is smaller than signal length; use n=4 instead
B. The input signal must be a NumPy array, not a list
C. rfft does not accept the parameter n
D. The import statement is incorrect; use import scipy.fftpack

Solution

  1. Step 1: Check the n parameter usage

    The n parameter sets the length of the FFT. Here, n=2 is less than the signal length 4, so the signal is truncated.
  2. Step 2: Fix the parameter to match or exceed signal length

    Using n=4 ensures the full signal is transformed without truncation.
  3. Final Answer:

    The parameter n is smaller than signal length; use n=4 instead -> Option A
  4. Quick Check:

    FFT length must cover signal length [OK]
Hint: Set n ≥ signal length to avoid truncation [OK]
Common Mistakes:
  • Passing list instead of array (allowed but less efficient)
  • Ignoring effect of n parameter truncation
  • Wrong import module for rfft
5. You have a real sensor signal sampled at 100 Hz with 1000 points. You want to find the frequency with the highest amplitude using rfft. Which code snippet correctly computes this frequency?
hard
A. import numpy as np from scipy.fft import rfft, rfftfreq signal = np.random.rand(1000) fft_vals = rfft(signal) freqs = rfftfreq(len(signal), 1/100) peak_freq = freqs[np.argmax(np.abs(fft_vals))] print(peak_freq)
B. import numpy as np from scipy.fft import fft signal = np.random.rand(1000) fft_vals = fft(signal) freqs = np.fft.fftfreq(len(signal), 1/100) peak_freq = freqs[np.argmax(np.abs(fft_vals))] print(peak_freq)
C. import numpy as np from scipy.fft import rfft signal = np.random.rand(1000) fft_vals = rfft(signal) peak_freq = np.argmax(np.abs(fft_vals)) print(peak_freq)
D. import numpy as np from scipy.fft import rfft, rfftfreq signal = np.random.rand(1000) fft_vals = rfft(signal) freqs = rfftfreq(len(signal), 100) peak_freq = freqs[np.argmax(np.abs(fft_vals))] print(peak_freq)

Solution

  1. Step 1: Use rfft and rfftfreq correctly

    For real signals, rfft computes the positive frequencies, and rfftfreq gives the corresponding frequency bins. The sampling interval is 1/100 seconds.
  2. Step 2: Find the peak frequency

    Calculate the absolute values of FFT coefficients, find the index of the max, then map to frequency using freqs.
  3. Final Answer:

    Code snippet A correctly computes the peak frequency -> Option A
  4. Quick Check:

    Use rfft + rfftfreq with correct sampling interval [OK]
Hint: Use rfftfreq with 1/sampling_rate for correct frequencies [OK]
Common Mistakes:
  • Using fft and fftfreq for real signals (less efficient)
  • Wrong sampling interval in rfftfreq
  • Not mapping peak index to frequency
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard