Short-Time Fourier Transform – Spectrogram

3.9 Short-Time Fourier Transform – Spectrogram

The short-time or short-term Fourier transform (STFT) breaks a long signal into small segments, optionally overlapped and/or windowed, and finds the DFT of each (windowed) segment separately to record the local spectra in a matrix with frequency/time indices:

N −1

STFT{x[n]} = X[k, n] =

[m + n]w[m]W N

m→m−n n+N −1

= k x (m−n)

[m]w[m − n]W N

for k = 0, 1, · · · , N − 1 (3.9.1b)

m=n

%sig03f20.m : to plot Fig.3.20 clear, clf T=0.1; Fs=1/T; % Sampling period and Sampling frequency w1=25 ∗ pi/16; w2=30 ∗ pi/16; w3=40 ∗ pi/16; w4=60 ∗ pi/16; n=[0:31]; x=[cos(w1 ∗ T ∗ n) sin(w2 ∗ T ∗ n) cos(w3 ∗ T ∗ n) sin(w4 ∗ T ∗ n)]; Nx=length(x); nn=0:Nx-1; % Length and duration (period) of the signal N=32; kk=0:N/2; ff=kk ∗ Fs/N; % DFT size and frequency range wnd= hamming(N).’; % Hamming window of length N Noverlap=N/4; % the number of overlap M=N-Noverlap; % the time spacing between DFT computations for i=1:fix((Nx-Noverlap)/M)

xiw= x((i-1) ∗ M+[1:N]). ∗ wnd; % ith windowed segment Xi= fft(xiw); % DFT X(k,i) of ith windowed segment X(:,i)= Xi(kk+1).’; % insert X(0:N/2,i) into the ith column tt(i)=(N/2+(i-1) ∗ M) ∗ T;

end % Use the MATLAB signal processing toolbox function specgram() [X sp,ff1,tt1] = spectrogram(x,wnd,Noverlap,N,Fs,’yaxis’); % Any discrepancy between the above result and spectrogram()? discrepancy= norm(X-X sp)/norm(X sp) figure(2), clf, colormap(gray(256)); subplot(221), imagesc(tt,ff,log10(abs(X))), axis xy subplot(222), imagesc(tt1,ff1,log10(abs(X sp))), axis xy % specgram(x,N,Fs,wnd,noverlap) in MATLAB of version 6.x

where the window sequence w[m] is used to select a finite-length (local) segment from the sliding sequence x[m + n] and possibly to reduce the spectral leakage.

Note that the frequency and time denoted by frequency index k and time index n of

X [k, n] are

3.9 Short-Time Fourier Transform – Spectrogram 181 x [n] 0 20 40 60 0 100 80 120

(a) A non-stationary signal with time-varying frequency content

w[m] m

0 31 ω [m-24]

x [m]w [m ] ω [m-48]

48 78 m x [m]w [m-24] (b) Sliding windows and windowed signals

x [m]w [m-48]

N = 32-point DFT N = 32-point DFT

N = 32-point DFT

X [k,n][dB] X [k,16] X [k,40] X [k,64] X [k,88] X [k,112] k

kk 112

(c) The short-time Fourier transform (STFT ) obtained with DFT size N = 32 and Noverlap = 8

NT 5 5 5

F requency

F F requency requency

2 4 Time t = nT 6 8 10 12 2 4 Time t = nT 6 8 10

(d1) X [k,n] obtained with N = 32 and

(d2) X [k,n] obtained with N = 48 and Noverlap = 8 Noverlap = 12

Fig. 3.20 Short-time Fourier transforms with different time/frequency resolution

f s [Hz] and t n

= 2 +n T [sec] (3.9.2)

NT

where t n is set to the center of the sliding window (Fig. 3.20(b)). If we take the STFT not for every n but only for n = i M (a multiple of M), each segment

gets overlapped with its neighboring one(s) by N overlap = N − M samples.

182 3 Discrete-Time Fourier Analysis Note that M determines the width of interval time between the DFT computations.

The MATLAB function “ spectrogram()” can be used to find the STFT (see Appendix E.8).

We run the above MATLAB program “sig03f20.m” to get the STFT X [k, n] of x[n] (Fig. 3.20(a)) as depicted in Fig. 3.20(d1) (with DFT size N = 32 and

N overlap = 8) and (d2) (with N = 48 and N overlap = 12), which show that the frequency becomes higher as time goes by. Such a STFT can be used to determine

and display the spectrum of a non-stationary signal (such as speech or music) whose frequency content changes with time.

Now, comparing Fig. 3.20(d1) and (d2), let us consider the effect of segment or window duration NT on the time and frequency resolutions of the STFT. We might say that the time resolution is NT [sec] (the segment duration) and the frequency resolution is 1/N T [Hz]. This implies that if we increase or decrease the segment duration, the time/frequency resolution gets poorer/better or better/poorer, show- ing the trade-off between the time and frequency resolutions. It is an illustration of uncertainty principle (Remark 2.9(1)) that both time resolution and frequency resolution cannot be improved. Although the boundary of the uncertainty principle (best simultaneous resolution of both) is reached with a Gaussian window function, the wavelet transform or multi-resolution analysis was devised, which can give a good time/frequency resolution for high/low-frequency components.