Signal Processing Toolbox | Search Help Desk |

specgram | Examples See Also |

Time-dependent frequency analysis (spectrogram).

Syntax

B = specgram(a) B = specgram(a,nfft) [B,f] = specgram(a,nfft,Fs) [B,f,t] = specgram(a,nfft,Fs) B = specgram(a,nfft,Fs,window) B = specgram(a,nfft,Fs,window,noverlap) specgram(a) B = specgram(a,f,Fs,window,noverlap)

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 spectrogram 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`

. `noverlap`

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
k = fix((n-noverlap)/(length(window)-noverlap))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. Specify `nfft`

as a power of 2 for fastest execution.
```
[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 `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 Hanning window of that length. The length of the window must be less than or equal to `nfft`

; `specgram`

zero pads the sections if the length of the window exceeds `nfft`

.
```
B = specgram(a,nfft,Fs,window,noverlap)
```

overlaps the sections of `x`

by `noverlap`

samples.
You can use the empty matrix `[]`

to specify the default value for any input argument. For example,
B = specgram(x,[],10000)is equivalent to

B = specgram(x)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
`imagesc(t,f,20``*`

log10(abs(b))),axis xy,colormap(jet)

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,noverlap)
```

computes the spectrogram at the frequencies specified in `f`

, using either the chirp `f`

is a vector of frequencies in Hertz; it must have at least two elements.
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`

.`specgram`

zero pads the windowed sections if`nfft > length(window)`

, so the quantity`(length(window) - noverlap)`

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.

Example

Plot the spectrogram of a digitized speech signal:load mtlb specgram(mtlb,512,Fs,kaiser(500,5),475) title('Spectrogram')

Diagnostics

An appropriate diagnostic message is displayed when incorrect arguments are used:Requires window's length to be no greater than the FFT length. Requires NOVERLAP to be strictly less than the window length. Requires positive integer values for NFFT and NOVERLAP. Requires vector input.

See Also

`cohere` |
Estimate magnitude squared coherence function between two signals. |

`csd` |
Estimate the cross spectral density (CSD) of two signals. |

`pwelch` |
Estimate the power spectral density (PSD) of a signal using Welch's method. |

`tfe` |
Transfer function estimate from input and output. |

References

[1] Oppenheim, A.V., and R.W. Schafer.