%% 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 [1]. The feature extraction involves 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 signal and the pretrained network used to detect the arc faults are provided in the example folder. As training the network and arc detection in 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 Specialized Power System library in Simscape Electrical™.
%
%
% Figure 1: DCNoArc model for generating load signal under normal conditions.
% The voltage sources are modeled using the following parameters:
% AC Harmonic Source 1: 10V ac voltage and 120 Hz frequency
% AC Harmonic Source 2: 20V ac voltage and 2000 Hz frequency
% DC voltage source: 1000V
% 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.
%% Cassie Arc Model:
% The Cassie arc model is one of the most studied black box models for generating synthetic arc. The model is described by the following differential equation:
% \frac{dg}{dt} = \frac{g}{\tau} \Bigg(\frac{u^2}{U_c^2}-1\Bigg)
% 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 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 1.2e-6 seconds
% The contact separation times for the arc models are chosen at random. All the parameters have been loaded in the initFcn callbacks in the Model Properties of the Model Settings tab. We use the DCArcModelFinal model to generate a faulty load signal to test the autoencoder.
%
%% Anomaly Detection with Autoencoder
% Autoencoders are used to detect anomalies in a signal. To this end, the autoencoder accepts training data without anomalies as input and tries to reconstruct the input signal using a neural network. The network weights are calculated such that the reconstruction error is minimized. 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. Every time the autoencoder encounters an anomaly in the testing data, it produces a large reconstruction error. If the error is above the threshold in the anomaly detection block, the encoder declares it to be an anomaly. 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 feature under normal conditions.
%
% Figure 4: Raw load signal and wavelet-filtered feature under faulty conditions.
%
%% Model Training
% The load signal under normal conditions is specified in the normal.mat file in the example folder. Use the normal load signal to obtain the training features that are used as the input of the autoencoder. Train the autoencoder using these layers and training options.

% Training data: load voltage under normal condition 
featureDimension = 100;

% Create network layers
numHiddenUnits = 50;

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
options = trainingOptions('adam', ...
    'MaxEpochs',20, ...
    'MiniBatchSize',16, ...
    'Plots','training-progress');

%
% Figure 5: Training progress for the autoencoder.
%
% The trained network is specified in the file netData.mat in the example folder. The trained network can be uploaded into the Predict block from Deep Learning Toolbox™ to detect the arc fault in the load signal. 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 for 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 noisy DC load signal with arc fault in continuous time. The load voltage is then converted into a discrete-time signal sampled at 20kHz 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. We can observe the prediction from the block adding a scope to the model. The red line in the plot indicates where the transition due to anomaly is detected. The autoencoder identified most of the arc faults.
%
% Figure 8: Real-time arc fault detection as seen from scope.
%
% 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 9: Detection performance for the autoencoder using wavelet-filtered features.
%
% This plot shows the anomaly detection performance of the raw data trained autoencoder. When we used raw data for anomaly detection, the encoder was able to identify seven out of 10 regions correctly.
%
%
% Figure 10: Detection performance for the autoencoder using raw load signal.
%
% Finally, 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.
%% 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
%
%
% Copyright 2021 The MathWorks, Inc.