Main Content

Estimate Blood Pressure Using Algorithm Export Workflow on Android Device

This example shows how to use the Simulink® Support Package for Android® Devices to integrate an algorithm developed in Simulink into an existing Android application using Android Studio. This example shows how to create a library module for the Simulink model that estimates blood pressure and displays it on an Android application. You can use this workflow to integrate any algorithm with an existing Android application using Android Studio.

Prerequisites

For more information on how to use the Simulink Support Package for Android Devices to run a Simulink model on your Android device, see Getting Started with Android Devices.

Required Hardware

  • Android device such as a phone or tablet.

  • USB cable

Hardware Setup

Connect the Android device to the host computer using the USB cable.

Dependencies

Implementing this example requires these Simulink models.

   Simulink Model Name             |                                     Purpose
--------------------------------------------------------------------------------------------------------------------------------------------------------------
1. androidLinearRegressionDeploy   |   Use this model to observe if the neural network is trained accurately to display the blood pressure of an individual.
                                   |   Simulate the model and observe the estimated blood pressure results on the Display blocks.
                                   |   Deploy the model as a standalone application on your Android device.
---------------------------------------------------------------------------------------------------------------------------------------------------------------
2. androidLinearRegressionExport   |   Use this model to create a library module after you verify that the algorithm in the androidLinearRegressionDeploy
                                   |   Simulink model works as expected as displays the estimated blood pressure results.
                                   |   Use Android Studio to export the algorithm into an existing Android application.

Task 1: Load Data Set for Training Machine Learning Algorithm

This example uses the sample data set hospital.mat from the Statistics and Machine Learning Toolbox™. Run these commands in the MATLAB® Command Window.

1. Load the sample data set in your workspace.

load hospital

2. Create a table and populate it with the Sex, BloodPressure, Age, and Smoker workspace variables.

t = table(hospital.Sex,hospital.BloodPressure(:,1),hospital.Age,hospital.Smoker, 'VariableNames',{'Sex','BloodPressure','Age','Smoker'})
t =
100x4 table
   Sex      BloodPressure    Age    Smoker
  ______    _____________    ___    ______
  Male           124         38     true
  Male           109         43     false
  Female         125         38     false
  Female         117         40     false
    :             :           :       :

3. Fit a linear regression model using a matrix input data set.

T = [0 0 0 0;1 0 0 0;0 0 1 0;0 0 0 1];
T =
   0     0     0     0
   1     0     0     0
   0     0     1     0
   0     0     0     1
mdl1 = fitlm(t,T)
mdl1 =
Linear regression model:
  BloodPressure ~ 1 + Sex + Age + Smoker
Estimated Coefficients:
                 Estimate       SE        tStat        pValue
                 ________    ________    ________    __________
  (Intercept)      116.14      2.6107      44.485    7.1287e-66
  Sex_Male       0.050106     0.98364    0.050939       0.95948
  Age            0.085276    0.066945      1.2738        0.2058
  Smoker_1           9.87      1.0346      9.5395    1.4516e-15
Number of observations: 100, Error degrees of freedom: 96
Root Mean Squared Error: 4.78
R-squared: 0.507,  Adjusted R-Squared: 0.492
F-statistic vs. constant model: 33, p-value = 9.91e-15

4. Save the linear regression model to the file TrainedLinearRegressionModel.mat by using the saveLearnerForCoder function.

saveLearnerForCoder(mdl1.compact, 'TrainedLinearRegressionModel.mat')

Task 2: Simulate and Deploy androidLinearRegressionDeploy Simulink Model

Open the androidLinearRegressionDeploy Simulink model.

In the simulation mode, the Simulink model uses the Constant blocks to provide the gender, age, and smoking information to the predictBP MATLAB Function block. Whereas, in the deployment mode, the Simulink model uses the Data Input blocks to provide this information from the user interface on the Android application to the predictBP MATLAB Function block.

The predictBP MATLAB function block uses the loadLearnerForCoder function to reconstruct a regression model from the model stored in the MATLAB formatted binary file TrainedLinearRegressionModel.

Run androidLinearRegressionDeploy Model in Simulation Mode

  1. Ensure to position the manual switch to the output port of the Const Gender, Const Age, and Const Smoker Constant blocks.

  2. On the Simulation tab of the Simulink model, in the Simulate section, click Run.

The display connected to the labels output port of the predictBP MATLAB Function block displays the estimated blood pressure. While the display connected to the confidence output of the predictBP MATLAB Function block displays the confidence interval for each instance of the estimated blood pressure.

Deploy androidLinearRegressionDeploy Model on Android Device

  1. Ensure to position the manual switch to the output of the Gender, Age, and Smoker Data Input blocks.

  2. On the Hardware tab of the Simulink model, in the Mode section, select Run on board. In the Deploy section, click Build, Deploy & Start.

The androidLinearRegressionDeploy application launches automatically on your Android device.

Task 3: Configure androidLinearRegressionExport Simulink Model and Calibrate Parameters

Open the androidLinearRegressionExport Simulink model.

The FromApp block receives data from the onButtonClick method of the Android application. This method receives a data 1 after you press the Estimate Blood Pressure button on the Android application.

For the onButtonClick method, the FromApp block parameters have these values.

  1. Method nameonButtonClick.

  2. Data typeuint8.

  3. Data size (N)32.

  4. Sample time0.1.

Once the FromApp block receives data from the onButtonClick method, it triggers the Algorithm subsystem.

The FromApp blocks receive data from these methods of the Android application.

  • getGenderValue — Get gender information of an individual.

  • getAgeValue — Get age information of an individual.

  • isSmoker — Get information whether an individual is a smoker or not.

For the getGenderValue method, the FromApp block parameters have these values.

  1. Method namegetGenderValue.

  2. Data typedouble.

  3. Data size (N)1.

  4. Sample time0.1.

For the getAgeValue method, the FromApp block parameters have these values.

  1. Method namegetAgeValue.

  2. Data typedouble.

  3. Data size (N)1.

  4. Sample time0.1.

For the isSmoker method, the FromApp block parameters have these values.

  1. Method nameisSmoker.

  2. Data typedouble.

  3. Data size (N)1.

  4. Sample time0.1.

The predictBP MATLAB function block receives the gender, age, and smoker information from the Android application and predicts the blood pressure. The loadLearnerForCoder function reconstructs a regression model from the model stored in the MATLAB formatted binary file TrainedLinearRegressionModel.

The ToApp block sends the predicted blood pressure data to the Android application. The ToApp block parameters have these values.

  1. Method namesetBloodPressureValue.

  2. Number of arguments1.

Task 4: Build androidLinearRegressionExport Simulink Model

  1. On the Modeling tab of the Simulink model, in the Setup section, click Model Settings.

  2. In the Configuration Parameters dialog box, in the Target hardware resources section, under App options, select Create Android library module for Simulink model. For more information, see App Options.

  3. On the Hardware tab of the Simulink model, in the Mode section, select Run on board. In the Deploy section, click Build.

Note that:

  1. Creating a library model is supported only when building a Simulink model.

  2. Deploying a Simulink model with user interface elements such as Display and Knob is not supported while integrating an algorithm with an existing Android application.

The androidLinearRegressionExport model is built. In the Current Folder pane of MATLAB, Simulink creates a androidLinearRegressionExport_ert_rtw library module.

Task 5: Integrate Android Library Module with Existing Project in Android Studio

Use the steps described in this section to integrate any algorithm with an existing Android application using Android Studio. You can also use a blank Android project and follow the same steps as described below to integrate an algorithm.

The README.txt file under androidLinearRegressionExport_ert_rtw > README folder in MATLAB also contains the step-by-step procedure to integrate the androidLinearRegressionExport Android library module with an existing project in Android Studio.

1. In the Current Folder section, navigate to androidLinearRegressionExport_ert_rtw folder and copy the androidLinearRegressionExport folder. In Android Studio, paste the folder in the root directory of the existing project. The project folder must be at the same hierarchical level as the app folder.

Tip: Right-click the app folder and select Show in Explorer. Paste the androidLinearRegressionExport folder.

2. In the app folder, open the settings.gradle file and add this code at the beginning of the file.

include ':androidLinearRegressionExport'

3. In the app folder, open the build.gradle.app file and add this code at the beginning of the dependencies section.

implementation project(path: ':androidLinearRegressionExport')

4. Determine an Android activity required to extend the abstract base class and override the methods in the ToApp and FromApp blocks. In this example, the Android activity is performed in the MainActivity.java file.

5. To import the generated class as decided in step 4, add this code in the Import section of the activity file.

import com.example.androidLinearRegressionExport.androidLinearRegressionExport;

6. Update the activity to extend the base class *androidLinearRegressionExport *.

public class MainActivity extends androidLinearRegressionExport {

7. Override these methods.

public abstract void setBloodPressureValue(double[] arg1);
public abstract double[] getGenderValue();
public abstract double[] getAgeValue();
public abstract double[] isSmoker();
public abstract byte[] oneButtonClick();

Alternatively, you can also use the code in the MainActivity.java file in the example folder.

8. Develop a user interface on the Android application. Copy the code from the activity_main file from the example folder and paste it to the app > res > layout > activity_main file in Android Studio.

Note: This step is specific to the example. You can skip this step for your application.

9. Clean and rebuild the project.

10. From the device name drop-down list below the Android Studio IDE toolbar, select your device.

11. Run the project. The application launches automatically.

12. Provide inputs to the application such as age, gender, and smoking information.

13. Click Estimate Blood Pressure. The blood pressure information is displayed on the application.

Other Things to Try

Use the workflow described in this example to integrate the androidCreateLibrary Android library module with an existing project in Android Studio.

The androidCreateLibrary Simulink model multiplies the input received from the Android application by two and displays the result back on the Android application.

See Also

Create Custom User Interface for Android Application