5

The MATLAB documentation examples for the spectrogram function gives examples that have the frequency axis set to [0 500]. Can I change this to something like [0 100]? Obviously running the axis command will do this for me, but that adjusts the end result and "blows up" the resultant plot, make it pixelated. I am basically looking to build a spectrogram that only looks for frequencies between 0-100, not rescaling after building the spectrogram.

Here's an example from that documentation:

T = 0:0.001:2;
X = chirp(T,0,1,150);
spectrogram(X,256,250,256,1E3,'yaxis');

This produces the following: Linear chirp spectrogram

Everything below 350Hz is unneeded. Is there a way to not include everything between 350 to 500 when building the spectrogram, rather than adjusting axes after the fact?

Nick
  • 4,302
  • 2
  • 24
  • 38
Dang Khoa
  • 5,693
  • 8
  • 51
  • 80

2 Answers2

7

From the documentation:

[S,F,T] = spectrogram(x,window,noverlap,F) uses a vector F of frequencies in Hz. F must be a vector with at least two elements. This case computes the spectrogram at the frequencies in F using the Goertzel algorithm. The specified frequencies are rounded to the nearest DFT bin commensurate with the signal's resolution. In all other syntax cases where nfft or a default for nfft is used, the short-time Fourier transform is used. The F vector returned is a vector of the rounded frequencies. T is a vector of times at which the spectrogram is computed. The length of F is equal to the number of rows of S. The length of T is equal to k, as defined above and each value corresponds to the center of each segment.

Does that help you?

im so confused
  • 2,091
  • 1
  • 16
  • 25
  • Maybe. I've been poring over the documentation trying to understand what the function is doing; I don't have a background in signal analysis so this is all pretty new to me. Could you perhaps provide an example with the output spectrogram plot? – Dang Khoa Oct 09 '12 at 21:03
  • @DangKhoa Sorry, I don't have MATLAB in front of me. If this is still unresolved later I'll try and produce one for you. However, what this is trying to say is that if you provide it with a vector of frequencies such as `F = [1 10 20 50 100];` and pass it into the `spectrogram` function, it will only compute the spectrogram at those frequencies (which is what you want, i think) – im so confused Oct 09 '12 at 21:07
  • Ended up using the form `spectrogram(x,window,noverlap,F,fs, 'yaxis')` to do this. Thank you! – Dang Khoa Oct 09 '12 at 22:36
  • 1
    @DangKhoa excellent! glad it worked out for you! if you have some time, maybe you could post some example code in your question to help out people who view it later. But i didn't even have time to open up matlab so i shouldn't be talking hehehe – im so confused Oct 10 '12 at 14:21
  • `fs must be the fifth input to spectrogram. To input a sample rate and still use the default values of the preceding optional arguments, specify these arguments as empty, [].` From the newer Matlab docs, in case someone is searching for how to keep the default arguments as well – Thomas Dec 14 '20 at 18:08
-1

The FFT is so fast that it is better to increase the resolution and then just discard the unwanted data. If you need better spectral resolution (more frequency bins) then increase the FFT size. To get smoother looking spectrum in time dimension, increase the noverlap value to reduce the increments for each consequtive FFT. In this case you would not specify the F. If FFT size is 1024 then you get 1024/2+1 frequency bins.

FFTN = 512;
start = 512*(350/500); % Only care about freq bins above this value
WIN_SIZE = FFTN;
overlap = floor(FFTN*0.8);
[~,F,T,P] = spectrogram(y, WIN_SIZE, overlap, FFTN);
f = 0:(length(F)-1);
f = f*((Fs/2)/length(F));
P = P(start:512,:);
f = f(1,start:512);
imagesc(T,f,10*log10(P),[-70 20]); 
kashiraja
  • 740
  • 11
  • 24