0
0
SciPydata~20 mins

Spectrogram generation in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Spectrogram Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A(129, 10)
B(128, 10)
C(130, 10)
D(129, 9)
Attempts:
2 left
💡 Hint
Think about how the spectrogram divides the signal and the default window size.
data_output
intermediate
1: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))
A0.0
B5.0
C2.5
D50.0
Attempts:
2 left
💡 Hint
Frequency bins start from zero frequency (DC component).
visualization
advanced
2: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()
AAmplitude of the signal at each frequency and time
BRaw signal values over time
CPhase angle of the signal at each frequency and time
DPower spectral density in decibels at each frequency and time
Attempts:
2 left
💡 Hint
The plot uses 10 * log10 of Sxx values.
🔧 Debug
advanced
2: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)
AIndexError: index out of range
BTypeError: spectrogram() got an unexpected keyword argument 'nperseg'
CValueError: nperseg must not be greater than input length
DNo error, prints shape (1001, 1)
Attempts:
2 left
💡 Hint
Check the length of the input signal vs nperseg parameter.
🚀 Application
expert
3: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
Adominant_freqs = np.max(Sxx, axis=1)
Bdominant_freqs = f[np.argmax(Sxx, axis=0)]
Cdominant_freqs = t[np.argmax(Sxx, axis=1)]
Ddominant_freqs = f[np.argmax(Sxx, axis=1)]
Attempts:
2 left
💡 Hint
Find the frequency index with max power for each time column.