Spectrogram using short-time Fourier transform


S = spectrogram(x)
S = spectrogram(x,window)
S = spectrogram(x,window,noverlap)
S = spectrogram(x,window,noverlap,nfft)
S = spectrogram(x,window,noverlap,nfft,fs)
[S,F,T] = spectrogram(...)
[S,F,T] = spectrogram(x,window,noverlap,F)
[S,F,T] = spectrogram(x,window,noverlap,F,fs)
[S,F,T,P] = spectrogram(...)


spectrogram, when used without any outputs, plots a spectrogram or, when used with an S output, returns the short-time Fourier transform of the input signal. To create a spectrogram from the returned short-time Fourier transform data, refer to the [S,F,T,P] syntax described below.

S = spectrogram(x) returns S, the short time Fourier transform of the input signal vector x. By default, x is divided into eight segments. If x cannot be divided exactly into eight segments, it is truncated. These default values are used.

  • window is a Hamming window of length nfft.

  • noverlap is the number of samples that each segment overlaps. The default value is the number producing 50% overlap between segments.

  • nfft is the FFT length and is the maximum of 256 or the next power of 2 greater than the length of each segment of x. Instead of nfft, you can specify a vector of frequencies, F. See below for more information.

  • fs is the sampling frequency, which defaults to normalized frequency.

Each column of S contains an estimate of the short-term, time-localized frequency content of x. Time increases across the columns of S and frequency increases down the rows.

If x is a length Nx complex signal, S is a complex matrix with nfft rows and k columns, where for a scalar window

k = fix((Nx-noverlap)/(window-noverlap))

or if window is a vector

k = fix((Nx-noverlap)/(length(window)-noverlap))

For real x, the output S has (nfft/2+1) rows if nfft is even, and (nfft+1)/2 rows if nfft is odd.

S = spectrogram(x,window) uses the window specified. If window is an integer, x is divided into segments equal to that integer value and a Hamming window is used. If window is a vector, x is divided into segments equal to the length of window and then the segments are windowed using the window functions specified in the window vector. For a list of available windows see Windows.

    Note:   To obtain the same results for the removed specgram function, specify a 'Hann' window of length 256.

S = spectrogram(x,window,noverlap) overlaps noverlap samples of each segment. noverlap must be an integer smaller than window or if window is a vector, smaller than the length of window.

S = spectrogram(x,window,noverlap,nfft) uses the nfft number of sampling points to calculate the discrete Fourier transform. nfft must be a scalar.

S = spectrogram(x,window,noverlap,nfft,fs) uses fs sampling frequency in Hz. If fs is specified as empty [], it defaults to 1 Hz.

[S,F,T] = spectrogram(...) returns a vector of frequencies, F, and a vector of times, T, at which the spectrogram is computed. F has length equal to the number of rows of S. T has length k (defined above) and the values in T correspond to the center of each segment.

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

[S,F,T] = spectrogram(x,window,noverlap,F,fs) uses a vector F of frequencies in Hz as above and uses the fs sampling frequency in Hz. If fs is specified as empty [], it defaults to 1 Hz.

[S,F,T,P] = spectrogram(...) returns a matrix P containing the power spectral density (PSD) of each segment. For real x, P contains the one-sided modified periodogram estimate of the PSD of each segment. For complex x and when you specify a vector of frequencies F, P contains the two-sided PSD.

spectrogram(...,FREQLOCATION) specifies which axis to use as the frequency axis in displaying the spectrogram. Specify FREQLOCATION as a trailing string argument. Valid options are 'xaxis' or 'yaxis'. The strings are not case sensitive. If you do not specify FREQLOCATION, spectrogram uses the x-axis as the frequency axis by default.

The elements of the PSD matrix P are given by P(i,j)=k|S(i,j)|2 where k is a real-valued scalar defined as follows.

  • For the one-sided PSD,


    where w(n) denotes the window function (Hamming by default) and Fs is the sampling frequency. At zero and the Nyquist frequencies, the factor of 2 in the numerator is replaced by 1.

  • For the two-sided PSD,


    at all frequencies.

  • If the sampling frequency is not specified, Fs is replaced in the denominator by 2π.

spectrogram(...) plots the PSD estimate for each segment on a surface in a figure window. The plot is created using

axis tight;

Using spectrogram(...,'freqloc') syntax and adding a 'freqloc' string (either 'xaxis' or 'yaxis') controls where the frequency axis is displayed. Using 'xaxis' displays the frequency on the x-axis. Using 'yaxis' displays frequency on the y-axis and time on the x-axis. The default is 'xaxis'. If you specify both a 'freqloc' string and output arguments, 'freqloc' is ignored.


expand all

Power Spectral Densities of Chirps

Compute and display the PSD of each segment of a quadratic chirp that starts at 100 Hz and crosses 200 Hz at t = 1 s. Specify a sample rate of 1 kHz.

T = 0:0.001:2;
X = chirp(T,100,1,200,'q');
title('Quadratic chirp')

Compute and display the PSD of each segment of a linear chirp that starts at DC and crosses 150 Hz at t = 1 s. Specify a sample rate of 1 kHz.

T = 0:0.001:2;
X = chirp(T,0,1,150);
[S,F,T,P] = spectrogram(X,256,250,256,1E3);

sf = surf(T,F,10*log10(P))
sf.EdgeColor = 'none';
axis tight
xlabel('Time (s)')
ylabel('Frequency (Hz)')
title('Linear chirp')
sf = 

  Surface with properties:

       EdgeColor: [0 0 0]
       LineStyle: '-'
       FaceColor: 'flat'
    FaceLighting: 'flat'
       FaceAlpha: 1
           XData: [1x291 double]
           YData: [129x1 double]
           ZData: [129x291 double]
           CData: [129x291 double]

  Use GET to show all properties

More About


[1] Oppenheim, Alan V., Ronald W. Schafer, and John R. Buck. Discrete-Time Signal Processing. 2nd Ed. Upper Saddle River, NJ: Prentice Hall, 1999.

[2] Rabiner, Lawrence R., and Ronald W. Schafer. Digital Processing of Speech Signals. Englewood Cliffs, NJ: Prentice-Hall, 1978.

Was this topic helpful?