Real-Time Noise Detection on Raspberry Pi Using Deep Signal Anomaly Detector
This example shows how to detect the presence of noise in sinusoidal signals on a Raspberry Pi device using Simulink® Support Package for Raspberry Pi® Hardware and Deep Signal Anomaly Detector (DSP System Toolbox) block from DSP System Toolbox™.
Introduction
The Deep Signal Anomaly Detector block detects anomalies in input data by reconstructing the input signal using a trained deep learning network and thresholding the loss of reconstruction. This example uses a long short-term memory (LSTM) forecaster model trained on normal sinusoid data and uses the trained model to detect the presence of anomalies in sinusoid data containing anomalies. The anomaly detection model is deployed on Raspberry Pi hardware. The host computer sends anomalous data to the Raspberry Pi via UDP, which performs anomaly detection and illuminates the in-built LED when the presence of an anomaly is detected.
Prerequisites
For more information on how to use the Simulink Support Package for Raspberry Pi Hardware to run a Simulink model on Raspberry Pi hardware, see Get Started with Simulink Support Package for Raspberry Pi Hardware (Simulink).
Required Hardware
To run this example, you need a Raspberry Pi board.
Data and Model Description
This example detects the presence of anomalies in sinusoidal signals. Here, anomalies are defined as noise present in the signal. This figure illustrates regions of a sinusoidal signal with and without anomalies. The objective of this example is to identify these anomalous regions in the input signal.
This example uses an LSTM forecaster model to detect signal anomalies. The model has been created and trained using the deepSignalAnomalyDetector
(Signal Processing Toolbox) function in Signal Processing Toolbox. For details on training the model used for noise detection in this example, refer to Detect Independent Anomalies in Multichannel Sinusoidal Signal (DSP System Toolbox). The model has been trained on a set of sinusoidal signals of constant frequency and amplitude which do not contain anomalies.
The trained network model and parameters have been exported to a MAT file (LSTMForecasterModel.mat
) using the saveModel
(Signal Processing Toolbox) method, which you can provide to the Deep Signal Anomaly Detector block.
Streaming Setup
This example uses two Simulink models to illustrate anomaly detection on a Raspberry Pi device. The model hostModel.slx
creates the noisy sinusoidal signal and sends it to the Raspberry Pi device using a UDP Send (DSP System Toolbox) block. This model runs on the host computer.
The model hardwareModel.slx
runs on the target Raspberry Pi hardware. This model receives the noisy data from the hostModel
using a UDP Receive (DSP System Toolbox) block, detects anomalies in the noisy signal, and sends back the anomaly detection and the delayed input signals to hostModel
using a UDP Send block. The model also illuminates the built-in LED on the Raspberry Pi device when an anomaly is detected in the input signal.
The hostModel
receives the signals sent by hardwareModel
using a UDP Receive block and plots them on a Time Scope (DSP System Toolbox) block.
Host Model
Open and examine the Simulink model hostModel.slx
. This model runs on the host computer.
model1 = 'hostModel';
open_system(model1);
The model uses a Sine Wave (DSP System Toolbox) block to generate the sinusoid signal of frequency 1 Hz at a sampling rate of 100 Hz. The Random Source (DSP System Toolbox) and Pulse Generator (Simulink)blocks add a random periodic noise to the sinusoid signal. The UDP Send to raspi block sends the noisy signal to the Raspberry Pi via UDP.
Update the target IP address for your Raspberry Pi on the UDP Send block.
targetIPAddress = '''172.21.19.91'''; set_param([model1 '/UDP Send to raspi'], 'remoteURL', targetIPAddress)
The model uses a UDP Receive block to receive the UDP packets from hardwareModel
. The received packets contain data delayed input and anomaly detection signals. Split the two signals using a Demux (Simulink) block and plot them on a Time scope.
Anomaly Detection Model
Open and examine the Simulink model hardwareModel.slx
. This model runs on the target Raspberry Pi device.
model2 = 'hardwareModel';
open_system(model2);
The model uses a UDP Receive block to receive the UDP packets sent from the host computer. Verify that the Local IP Port
parameter on the UDP Receive from host block has been set to the same value (25001) as the Remote IP Port
parameter on the UDP Send to raspi block in hostModel.slx
to ensure proper transmission of data.
The UDP Receive from host block sends the received data to the Deep Signal Anomaly Detector block. The Detector MAT-file path
parameter of the block is set to LSTMForecasterModel.mat
, which is the MAT file that contains the trained model. For more information, see the Data and Model Description section. The Deep Signal Anomaly Detector block outputs the anomaly signal, which has a value of 1
when there is an anomaly in the corresponding input sample, and a value of 0
when there is no anomaly.
The Downsample block downsamples the anomaly detection signal by a factor of 10 to a sample rate of 10 Hz and passes the signal to an LED block. The LED block controls the built-in LED on Raspberry Pi board based on the input signal and is now illuminated when an anomaly is detected.
The Delay block delays the input signal to align it with the anomaly detection flag. The Mux (Simulink) block combines the two signals and sends the combined signal to the host machine using a UDP Send block. Update the IP address of your host machine on the UDP Send to host block.
hostIPAddress = '''172.21.19.247'''; set_param([model2 '/UDP Send to host'], 'remoteURL', hostIPAddress)
Ensure that the Remote IP Port
parameter on the UDP Send to host block has been set to the same value (25000) as the Local IP Port
parameter on the UDP Receive from raspi block in hostModel.slx to
ensure proper transmission of data.
Configure Anomaly Detection Model
In order to run the anomaly detection model on the Raspberry Pi board, configure the hardwareModel.slx
model using the following steps:
To open the Model Configuration Parameters dialog box, click the gear icon on the Simulink model toolstrip.
In the Configuration Parameters dialog box, select Hardware Implementation.
Set the Hardware board parameter to
Raspberry Pi
. This selection automatically populates the parameters in the Hardware board settings with the default values for the Raspberry Pi board.In the Hardware board settings pane, expand Target hardware resources and select Board Parameters. Specify these parameter values:
Device Address: The IP address or host name of the hardware.
Username: Specify the root user name of the Linux® system running on the hardware. The default user name of the Raspian Linux distribution is
pi
.Password: Specify the root password of the Linux system running on the hardware. The default password of the Raspian Linux distribution is
raspberry
.
5. Click Apply. Click OK to save your changes
Deploy Anomaly Detection Model on Raspberry Pi Hardware
Generate an executable from the model hardwareModel.slx
.
rtwbuild(model2)
### Starting build procedure for: hardwareModel ### Successful completion of build procedure for: hardwareModel Build Summary Top model targets: Model Build Reason Status Build Duration ================================================================================================================= hardwareModel Information cache folder or artifacts were missing. Code generated and compiled. 0h 3m 0.041159s 1 of 1 models built (0 models already up to date) Build duration: 0h 3m 33.858s
Copy the executable to the Raspberry Pi device and deploy.
r = raspi; exeName =[model2 '.elf']; destination = '/home/pi/'; putFile(r,exeName,destination); runCommand = ['./' exeName ' &']; system(r, runCommand);
Start Simulation
To simulate the hostModel
, select the Run button on the Simulation tab. Note that this example uses simulation pacing to slow down the simulation such that the value of the Simulation time per wall clock parameter is 1 second. This value ensures that the rates at which the two models send and receive data match. You can use simulation pacing to slow down or speed up the simulation to observe its behavior in real time or at a specified speed.
The first display in the Time Scope shows the received signal and the second display shows the signal corresponding to anomaly detection. You can see that the model is able to detect the noisy regions of the sinusoid signal.
Stop the execution of the deployed model.
stopExecutable(codertarget.raspi.raspberrypi,exeName)
See Also
Functions
deepSignalAnomalyDetector
(Signal Processing Toolbox) |saveModel
(Signal Processing Toolbox)
Blocks
- Deep Signal Anomaly Detector (DSP System Toolbox) | UDP Send (DSP System Toolbox) | UDP Receive (DSP System Toolbox) | Sine Wave (DSP System Toolbox) | Random Source (DSP System Toolbox) | Pulse Generator (Simulink)
Related Topics
- Get Started with Simulink Support Package for Raspberry Pi Hardware (Simulink)
- Detect Independent Anomalies in Multichannel Sinusoidal Signal (DSP System Toolbox)
- Fault Detection and Localization in Three-Phase Power Transmission Using Deep Signal Anomaly Detector in Simulink (DSP System Toolbox)
- Detect Anomalies in ECG Data Using Wavelet Scattering and LSTM Autoencoder in Simulink (DSP System Toolbox)
- Deploy Signal Classifier Using Wavelets and Deep Learning on Raspberry Pi
- Deploy Signal Segmentation Deep Network on Raspberry Pi