0
0
SciPydata~10 mins

Windowing before FFT in SciPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Windowing before FFT
Start: Raw Signal
Apply Window Function
Windowed Signal
Compute FFT
Frequency Spectrum Output
The raw signal is first multiplied by a window function to reduce edge effects, then the FFT is computed on the windowed signal to get a cleaner frequency spectrum.
Execution Sample
SciPy
import numpy as np
from scipy.signal import windows
from scipy.fft import fft

signal = np.array([1, 2, 3, 4, 5, 6, 7, 8])
window = windows.hann(len(signal))
windowed_signal = signal * window
spectrum = fft(windowed_signal)
This code applies a Hann window to a simple signal and then computes its FFT.
Execution Table
StepVariableValue/ActionResult/Output
1signal[1 2 3 4 5 6 7 8]Raw input signal array
2window[0. 0.1882551 0.61126047 0.95048443 0.95048443 0.61126047 0.1882551 0. ]Hann window values
3windowed_signal[0. 0.3765102 1.8337814 3.8019377 4.7524221 3.6675628 1.3187857 0. ]Signal after element-wise multiplication with window
4spectrumfft(windowed_signal)Complex frequency spectrum array
5spectrum (magnitude)[15.751 -6.371+0.j -2.059-1.927j -0.25 -0.25j -0.25 -0.25j -0.25 +0.25j -0.25 +0.25j -2.059+1.927j]Magnitude and phase of FFT output
💡 FFT computed on windowed signal to reduce spectral leakage
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
signal[1 2 3 4 5 6 7 8][1 2 3 4 5 6 7 8][1 2 3 4 5 6 7 8][1 2 3 4 5 6 7 8][1 2 3 4 5 6 7 8]
windowN/A[0. 0.1882551 0.61126047 0.95048443 0.95048443 0.61126047 0.1882551 0. ][0. 0.1882551 0.61126047 0.95048443 0.95048443 0.61126047 0.1882551 0. ][0. 0.1882551 0.61126047 0.95048443 0.95048443 0.61126047 0.1882551 0. ][0. 0.1882551 0.61126047 0.95048443 0.95048443 0.61126047 0.1882551 0. ]
windowed_signalN/AN/A[0. 0.3765102 1.8337814 3.8019377 4.7524221 3.6675628 1.3187857 0. ][0. 0.3765102 1.8337814 3.8019377 4.7524221 3.6675628 1.3187857 0. ][0. 0.3765102 1.8337814 3.8019377 4.7524221 3.6675628 1.3187857 0. ]
spectrumN/AN/AN/A[15.751 -6.371+0.j -2.059-1.927j -0.25 -0.25j -0.25 -0.25j -0.25 +0.25j -0.25 +0.25j -2.059+1.927j][15.751 -6.371+0.j -2.059-1.927j -0.25 -0.25j -0.25 -0.25j -0.25 +0.25j -0.25 +0.25j -2.059+1.927j]
Key Moments - 3 Insights
Why do we multiply the signal by a window before FFT?
Multiplying by a window reduces edge effects and spectral leakage, as shown in step 3 of the execution_table where the signal is modified before FFT.
What happens if we skip the window and compute FFT directly?
Without windowing, the FFT may show spectral leakage causing less clear frequency peaks. The execution_table shows the windowed_signal differs from the raw signal, which improves FFT results.
Why is the window array zero at the first and last points?
The Hann window tapers the signal edges to zero to smooth transitions, visible in step 2 of execution_table where window values at edges are zero.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the value of windowed_signal at index 3?
A4.7524221
B3.8019377
C1.8337814
D0.3765102
💡 Hint
Check the 'windowed_signal' value in step 3 of execution_table at the 4th element (index 3).
At which step is the FFT actually computed?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look for the step where 'spectrum' is assigned using fft(windowed_signal) in execution_table.
If we used a rectangular window (all ones) instead of Hann, how would windowed_signal change at step 3?
AIt would be the same as the original signal
BIt would be all zeros
CIt would be half the original signal
DIt would be the square of the original signal
💡 Hint
Recall windowing multiplies signal by window values; rectangular window is all ones, so signal stays unchanged.
Concept Snapshot
Windowing before FFT:
- Multiply signal by window (e.g., Hann) to reduce edge effects
- Window tapers signal edges to zero
- Compute FFT on windowed signal
- Results in cleaner frequency spectrum
- Use scipy.signal.windows and scipy.fft for implementation
Full Transcript
Windowing before FFT means we first take our raw signal and multiply it by a window function like the Hann window. This step reduces edge effects and spectral leakage that happen when we directly compute FFT on raw data. The window function tapers the signal edges smoothly to zero. After applying the window, we compute the FFT on this modified signal. This process results in a cleaner frequency spectrum output. In the example, we start with a simple signal array, create a Hann window of the same length, multiply them element-wise to get the windowed signal, and then compute the FFT. The execution table shows each step's values, including the window values, the windowed signal, and the final FFT spectrum. This approach is important to get accurate frequency analysis in real-world signals.