Bird
Raised Fist0
Signal Processingdata~20 mins

Spectrogram visualization in Signal Processing - Practice Problems & Coding Challenges

Choose your learning style10 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
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)
A(256, 8)
B(128, 8)
C(129, 8)
D(129, 7)
Attempts:
2 left
💡 Hint
Remember the number of frequency bins is nperseg//2 + 1, and the number of time segments depends on overlap.
❓ data_output
intermediate
2: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))
A60
B120
C90
D30
Attempts:
2 left
💡 Hint
Look for the frequency with the highest mean power across all time segments.
❓ visualization
advanced
2: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()
AA plot with frequency on y-axis from 0 to 1000 Hz, time on x-axis from 0 to 1 sec, and no visible lines.
BA plot with time on y-axis and frequency on x-axis, showing a vertical line at 100 Hz.
CA plot with frequency on y-axis from 0 to 500 Hz, time on x-axis from 0 to 1 sec, and a bright horizontal line at 100 Hz.
DA plot with frequency on x-axis and time on y-axis, showing a bright horizontal line at 100 Hz.
Attempts:
2 left
💡 Hint
Remember the spectrogram frequency axis goes from 0 to Nyquist frequency (fs/2).
🔧 Debug
advanced
2: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()
AThe sampling frequency fs is too low to capture 50 Hz signal.
BThe window size nperseg is larger than the signal length, causing no segments to be computed.
CThe signal is zero, so no power is shown.
DThe plotting function plt.pcolormesh is used incorrectly without axis labels.
Attempts:
2 left
💡 Hint
Check the relationship between signal length and nperseg parameter.
🚀 Application
expert
3: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?
AUse a short window size (e.g., 64 samples) with overlap to get good time resolution and plot the spectrogram.
BUse a very long window size (e.g., 1024 samples) without overlap to get best frequency resolution.
CUse no windowing and plot the raw FFT of the entire signal.
DUse a window size equal to the entire signal length to maximize frequency resolution.
Attempts:
2 left
💡 Hint
Short windows improve time resolution but reduce frequency resolution.

Practice

(1/5)
1. What does a spectrogram primarily show in signal processing?
easy
A. How signal frequencies change over time
B. The total energy of a signal
C. The raw amplitude values of a signal
D. The phase shift of a signal

Solution

  1. Step 1: Understand spectrogram purpose

    A spectrogram is a visual tool that shows frequency content of a signal as it changes over time.
  2. Step 2: Compare options to definition

    Only How signal frequencies change over time describes frequency changes over time, which matches the spectrogram's function.
  3. Final Answer:

    How signal frequencies change over time -> Option A
  4. Quick Check:

    Spectrogram = frequency over time [OK]
Hint: Spectrogram = frequency vs time graph [OK]
Common Mistakes:
  • Confusing spectrogram with waveform plot
  • Thinking it shows only amplitude
  • Mixing phase information with frequency
2. Which Python library is commonly used to create spectrograms?
easy
A. matplotlib
B. pandas
C. scikit-learn
D. numpy

Solution

  1. Step 1: Identify libraries for plotting

    matplotlib is widely used for plotting graphs including spectrograms.
  2. Step 2: Check other libraries' main uses

    pandas is for data frames, scikit-learn for machine learning, numpy for arrays but not direct plotting.
  3. Final Answer:

    matplotlib -> Option A
  4. Quick Check:

    Plotting spectrogram = matplotlib [OK]
Hint: Plotting graphs? Think matplotlib [OK]
Common Mistakes:
  • Choosing numpy for plotting directly
  • Confusing pandas with plotting library
  • Selecting scikit-learn which is for ML
3. Given this Python code snippet, what is the shape of the output spectrogram array?
import numpy as np
from scipy.signal import spectrogram

fs = 1280  # Sampling frequency
x = np.sin(2 * np.pi * 50 * np.linspace(0, 1, fs))
f, t, Sxx = spectrogram(x, fs)
print(Sxx.shape)
medium
A. (1000, 1000)
B. (129, 9)
C. (50, 1000)
D. (1000, 129)

Solution

  1. Step 1: Understand spectrogram output dimensions

    The spectrogram function returns frequency bins and time bins. For default parameters, frequency bins are nfft/2+1 (default nfft=256), so 129 frequency bins.
  2. Step 2: Calculate time bins

    With 1 second signal sampled at 1280 Hz and default windowing, time bins are 9 segments.
  3. Final Answer:

    (129, 9) -> Option B
  4. Quick Check:

    Frequency bins = 129, Time bins = 9 [OK]
Hint: Spectrogram shape = (freq_bins, time_bins) [OK]
Common Mistakes:
  • Assuming output shape matches input length
  • Confusing frequency bins with sampling rate
  • Mixing up rows and columns in output
4. Identify the error in this code snippet for plotting a spectrogram:
import matplotlib.pyplot as plt
from scipy.signal import spectrogram

fs = 500
x = [1, 2, 3, 4, 5]
f, t, Sxx = spectrogram(x, fs)
plt.specgram(Sxx)
plt.show()
medium
A. Using plt.specgram instead of plt.plot
B. Missing import for numpy
C. Sampling frequency fs is too high
D. Passing Sxx instead of x to plt.specgram

Solution

  1. Step 1: Understand spectrogram output and plotting

    The spectrogram function returns frequencies, times, and the spectrogram matrix Sxx. plt.specgram expects raw signal, not Sxx.
  2. Step 2: Identify misuse of plt.specgram

    Passing Sxx (already spectrogram data) to plt.specgram causes error; should plot Sxx with plt.imshow or pass raw signal to plt.specgram.
  3. Final Answer:

    Passing Sxx instead of x to plt.specgram -> Option D
  4. Quick Check:

    Plot raw signal with plt.specgram, not spectrogram matrix [OK]
Hint: plt.specgram needs raw signal, not spectrogram data [OK]
Common Mistakes:
  • Passing spectrogram matrix to plt.specgram
  • Confusing plt.specgram with plt.imshow
  • Ignoring correct input types for plotting
5. You want a spectrogram with smoother frequency resolution but less time detail. Which parameter should you increase?
hard
A. Sampling frequency (fs)
B. Overlap between windows (noverlap)
C. Window size (nperseg)
D. FFT length (nfft) smaller than window size

Solution

  1. Step 1: Understand window size effect on spectrogram

    Larger window size increases frequency resolution but reduces time resolution (smoother frequency, less time detail).
  2. Step 2: Evaluate other parameters

    Increasing overlap smooths time axis but does not improve frequency resolution as much. Sampling frequency affects max frequency, not resolution. nfft smaller than window size is invalid.
  3. Final Answer:

    Window size (nperseg) -> Option C
  4. Quick Check:

    Increase window size for better frequency resolution [OK]
Hint: Bigger window = better frequency, worse time detail [OK]
Common Mistakes:
  • Increasing overlap to improve frequency resolution
  • Changing sampling frequency to affect resolution
  • Setting nfft smaller than window size
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard