How to choose Spectrogram parameter ?
Show older comments
I have a signal with 3Hz frequency and runs from 0:10 second. The signal is zeroed everywhere except from 2 to 4 and 7 to 8 second in the signal as per the image below. I tried to to get the spectrogram but it didn't give correct representation and accurate one. When i surf the spectrogram i can see the 3 signal but the time is shifted. How to choose the correct parameters for spectrogram ? My code:
t=0:1/50:10;
x=sin(2*pi*t*3);
x[1:100]=0;
x[200:350]=0;
x[400:501]=0;
[s,ff,tt,p]=spectrogram(x,50,25,2048,50);
surf(tt,ff,(p),'edgecolor','none'); axis tight; view(0,90);
Thank you

1 Comment
omar thamer
on 28 Oct 2013
Accepted Answer
More Answers (1)
Wayne King
on 29 Oct 2013
0 votes
NFFT is based on the length of the segment, not the length of the signal. Choosing the segment length is the most important parameter in the spectrogram because that determines and fixes your frequency resolution. Picking a value of NFFT greater than the segment length only provides an interpolation of the DFT estimates at the fundamental (Fourier) frequencies, it does not improve your frequency resolution.
4 Comments
omar thamer
on 29 Oct 2013
Jeremy
on 29 Oct 2013
the nfft is not a function of the sampling rate, the results of any Fourier transform will always be over a frequency range from 0 to the 1/2 the sampling rate (nyquist frequency). The result of the spectrogram in my example will always run from 0 to 25 Hz. the nfft determines the bin width, or the number of steps between 0 and the nyquist frequency. the bin width is equal to the sampling rate/nfft. In the case of a spectrogram you will need to balance the need for time vs. frequency resolution for the nfft you use. an nfft equal to the length of your time history will give the greatest frequency resolution, but only one time step...
This example makes the inputs a little more clear. you can increase the last nfft to smooth out the display, but it just zero padding so it is not really increasing you frequency resolution.
fs=50; t=1/fs:1/fs:10; x=sin(2*pi*t*3); x(1:100)=0; x(200:350)=0; x(400:501)=0; nfft=100; [s,ff,tt,p]=spectrogram(x,hanning(nfft),nfft/2,nfft*2,fs); surf(tt,ff,(p),'edgecolor','none'); axis tight;view(0,90)
omar thamer
on 29 Oct 2013
Jeremy
on 29 Oct 2013
your spectrogram is very odd. it would appear there is something like a burst of a square wave with a consistent duration starting at random intervals. I'm not sure about the blue stripes, they are are consistent over time when there is no signal, they might be side lobes due to zero padding. There is also a DC component to the signal; it is not zero when there is no square wave.
Categories
Find more on Time-Frequency Analysis in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!