Challenge - 5 Problems
Spectrogram Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of spectrogram shape
What is the shape of the spectrogram array generated by this code snippet?
SciPy
import numpy as np from scipy.signal import spectrogram fs = 1000 x = np.sin(2 * np.pi * 50 * np.linspace(0, 1, fs)) f, t, Sxx = spectrogram(x, fs) print(Sxx.shape)
Attempts:
2 left
💡 Hint
Think about how the spectrogram divides the signal and the default window size.
✗ Incorrect
The default window length is 256 samples, so the frequency bins are 129 (NFFT/2 + 1). The time bins depend on the overlap and signal length, resulting in 10 segments.
❓ data_output
intermediate1:30remaining
Spectrogram frequency bins values
Given this code, what is the first frequency bin value printed?
SciPy
import numpy as np from scipy.signal import spectrogram fs = 500 x = np.random.randn(fs) f, t, Sxx = spectrogram(x, fs, nperseg=100) print(round(f[0], 2))
Attempts:
2 left
💡 Hint
Frequency bins start from zero frequency (DC component).
✗ Incorrect
The first frequency bin is always 0.0 Hz representing the DC component of the signal.
❓ visualization
advanced2:30remaining
Spectrogram visualization output
What will the color intensity represent in the spectrogram plot generated by this code?
SciPy
import numpy as np import matplotlib.pyplot as plt from scipy.signal import spectrogram fs = 1000 t = np.linspace(0, 1, fs) x = np.sin(2 * np.pi * 100 * t) + np.sin(2 * np.pi * 200 * t) f, t, Sxx = spectrogram(x, fs) plt.pcolormesh(t, f, 10 * np.log10(Sxx)) plt.ylabel('Frequency [Hz]') plt.xlabel('Time [sec]') plt.title('Spectrogram') plt.colorbar(label='Intensity [dB]') plt.show()
Attempts:
2 left
💡 Hint
The plot uses 10 * log10 of Sxx values.
✗ Incorrect
The spectrogram shows power spectral density (PSD) which is the power of each frequency component over time. The 10 * log10 converts it to decibels (dB) for better visualization.
🔧 Debug
advanced2:00remaining
Identify error in spectrogram call
What error will this code produce and why?
SciPy
import numpy as np from scipy.signal import spectrogram fs = 1000 x = np.sin(2 * np.pi * 50 * np.linspace(0, 1, fs)) f, t, Sxx = spectrogram(x, fs, nperseg=2000) print(Sxx.shape)
Attempts:
2 left
💡 Hint
Check the length of the input signal vs nperseg parameter.
✗ Incorrect
The input signal length is 1000 samples but nperseg is set to 2000, which is invalid and causes a ValueError.
🚀 Application
expert3:00remaining
Detecting frequency components from spectrogram data
Given a spectrogram output Sxx and frequency array f, which code snippet correctly identifies the dominant frequency at each time slice?
SciPy
import numpy as np from scipy.signal import spectrogram fs = 1000 t = np.linspace(0, 1, fs) x = np.sin(2 * np.pi * 150 * t) + np.sin(2 * np.pi * 300 * t) f, t, Sxx = spectrogram(x, fs) # Identify dominant frequency code here
Attempts:
2 left
💡 Hint
Find the frequency index with max power for each time column.
✗ Incorrect
Sxx shape is (frequency_bins, time_bins). To find dominant frequency per time, find max along frequency axis (axis=0) for each time slice, then map indices to frequencies.