# dlstft

Deep learning short-time Fourier transform

## Syntax

[yr,yi] = dlstft(x)
[yr,yi] = dlstft(x,fs)
[yr,yi] = dlstft(x,ts)
[yr,yi] = dlstft(___,Name,Value)
[yr,yi,f,t] = dlstft(___)

## Description

example

[yr,yi] = dlstft(x) returns the deep learning Short-Time Fourier Transform (STFT) of x. dlstft requires Deep Learning Toolbox™.

example

[yr,yi] = dlstft(x,fs) returns the deep learning STFT assuming that x was sampled at the rate fs.

example

[yr,yi] = dlstft(x,ts) returns the deep learning STFT assuming that x was sampled with sample time ts.

example

[yr,yi] = dlstft(___,Name,Value) specifies additional options using name-value arguments. Options include the spectral window and the FFT length. These arguments can be added to any of the previous input syntaxes. For example, 'DataFormat','CBT' specifies the data format of x as CBT.

example

[yr,yi,f,t] = dlstft(___) returns the frequencies f and times t at which the deep learning STFT is computed.

## Examples

collapse all

Generate a signal sampled at 600 Hz for 2 seconds. The signal consists of a chirp with sinusoidally varying frequency content.

fs = 6e2; t = 0:1/fs:2; x = vco(sin(2*pi*t),[0.1 0.4]*fs,fs);

Store the signal in an unformatted deep learning array. Compute the short-time Fourier transform of the signal. Input the sample time as a duration scalar. (Alternatively, input the sample rate as a numeric scalar.) Specify that the input array is in 'CTB' format.

dlx = dlarray(x); [yr,yi,f,t] = dlstft(dlx,seconds(1/fs),'DataFormat','CTB');

Convert the outputs to numeric arrays. Compute the magnitude of the short-time Fourier transform and display it as a waterfall plot.

yr = extractdata(yr); yi = extractdata(yi); f = extractdata(f); t = seconds(t); waterfall(f,t,squeeze(hypot(yr,yi))') ax = gca; ax.XDir = 'reverse'; view(30,45) ylabel('Time (s)') xlabel('Frequency (Hz)') zlabel('Magnitude') Generate a 3-by-160(-by-1) array containing one batch of a three-channel, 160-sample sinusoidal signal. The normalized sinusoid frequencies are $\pi /4$ rad/sample, $\pi /2$ rad/sample, and $3\pi /4$ rad/sample. Save the signal as a dlarray, specifying the dimensions in order. dlarray permutes the array dimensions to the 'CBT' shape expected by a deep learning network. Display the array dimension sizes.

x = dlarray(cos(pi.*(1:3)'/4*(0:159)),'CTB'); [nchan,nbtch,nsamp] = size(x)
nchan = 3 
nbtch = 1 
nsamp = 160 

Compute the deep learning short-time Fourier transform of the signal. Specify a 64-sample rectangular window and an FFT length of 1024.

[re,im,f,t] = dlstft(x,'Window',rectwin(64),'FFTLength',1024);

dlstft computes the transform along the 'T' dimension. The output arrays are in 'SCBT' format. The 'S' dimension corresponds to frequency in the short-time Fourier transform.

Extract the data from the deep learning arrays.

re = squeeze(extractdata(re)); im = squeeze(extractdata(im)); f = extractdata(f); t = extractdata(t);

Compute the magnitude of the short-time Fourier transform. Plot the magnitude separately for each channel in a waterfall plot.

z = abs(re + 1j*im); for kj = 1:nchan subplot(nchan,1,kj) waterfall(f/pi,t,squeeze(z(:,kj,:))') view(30,45) end xlabel('Frequency (\times\pi rad/sample)') ylabel('Samples') ## Input Arguments

collapse all

Input array, specified as an unformatted dlarray (Deep Learning Toolbox), a formatted dlarray in 'CBT' format, or a numeric array. If x is an unformatted dlarray or a numeric array, you must specify the 'DataFormat' as some permutation of 'CBT'.

Example: dlarray(cos(pi./[4;2]*(0:159)),'CTB') and dlarray(cos(pi./[4;2]*(0:159))','TCB') both specify one batch observation of a two-channel sinusoid in the 'CBT' format.

Sample rate, specified as a positive numeric scalar.

Sample time, specified as duration scalar. Specifying ts is equivalent to setting a sample rate fs = 1/ts.

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

### Name-Value Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is 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 Name1,Value1,...,NameN,ValueN.

Example: 'Window',hamming(100),'OverlapLength',50,'FFTLength',128 windows the data using a 100-sample Hamming window, with 50 samples of overlap between adjoining segments and a 128-point FFT.

Data format of input, specified as a character vector or string scalar. This argument is valid only if x is unformatted.

Each character in this argument must be one of these labels:

• C — Channel

• B — Batch observations

• T — Time

The dlstft function accepts any permutation of 'CBT'. You can specify at most one of each of the C, B, and T labels.

Each element of the argument labels the matching dimension of x. If the argument is not in the listed order ('C' followed by 'B' and so on), then dlstft implicitly permutes both the argument and the data to match the order, but without changing how the data is stored.

Example: 'CBT'

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, see Windows.

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

Data Types: double | single

Number of overlapped samples, specified as a nonnegative 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

## Output Arguments

collapse all

Short-time Fourier transform, returned as two formatted dlarray (Deep Learning Toolbox) objects. yr contains the real part of the transform. yi contains the imaginary part of the transform.

• If x is a formatted dlarray, yr and yi are 'SCBT' formatted dlarray objects. The 'S' dimension corresponds to frequency in the short-time Fourier transform.

• If x is an unformatted dlarray or a numeric array, yr and yi are unformatted dlarray objects. The dimension order in yr and yi is 'SCBT'.

If no time information is specified, then the STFT is computed over the Nyquist range [0, π] if 'FFTLength' is even and over [0, π) if 'FFTLength' is odd. If you specify time information, then the intervals are [0, fs/2] and [0, fs/2), respectively, where fs is the effective sample rate.

Frequencies at which the deep learning STFT is computed, returned as a dlarray object.

• If the input array does not contain time information, then the frequencies are in normalized units of rad/sample.

• If the input array contains time information, then f contains frequencies expressed in Hz.

Times at which the deep learning STFT is computed, returned as a dlarray object or a duration array.

• If you do not specify time information, then t contains sample numbers.

• If you specify a sample rate, then t contains time values in seconds.

• If you specify a sample time, then t is a duration array with the same time format as x.

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 an analysis window of 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 columns in the STFT matrix is given by

$k=⌊\frac{{N}_{x}-L}{M-L}⌋,$

where ${N}_{x}$ is the length of the original signal $x\left(n\right)$ and the ⌊⌋ symbols denote the floor function. The number of rows in the matrix equals NDFT, the number of DFT points, for centered and two-sided transforms and NDFT/2⌋ + 1 for one-sided transforms.

The STFT matrix is given by $X\left(f\right)=\left[\begin{array}{ccccc}{X}_{1}\left(f\right)& {X}_{2}\left(f\right)& {X}_{3}\left(f\right)& \cdots & {X}_{k}\left(f\right)\end{array}\right]$ such that the $m$th element of this matrix is

${X}_{m}\left(f\right)=\sum _{n=-\infty }^{\infty }x\left(n\right)g\left(n-mR\right){e}^{-j2\pi fn},$

where

• $g\left(n\right)$ — Window function of length $M$.

• ${X}_{m}\left(f\right)$ — DFT of windowed data centered about time $mR$.

• $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 the spectrogram representation of the power spectral density of the function. ## Extended Capabilities

Introduced in R2021a