Lesson 10 of 15
STFT Framing
Short-Time Fourier Transform Framing
The Short-Time Fourier Transform (STFT) analyzes how the frequency content of a signal changes over time. The key idea: divide the signal into short overlapping frames, then apply the DFT to each frame.
Framing
Given a signal of length , extract frames of size with a hop size :
Frames are extracted as long as the full frame fits within the signal. The number of frames is:
Overlap: When , frames overlap. A 50% overlap means .
STFT Frame Spectrum
For each frame, apply a window and compute the DFT magnitude:
The result is a 2D time-frequency representation called a spectrogram.
Example
Signal , frame size , hop :
- Frame 0:
- Frame 1:
- Frame 2:
Your Task
Implement:
frame_signal(x, frame_size, hop)— returns list of frames (each a list of floats)stft_frame_spectrum(frame, window)— returns
import math
def dft(x):
N = len(x)
X = []
for k in range(N):
val = 0 + 0j
for n in range(N):
val += x[n] * math.e ** (-2j * math.pi * k * n / N)
X.append(val)
return X
def frame_signal(x, frame_size, hop):
frames = []
i = 0
while i + frame_size <= len(x):
frames.append(x[i:i + frame_size])
i += hop
return frames
def stft_frame_spectrum(frame, window):
windowed = [f * w for f, w in zip(frame, window)]
return [abs(v) for v in dft(windowed)]Python runtime loading...
Loading...
Click "Run" to execute your code.