The Short-Time Fourier Transform helps us see how the frequencies in a signal change over time. It breaks a long signal into small pieces and finds the frequencies in each piece.
Short-Time Fourier Transform (STFT) in Signal Processing
STFT(signal, window, step_size, n_fft) # signal: the input signal array # window: the size of each small piece # step_size: how much to move the window each time # n_fft: number of points for the Fourier transform
The window size controls the time resolution and frequency resolution trade-off.
Step size controls how much the window moves forward each time, affecting overlap.
stft_result = STFT(signal, window=256, step_size=128, n_fft=256)
stft_result = STFT(signal, window=512, step_size=256, n_fft=512)
This code creates a signal with two different frequencies in two time halves. It then calculates the STFT and shows how frequency content changes over time in a color plot.
import numpy as np import matplotlib.pyplot as plt from scipy.signal import stft # Create a sample signal: 2 seconds, 1000 Hz sample rate fs = 1000 T = 2 t = np.linspace(0, T, T*fs, endpoint=False) # Signal with two frequencies changing over time signal = np.sin(2*np.pi*50*t) * (t < 1) + np.sin(2*np.pi*120*t) * (t >= 1) # Compute STFT f, time_segments, Zxx = stft(signal, fs=fs, window='hann', nperseg=256, noverlap=128) # Plot the magnitude of STFT plt.pcolormesh(time_segments, f, np.abs(Zxx), shading='gouraud') plt.title('STFT Magnitude') plt.ylabel('Frequency [Hz]') plt.xlabel('Time [sec]') plt.colorbar(label='Magnitude') plt.tight_layout() plt.show()
STFT gives a 2D view: time on one axis, frequency on the other.
Choosing window size is a balance: smaller windows show time changes better, larger windows show frequency details better.
STFT breaks a signal into small time pieces and finds frequencies in each piece.
It helps us see how frequencies change over time in signals like audio or sensor data.
Window size and step size affect the detail and smoothness of the analysis.
Practice
Solution
Step 1: Understand STFT function
STFT breaks a signal into small time segments and finds frequency content in each segment.Step 2: Identify main goal
This helps us see how frequencies change over time, unlike a normal Fourier transform which shows overall frequencies.Final Answer:
To analyze how frequencies change over time in a signal -> Option BQuick Check:
STFT = frequency changes over time [OK]
- Confusing STFT with noise removal
- Thinking STFT compresses data
- Mixing STFT with analog conversion
signal with a window size of 256?Solution
Step 1: Recall scipy STFT syntax
The STFT function is in scipy.signal as stft(), and the window size is set by nperseg parameter.Step 2: Match correct syntax
scipy.signal.stft(signal, nperseg=256) uses scipy.signal.stft(signal, nperseg=256), which is correct syntax.Final Answer:
scipy.signal.stft(signal, nperseg=256) -> Option DQuick Check:
Correct function and parameter = scipy.signal.stft(signal, nperseg=256) [OK]
- Using scipy.stft instead of scipy.signal.stft
- Confusing fft() with stft()
- Passing window size as 'window' instead of 'nperseg'
Zxx?
import numpy as np from scipy.signal import stft signal = np.sin(2 * np.pi * 5 * np.linspace(0, 1, 100)) f, t, Zxx = stft(signal, nperseg=20) print(Zxx.shape)
Solution
Step 1: Calculate frequency bins
With nperseg=20, frequency bins = nperseg//2 + 1 = 10 + 1 = 11 rows.Step 2: Calculate time segments
Signal length=100, window=20, default no overlap means about 6 segments (100/20 rounded up).Final Answer:
(11, 6) -> Option AQuick Check:
Zxx shape = (freq bins, time segments) = (11, 6) [OK]
- Confusing nperseg with output shape rows
- Miscounting time segments
- Assuming output shape equals input length
from scipy.signal import stft signal = [1, 2, 3, 4, 5] f, t, Zxx = stft(signal, nperseg=10) print(Zxx)
Solution
Step 1: Check nperseg vs signal length
Signal length is 5, but nperseg=10 is larger, which causes a warning or error in scipy.Step 2: Fix nperseg value
Set nperseg to 5 or less to match signal length and avoid error.Final Answer:
Error: nperseg is larger than signal length; fix by setting nperseg=5 -> Option AQuick Check:
nperseg ≤ signal length to avoid error [OK]
- Ignoring nperseg size error
- Thinking signal type causes error
- Adding unnecessary window parameter
Solution
Step 1: Understand window size effect
Small window size improves time resolution but reduces frequency resolution.Step 2: Understand overlap effect
High overlap keeps time segments close, improving time detail further.Final Answer:
Small window size with high overlap -> Option CQuick Check:
Better time resolution = small window + high overlap [OK]
- Choosing large window for better time resolution
- Ignoring overlap effect
- Assuming low overlap improves time resolution
Ready to prove it?
Pass the test and this pattern moves unknown
