# How to get Spectrogram after fft in matlab?

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.

• Does this answer your question? Discrete and Continuous Signals – jithin Apr 5 '20 at 10:11
• 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. – M.Barandov Apr 5 '20 at 10:41

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. 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

clc
clear all
close all

N=1000;
x1=exp(1i*0.5*pi*(0:N-1));
x2=exp(1i*0.6*pi*(0:N-1));
w=0.05*(randn(1,N)+1i*randn(1,N));
x = x1+x2+w;

W = 128;
L=50;

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

W = 64;
L = 50;
figure(2)
spectrogram(x,W, L,W,'yaxis');
title('L=50, W=64')  • 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. – M.Barandov Apr 6 '20 at 9:39
• @M.Barandov I have illustrated on choice of $W$ on how it affects spectrogram. I will try to create for $L$ also. – jithin Apr 6 '20 at 10:05
• Wow! thanks, man! you are amazing. I'm very thankful for your great explanation with code and plot. Thanks a million. – M.Barandov Apr 6 '20 at 10:40
• 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. – M.Barandov Apr 6 '20 at 11:12
• 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. – jithin Apr 6 '20 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.

• 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? – M.Barandov Apr 6 '20 at 9:37
• What is the resolution of the FFT you have taken? – Dsp guy sam Apr 6 '20 at 9:51
• 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. – M.Barandov Apr 6 '20 at 9:57
• 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. – Dsp guy sam Apr 6 '20 at 10:06
• 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 – Dsp guy sam Apr 6 '20 at 10:34