Short-time Fourier transform
s = stft(x)
s = stft(x,fs)
s = stft(x,ts)
s = stft(___,Name,Value)
[s,f] = stft(___)
[s,f,t] = stft(___)
stft(___) with no output arguments plots the
magnitude of the STFT in the current figure window. The STFT is plotted as two-sided and
Generate two seconds of a voltage controlled oscillator output, controlled by a sinusoid sampled at 10 kHz.
fs = 10e3; t = 0:1/fs:2; x = vco(sin(2*pi*t),[0.1 0.4]*fs,fs);
Compute and plot the STFT of the signal. Use a Kaiser window of length 256 and shape parameter . Specify the length of overlap as 220 samples and DFT length as 512 points. Plot the STFT with default colormap and view.
Change the view to display the STFT as a waterfall plot. Set the colormap to
view(-45,65) colormap jet
Generate a quadratic chirp sampled at 1 kHz for 2 seconds. The instantaneous frequency is 100 Hz at and crosses 200 Hz at second.
ts = 0:1/1e3:2; f0 = 100; f1 = 200; x = chirp(ts,f0,1,f1,'quadratic',,'concave');
Compute and display the STFT of the quadratic chirp with a duration of 1 ms.
d = seconds(1e-3); win = hamming(100,'periodic'); stft(x,d,'Window',win,'OverlapLength',98,'FFTLength',128);
x— Input signal
Input signal, specified as a vector or a MATLAB®
For a timetable input,
x must contain only a single vector
and uniformly increasing finite row times. If a timetable has missing or duplicate
time points, you can fix it using the tips in Clean Timetable with Missing, Duplicate, or Nonuniform Times (MATLAB).
For a vector input, the length of
x must be greater than
the window length.
x = chirp(0:1/4e3:2,250,1,500,'quadratic')
Complex Number Support: Yes
fs— Sample rate
2π(default) | positive scalar
Sample rate, specified as a positive scalar. This argument applies only when
x is a vector.
comma-separated pairs of
the argument name and
Value is the corresponding value.
Name must appear inside quotes. You can specify several name and value
pair arguments in any order as
stft('Window',win,'OverlapLength',50,'FFTLength',128)windows the data using the window
win, with 50 samples overlap between adjoining segments and 128 point FFT.
'Window'— Spectral window
hann(128,'periodic')(default) | vector
Spectral window, specified as the comma-separated pair consisting of
'Window' and a vector. If you do not specify the window or
specify it as empty, the function uses a Hann window of length 128. The length of
Window must be greater than or equal to 2.
For a list of available windows, see Windows.
(1-cos(2*pi*(0:N)'/N))/2 both specify a Hann window of length
N + 1.
'OverlapLength'— Number of overlapped samples
75%of window length (default) | nonnegative integer
Number of overlapped samples, specified as a nonnegative integer smaller than the
window. If you omit
or specify it as empty, it is set to the largest integer less than 75% of the window
length, which is 96 samples for the default Hann window.
'FFTLength'— Number of DFT points
128(default) | positive integer
Number of DFT points, specified as a positive integer. The value must be greater than or equal to the window length. If the length of the input signal is less than the DFT length, the data is padded with zeros.
'Centered'— Frequency range
Frequency range, specified as
If this option is set to
true, then the spectrum is centered and is
computed over the interval –π to π. Otherwise, the spectrum is computed over
the interval 0 to 2π.
s— Short-time Fourier transform
Short-time Fourier transform, returned as a vector. Time increases across the
s and frequency increases down the rows.
If the signal
x has length
k columns, where
k = ⌊(Nx –
window is a vector,
L is equal to
'OverlapLength', and the ⌊ ⌋ symbols denote the floor
The number of rows in
s is equal to the value specified in
Frequencies at which the STFT is evaluated, returned as a vector.
t— Time instants
Time instants, returned as a vector.
t contains the time values
corresponding to the centers of the data segments used to compute short-time power
The short-time Fourier transform (STFT) is used to analyze how the frequency content of a nonstationary signal changes over time.
The STFT of a signal is calculated by sliding an analysis window of length over the signal and calculating the discrete Fourier transform of the windowed data. The window hops over the original signal at intervals of samples. Most window functions taper off at the edges to avoid spectral ringing. If a nonzero overlap length is specified, overlap-adding the windowed segments compensates for the signal attenuation at the window edges. The DFT of each windowed segment is added to a matrix that contains the magnitude and phase for each point in time and frequency. The number of rows in the STFT matrix equals the number of DFT points, and the number of columns is given by
where is the length of the original signal and the ⌊⌋ symbols denote the floor function.
The STFT matrix is given by such that the th element of this matrix is
— Window function of length .
— DFT of windowed data centered about time .
— Hop size between successive DFTs. The hop size is the difference between the window length and the overlap length .
The magnitude squared of the STFT yields the
spectrogram representation of the power spectral density of the function.
 Mitra, Sanjit K. Digital Signal Processing: A Computer-Based Approach. 2nd Ed. New York: McGraw-Hill, 2001.
 Smith, J. O. Spectral Audio Signal Processing. https://ccrma.stanford.edu/~jos/sasp/, online book, 2011 edition, accessed Nov 2018.