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,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,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 where is a real-valued scalar defined as follows.
For the one-sided PSD,
where denotes the window function (Hamming by default) and 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, is replaced in the denominator by .
surf(T,F,10*log10(abs(P))); axis tight; view(0,90);
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.
Compute and display the PSD of each segment of a quadratic chirp that starts at 100 Hz and crosses 200 Hz at s. Specify a sampling rate of 1 kHz.
T = 0:0.001:2; X = chirp(T,100,1,200,'q'); spectrogram(X,128,120,128,1E3,'yaxis') title 'Quadratic chirp'
Compute and display the PSD of each segment of a linear chirp that starts at DC and crosses 150 Hz at s. Specify a sampling 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); surf(T,F,10*log10(P),'edgecolor','none') axis tight, view(0,90) xlabel 'Time (s)', ylabel 'Frequency (Hz)', title 'Linear chirp'
 Oppenheim, Alan V., Ronald W. Schafer, and John R. Buck. Discrete-Time Signal Processing. 2nd Ed. Upper Saddle River, NJ: Prentice Hall, 1999.
 Rabiner, Lawrence R., and Ronald W. Schafer. Digital Processing of Speech Signals. Englewood Cliffs, NJ: Prentice-Hall, 1978.