%% Classify ECG Signals in Simulink Using Deep Learning
% This example shows how to use wavelet transforms and a deep learning
% network within a Simulink (R) model to classify ECG signals. This example
% uses the pretrained convolutional neural network from the _Classify Time
% Series Using Wavelet Analysis and Deep Learning_ example of the Wavelet
% Toolbox(TM)  to classify ECG signals based on images from the CWT of the
% time series data. For information on training, see
% <docid:wavelet_ug#mw_1972856e-c413-4b6d-bc0a-8fd006ec8b8e Classify Time
% Series Using Wavelet Analysis and Deep Learning>.
%

%% ECG Data Description
% This example uses ECG data from <https://physionet.org/ PhysioNet>
% database. It contains data from three groups of people:
%
% # Persons with cardiac arrhythmia (ARR)
% # Persons with congestive heart failure (CHF)
% # Persons with normal sinus rhythms (NSR)
%
% It includes 96 recordings from persons with ARR, 30 recordings from
% persons with CHF, and 36 recordings from persons with NSR. The
% |ecg_signals| MAT-file contains the test ECG data in time series format.
% The image classifier in this example distinguishes between ARR, CHF, and
% NSR.

%% Algorithmic Workflow
% The block diagram for the algorithmic workflow of the Simulink model is
% shown.
%
% <<../ECGBlockDiagram.jpg>>
%

%% ECG Deep Learning Simulink Model
% The Simulink model for classifying the ECG signals is shown. When the
% model runs, the |Video Viewer| block displays the classified ECG signal.
open_system('ecg_dl_cwtMDL');

%% ECG Preprocessing Subsystem
% The |ECG Preprocessing| subsystem contains a |MATLAB Function| block that
% performs CWT to obtain scalogram of the ECG signal and then processes the
% scalogram to obtain an image. It also contains an |Image Classifier|
% block from the Deep Learning Toolbox(TM) that loads the pretrained
% network from |trainedNet.mat| and performs prediction for image
% classification based on SqueezeNet deep learning CNN.
open_system('ecg_dl_cwtMDL/ECG Preprocessing');

%%
% The |ScalogramFromECG| function block defines a function called
% |ecg_to_scalogram| that:
%
% * Uses 65536 samples of double-precision ECG data as input.
% * Create time frequency representation from the ECG data by applying
% Wavelet transform.
% * Obtain scalogram from the wavelet coefficients.
% * Convert the scalogram to image of size (227-by-227-by-3).
%
% The function signature of |ecg_to_scalogram| is shown.
type ecg_to_scalogram

%% ECG Postprocessing
% The |ECG Postprocessing| MATLAB function block defines the
% |label_prob_image| function that finds the label for the scalogram image
% based on the highest score from the scores outputed by the image
% classifier. It outputs the scalogram image with the label and confidence
% overlayed.
type label_prob_image

%% Run the Simulation
% To verify the algorithm and display the labels and confidence score of
% the test ECG signal loaded in the workspace, run the simulation.
set_param('ecg_dl_cwtMDL', 'SimulationMode', 'Normal');
sim('ecg_dl_cwtMDL');


%% Code Generation
% With GPU Coder(TM), you can accelerate the execution of model on
% NVIDIA(R) GPUs and generate CUDA(R) code for model. See the
% <docid:gpucoder_ug#mw_9dcfa213-faa3-4e39-9475-9c3cb582fdcf> for more
% details.

%% Cleanup
% Close the Simulink model.
close_system('ecg_dl_cwtMDL/ECG Preprocessing');
close_system('ecg_dl_cwtMDL');

% Copyright 2020 The MathWorks, Inc.