Signal Processing Toolbox    
specgram

Time-dependent frequency analysis (spectrogram)

Syntax

Description

specgram computes the windowed discrete-time Fourier transform of a signal using a sliding window. The spectrogram is the magnitude of this function.

B = specgram(a) calculates the windowed discrete-time Fourier transform for the signal in vector a. This syntax uses the default values:

nfft specifies the FFT length that specgram uses. This value determines the frequencies at which the discrete-time Fourier transform is computed. fs is a scalar that specifies the sampling frequency. window specifies a windowing function and the number of samples specgram uses in its sectioning of vector a. numoverlap is the number of samples by which the sections overlap. Any arguments that you omit from the end of the input parameter list use the default values shown above.

If a is real, specgram computes the discrete-time Fourier transform at positive frequencies only. If n is even, specgram returns nfft/2+1 rows (including the zero and Nyquist frequency terms). If n is odd, specgram returns nfft/2 rows. The number of columns in B is

If a is complex, specgram computes the discrete-time Fourier transform at both positive and negative frequencies. In this case, B is a complex matrix with nfft rows. Time increases linearly across the columns of B, starting with sample 1 in column 1. Frequency increases linearly down the rows, starting at 0.

B = specgram(a,nfft) uses the specified FFT length nfft in its calculations.

[B,f] = specgram(a,nfft,fs) returns a vector f of frequencies at which the function computes the discrete-time Fourier transform. fs has no effect on the output B; it is a frequency scaling multiplier.

[B,f,t] = specgram(a,nfft,fs) returns frequency and time vectors f and t respectively. t is a column vector of scaled times, with length equal to the number of columns of B. t(j) is the earliest time at which the jth window intersects a. t(1) is always equal to 0.

B = specgram(a,nfft,fs,window) specifies a windowing function and the number of samples per section of the x vector. If you supply a scalar for window, specgram uses a Hann window of that length. The length of the window must be less than or equal to nfft.

B = specgram(a,nfft,fs,window,numoverlap) overlaps the sections of x by numoverlap samples.

You can use the empty matrix [] to specify the default value for any input argument. For example,

is equivalent to

but with a sampling frequency of 10,000 Hz instead of the default 2 Hz.

specgram(...) with no output arguments displays the scaled logarithm of the spectrogram in the current figure window using

The axis xy mode displays the low-frequency content of the first portion of the signal in the lower-left corner of the axes. specgram uses fs to label the axes according to true time and frequency.

B = specgram(a,f,fs,window,numoverlap) computes the spectrogram at the frequencies specified in f, using either the chirp z-transform (for more than 20 evenly spaced frequencies) or a polyphase decimation filter bank. f is a vector of frequencies in hertz; it must have at least two elements.

Examples

Display the spectrogram of a digitized speech signal:

Algorithm

specgram calculates the spectrogram for a given signal as follows:

  1. It splits the signal into overlapping sections and applies the window specified by the window parameter to each section.
  2. It computes the discrete-time Fourier transform of each section with a length nfft FFT to produce an estimate of the short-term frequency content of the signal; these transforms make up the columns of B. The quantity (length(window) - numoverlap) specifies by how many samples specgram shifts the window.
  3. For real input, specgram truncates the spectrogram to the first nfft/2 + 1 points for nfft even and (nfft + 1)/2 for nfft odd.

Diagnostics

An appropriate diagnostic message is displayed when incorrect arguments are used:

See Also

cohere, csd, pwelch, tfe

References

[1] Oppenheim, A.V., and R.W. Schafer, Discrete-Time Signal Processing, Prentice-Hall, Englewood Cliffs, NJ, 1989, pp. 713-718.

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


  sosfilt sptool