# deepSignalAnomalyDetectorCNN

## Description

The `deepSignalAnomalyDetectorCNN`

object uses a 1-D convolutional
autoencoder model to detect signal anomalies.

## Creation

Create a `deepSignalAnomalyDetectorCNN`

object using `deepSignalAnomalyDetector`

and specifying `"convautoencoder"`

as
the model type.

## Properties

### General

`IsTrained`

— Training status

`0`

(`false`

) | `1`

(`true`

)

This property is read-only.

Training status, returned as `0`

(`false`

) or
`1`

(`true`

). Once you train the detector,
`IsTrained`

is equal to `1`

.

**Data Types: **`logical`

`NumChannels`

— Number of channels

positive integer

This property is read-only.

Number of channels in each input signal, returned as a positive integer.

**Data Types: **`single`

| `double`

| `int8`

| `int16`

| `int32`

| `int64`

| `uint8`

| `uint16`

| `uint32`

| `uint64`

### Model

`ModelType`

— Type of deep learning model

`"convautoencoder"`

This property is read-only.

Type of deep learning model implemented by the detector, returned as
`"convautoencoder"`

.

`NumDownsampleLayers`

— Number of convolutional layers in downsampling section

`2`

(default) | positive integer

This property is read-only.

Number of convolutional layers in the downsampling section of the model, returned as a positive integer.

The model uses the same number of transposed convolutional layers in the upsampling section, with the parameters in reverse order.

**Data Types: **`single`

| `double`

| `int8`

| `int16`

| `int32`

| `int64`

| `uint8`

| `uint16`

| `uint32`

| `uint64`

`DownsampleFactor`

— Downsampling factor

`2`

(default) | positive integer | vector of positive integers

This property is read-only.

Downsampling factor, returned as a positive integer or a vector of positive
integers. When `DownsampleFactor`

is a scalar, each layer uses the
same downsampling factor. When `DownsampleFactor`

is a vector, the
*i*th layer uses a downsampling factor equal to the value of the
*i*th element in the vector.

**Data Types: **`single`

| `double`

| `int8`

| `int16`

| `int32`

| `int64`

| `uint8`

| `uint16`

| `uint32`

| `uint64`

`NumFilters`

— Number of filters

`32`

(default) | positive scalar integer | vector of positive integers

This property is read-only.

Number of filters in downsampling convolution layers, returned as a positive scalar integer or a vector of positive integers.

**Data Types: **`single`

| `double`

| `int8`

| `int16`

| `int32`

| `int64`

| `uint8`

| `uint16`

| `uint32`

| `uint64`

`FilterSize`

— Size of filters

`8`

(default) | positive scalar integer | vector of positive integers

This property is read-only.

Size of filters in downsampling convolution layers, returned as a positive scalar integer or a vector of positive integers.

**Data Types: **`single`

| `double`

| `int8`

| `int16`

| `int32`

| `int64`

| `uint8`

| `uint16`

| `uint32`

| `uint64`

`DropoutProbability`

— Dropout probability

`0.2`

(default) | nonnegative scalar

This property is read-only.

Dropout probability used to avoid overfitting, returned as a nonnegative scalar less than 1.

**Data Types: **`single`

| `double`

| `int8`

| `int16`

| `int32`

| `int64`

| `uint8`

| `uint16`

| `uint32`

| `uint64`

### Window

`WindowLength`

— Window length

`1`

(default) | positive integer | `"fullSignal"`

This property is read-only.

Window length of each signal segment, returned as a positive integer or as
`"fullSignal"`

.

If

`WindowLength`

is an integer, the detector divides each input signal into segments. The length of each segment is equal to the`WindowLength`

value in samples.If

`WindowLength`

is`"fullSignal"`

, the detector treats each input signal as a single segment.

Use `updateDetector`

to modify this and other window properties.

**Data Types: **`single`

| `double`

| `int8`

| `int16`

| `int32`

| `int64`

| `uint8`

| `uint16`

| `uint32`

| `uint64`

| `char`

| `string`

`OverlapLength`

— Number of overlapped samples

`"auto"`

(default) | positive integer

This property is read-only.

Number of overlapped samples between window segments, returned as a positive integer
or as `"auto"`

.

If

`OverlapLength`

is a positive integer, the detector sets the number of overlapped samples equal to the`OverlapLength`

value.If

`OverlapLength`

is`"auto"`

, the detector sets the number of overlapped samples equal to`WindowLength`

– 1.

**Data Types: **`single`

| `double`

| `int8`

| `int16`

| `int32`

| `int64`

| `uint8`

| `uint16`

| `uint32`

| `uint64`

| `char`

| `string`

`WindowLossAggregation`

— Method to aggregate sample loss

`"mean"`

(default) | `"max"`

| `"min"`

| `"median"`

This property is read-only.

Method to aggregate sample loss within each window segment, returned as one of these:

`"max"`

— Compute the aggregated window loss as the maximum value of all the sample losses within the window.`"mean"`

— Compute the aggregated window loss as the mean value of all the sample losses within the window.`"median"`

— Compute the aggregated window loss as the median value of all the sample losses within the window.`"min"`

— Compute the aggregated window loss as the minimum value of all the sample losses within the window.

**Data Types: **`char`

| `string`

### Threshold

`ThresholdMethod`

— Method to compute detection threshold

`"contaminationFraction"`

(default) | `"max"`

| `"median"`

| `"mean"`

| `"manual"`

| `"customFunction"`

This property is read-only.

Method to compute the detection threshold, returned as one of these:

`"contaminationFraction"`

— Value corresponding to the detection of anomalies within a specified fraction of windows. The fraction value is specified by`ThresholdParameter`

.`"max"`

— Maximum window loss measured over the entire training data set and multiplied by`ThresholdParameter`

.`"median"`

— Median window loss measured over the entire training data set and multiplied by`ThresholdParameter`

.`"mean"`

— Mean window loss measured over the entire training data set and multiplied by`ThresholdParameter`

.`"manual"`

— Manual detection threshold value based on`Threshold`

.`"customFunction"`

— Custom detection threshold value based on`ThresholdFunction`

.

Use `updateDetector`

to modify this and other threshold properties.

**Data Types: **`char`

| `string`

`ThresholdParameter`

— Detection threshold

real scalar

This property is read-only.

Detection threshold, returned as a nonnegative scalar when you set
`ThresholdMethod`

to `"contaminationFraction"`

,
and as positive scalar when you set `ThresholdMethod`

to
`"max"`

, `"median"`

, or
`"mean"`

.

**Data Types: **`single`

| `double`

| `int8`

| `int16`

| `int32`

| `int64`

| `uint8`

| `uint16`

| `uint32`

| `uint64`

`Threshold`

— Manual detection threshold

positive scalar

This property is read-only.

Manual detection threshold, retuned as a positive scalar. This property applies only when you
set `ThresholdMethod`

to `"manual"`

.

**Data Types: **`single`

| `double`

| `int8`

| `int16`

| `int32`

| `int64`

| `uint8`

| `uint16`

| `uint32`

| `uint64`

`ThresholdFunction`

— Function to compute custom detection threshold

function handle

This property is read-only.

Function to compute custom detection threshold, returned as a function handle. This property
applies only when you set `ThresholdMethod`

to
`"customFunction"`

.

**Data Types: **`function_handle`

## Object Functions

`detect` | Detect anomalies in signals |

`getModel` | Get underlying neural network model of signal anomaly detector |

`plotAnomalies` | Plot signal anomalies |

`plotLoss` | Plot window reconstruction loss |

`plotLossDistribution` | Plot CDF and histogram of aggregated window loss distribution |

`saveModel` | Save detector network and parameters |

`trainDetector` | Train signal anomaly detector |

`updateDetector` | Update settings of trained detector and recompute detection threshold |

## Examples

### Detect Anomalies in Sinusoids

Load the file `sineWaveAnomalyData.mat`

, which contains two sets of synthetic three-channel sinusoidal signals.

`sineWaveNormal`

contains 10 sinusoids of stable frequency and amplitude. Each signal has a series of small-amplitude impact-like imperfections. The signals have different lengths and initial phases. Plot the first three normal signals.

load sineWaveAnomalyData.mat sl = 3; tiledlayout("vertical") ax = zeros(sl,1); for kj = 1:sl ax(kj) = nexttile; plot(sineWaveNormal{kj}) title("Normal Signal") end

`sineWaveAbnormal`

contains three signals, all of the same length. Each signal in the set has one or more anomalies.

All channels of the first signal have an abrupt change in frequency that lasts for a finite time.

The second signal has a finite-duration amplitude change in one of its channels.

The third signal has spikes at random times in all channels.

Plot the three signals with anomalies.

for kj = 1:sl plot(ax(kj),sineWaveAbnormal{kj}) title(ax(kj),"Signal with Anomalies") end

Create an autoencoder object to detect the anomalies in the abnormal signals. The object contains a neural network that you can train to best reproduce an input set of anomaly-free data. The trained object encodes the patterns inherent in the data and can recognize if a signal contains regions that deviate from the patterns. You do not need to label any data to train the detector. The training process is unsupervised.

By default, the `deepSignalAnomalyDetector`

function creates objects with convolutional autoencoders. The only mandatory argument is the number of channels in the training and testing signals.

D = deepSignalAnomalyDetector(sl)

D = deepSignalAnomalyDetectorCNN with properties: IsTrained: 0 NumChannels: 3 Model Information ModelType: 'convautoencoder' FilterSize: 8 NumFilters: 32 NumDownsampleLayers: 2 DownsampleFactor: 2 DropoutProbability: 0.2000 Threshold Information Threshold: [] ThresholdMethod: 'contaminationFraction' ThresholdParameter: 0.0100 Window Information WindowLength: 1 OverlapLength: 'auto' WindowLossAggregation: 'mean'

Use the `trainDetector`

function to train the convolutional neural network with the normal data. Use the training options for the adaptive moment estimation (Adam) optimizer. Specify a maximum number of 100 epochs to use for training. For more information, see `trainingOptions`

(Deep Learning Toolbox).

```
opts = trainingOptions("adam",MaxEpochs=100);
trainDetector(D,sineWaveNormal,opts)
```

Iteration Epoch TimeElapsed LearnRate TrainingLoss _________ _____ ___________ _________ ____________ 1 1 00:00:04 0.001 0.49381 50 50 00:00:16 0.001 0.065911 100 100 00:00:26 0.001 0.047147 Training stopped: Max epochs completed Computing threshold... Threshold computation completed.

Use the trained detector to detect the anomalies in the abnormal data. The `detect`

function outputs two cell arrays, with each array element corresponding to a signal in the testing data.

[lbls,loss] = detect(D,sineWaveAbnormal);

The first output of `detect`

is a categorical array that declares each sample of a signal as being anomalous or not. A sample can be either a point, a signal region, or an entire signal. An anomaly is detected when the *reconstruction loss*, or the difference between the value of a signal and the value reconstructed by the detector based on the training data, exceeds a given *threshold*. You can set a threshold manually or you can direct the detector to compute a threshold based on a specified statistic of loss values computed on training data.

for kj = 1:sl hold(ax(kj),"on") plot(ax(kj),lbls{kj},LineWidth=2) title(ax(kj),"Signal with Anomalies") hold(ax(kj),"off") end

Alternatively, use the `plotAnomalies`

function to plot each channel of each signal and the anomalies found by the detector. The detector decides that there is an anomaly in a signal if it detects one in any of the signal channels. `plotAnomalies`

plots the anomaly locations in all channels.

for kj = 1:sl figure plotAnomalies(D,sineWaveAbnormal{kj}) end

The second output of `detect`

is the computed reconstruction loss that the object uses to determine the labeling. You can plot the reconstruction loss directly from the output of `detect`

, but it is more convenient to use the `plotLoss`

function, which also displays the the threshold value above which a sample is declared to be anomalous.

tiledlayout("vertical") for kj = 1:sl nexttile plotLoss(D,sineWaveAbnormal{kj}) end

The `plotLossDistribution`

function displays the reconstruction loss distribution and gives insight into the way the detector chooses the threshold. You can plot the distribution loss for the normal data alongside the distribution loss for the abnormal data. The threshold chosen by the detector is larger than the loss values for most of the normal-data samples but smaller than an appreciable number of abnormal-data losses that signal the possible presence of anomalies. You can improve the choice of threshold by using the `updateDetector`

function.

clf plotLossDistribution(D,sineWaveNormal,sineWaveAbnormal)

Use the `getModel`

function to obtain the underlying neural network model corresponding to the detector. You can then inspect the model using `analyzeNetwork`

(Deep Learning Toolbox).

M = getModel(D); analyzeNetwork(M)

## Extended Capabilities

### GPU Arrays

Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.

Usage notes and limitations:

The `ExecutionEnvironment`

option must be `"gpu"`

or
`"auto"`

when the input data is:

A

`gpuArray`

A cell array containing

`gpuArray`

objectsA datastore that outputs cell arrays containing

`gpuArray`

objects

For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

## Version History

**Introduced in R2023a**

## See Also

### Functions

### Objects

### Topics

- Detect Anomalies in Signals Using deepSignalAnomalyDetector
- Detect Anomalies in Machinery Using LSTM Autoencoder
- Detect Anomalies in ECG Data Using Wavelet Scattering and LSTM Autoencoder in Simulink
- Anomaly Detection in Industrial Machinery Using Three-Axis Vibration Data (Predictive Maintenance Toolbox)

## 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)