# stftLayer

Short-time Fourier transform layer

## Description

An STFT layer computes the short-time Fourier transform of the input. Use of this layer requires Deep Learning Toolbox™.

## Creation

### Description

creates a Short-Time Fourier Transform (STFT) layer. The input to
`layer`

= stftLayer`stftLayer`

must be a `dlarray`

(Deep Learning Toolbox) object in
`"CBT"`

format with a size along the time dimension greater than the
length of `Window`

.

specifies optional parameters using name-value arguments. You can specify the analysis
window and the format of the output, among others.`layer`

= stftLayer(`Name=Value`

)

## Properties

### STFT

`Window`

— Analysis window

`hann`

(128,'periodic')

(default) | vector

`hann`

(128,'periodic')This property is read-only.

Analysis window used to compute the STFT, specified as a vector with two or more elements.

**Example: **`(1-cos(2*pi*(0:127)'/127))/2`

and

both specify a Hann window of
length 128.`hann`

(128)

**Data Types: **`double`

| `single`

`OverlapLength`

— Number of overlapped samples

`96`

(default) | positive integer

This property is read-only.

Number of overlapped samples, specified as a positive integer strictly smaller
than the length of `Window`

.

The stride between consecutive windows is the difference between the window length and the number of overlapped samples.

**Data Types: **`double`

| `single`

`FFTLength`

— Number of DFT points

`128`

(default) | positive integer

This property is read-only.

Number of frequency points used to compute the discrete Fourier transform, specified as a positive integer greater than or equal to the window length. If not specified, this argument defaults to the length of the window.

If the length of the input data along the time dimension is less than the number
of DFT points, `stftLayer`

right-pads the data and the window with
zeros so they have a length equal to `FFTLength`

.

**Data Types: **`double`

| `single`

`TransformMode`

— Layer transform mode

`"mag"`

(default) | `"squaremag"`

| `"logmag"`

| `"logsquaremag"`

| `"realimag"`

Layer transform mode, specified as one of these:

`"mag"`

— STFT magnitude`"squaremag"`

— STFT squared magnitude`"logmag"`

— Natural logarithm of the STFT magnitude`"logsquaremag"`

— Natural logarithm of the STFT squared magnitude`"realimag"`

— Real and imaginary parts of the STFT, concatenated along the channel dimension

**Data Types: **`char`

| `string`

`OutputMode`

— Layer output mode

`"spatiotemporal"`

(default) | `"spatial"`

| `"temporal"`

Layer output mode, specified as one of these:

`"spatiotemporal"`

— Format the output as a sequence of 1-D images where the image height corresponds to frequency, the second dimension corresponds to channel, the third dimension corresponds to batch, and the fourth dimension corresponds to time.You can use this output mode to feed the output of

`stftLayer`

to a 1-D convolutional layer when you want to convolve along frequency. For more information, see`convolution1dLayer`

(Deep Learning Toolbox).`"spatial"`

— Format the output as a sequence of 2-D images where the image height corresponds to frequency and the image width corresponds to time. The third and fourth dimensions correspond to channel and batch, respectively.You can use this output mode to feed the output of

`stftLayer`

to a 2-D convolutional layer when you want to convolve along the two spatial dimensions. For more information, see`convolution2dLayer`

(Deep Learning Toolbox).`"temporal"`

— Format the output as a 1-D sequence. This format takes the`"spatiotemporal"`

output format and flattens the image height into the channel dimension. The second dimension of the STFT output corresponds to batch and the third dimension corresponds to time.You can use this output mode to feed the output of

`stftLayer`

to a 1-D convolutional layer when you want to convolve along time. For more information, see`convolution1dLayer`

(Deep Learning Toolbox). You can also use this output mode to use`stftLayer`

as part of a recurrent neural network. For more information, see`lstmLayer`

(Deep Learning Toolbox) and`gruLayer`

(Deep Learning Toolbox).

**Data Types: **`char`

| `string`

### Layer

`WeightLearnRateFactor`

— Multiplier for weight learning rate

`0`

(default) | nonnegative scalar

Multiplier for weight learning rate, specified as a nonnegative scalar. If not
specified, this property defaults to zero, resulting in weights that do not update
with training. You can also set this property using the `setLearnRateFactor`

(Deep Learning Toolbox) function.

**Data Types: **`double`

| `single`

`Name`

— Layer name

`''`

(default) | character vector | string scalar

Layer name, specified as a character vector or a string scalar.
For `Layer`

array input, the `trainNetwork`

,
`assembleNetwork`

, `layerGraph`

, and
`dlnetwork`

functions automatically assign names to layers with
`Name`

set to `''`

.

**Data Types: **`char`

| `string`

`NumInputs`

— Number of inputs

`1`

(default)

This property is read-only.

Number of inputs of the layer. This layer accepts a single input only.

**Data Types: **`double`

`InputNames`

— Input names

`{'in'}`

(default)

This property is read-only.

Input names of the layer. This layer accepts a single input only.

**Data Types: **`cell`

`NumOutputs`

— Number of outputs

`1`

(default)

This property is read-only.

Number of outputs of the layer. This layer has a single output only.

**Data Types: **`double`

`OutputNames`

— Output names

`{'out'}`

(default)

This property is read-only.

Output names of the layer. This layer has a single output only.

**Data Types: **`cell`

## Examples

### Short-Time Fourier Transform of Chirp

Generate a signal sampled at 600 Hz for 2 seconds. The signal consists of a chirp with sinusoidally varying frequency content. Store the signal in a deep learning array with `"CTB"`

format.

```
fs = 6e2;
x = vco(sin(2*pi*(0:1/fs:2)),[0.1 0.4]*fs,fs);
dlx = dlarray(x,"CTB");
```

Create a short-time Fourier transform layer with default properties. Create a `dlnetwork`

object consisting of a sequence input layer and the short-time Fourier transform layer. Specify a minimum sequence length of 128 samples. Run the signal through the `predict`

method of the network.

ftl = stftLayer; dlnet = dlnetwork([sequenceInputLayer(1,MinLength=128) ftl]); netout = predict(dlnet,dlx);

Convert the network output to a numeric array. Use the `squeeze`

function to remove the length-1 channel and batch dimensions. Plot the magnitude of the STFT. The first dimension of the array corresponds to frequency and the second to time.

q = extractdata(netout); waterfall(squeeze(q)') set(gca,XDir="reverse",View=[30 45]) xlabel("Frequency") ylabel("Time")

### Short-Time Fourier Transform of Sinusoid

Generate a 3 × 160 (× 1) array containing one batch of a three-channel, 160-sample sinusoidal signal. The normalized sinusoid frequencies are *π*/4 rad/sample, *π*/2 rad/sample, and 3*π*/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.

```
nch = 3;
N = 160;
x = dlarray(cos(pi.*(1:nch)'/4*(0:N-1)),"CTB");
```

Create a short-time Fourier transform layer that can be used with the sinusoid. Specify a 64-sample rectangular window, 48 samples of overlap between adjoining windows, and 1024 DFT points. Specify the layer output mode as `"spatial"`

. By default, the layer outputs the magnitude of the STFT.

stfl = stftLayer(Window=rectwin(64), ... OverlapLength=48, ... FFTLength=1024, ... OutputMode="spatial");

Create a two-layer `dlnetwork`

object containing a sequence input layer and the STFT layer you just created. Treat each channel of the sinusoid as a feature. Specify the signal length as the minimum sequence length for the input layer.

layers = [sequenceInputLayer(nch,MinLength=N) stfl]; dlnet = dlnetwork(layers);

Run the sinusoid through the `forward`

method of the network.

dataout = forward(dlnet,x);

Convert the network output to a numeric array. Use the `squeeze`

function to collapse the size-1 batch dimension. Plot the STFT magnitude separately for each channel in a waterfall plot.

q = squeeze(extractdata(dataout)); for kj = 1:nch subplot(nch,1,kj) waterfall(q(:,:,kj)') view(30,45) zlabel("Ch. "+string(kj)) end

## More About

### 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=\lfloor \frac{{N}_{x}-L}{M-L}\rfloor ,$$

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

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

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

where

$$g(n)$$ — Window function of length $$M$$.

$${X}_{m}(f)$$ — 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.

## See Also

### Apps

- Deep Network Designer (Deep Learning Toolbox)

### Objects

### Functions

`dlstft`

|`stft`

|`istft`

|`stftmag2sig`

### Topics

- List of Deep Learning Layers (Deep Learning Toolbox)

**Introduced in R2021b**

## MATLAB 명령

다음 MATLAB 명령에 해당하는 링크를 클릭했습니다.

명령을 실행하려면 MATLAB 명령 창에 입력하십시오. 웹 브라우저는 MATLAB 명령을 지원하지 않습니다.

# Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

## How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

### Americas

- América Latina (Español)
- Canada (English)
- United States (English)

### Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)