Main Content

stft

Short-time Fourier transform

Description

s= stft(x)returns the short-time Fourier transform (STFT) ofx

example

s= stft(x,fs)returns the STFT ofxusing sample ratefs

s= stft(x,ts)returns the STFT ofxusing sample timets

example

s= stft(___,Name,Value)specifies additional options using name-value pair arguments. Options include the FFT window and length. These arguments can be added to any of the previous input syntaxes.

example

[s,f] = stft(___)returns the frequenciesfat which the STFT is evaluated.

example

[s,f,t] = stft(___)returns the times at which the STFT is evaluated.

stft(___)with no output arguments plots the magnitude of the STFT in the current figure window.

Examples

collapse all

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 β = 5 .Specify the length of overlap as 220 samples and DFT length as 512 points. Plot the STFT with default colormap and view.

stft(x,fs,'Window',kaiser(256,5),'OverlapLength',220,'FFTLength',512);

Figure contains an axes. The axes with title Short-Time Fourier Transform contains an object of type image.

Change the view to display the STFT as a waterfall plot. Set the colormap tojet

view(-45,65) colormapjet

Figure contains an axes. The axes with title Short-Time Fourier Transform contains an object of type surface.

Generate a quadratic chirp sampled at 1 kHz for 2 seconds. The instantaneous frequency is 100 Hz at t = 0 and crosses 200 Hz at t = 1 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);

Figure contains an axes. The axes with title Short-Time Fourier Transform contains an object of type image.

Generate a signal sampled at 5 kHz for 4 seconds. The signal consists of a set of pulses of decreasing duration separated by regions of oscillating amplitude and fluctuating frequency with an increasing trend. Plot the signal.

fs = 5000; t = 0:1/fs:4-1/fs; x = besselj(0,600*(sin(2*pi*(t+1).^3/30).^5)); plot(t,x)

Figure contains an axes. The axes contains an object of type line.

Compute the one-sided, two-sided, and centered short-time Fourier transforms of the signal. In all cases, use a 202-sample Kaiser window with shape factor β = 10 to window the signal segments. Display the frequency range used to compute each transform.

rngs = ["onesided""twosided""centered"];forkj = 1:length(rngs) opts = {'Window',kaiser(202,10),的频率uencyRange',rngs(kj)}; [~,f] = stft(x,fs,opts{:}); subplot(length(rngs),1,kj) stft(x,fs,opts{:}) title(sprintf('''%s'': [%5.3f, %5.3f] kHz',rngs(kj),[f(1) f(end)]/1000))end

Figure contains 3 axes. Axes 1 with title 'onesided': [0.000, 2.500] kHz contains an object of type image. Axes 2 with title 'twosided': [0.000, 4.975] kHz contains an object of type image. Axes 3 with title 'centered': [-2.475, 2.500] kHz contains an object of type image.

Repeat the computation, but now change the length of the Kaiser window to 203, an odd number. The'twosided'frequency interval does not change. The other two frequency intervals become open at the higher end.

forkj = 1:length(rngs) opts = {'Window',kaiser(203,10),的频率uencyRange',rngs(kj)}; [~,f] = stft(x,fs,opts{:}); subplot(length(rngs),1,kj) stft(x,fs,opts{:}) title(sprintf('''%s'': [%5.3f, %5.3f] kHz',rngs(kj),[f(1) f(end)]/1000))end

Figure contains 3 axes. Axes 1 with title 'onesided': [0.000, 2.488] kHz contains an object of type image. Axes 2 with title 'twosided': [0.000, 4.975] kHz contains an object of type image. Axes 3 with title 'centered': [-2.488, 2.488] kHz contains an object of type image.

Generate a three-channel signal consisting of three different chirps sampled at 1 kHz for one second.

  1. The first channel consists of a concave quadratic chirp with instantaneous frequency 100 Hz att =0 and crosses 300 Hz att =1 second. It has an initial phase equal to 45 degrees.

  2. The second channel consists of a convex quadratic chirp with instantaneous frequency 100 Hz att =0 and crosses 500 Hz att =1 second.

  3. The third channel consists of a logarithmic chirp with instantaneous frequency 300 Hz att =0 and crosses 500 Hz att =1 second.

Compute the STFT of the multichannel signal using a periodic Hamming window of length 128 and an overlap length of 50 samples.

fs = 1e3; t = 0:1/fs:1-1/fs; x = [chirp(t,100,1,300,'quadratic',45,'concave'); chirp(t,100,1,500,'quadratic',[],'convex'); chirp(t,300,1,500,'logarithmic')]'; [S,F,T] = stft(x,fs,'Window',hamming(128,'periodic'),'OverlapLength',50);

Visualize the STFT of each channel as a waterfall plot. Control the behavior of the axes using the helper functionhelperGraphicsOpt

waterfall(F,T,abs(S(:,:,1))') helperGraphicsOpt(1)

Figure contains an axes. The axes with title Input Channel: 1 contains an object of type patch.

waterfall(F,T,abs(S(:,:,2))') helperGraphicsOpt(2)

Figure contains an axes. The axes with title Input Channel: 2 contains an object of type patch.

waterfall(F,T,abs(S(:,:,3))') helperGraphicsOpt(3)

Figure contains an axes. The axes with title Input Channel: 3 contains an object of type patch.

This helper function sets the appearance and behavior of the current axes.

functionhelperGraphicsOpt(ChannelId) ax = gca; ax.XDir =“反向”; ax.ZLim = [0 30]; ax.Title.String = ['Input Channel: 'num2str(ChannelId)]; ax.XLabel.String =的频率uency (Hz)'; ax.YLabel.String ='Time (seconds)'; ax.View = [30 45];end

Input Arguments

collapse all

Input signal, specified as a vector, a matrix, or a MATLAB®timetable

Note

If you invertsusingistftand want the result to be the same length asx, the value of(length(x)-noverlap)/(length(window)-noverlap)must be an integer.

  • If the input has multiple channels, specifyxas a matrix where each column corresponds to a channel.

  • For timetable input,xmust contain uniformly increasing finite row times. If a timetable has missing or duplicate time points, you can fix it using the tips inClean Timetable with Missing, Duplicate, or Nonuniform Times

  • For multichannel timetable input, specifyxas a timetable with a single variable containing a matrix or a timetable with multiple variables each containing a column vector. All variables must have the same precision.

Each channel ofxmust have a length greater than or equal to the window length.

Example:chirp(0:1/4e3:2,250,1,500,'quadratic')specifies a single-channel chirp.

Example:timetable(rand(5,2),'SampleRate',1)specifies a two-channel random variable sampled at 1 Hz for 4 seconds.

Example:timetable(rand(5,1),rand(5,1),'SampleRate',1)specifies a two-channel random variable sampled at 1 Hz for 4 seconds.

Data Types:double|single
Complex Number Support:Yes

Sample rate, specified as a positive scalar. This argument applies only whenxis a vector or a matrix.

Data Types:double|single

Sample time, specified as adurationscalar. This argument applies only whenxis a vector or a matrix

Example:seconds(1)is adurationscalar representing a 1-second time difference between consecutive signal samples.

Data Types:duration

Name-Value Pair Arguments

Specify optional comma-separated pairs ofName,Valuearguments.Nameis the argument name andValueis the corresponding value.Namemust appear inside quotes. You can specify several name and value pair arguments in any order asName1,Value1,...,NameN,ValueN

Example:'Window',hamming(100),'OverlapLength',50,'FFTLength',128窗口的数据我们ing a 100-sample Hamming window, with 50 samples of overlap between adjoining segments and a 128-point FFT.

Spectral window, specified as 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, seeWindows

Example:hann(N+1)and(1-cos(2*pi*(0:N)'/N))/2both specify a Hann window of lengthN+ 1.

Data Types:double|single

Number of overlapped samples, specified as a positive integer smaller than the length of'Window'.If you omit'OverlapLength'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.

Data Types:double|single

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.

Data Types:double|single

STFT frequency range, specified as'centered','twosided', or'onesided'

  • 'centered'— Compute a two-sided, centered STFT. If'FFTLength'is even, thensis computed over the interval(–π,π]rad/sample. If'FFTLength'is odd, thensis computed over the interval(–π,π)rad/sample. If you specify time information, then the intervals are(–fs,fs/2]cycles/unit time and(–fs,fs/2)cycles/unit time, respectively, wherefsis the effective sample rate.

  • 'twosided'— Compute a two-sided STFT over the interval[0, 2π)rad/sample. If you specify time information, then the interval is[0,fs)cycles/unit time.

  • 'onesided'— Compute a one-sided STFT. If'FFTLength'is even, thensis computed over the interval[0,π]rad/sample. If'FFTLength'is odd, thensis computed over the interval[0,π)rad/sample. If you specify time information, then the intervals are[0,fs/2]cycles/unit time and[0,fs/2)cycles/unit time, respectively, wherefsis the effective sample rate. This option is valid only for real signals.

    Note

    When this argument is set to'onesided',stft在积极的奈奎斯特范围输出值nd does not conserve the total power.

For an example, seeSTFT Frequency Ranges

Data Types:char|string

Output time dimension, specified as'acrosscolumns'or'downrows'.Set this value to'downrows'if you want the time dimension ofsdown the rows and the frequency dimension across the columns. Set this value to'acrosscolumns'if you want the time dimension ofsacross the columns and the frequency dimension down the rows. This input is ignored if the function is called without output arguments.

Output Arguments

collapse all

Short-time Fourier transform, returned as a matrix or a 3-D array. Time increases across the columns ofsand frequency increases down the rows. The third dimension, if present, corresponds to the input channels.

  • If the signalxhasNxtime samples, thenshaskcolumns, wherek= ⌊(NxL)/(ML)⌋,Mis the length of'Window',Lis the'OverlapLength', and the ⌊ ⌋ symbols denote the floor function.

  • The number of rows insis equal to the value specified in'FFTLength'

Data Types:double|single

Frequencies at which the STFT is evaluated, returned as a vector.

Data Types:double|single

Time instants, returned as a vector.tcontains the time values corresponding to the centers of the data segments used to compute short-time power spectrum estimates.

  • If a sample ratefsis provided, then the vector contains time values in seconds.

  • If a sample timetsis provided, then the vector is a duration array with the same time format as the input.

  • If no time information is provided, then the vector contains sample numbers.

Data Types:double|single

More About

collapse all

Short-Time Fourier Transform

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 ananalysis windowof length M over the signal and calculating the discrete Fourier transform of the windowed data. The window hops over the original signal at intervals of R samples. Most window functions taper off at the edges to avoid spectral ringing. If a nonzero overlap length L 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

k = N x L M L ,

where N x is the length of the original signal x ( n ) and the⌊⌋symbols denote the floor function.

The STFT matrix is given by X ( f ) = [ X 1 ( f ) X 2 ( f ) X 3 ( f ) X k ( f ) ] such that the m th element of this matrix is

X m ( f ) = n = x ( n ) g ( n m R ) e j 2 π f n ,

where

  • g ( n ) — Window function of length M

  • X m ( f ) — DFT of windowed data centered about time m R

  • R — Hop size between successive DFTs. The hop size is the difference between the window length M and the overlap length L

The magnitude squared of the STFT yields thespectrogramrepresentation of the power spectral density of the function.

Perfect Reconstruction

In general, computing the STFT of an input signal and inverting it does not result in perfect reconstruction. If you want the output of ISTFT to match the original input signal as closely as possible, the signal and the window must satisfy the following conditions:

  • Input size — If you invert the output ofstftusingistftand want the result to be the same length as the input signalx, the value of k = ( l e n g t h ( x ) - n o v e r l a p ) ( l e n g t h ( w i n d o w ) - n o v e r l a p ) must be an integer.

  • COLA compliance — Use COLA-compliant windows, assuming that you have not modified the short-time Fourier transform of the signal.

  • Padding — If the length of the input signal is such that the value ofkis not an integer, zero-pad the signal before computing the short-time Fourier transform. Remove the extra zeros after inverting the signal.

References

[1] Mitra, Sanjit K.Digital Signal Processing: A Computer-Based Approach.第二版。纽约:麦格劳-希尔,2001年。

[2] Sharpe, Bruce.Invertibility of Overlap-Add Processinghttps://gauss256.github.io/blog/cola.html, accessed July 2019.

[3] Smith, Julius Orion.Spectral Audio Signal Processinghttps://ccrma.stanford.edu/~jos/sasp/, online book, 2011 edition, accessed Nov 2018.

Extended Capabilities

See Also

Functions

Introduced in R2019a