# Anomaly Detection Using Autoencoder and Wavelets

This example shows how wavelet features can be used to detect arc faults in a DC system. For the safe operation of DC distribution systems, it is important to identify arc faults and prefault signals that can be caused by deterioration of wire insulation due to aging, abrasion, or rodent bites. These arc faults can result in shock, fires, and system failures in the microgrid. Unlike the fault signals in AC distribution systems, these prefault arc flash signals are difficult to identify as they do not generate significant power to trigger the circuit breakers. As a result, these signals can exist in the system for hours without being detected.

Arc fault detection using the wavelet transform was studied in [1]. This example follows the feature extraction procedure detailed in that work which consists of filtering the load signals using the Daubechies db3 wavelet followed by normalization. Further, an autoencoder trained with signal features under normal conditions is used to detect arc faults in load signals. The DC arc model used to generate the fault signals and the pretrained network used to detect the arc faults are provided in the example folder. As training the network for arc detection of larger signals can take significantly long simulation time, in this example we only report the detection results.

### Training and Testing Setup

The autoencoder is trained using the load signal generated by the Simulink® model DCNoArc under normal conditions, i.e., without arc faults. The model DCNoArc was built using components from the Simscape™ Electrical™ Specialized Power Systems library.

Figure 1: DCNoArc model for generating load signal under normal conditions.

The voltage sources are modeled using the following parameters:

• AC Harmonic Source 1: 10 V AC voltage and 120 Hz frequency

• AC Harmonic Source 2: 20 V AC voltage and 2000 Hz frequency

• DC voltage source: 1000 V

In the model DCArcModelFinal we add arc fault generation in every load branch. The model uses the Cassie arc model for synthetic arc fault generation. The arc model works like an ideal conductance until the arc ignites at the contact separation time.

Figure 2: DCArcModelFinal model for generating load signal with arc fault.

The Cassie arc model is one of the most studied black box models for generating synthetic arcs. The model is described by the following differential equation:

$\frac{dg}{dt}=\frac{g}{\tau }\left(\frac{{u}^{2}}{{U}_{c}^{2}}-1\right)$

where

• g is the conductance of the arc in siemens

• $\tau$ is the arc time constant in seconds

• u is the voltage across the arc in volts

• ${U}_{c}$ is the constant arc voltage in volts

The Cassie arc models were implemented in Simulink® using the following parameter values:

• Initial conductance g(0) is 1e4 siemens

• Constant arc voltage ${U}_{c}$ = 100 V

• Arc time constant is 1.2e-6 seconds

The contact separation times for the arc models are chosen at random. All the parameters have been loaded in the PreLoadFcn callbacks in the Model Properties of the Model Settings tab.

At the contact separation time, the voltage across the mathematical Cassie arc model drops by some level and stays at that value during the remaining simulation period. However, in real power system branches the arc is sustained for a small time interval. To ensure that the voltage across the Cassie arc model emulates the behavior of real life arc faults, we use a switch across each model to limit the arc time. We use the DCArcModelFinalmodel to generate a faulty load signal to test the autoencoder.

To detect arc faults in all the load branches simultaneously the sensing system measures the load voltage at each branch. The sensing system combines the load voltages and sends the resulting signal to the feature generation block. The generated features are then used to detect the arc faults in all the branches using a deep network.

### Anomaly Detection with Autoencoder

Autoencoders are used to detect anomalies in a signal. The autoencoder is trained on data without anomalies. As a result, the learned network weights minimize the reconstruction error for load signals without arc faults. The statistics of the reconstruction error for the training data can be used to select the threshold in the anomaly detection block that determines the detection performance of the autoencoder. The detection block declares the presence of an anomaly when it encounters a reconstruction error above threshold. In this example, we used root-mean-square error (RMSE) as the reconstruction error metric.

For this example, we trained two autoencoders using the load signal under normal conditions without arc fault. One autoencoder was trained using the raw load signal as training data. This encoder uses the raw faulty load signal to detect arc faults. The second autoencoder was trained using wavelet features. Arc fault detection is subsequently done on wavelet features as opposed to the raw data. For training and testing the network, we assume that the load consists of 10 parallel resistive branches with randomly chosen resistance values. For arc fault signal generation, we add a Cassie arc model in every load branch. The contact separation times of the models are such that they are triggered randomly throughout the simulation period. Just like in a real-time DC system, the load signals from both normal and faulty conditions have added white noise.

#### Feature Extraction

The wavelet-based autoencoder was trained and tested on signals filtered using the discrete wavelet transform (DWT). Following [1], the Daubechies db3 wavelet was used.

The following figures show the wavelet-filtered load signals under normal and faulty conditions. The wavelet-filtered faulty signal captures the variation due to arc faults. For training and testing purposes, the wavelet-filtered signals are segmented into 100-sample frames.

Figure 3: Raw load signal and wavelet-filtered signal under normal conditions.

Figure 4: Raw load signal and wavelet-filtered signal under faulty conditions.

#### Model Training

The autoencoder is trained using wavelet-filtered features from the load signal under normal conditions. For the training stage you have two options:

1. Train your own autoencoder and load the network into the prediction block of the DCArcModelFinal model.

2. Use the DCArcModelFinal model that has been preloaded with the pretrained model available in the netData.mat file in the example folder.

To train your own autoencoder you can use the following steps.

• First, generate the load signal under normal operating conditions using the DCNoArc model. Load, open, and run the model using the following commands. Extract the load signal from the simulation output.

open_system('DCNoArc.slx');
out = sim('DCNoArc.slx');

% extract normal load signal from the simulation output
xn = out.xn;
• Next, extract the wavelet-filtered features from the load signal. You use the features as the input to the autoencoder.

% training data: load voltage under normal conditions
featureDimension = 100;
xn = sigresize(xn,featureDimension);

% Obtain training features
trnd4 = getDet(xn);
trainData = getFeature(trnd4, featureDimension);

The pretrained autoencoder was trained using the following network layers and training options.

% Create network layers
layers = [ sequenceInputLayer(1,Name='in')
bilstmLayer(32,Name='bilstm1')
reluLayer(Name='relu1')
bilstmLayer(16,Name='bilstm2')
reluLayer(Name='relu2')
bilstmLayer(32,Name='bilstm3')
reluLayer(Name='relu3')
fullyConnectedLayer(1,Name='fc')
regressionLayer(Name='out') ];

% Set options
MaxEpochs=20, ...
MiniBatchSize=16, ...
Plots='training-progress');

The training steps takes several minutes. If you want to train the network, select trainingFlag = “Train network”. Then, you can load the trained network into the Predict block from Deep Learning Toolbox™ used in the DCArcModelFinal model.

trainingFlag = "Use pretrained network"

if trainingFlag == "Train network"
% training network
net = trainNetwork(trainData,trainData,layers,options);
save('network.mat','net');
end

If you want to skip the training steps, you can run the DCArcModelFinal model loaded with the pretrained network in netData.mat to detect arc faults in load signals.

Figure 5: Training progress for the autoencoder.

The figure shows the histogram for the reconstruction error produced by the autoencoder when the input is the training data. You can use the statistics for the reconstruction error to choose the detection threshold. For instance, choose the detection threshold to be three times the standard deviation of the reconstruction error.

Figure 6: Histogram for the reconstruction error produced by the autoencoder when the input is the training data.

#### Model for Anomaly Detection Using Autoencoder

The DCArcModelFinal model is used for real-time detection of the arc fault in a DC load signal. Before running the model, you must specify the simulation stop time in seconds in the workspace variable t.

Figure 7: DCArcModelFinal for arc fault detection.

The first block generates a noisy DC load signal with arc fault in continuous time. The load voltage is then converted into a discrete-time signal sampled at 20 kHz by the Rate transition block in DSP System Toolbox™. The discrete time signal is then buffered to the LWTFeatureGen block that obtains the desired level 4 detail projection after preprocessing. The detail projection is then segmented in 100 sample frames that are the test features for the Predict block. The Predict block has been preloaded with the network pretrained using the load signal under normal conditions. The anomaly detection block then calculates the root-mean-square error (RMSE) for each frame and declares the presence of an arc fault if the error is above some predefined threshold.

This plot shows the regions predicted by the network when the wavelet-filtered features are used. The autoencoder was able to detect all 10 arc fault regions correctly. In other words, we obtained a 100% probability of detection in this case.

Figure 8: Detection performance for the autoencoder using wavelet-filtered features.

This plot shows the anomaly detection performance of the raw data trained autoencoder (pretrained network included in netDataRaw.mat). When we used raw data for anomaly detection, the encoder was able to identify seven out of 10 regions correctly.

Figure 9: Detection performance for the autoencoder using raw load signal.

We generated a 50 second long anomalous signal with 40 arc fault regions (this data is not included with the example). When tested with the autoencoder trained with raw signals, the arc regions were detected with a 57.85% probability of detection. In contrast, the autoencoder trained with the wavelet-filtered signals was able to detect the arc fault regions with a 97.52% probability of detection.

We also investigated the impact of the load signal normalization on the fault detection performance of the autoencoder. To this end, we modified the sequence input layer of the autoencoder model such that the input data is normalized when it is forward propagated through the input layer. We chose the ‘zscore’ normalization for this purpose. The modified autoencoder layers are:

layers = [ sequenceInputLayer(1,Name='in',Normalization='zscore')
bilstmLayer(32,Name='bilstm1')
reluLayer(Name='relu1')
bilstmLayer(16,Name='bilstm2')
reluLayer(Name='relu2')
bilstmLayer(32,Name='bilstm3')
reluLayer(Name='relu3')
fullyConnectedLayer(1,Name='fc')
regressionLayer(Name='out') ];

Similar to the previous experimental setup, we trained one autoencoder with raw data and another autoencoder with wavelet-filtered load signal under normal conditions. Then, we monitored the fault detection performance for both autoencoders. We ran the simulation for 5 minutes. The faulty load signal included 50 arc faults occurring at random time instances. The autoencoder trained with raw data achieved a detection probability of 80%. In contrast, the autoencoder trained with the wavelet-filtered signals was able to detect the arc fault regions with a 96% probability of detection.

### Summary

In this example, we demonstrated how autoencoders can be used to identify arc faults in DC systems. Both the raw and wavelet filtered load signals under normal conditions can be used as features to train the autoencoders. These anomaly detection mechanisms can be used to detect arc faults in a timely manner and thus protect a DC system from damages caused by the faults.

### References

[1] Wang, Zhan, and Robert S. Balog. “Arc Fault and Flash Signal Analysis in DC Distribution Systems Using Wavelet Transformation.” IEEE Transactions on Smart Grid 6, no. 4 (July 2015): 1955–63. https://doi.org/10.1109/TSG.2015.2407868

### Helper Functions

getDet - this function obtains the wavelet-filtered normal load signal and normalizes them.

function d4 = getDet(x)
% This function is only intended to support examples in the Wavelet
% Toolbox. It may be changed or removed in a future release.

LS = liftingScheme(Wavelet='db3');
[ca4,cd4]= lwt(x,Level=4,LiftingScheme=LS);
D4 = lwtcoef(ca4,cd4,LiftingScheme=LS,OutputType="projection",...
Type="detail");
d4 = normalize(D4);
end

getFeature - this function segments the wavelet-filtered into features of the size featureDimension.

function feature = getFeature(x, sz)
% This function is only intended to support examples in the Wavelet
% Toolbox. It may be changed or removed in a future release.

n = floor(length(x)/sz);
feature = cell(n,1);

for ii = 1:n
c1 = 1+((ii-1)*sz);
c2 = sz+((ii-1)*sz);
ind = c1:c2;
feature{ii} = transpose(x(ind,:));
end
end

sigresize - this function removes the transient part of the load signal.

function xn = sigresize(x,sz)
% This function is only intended to support examples in the Wavelet
% Toolbox. It may be changed or removed in a future release.

n = floor(length(x)/sz);
lf = n*sz;
xn = zeros(lf,1);
xn(1:lf) = x(1:lf);
end