Challenge - 5 Problems
Spectrogram Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the shape of the spectrogram matrix?
Given a signal sampled at 1024 Hz with 1 second duration, a window size of 256 samples, and an overlap of 128 samples, what is the shape of the spectrogram matrix produced by the following code?
Signal Processing
import numpy as np from scipy.signal import spectrogram fs = 1024 signal = np.sin(2 * np.pi * 50 * np.linspace(0, 1, fs, endpoint=False)) f, t, Sxx = spectrogram(signal, fs=fs, window='hann', nperseg=256, noverlap=128) print(Sxx.shape)
Attempts:
2 left
💡 Hint
Remember the number of frequency bins is nperseg//2 + 1, and the number of time segments depends on overlap.
✗ Incorrect
The number of frequency bins is 256/2 + 1 = 129. The number of time segments is calculated as floor((1024 - 256) / (256 - 128)) + 1 = floor(768 / 128) + 1 = 6 + 1 = 7. So the shape is (129, 7).
❓ data_output
intermediate2:00remaining
Identify the dominant frequency from the spectrogram data
Using the spectrogram data from the code below, which frequency has the highest average power over time?
Signal Processing
import numpy as np from scipy.signal import spectrogram fs = 500 t = np.linspace(0, 2, 2*fs, endpoint=False) signal = np.sin(2 * np.pi * 60 * t) + 0.5 * np.sin(2 * np.pi * 120 * t) f, _, Sxx = spectrogram(signal, fs=fs, nperseg=128) avg_power = Sxx.mean(axis=1) max_freq = f[np.argmax(avg_power)] print(round(max_freq))
Attempts:
2 left
💡 Hint
Look for the frequency with the highest mean power across all time segments.
✗ Incorrect
The signal has two frequencies: 60 Hz and 120 Hz. The 60 Hz component has higher amplitude, so its average power is higher, making 60 Hz the dominant frequency.
❓ visualization
advanced2:00remaining
Which plot shows the correct spectrogram visualization?
You run the following code to plot a spectrogram of a signal. Which option shows the correct plot output?
Signal Processing
import numpy as np import matplotlib.pyplot as plt from scipy.signal import spectrogram fs = 1000 t = np.linspace(0, 1, fs, endpoint=False) signal = np.sin(2 * np.pi * 100 * t) f, t_spec, Sxx = spectrogram(signal, fs=fs, nperseg=256) plt.pcolormesh(t_spec, f, 10 * np.log10(Sxx), shading='gouraud') plt.ylabel('Frequency [Hz]') plt.xlabel('Time [sec]') plt.title('Spectrogram') plt.colorbar(label='Intensity [dB]') plt.show()
Attempts:
2 left
💡 Hint
Remember the spectrogram frequency axis goes from 0 to Nyquist frequency (fs/2).
✗ Incorrect
The spectrogram frequency axis ranges from 0 to 500 Hz (half the sampling rate). The signal frequency is 100 Hz, so a bright horizontal line appears at 100 Hz over time.
🔧 Debug
advanced2:00remaining
Why does this spectrogram plot show no signal?
The code below produces a spectrogram plot, but the plot is completely dark with no visible signal. What is the most likely cause?
Signal Processing
import numpy as np import matplotlib.pyplot as plt from scipy.signal import spectrogram fs = 1000 t = np.linspace(0, 1, fs, endpoint=False) signal = np.sin(2 * np.pi * 50 * t) f, t_spec, Sxx = spectrogram(signal, fs=fs, nperseg=1024) plt.pcolormesh(t_spec, f, 10 * np.log10(Sxx), shading='gouraud') plt.show()
Attempts:
2 left
💡 Hint
Check the relationship between signal length and nperseg parameter.
✗ Incorrect
The signal length is 1000 samples, but nperseg=1024 > 1000. This causes the array of start indices np.arange(0, 1000 - 1024 + 1, ...) to be empty (upper limit negative), so no segments are computed and Sxx is empty, resulting in a blank plot.
🚀 Application
expert3:00remaining
How to detect time-varying frequency components using spectrogram?
You have a signal composed of two sine waves: one at 50 Hz active during the first half of the signal, and another at 150 Hz active during the second half. Which approach using spectrogram visualization best reveals these time-varying frequency components?
Attempts:
2 left
💡 Hint
Short windows improve time resolution but reduce frequency resolution.
✗ Incorrect
To detect changes in frequency over time, good time resolution is needed. Short windows with overlap provide this, allowing the spectrogram to show when frequencies appear and disappear.