0
0
SciPydata~10 mins

Spectrogram generation in SciPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Spectrogram generation
Load audio signal
Set parameters: window size, overlap, FFT points
Split signal into overlapping windows
Apply window function to each segment
Compute FFT for each window
Calculate magnitude squared (power) for each FFT
Arrange power values over time and frequency
Display or return spectrogram matrix
The process starts with an audio signal, splits it into overlapping windows, applies FFT to each, then computes power to form a time-frequency spectrogram.
Execution Sample
SciPy
import numpy as np
from scipy.signal import spectrogram

fs = 1000
x = np.sin(2*np.pi*50*np.linspace(0,1,fs,endpoint=False))
f, t, Sxx = spectrogram(x, fs)
print(Sxx.shape)
This code generates a spectrogram of a 50 Hz sine wave sampled at 1000 Hz and prints the shape of the spectrogram matrix.
Execution Table
StepActionInput/VariableOutput/Result
1Generate time vectorfs=1000t = array of 1000 points from 0 to 1 sec
2Create sine wave signaltx = sine wave at 50 Hz, length 1000
3Call spectrogram()x, fs=1000Compute windows, FFTs, power spectrum
4Split signal into windowsxSegments of length 256 with overlap
5Apply window functionEach segmentWindowed segments
6Compute FFTWindowed segmentsFFT results per segment
7Calculate power spectrumFFT resultsPower values per frequency bin and time
8Return f, t, SxxPower matrixf: freq bins, t: time bins, Sxx: power matrix
9Print Sxx.shapeSxx(129, 4) - 129 freq bins, 4 time bins
10End--
💡 Spectrogram computed and shape printed; execution ends.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
fs1000100010001000
tundefinedarray(1000 points 0-1s)array(1000 points 0-1s)array(1000 points 0-1s)
xundefinedsine wave 50Hz length 1000sine wave 50Hz length 1000sine wave 50Hz length 1000
fundefinedundefinedarray(129 freq bins)array(129 freq bins)
t (time bins)undefinedundefinedarray(4 time bins)array(4 time bins)
Sxxundefinedundefined2D power matrix shape (129,4)2D power matrix shape (129,4)
Key Moments - 3 Insights
Why does the spectrogram output have fewer time bins than the original signal length?
Because the signal is split into overlapping windows of fixed size (e.g., 256 samples), the number of time bins equals how many windows fit, not the total samples. See execution_table step 4 and 8.
What does each value in the spectrogram matrix represent?
Each value is the power (magnitude squared) of a frequency component at a specific time window. This is shown in execution_table step 7.
Why do we apply a window function before FFT?
To reduce edge effects and spectral leakage in each segment. This is done in step 5 of execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 9, what is the shape of the spectrogram matrix Sxx?
A(256, 256)
B(1000, 1000)
C(129, 4)
D(50, 100)
💡 Hint
Refer to execution_table row 9 where Sxx.shape is printed.
At which step in the execution_table is the FFT computed for each window?
AStep 4
BStep 6
CStep 7
DStep 5
💡 Hint
Look for the step mentioning FFT computation in execution_table.
If the window size is increased, how would the number of time bins in the spectrogram change?
ADecrease
BIncrease
CStay the same
DBecome zero
💡 Hint
Refer to key_moments about how window size affects time bins.
Concept Snapshot
Spectrogram generation:
- Input: signal and sampling rate
- Split signal into overlapping windows
- Apply window function
- Compute FFT per window
- Calculate power spectrum
- Output: frequency bins, time bins, power matrix
- Shows how frequency content changes over time
Full Transcript
Spectrogram generation starts by loading an audio signal sampled at a known rate. The signal is split into overlapping windows, each window is multiplied by a window function to reduce edge effects. Then, FFT is computed on each window to get frequency components. The magnitude squared of these FFTs forms the power spectrum for each time window. The result is a matrix showing how power varies by frequency and time. This matrix can be visualized as a spectrogram image. The example code generates a 50 Hz sine wave, computes its spectrogram, and prints the shape of the resulting power matrix, showing 129 frequency bins and 4 time bins.