I have many EEG signals which record at 100 Hz. I use FFT on them with fft Matlab toolbox. Now I wanted to get a Spectrogram. As I searched and read a lot, I figure that I should window my signal and do DFT on each window, which gives me a 2D Matrix, that its rows are frequency and its columns are time. But my problems are these:

1) What should be the length of my windows?

2) Should I use a spectrogram Matlab toolbox which performs STFT?!

3) How should I do the windowing?

My purpose is to get a spectrogram then perform Wavelet Transform which performs dimension reduction and then I pass that to a Neural Network as Input.

I don't know for this purpose, what size of windowing should I use!

Any help could be great and thankful.

  • $\begingroup$ Does this answer your question? Discrete and Continuous Signals $\endgroup$
    – jithin
    Apr 5, 2020 at 10:11
  • $\begingroup$ No, it doesn't. That question is very general and about the Theorem of Discrete and Continuous Signals, but my question is specific for EEG signals with Fast Fourier Transform and Spectrogram. I know that theorem and I don't need that. My question is about windowing to perform Spectrogram. $\endgroup$
    – M.Barandov
    Apr 5, 2020 at 10:41

2 Answers 2


1) Length of your window will determine the frequency resolution in each row. Since you mentioned you have sampled at 100Hz, if window length is 10, then each row will be having resolution of 100/10=10Hz. If you increase your window size to 20, then each row will have resolution of 100/20=5Hz.

2) MATLAB has spectrogram command to get the spectrogram as 2-D array. It's documentation/help is very comprehensive.(https://in.mathworks.com/help/signal/ref/spectrogram.html)

3) Windowing operation is just taking $W$ samples and multiplying by them by the window size $W$ sample by sample $x[n]w[n]\,0\le n\le W-1$. After FFT, you move the window by step size of $L$ samples and do the windowing and FFT again to get the spectrum at next time interval. $L$ will determine how smoothly your spectrogram varies across time. If $L$ is too high, you will find the spectrogram is like a grid with no smooth transition in time. If too less, you will over compute leading high memory and computation requirements.

enter image description here

EDIT: Adding more details on how $W$ and $L$ will affect spectrogram. Consider 2 closely spaced signals, $x_1 = e^{j0.5\pi n}$ and $x_2 = e^{j0.6\pi n}$ , along with white gaussian noise $w$. There are 1000 samples of this composite signal.

If $W=128$, you can resolve these two closely spaced frequencies in the spectrogram. If $W=64$, it is difficult to visually resolve these 2 closely spaced frequencies. It appears as a thick single line. It is illustrated by following MATLAB code and plot

clear all
close all

x = x1+x2+w;

W = 128;

spectrogram(x,W, L,W,'yaxis'); 
title('L=50, W=128')

W = 64;
L = 50;
spectrogram(x,W, L,W,'yaxis'); 
title('L=50, W=64')

enter image description here

enter image description here

  • $\begingroup$ Thanks for your good replay, but could you please explain case 3 a little more and clearer? For example, how should I consider W and L? If you could expand case 3 for more explanation it would be really great. $\endgroup$
    – M.Barandov
    Apr 6, 2020 at 9:39
  • $\begingroup$ @M.Barandov I have illustrated on choice of $W$ on how it affects spectrogram. I will try to create for $L$ also. $\endgroup$
    – jithin
    Apr 6, 2020 at 10:05
  • $\begingroup$ Wow! thanks, man! you are amazing. I'm very thankful for your great explanation with code and plot. Thanks a million. $\endgroup$
    – M.Barandov
    Apr 6, 2020 at 10:40
  • $\begingroup$ Last question, from where I can find the better value of W and L? In case 1 you said 10 or 20 but in the example you set 128 and 64. $\endgroup$
    – M.Barandov
    Apr 6, 2020 at 11:12
  • $\begingroup$ So that depends on your requirement on how sharp you want your plot to be. I could have also taken 10 or 20 but plot would have looked really bad in my case. You can experiment with your data using different values of $W$, $L$. Since you mentioned in other comment yo have 22 hours of data sampled at 100Hz, it is a huge amount of data. So take about 10000 points and experiment with $W=64$, $L=50$ etc. See if it meets your requirements. $\endgroup$
    – jithin
    Apr 6, 2020 at 11:23

It would depend on the resolution you are looking for in the frequency /DFT. Sampling at 100HZ, one would get time domain samples every 10ms. For a decent DFT resolution you would be looking around 64 samples. Meaning 640ms, 140ms (14 samples) would be a good enough overlap in this scenario. So you could take the DFT of 64 samples, retain the last 14 time domain samples and add 36 new samples from next data frame (each data frame Being 36 samples, except the first one) and so on.

If looking for a finer DFT resolution go for higher number of time domain samples to constitute a frame and define a decent overlap as explained above.

  • $\begingroup$ Thanks for your reply, but I was wondering what about FFT that I have used?! Right Now I have a matrix of FFT so if I'm going to do that windowing, what happens to the FFT? $\endgroup$
    – M.Barandov
    Apr 6, 2020 at 9:37
  • $\begingroup$ What is the resolution of the FFT you have taken? $\endgroup$ Apr 6, 2020 at 9:51
  • $\begingroup$ My data is recorded in 22 hours, I get the first 10 seconds of it and remove low frequency and high-frequency noises, then I do this FFT = abs(fft(denoised,1000))/1000; and the result was a 1x1000 double value Matrix. I get FFT so then I can get Spectrogram and after that, I perform 2D wavelet Transform which does dimension reduction and I pass that to a neutral network. $\endgroup$
    – M.Barandov
    Apr 6, 2020 at 9:57
  • $\begingroup$ Firstly, let me explain why we bother to get a spectrogram instead of FFT. Suppose you had a data of samples, to see it's spectral behaviour you could simply take a big enough FFT. But that would not show us at what time which frequency components are more active. Think of a voice signal over a considerable time, to capture the pitches at different times, you would require to window "in time domain" and then take a FFT of this short signal. In your case since you have already taken the FFT of the entire length so hence you have lost time time dependent spectral behaviour. $\endgroup$ Apr 6, 2020 at 10:06
  • $\begingroup$ Take the full IFFT of the FFT you have. Break down the time domain signal into data frames as explained in the answer and then take FFT over these frames. Hope that answers the question $\endgroup$ Apr 6, 2020 at 10:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.