Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to compute the real FFT of the signal array.
SciPy
import numpy as np from scipy.fft import [1] signal = np.array([0, 1, 0, -1]) result = [1](signal)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using fft instead of rfft causes output to be complex and longer.
Using ifft or irfft is for inverse transforms, not forward.
✗ Incorrect
The function rfft computes the real FFT of a real-valued input signal.
2fill in blank
mediumComplete the code to compute the frequencies corresponding to the rfft output.
SciPy
import numpy as np from scipy.fft import rfft, [1] signal = np.array([0, 1, 0, -1]) sample_rate = 4 freqs = [1](len(signal), 1/sample_rate)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using fftfreq returns frequencies for full FFT, not real FFT.
Using irfft or fftshift does not return frequencies.
✗ Incorrect
rfftfreq returns the sample frequencies for the output of rfft.
3fill in blank
hardFix the error in the code to correctly compute the inverse real FFT.
SciPy
import numpy as np from scipy.fft import rfft, [1] signal = np.array([0, 1, 0, -1]) transformed = rfft(signal) recovered = [1](transformed)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using ifft instead of irfft may cause shape mismatch.
Using rfft or fft again does not invert the transform.
✗ Incorrect
irfft computes the inverse of rfft, recovering the original real signal.
4fill in blank
hardFill both blanks to create a dictionary of frequencies and their corresponding rfft magnitudes.
SciPy
import numpy as np from scipy.fft import rfft, rfftfreq signal = np.array([0, 1, 0, -1]) sample_rate = 4 freqs = [1](len(signal), 1/sample_rate) magnitudes = np.abs([2](signal)) result = dict(zip(freqs, magnitudes))
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using fft instead of rfft causes mismatch in frequencies and magnitudes.
Using irfft instead of rfft for magnitudes is incorrect.
✗ Incorrect
rfftfreq gives frequencies for rfft output; rfft computes the transform.
5fill in blank
hardFill all three blanks to compute the normalized power spectrum of a real signal.
SciPy
import numpy as np from scipy.fft import rfft, rfftfreq signal = np.array([0, 1, 0, -1]) sample_rate = 4 freqs = [1](len(signal), 1/sample_rate) transform = [2](signal) power = np.square(np.abs(transform)) / [3](signal)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using float(len(signal)) instead of len(signal) causes type issues.
Using fftfreq instead of rfftfreq causes frequency mismatch.
✗ Incorrect
rfftfreq gets frequencies; rfft computes transform; len(signal) normalizes power.