Lesson 9 of 15
Windowing
Windowing
When computing the DFT of a finite signal, sharp edges at the start and end of the frame cause spectral leakage — energy from one frequency bin "leaks" into neighboring bins. A window function tapers the signal to zero at the edges, suppressing this effect.
The Hann Window
The Hann window (also called Hanning) is the most commonly used:
Properties:
- (tapers to zero at edges)
- (peak at center)
- Smooth roll-off reduces leakage dramatically
Example: 4-Point Hann Window
Applying the Window
Windowing is an elementwise multiplication before the DFT:
Your Task
Implement:
hann_window(N)— returnsapply_window(x, w)— returns elementwise product
import math
def hann_window(N):
return [0.5 * (1 - math.cos(2 * math.pi * n / (N - 1))) for n in range(N)]
def apply_window(x, w):
return [xi * wi for xi, wi in zip(x, w)]
print([round(v, 4) for v in hann_window(4)]) # [0.0, 0.75, 0.75, 0.0]Python runtime loading...
Loading...
Click "Run" to execute your code.