I'm trying to understand STFT overlapping, why segments are concatenated and what are the consequences of this.
My implementation (from other questions and repositories found) of the STFT in Python is:
def stft(x, L, overlap, window): # Linear Spectrum (LS) [V]
opoints = int(L*overlap)
hopsize = L - opoints
X = np.array([(2 * np.abs(np.fft.fft(window*x[i:i+L])[:L//2])) / np.sum(window)
for i in range(0,len(x)-L,hopsize)])
return X
where, x is the time-domain signal sampled at 44.1kHz, L is the FFT window size (4096), overlap is the overlapping factor [0, 1), and window is the window funtion used (Hanning). The absolute value of half the complex dft is scaled by 2/sum(win)
so that the units of the linear spectrum are the same as for the input signal (V).
I used to understand the frequency resolution is fs/L
and the width of the time slices or time resolution is L/fs
, so that the number of time windows is N/L
. However, if using overlapping (overlap!=0)
, the number of time windows increases ~(N/hopsize)-1
.
- Why are overlapped segments concatenated instead of added and what are the consequences of this regarding the time axis? (See picture below)
- Is compressing the concatenated segments into the original duration of the signal correct? (i.e 3 segments of 1 second into 2 seconds, see picture below)
EDIT1:
Now, I do understand:
Overlap-shift and compression of the time axis of the overlapped segment into the original duration is one way to make a smoother spectrograph image
Overlap is needed in order to avoid lossy signals caused by the window
However, from @user31990 comment I still need to clarify:
- What's the difference between the different methods/implementations?
Implementations I've found/understood:
- Overlap and concatenate side-by-side the overlapped blocks (increasing the number of time windows and compressing the x time axis to the original duration)
- Overlap adding the segments of the block that are overlapped together (the number of time windows doesn't increase = N/L)
- Overlap save (zero-pad the beginning of the block and discard the transformed segment, the number of time windows doesn't increase)
- Overlap shift with zero-pad, one of the overlapped segments is zero-padded and it is added to the non zero-padded segment of the following block (the number of time windows doesn't increase)
My implementation is an example of the overlap and concatenate method.
This and Google's pytfd stft implementation are examples of the overlap shift with zero-pad method.