Bird
Raised Fist0
MATLABdata~10 mins

Spectrograms in MATLAB - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Spectrograms
Input Signal
↓
Divide into Overlapping Segments
↓
Apply Window Function
↓
Compute FFT of Each Segment
↓
Calculate Magnitude Squared (Power)
↓
Arrange Power Spectra Over Time
↓
Display as Image (Time vs Frequency)
The spectrogram process splits a signal into small parts, finds frequency content for each, then shows how frequencies change over time.
Execution Sample
MATLAB
fs = 1000;
t = 0:1/fs:1-1/fs;
x = cos(2*pi*100*t) + cos(2*pi*200*t);
spectrogram(x,128,120,128,fs,'yaxis');
This code creates a signal with two frequencies and shows its spectrogram.
Execution Table
StepActionInput/VariableResult/Output
1Generate time vector tfs=1000t = 0 to 0.999 with step 0.001
2Create signal xtx = cos(2*pi*100*t) + cos(2*pi*200*t)
3Divide x into segmentsx, segment length=128, overlap=120Segments of length 128 with 120 overlap
4Apply window to each segmentSegmentsWindowed segments (e.g., Hamming)
5Compute FFT of each segmentWindowed segmentsFrequency spectra for each segment
6Calculate power spectrumFFT resultsMagnitude squared values
7Arrange power spectra over timePower spectraMatrix with time on one axis, frequency on other
8Display spectrogramPower matrixImage showing frequency vs time with color intensity
9EndAll steps doneSpectrogram displayed
💡 All signal segments processed and spectrogram image generated
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6After Step 7Final
tundefined0 to 0.9990 to 0.9990 to 0.9990 to 0.9990 to 0.9990 to 0.9990 to 0.999
xundefineddefined as sum of cosinesfull signal vectorfull signal vectorfull signal vectorfull signal vectorfull signal vectorfull signal vector
Segmentsundefinedundefined128-length overlapping segmentswindowed segmentswindowed segmentswindowed segmentswindowed segmentswindowed segments
FFTundefinedundefinedundefinedundefinedfrequency spectra per segmentfrequency spectra per segmentfrequency spectra per segmentfrequency spectra per segment
Powerundefinedundefinedundefinedundefinedundefinedmagnitude squaredmagnitude squaredmagnitude squared
Spectrogram Matrixundefinedundefinedundefinedundefinedundefinedundefinedtime-frequency matrixtime-frequency matrix
Key Moments - 3 Insights
Why do we divide the signal into overlapping segments instead of using the whole signal at once?
Dividing into overlapping segments lets us see how frequencies change over time. Using the whole signal at once only shows overall frequencies, losing time info. See execution_table rows 3 and 7.
What is the purpose of applying a window function to each segment?
Windowing reduces edge effects that cause distortion in the FFT. It smooths the segment edges before frequency analysis. See execution_table row 4.
Why do we calculate the magnitude squared of the FFT results?
Magnitude squared gives the power of each frequency component, which is what the spectrogram displays as intensity. See execution_table row 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What does the 'Segments' variable represent?
AThe FFT results
BThe entire signal as one piece
CSmall overlapping parts of the signal
DThe window function applied
💡 Hint
Check the 'Action' and 'Result/Output' columns at step 3 in execution_table
According to variable_tracker, what happens to the 'FFT' variable after step 5?
AIt holds frequency spectra for each segment
BIt is undefined
CIt becomes the time vector
DIt stores the windowed segments
💡 Hint
Look at the 'FFT' row and the 'After Step 5' column in variable_tracker
At which step in execution_table is the spectrogram image actually displayed?
AStep 6
BStep 8
CStep 7
DStep 9
💡 Hint
Check the 'Action' and 'Result/Output' columns for step 8 in execution_table
Concept Snapshot
Spectrograms show how signal frequencies change over time.
Split signal into overlapping segments.
Apply window to each segment.
Compute FFT and power spectrum per segment.
Display power as color image: time vs frequency.
Full Transcript
A spectrogram breaks a signal into small overlapping parts to analyze frequency changes over time. First, the signal is divided into segments with overlap to keep continuity. Each segment is windowed to reduce edge effects. Then, the FFT is computed for each segment to find frequency content. The magnitude squared of the FFT gives power, which is arranged over time and frequency axes. Finally, this data is shown as an image where colors represent power intensity at each frequency and time. This helps us see how frequencies vary as the signal progresses.

Practice

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

Solution

  1. Step 1: Understand the purpose of a spectrogram

    A spectrogram is a visual tool that shows how the frequency content of a signal varies over time.
  2. Step 2: Compare options with the definition

    Only How frequencies change over time in a signal correctly describes this as frequency changes over time, while others describe different signal properties.
  3. Final Answer:

    How frequencies change over time in a signal -> Option B
  4. Quick Check:

    Spectrogram = frequency vs time [OK]
Hint: Spectrogram = frequency changes over time [OK]
Common Mistakes:
  • Confusing amplitude with frequency
  • Thinking spectrogram shows total energy
  • Mixing phase shift with frequency content
2. Which of the following is the correct syntax to create a spectrogram in MATLAB for signal x with window size 256 and overlap 128?
easy
A. spectrogram(x,256,128)
B. spectrogram(x,128,256)
C. spectrogram(256,x,128)
D. spectrogram(x,128)

Solution

  1. Step 1: Recall MATLAB spectrogram syntax

    The syntax is spectrogram(signal,window,overlap), so the window size comes before overlap.
  2. Step 2: Match parameters to syntax

    spectrogram(x,256,128) correctly uses spectrogram(x,256,128) where 256 is window size and 128 is overlap.
  3. Final Answer:

    spectrogram(x,256,128) -> Option A
  4. Quick Check:

    Correct order: signal, window, overlap [OK]
Hint: Remember: spectrogram(signal, window, overlap) [OK]
Common Mistakes:
  • Swapping window and overlap values
  • Passing window size as first argument
  • Omitting required parameters
3. What will be the output size of the matrix S when running [S,F,T] = spectrogram(x,128,64,256,1000); if x is a 1-second signal sampled at 1000 Hz?
medium
A. 256 rows by 15 columns
B. 128 rows by 16 columns
C. 129 rows by 14 columns
D. 129 rows by 16 columns

Solution

  1. Step 1: Calculate frequency bins (rows)

    Frequency bins = nfft/2 + 1 = 256/2 + 1 = 129 rows.
  2. Step 2: Calculate time bins (columns)

    Signal length = 1000 samples, window=128, overlap=64, step = 128-64=64 samples.
    Number of segments = floor((1000-128)/64) + 1 = floor(872/64) + 1 = 13 + 1 = 14 columns.
  3. Final Answer:

    129 rows by 14 columns -> Option C
  4. Quick Check:

    Rows = 129, Columns = 14 [OK]
Hint: Rows = nfft/2+1, columns depend on window and overlap [OK]
Common Mistakes:
  • Confusing nfft with window size for rows
  • Miscounting number of segments
  • Ignoring overlap in time bins calculation
4. Given the code snippet:
x = randn(1,1000);
[S,F,T] = spectrogram(x,256,128,512,1000);
imagesc(T,F,abs(S)); axis xy;

The spectrogram plot looks very blurry. What is the most likely cause?
medium
A. Sampling frequency is too low
B. Overlap is too small, causing gaps in data
C. nfft is smaller than window size, causing error
D. Window size is too large, causing poor time resolution

Solution

  1. Step 1: Analyze window size effect on resolution

    A large window size (256) smooths over time, reducing time resolution and causing blur.
  2. Step 2: Check other parameters

    Overlap is 128 (50%), which is typical and not causing gaps.
    nfft (512) is larger than window (256), which is valid.
    Sampling frequency (1000) is standard.
  3. Final Answer:

    Window size is too large, causing poor time resolution -> Option D
  4. Quick Check:

    Large window = poor time resolution [OK]
Hint: Large window = better frequency, worse time resolution [OK]
Common Mistakes:
  • Confusing overlap with window size effect
  • Assuming nfft smaller than window causes blur
  • Blaming sampling frequency for blur
5. You want to analyze a signal with rapidly changing frequencies using MATLAB's spectrogram. Which combination of parameters will best capture quick changes without losing frequency detail?
hard
A. Small window size, large overlap, moderate nfft
B. Large window size, large overlap, small nfft
C. Large window size, small overlap, large nfft
D. Small window size, small overlap, large nfft

Solution

  1. Step 1: Understand trade-offs in spectrogram parameters

    Small window size improves time resolution to capture rapid changes.
    Large overlap ensures smooth transitions between windows.
    Moderate nfft balances frequency resolution without excessive computation.
  2. Step 2: Evaluate options

    Small window size, large overlap, moderate nfft matches these criteria.
    Small window size, small overlap, large nfft has small overlap, causing gaps.
    Options B and C use large windows, reducing time resolution.
  3. Final Answer:

    Small window size, large overlap, moderate nfft -> Option A
  4. Quick Check:

    Quick changes need small window + large overlap [OK]
Hint: Small window + large overlap = best for fast changes [OK]
Common Mistakes:
  • Using large window for fast changes
  • Ignoring overlap size
  • Choosing too small nfft losing frequency detail
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