0
$\begingroup$

The following code generates a spectrogram as shown in the image. The matrix s which is the spectrogram is of size 129x40 for a signal of length N=80. Based on my understanding, this means that the number of frames, frames = 40 which forms the X axis of the spectrogram image. Using the following values
step_size =1; w=2 (window), into the formula of nfft (the number of frequency datapoints in each frame), I obtain :

  • nfft = 2 * ((step_size) - 1) = 78 (Y Axis)and
  • frames = floor((N - w) / step_size) + 1 = 40. (X axis)

So frames form the X axis of the spectrogram and labelled as Time in the graph/image plot.

Q1: Can somebody please explain how the matrix s is formed and the meaning of the dimensions? The following code segments the time series into chunks of data by sliding a window of size w=2 that creates 40 frames each containing nfft = 78 frequency datapoints. I thought there should be nfft = 78 data points in each frame of size w=2. I recall that we take half of the frequency points since the other half is repeated due to the complex conjugate. I don't quite understand what the Y axis 129 points denote and how they come.

Q2: In the Matlab documentation, it is mentioned that nfft has to greater than 256. But I am getting 78. So is my spectrogram construction incorrect?

Please correct me where wrong.

t = 0:0.001:2; N=80; %signal length (shortening it)
signal  = chirp(t,100,1,200,'quadratic');
signal  = signal(1:N);
  
 
noverlap = 0; 
w = 2;
noverlap = 0;   
s=spectrogram(signal,w,noverlap);
specto= (20*log10(abs(s)));
imagesc(specto)
colormap Jet;
h = colorbar;
set(get(h,'label'),'string','power (dB)/freq (Hz)');
axis image;
xlabel('Time','FontSize',14,'FontWeight','bold');
ylabel('Frequency','FontSize',14,'FontWeight','bold');

enter image description here

$\endgroup$
8
  • $\begingroup$ Why do you use a window of length 2? nfft is the size of FFT and is generally equal to the window length if no zero-padding applied. And I still don't know how do you get the number 78. The number of frequency bins should be nfft / 2 + 1 for an even length FFT. As for Q2, I think MATLAB performs a zero-padding when nfft is smaller than 256. $\endgroup$ – ZR Han Feb 4 at 5:10
  • $\begingroup$ Thank you for your comment. I just gave a window size of 2 as an example since in reality I have very few data points to work with. So, this will give quite a decent number of frames or the Time / X axis component. The spectrogram is of dimension x*y = '129*40'. Can you please explain how the Y axis which is 129 points comes and does this mean that in each frame there are 129 frequency points? If so then what is nfft? $\endgroup$ – Srishti M Feb 4 at 5:15
  • $\begingroup$ Yes, 129 is the number of frequency bins in each frame and is equal to nfft / 2 + 1 when nfft is even. In MATLAB's document, it says that nfft is greater than 256, so you get a spectrogram with Y axis at least 129 point. Remove the third line in your code and change w to 128, 256, 512, and 1024 respectively and you will find it out. If window size (or nfft) is smaller than 256, it will automatically apply a zero-padding and then a 256-point FFT. $\endgroup$ – ZR Han Feb 4 at 5:24
  • $\begingroup$ Thank you for the clarification. So you think, my spectrogram is correct for an example where number of data N = 80 only since I do not have N=128 datapoints to set w>=128? Do you think there is a better w so that I still get a reasonable Time or X axis other than what I have done? Also, is matlab doing discrete of continuous fourier transform? $\endgroup$ – Srishti M Feb 4 at 5:55
  • $\begingroup$ 1. Yes. 2. Since you don’t have enough length of data, you will have a poor frequency resolution, which can’t be improved by zero-padding. If you still want a relatively high frequency resolution, you can try some modern spectral estimation methods, such as ARMA, instead of FFT-based methods. 3. What do you mean by discrete of continuous Fourier transform? $\endgroup$ – ZR Han Feb 4 at 6:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.