0
0
SciPydata~20 mins

2D FFT (fft2) in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
2D FFT Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of 2D FFT on a simple matrix
What is the output of the following code using scipy.fft.fft2 on a 2x2 matrix?
SciPy
import numpy as np
from scipy.fft import fft2

matrix = np.array([[1, 2], [3, 4]])
result = fft2(matrix)
print(np.round(result, 2))
A
[[10.+0.j  2.+0.j]
 [ 4.+0.j  0.+0.j]]
B
[[10.+0.j  2.+0.j]
 [-4.+0.j  0.+0.j]]
C
[[10.+0.j -2.+0.j]
 [ 4.+0.j  0.+0.j]]
D
[[10.+0.j -2.+0.j]
 [-4.+0.j  0.+0.j]]
Attempts:
2 left
💡 Hint
Recall that 2D FFT sums over rows and columns with complex exponentials.
data_output
intermediate
1:30remaining
Shape of 2D FFT result
If you apply scipy.fft.fft2 to a 5x3 matrix, what will be the shape of the output?
SciPy
import numpy as np
from scipy.fft import fft2

matrix = np.random.rand(5,3)
result = fft2(matrix)
print(result.shape)
A(8, 8)
B(3, 5)
C(5, 3)
D(1, 1)
Attempts:
2 left
💡 Hint
The FFT output shape matches the input shape unless specified otherwise.
visualization
advanced
3:00remaining
Visualizing magnitude spectrum of 2D FFT
Which option correctly plots the magnitude spectrum of the 2D FFT of a 256x256 image stored in image?
SciPy
import numpy as np
import matplotlib.pyplot as plt
from scipy.fft import fft2, fftshift

# image is a 256x256 numpy array
f_transform = fft2(image)
f_shifted = fftshift(f_transform)
magnitude_spectrum = np.abs(f_shifted)
plt.imshow(np.log1p(magnitude_spectrum), cmap='gray')
plt.title('Magnitude Spectrum')
plt.show()
AUse fft2, plot magnitude spectrum directly without fftshift or log scale.
BUse fft2, then fftshift, then plot log of magnitude spectrum with gray colormap.
CUse fftshift first, then fft2, then plot magnitude spectrum with jet colormap.
DPlot the real part of fft2 output without any transformation or scaling.
Attempts:
2 left
💡 Hint
Centering the zero frequency with fftshift and using log scale helps visualize details.
🧠 Conceptual
advanced
2:00remaining
Effect of zero-padding on 2D FFT resolution
What is the main effect of zero-padding a 2D signal before applying fft2?
AIt increases frequency resolution by interpolating the FFT output.
BIt changes the original frequency content of the signal.
CIt removes noise from the signal before FFT.
DIt reduces the computation time of the FFT.
Attempts:
2 left
💡 Hint
Think about how zero-padding affects the FFT output size and frequency bins.
🔧 Debug
expert
2:00remaining
Identify the error in 2D FFT magnitude calculation
What error will this code raise when calculating the magnitude of a 2D FFT result?
SciPy
import numpy as np
from scipy.fft import fft2

matrix = np.array([[1, 2], [3, 4]])
result = fft2(matrix)
magnitude = np.abs(result.real)
print(magnitude)
ANo error, but magnitude is incorrect because it uses only real part.
BTypeError because np.abs cannot be applied to real numbers.
CValueError due to shape mismatch in np.abs call.
DSyntaxError due to missing parentheses.
Attempts:
2 left
💡 Hint
Check how magnitude should be calculated from complex numbers.