0
0
SciPydata~20 mins

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

Choose your learning style9 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.