# How do I optimize the window lengths in STFT?

I have many EEG signals and I want to analyze them using linear methods such as STFT (Short Time Fourier Transform). In STFT , How can I optimize the analysis window length, to reflect the frequency spectrum of each analysis window in a proper way?

• If you're going to optimize something, you need an objective measure. Your question doesn't really say how you measure the "optimal" window length. What do you mean by reflecting the frequency spectrum in a "proper way?" – Jason R Sep 20 '11 at 0:50
• @Maen: you want to analyze EEG signals to do what with them? – Sriram Sep 23 '11 at 9:26
• Votes and best answer validation are required – Laurent Duval Jul 28 at 12:04

This is the classic "uncertainty principle" of the Fourier Transform. You can either have high resolution in time or high resolution in frequency but not both at the same time. The window lengths allow you to trade off between the two.

If you want to detect "events" in your EEG signal with a resolution of say 10ms, then this should be your window length. This will give you a frequency resolution of about 100 Hz.

The optimum window length will depend on your application. If your application is such that you need time domain information to be more accurate, reduce the size of your windows. If the application demands frequency domain information to be more specific, then increase the size of the windows. As Hilmar mentioned, the Uncertainty Principle really leaves you with no other choice. You cannot get perfect resolution in both domains at once. You can get perfect resolution in only one domain at the cost of zero resolution in the other (time and frequency domains) or in-between resolution, but in both domains.

I do not know if this answers your question since you asked specifically about STFT. You could try to use wavelet transforms to get at the information in the signal. Wavelet transforms will give you resolution over a much larger range by analyzing the signal at multiple window resolutions.

I don't know EEG but the basic (maybe I should say fundamental) issue when using the STFT is choosing a proper window length. If your EEG is periodic and you want to resolve the fundamental and harmonics you should use a 'long' window. If you instead want to detect the onset or presence of some event or you're more interested in the envelope of the spectrum you can use a 'short' window.

I have spent a lot of time optimizing windows in time-frequency analysis or filter-banks. One can optimize them for detection, denoising, signal separation... It is very dependant on the application. As time-frequency analysis is generally redundant, optimizing analysis or synthesis windows are different tasks. And length only one parameter in window design.

The problem is even more complex as the discretized formulation of optimality is much more complicated than the continuous time-domain case (see e.g. An optimally concentrated Gabor transform for localized time-frequency components).

So my present practical rule of thumb is: start with a window shape and length that seems ok. Then repeat the analysis with two windows with twice and half the length, and combine the results.

Usually wide window size gives better frequency resolution but poor time resolution and vice-versa. Look at this example where I generated a spectrogram of a sine wave with 5kHz and sample rate of 22050Hz, from my C++ code.

The above spectrogram has window size of 2048 samples and overlap of 1024 samples.

Look at this spectrogram:

This one has window size of 512 samples and overlap of 256 samples.

Can you see the difference? The first one has better frequency resolution than the second one. But the second one has better time resolution when compared to the first one. So, choosing window size depends upon your application. If you are dealing with speech samples to track pitch, choosing larger window size should be the appropriate one.

• Simple sine wave is no good for explaining the frequency resolution. Even sweep sine is better for that. – jojek Jan 26 '16 at 17:03
• So what kind of input would be good, according to you? – vishnu Jan 26 '16 at 17:56
• I already did mention that above. – jojek Jan 26 '16 at 17:58
• You mean sweep sine?. Is there any other kind of signal that I can use? I have a presentation on spectrogram and would like to display some good stuffs in my slide about windows. Would appreciate any tips :) – vishnu Jan 26 '16 at 18:00