%% Deploy Signal Classifier Using Wavelets and Deep Learning on Raspberry Pi
% This example shows the workflow to classify human electrocardiogram (ECG) signals using the Continuous Wavelet Transform (CWT) and a deep convolutional neural network (CNN). This example also provides information on how to generate and deploy the code and CNN for prediction on a Raspberry Pi target (ARM®-based device).
% SqueezeNet is a deep CNN originally designed to classify images in 1000 categories. In the example Classify Time Series Using Wavelet Analysis and Deep Learning, SqueezeNet is retrained to classify ECG waveforms based on their scalograms. A scalogram is a time-frequency representation of the signal and is the absolute value of the CWT of the signal. We reuse the retrained SqueezeNet in this example.
%
%% ECG Data Description
% In this example, ECG data from PhysioNet is used. The ECG data is obtained from three groups of people: persons with cardiac arrhythmia (ARR), persons with congestive heart failure (CHF), and persons with normal sinus rhythms (NSR). The data set includes 96 recordings from persons with ARR, 30 recordings from persons with CHF, and 36 recordings from persons with NSR. The 162 ECG recordings are from three PhysioNet databases: MIT-BIH Arrhythmia Database [2][3], MIT-BIH Normal Sinus Rhythm Database [3], and The BIDMC Congestive Heart Failure Database [1][3]. Shortened ECG data of the above references can be downloaded from the GitHub repository.
%
%% Prerequisites
% ARM processor that supports the NEON extension
% ARM Compute Library version 19.05 (on the target ARM hardware)
% Environment variables for the compilers and libraries
% MATLAB Support Package for Raspberry Pi Hardware
% MATLAB Coder Interface for Deep Learning Libraries support package
% For supported versions of libraries and for information about setting up environment variables, see Prerequisites for Deep Learning with MATLAB Coder. This example is not supported in MATLAB Online™.
%
%% Functionality of Generated Code
% The core function in the generated executable, processECG, uses 65,536 samples of single-precision ECG data as input. The function:
% Takes the CWT of the ECG data.
% Obtains scalogram from wavelet coefficients.
% Converts the scalogram to an RGB mage of dimension 227-by-227-by-3. This makes the image compatible with the SqueezeNet network architecture.
% Performs prediction to classify the image using SqueezeNet.

type processECG

%
%% Create Code Generation Configuration Object
% Create a code generation configuration object for generation of an executable program. Specify generation of C++ code.

cfg = coder.config('exe');
cfg.TargetLang = 'C++';

%
%% Set Up Configuration Object for Deep Learning Code Generation
% Create a coder.ARMNEONConfig object. Specify the same version of the ARM Compute library as the one on the Raspberry Pi. Specify the architecture of the Raspberry Pi.

dlcfg = coder.DeepLearningConfig('arm-compute');
dlcfg.ArmComputeVersion = '19.05';
dlcfg.ArmArchitecture = 'armv7';

%
%% Attach Deep Learning Configuration Object to Code Generation Configuration Object
% Set the DeepLearningConfig property of the code generation configuration object to the deep learning configuration object. Make the MATLAB Source Comments visible in the configuration object at the time of code generation.

cfg.DeepLearningConfig = dlcfg;
cfg.MATLABSourceComments = 1;

%
%% Create a Connection to the Raspberry Pi
% Use the MATLAB Support Package for Raspberry Pi Support Package function, raspi, to create a connection to the Raspberry Pi. In the following code, replace:
% 'raspiname' with the name of your Raspberry Pi
% 'pi' with your user name
% 'password' with your password

r = raspi('172.18.76.69','pi','raspberry');

%
%% Configure Code Generation Hardware Parameters for Raspberry Pi
% Create a coder.Hardware object for Raspberry Pi and attach it to the code generation configuration object.

hw = coder.hardware('Raspberry Pi');
cfg.Hardware = hw;

%
% Specify the build folder on the Raspberry Pi.

buildDir = '~/remdirECG';
cfg.Hardware.BuildDir = buildDir;

%
%% Provide C++ Main File for Code Execution
% The C++ main file reads the input ECG data, calls the processECG function to perform preprocessing and deep learning using CNN on the ECG data, and displays the classification probability.
% Specify the main file in the code generation configuration object. To learn more about generating and customizing main_ecg_raspi.cpp, refer to Generating Standalone C/C++ Executables from MATLAB Code.

cfg.CustomSource = 'main_ecg_raspi.cpp';

%
%% Generate Source C++ Code Using codegen
% Use the codegen function to generate the C++ code. When codegen is used with the MATLAB Support Package for Raspberry Pi Hardware, the executable is built on the Raspberry Pi board.
% Make sure to set the environment variables ARM_COMPUTELIB and LD_LIBRARY_PATH on the Raspberry Pi. See Prerequisites for Deep Learning with MATLAB Coder.

codegen -config cfg processECG -args {ones(1,65536,'single')} -d arm_compute

%
%% Fetch Generated Executable Directory
% To test the generated code on the Raspberry Pi, copy the input ECG signal to the generated code directory. You can find this directory manually or by using the raspi.utils.getRemoteBuildDirectory API. This function lists the directories of the binary files that are generated by using codegen.

applicationDirPaths = raspi.utils.getRemoteBuildDirectory('applicationName','processECG')

%
% The complete path to the remote build directory is derived from the present working directory. If you do not know which applicationDirPaths entry contains the generated code, use the helper function helperFindTargetDir. Otherwise, specify the proper directory.

directoryUnknown = true;

if directoryUnknown
    targetDirPath = helperFindTargetDir(applicationDirPaths);
else   
    targetDirPath = applicationDirPaths{1}.directory;
end

%
%% Copy Input File to Raspberry Pi
% The text file input_ecg_raspi.csv contains the ECG samples of a representative ARR signal. To copy the file required to run the executable program, use putFile, which is available with the MATLAB Support Package for Raspberry Pi Hardware.

r.putFile('input_ecg_raspi.csv', targetDirPath);

%
% For a pictorial representation, the first 1000 samples can be plotted by using these steps.

input = dlmread('input_ecg_raspi.csv');
plot(input(1:1000))
title('ARR Signal')

%
%% Run Executable on Raspberry Pi
% Run the executable program on the Raspberry Pi from MATLAB and direct the output back to MATLAB. The input file name is passed as the command line argument for the executable.

exeName = 'processECG.elf';           % executable name
fileName = 'input_ecg_raspi.csv';     % Input ECG file that is pushed to target
command = ['cd ' targetDirPath ';./' exeName ' ' fileName];
output = system(r,command)

%
%% References
% Baim, D. S., W. S. Colucci, E. S. Monrad, H. S. Smith, R. F. Wright, A. Lanoue, D. F. Gauthier, B. J. Ransil, W. Grossman, and E. Braunwald. "Survival of patients with severe congestive heart failure treated with oral milrinone." Journal of the American College of Cardiology. Vol. 7, Number 3, 1986, pp. 661–670.
% Goldberger A. L., L. A. N. Amaral, L. Glass, J. M. Hausdorff, P. Ch. Ivanov, R. G. Mark, J. E. Mietus, G. B. Moody, C.-K. Peng, and H. E. Stanley. "PhysioBank, PhysioToolkit,and PhysioNet: Components of a New Research Resource for Complex Physiologic Signals." Circulation. Vol. 101, Number 23: e215–e220. [Circulation Electronic Pages; http://circ.ahajournals.org/content/101/23/e215.full]; 2000 (June 13). doi: 10.1161/01.CIR.101.23.e215.
% Moody, G. B., and R. G. Mark. "The impact of the MIT-BIH Arrhythmia Database." IEEE Engineering in Medicine and Biology Magazine. Vol. 20. Number 3, May-June 2001, pp. 45–50. (PMID: 11446209)
%% Supporting Functions
% helperFindTargetDir

function targetDir = helperFindTargetDir(dirPaths)
%
% This function is only intended to support wavelet deep learning examples.
% It may change or be removed in a future release.

% find pwd
p = pwd;
if ispc
    % replace blank spaces with underscores
    p = strrep(p,' ','_');
    
    % split path into component folders
    pSplit = regexp(p,filesep,'split');
    
    % Since Windows uses colons, remove any colons that occur
    for k=1:numel(pSplit)
        pSplit{k} = erase(pSplit{k},':');
    end
    
    % now build the path using Linux file separation
    pLinux = '';
    for k=1:numel(pSplit)-1
        pLinux = [pLinux,pSplit{k},'/'];
    end
    pLinux = [pLinux,pSplit{end}];
else
    pLinux = p;
end

targetDir = '';
for k=1:numel(dirPaths)
    d = strfind(dirPaths{k}.directory,pLinux);
    if ~isempty(d)
        targetDir = dirPaths{k}.directory;
        break
    end
end

if numel(targetDir) == 0
    disp('Target directory not found.');
end
end

% Copyright 2020 The MathWorks, Inc.