Saturday, December 2, 2023
HomeMatlabHacking Higher Emergency Response Instances with MATLAB Cellular » Scholar Lounge

Hacking Higher Emergency Response Instances with MATLAB Cellular » Scholar Lounge

In the present day we’re joined by Rafael Otero, Alejandro Sánchez Roncero, Víctor Manuel López Higueras, and Pablo Pastor, who received first place at MATHACK 2022 by growing an software that may detect if somebody has skilled a bodily shock and, if that’s the case, ship an alert to a selected emergency contact. Over to you guys!


For MATHACK 2022, organized by the European MATLAB Scholar Ambassadors, we needed to develop a small software that contributed to the UN Sustainable Improvement Targets with the assistance of MATLAB, a cellular gadget and its sensors.By way of MATLAB Cellular we might purchase knowledge akin to accelerations (linear and angular) and place (3D coordinates) and stream it to our private computer systems in real-time. Measured knowledge ought to be processed (i.e., filtering and state estimation). Utilizing this knowledge, we needed to suggest an software thatcontributed positively to a minimum of 1 Sustainable Improvement Purpose (SDG).

Breaking down the issue

All of the members of the workforce had earlier expertise working with MATLAB since it’s included in our college diploma curriculum (Aerospace Engineering). Nonetheless, the actual fact of getting to use our information to an actual drawback was initially troublesome for us as a result of wehadn’t used MATLAB Cellular earlier than and we had problem collectively producing our personal datasets for coaching and validation (normally they’re supplied in school classes). For a similar motive, we have been additionally excited to get fingers on expertise and deal with this problem.After a couple of minutes of brainstorming and a few concepts, we got here up with the thought to implement a Shock Alert Monitor (SAM). SAM would have two predominant functionalities: estimate the present dynamic state of the individual carrying the cellular (i.e., idle, strolling or operating) and detecting shocks. On this approach, the appliance might notify to the individual’s kin by way of Telegram if a shock was detected, in order that medical help could possibly be supplied quicker than in regular conditions.

This challenge is thus associated to the next SDGs:

  • SDG3 (Good well being and well-being) by bettering healthcare because it drastically reduces the arrival time of medical workers.
  • SDG10 (Reduces inequalities): by serving to individuals who reside removed from hospitals in large cities and whose entry to healthcare shouldn’t be completely assured.
  • SDG11 (Sustainable cities and communities): by extending theSmart Metropolis idea since itprovides helpful data for metropolis companies.

How SAM works?

Through the competitors, all members of the workforce labored arduous to develop the Shock Alert Monitor. The SAM challenge will be divided into 5 components: Knowledge acquisition, Filtering, State classification, Shock detection and Shock Notification. These components will be seen within the followingfigure:

1. Knowledge acquisition

Utilizing the appliance MATLAB Cellular, we collect from the sensors of the smartphone the next knowledge:

  • Acceleration (linear and angular) within the physique reference system of the cellular.
  • Velocity.
  • Place (i.e., latitude, longitude and altitude).

The info is streamed repeatedly and in actual time from the cell phone to the pc by way of web. This offers a fantastic benefit since each units should not required to stay shut to one another for the appliance to work accurately. The acquisition frequency was set to 100 Hz (the utmost allowed by the appliance). With the assistance of MATLAB documentation, the implementation was easy: from the cellular facet, we solely want to put in the appliance and connect with the MATLAB cloud. From the pc facet, we additionally connect with the MATLAB cloud and create a moviledev object which will get up to date repeatedly.

Tovalidate the mannequin in later phases, we additionally allowed the choice to decide on between real-time knowledge or knowledge that was logged beforehand. Utilizing the second possibility, a datafile was loaded through the setup of our program.

obj.offline_data.Acceleration = Acceleration;

obj.offline_data.Place = Place;

elseif obj.isModeStream()

% Allows the cellular to log and stream knowledge

obj.mobile_obj = mobiledev;

obj.mobile_obj.SampleRate = 100; % Units pattern fee at which gadget will purchase the info

obj.mobile_obj.AngularVelocitySensorEnabled = 1;

obj.mobile_obj.OrientationSensorEnabled = 1;

obj.mobile_obj.AccelerationSensorEnabled = 1;

obj.mobile_obj.PositionSensorEnabled = 1;

obj.mobile_obj.MagneticSensorEnabled = 1;

obj.mobile_obj.Logging = 1; % Begin the transmission of information from all chosen sensors

error(‘Chosen mode: %s not legitimate’, obj.mode);

2. Filtering

The acceleration knowledge acquired from the cellular gadget is noisy, so after acquisition, a second stage of filtering is required to be carried out. As a result of our background in sign evaluation, we already knew that we should always implement a low-pass filter, because the data is contained the low-band frequency. Nonetheless, it’s all the time an excellent apply to analyse the sign, so we used the Sign Analyzer App of MATLAB. For the reason that acceleration is obtained within the 3-body axis, we first compute its norm. Within the following determine it may be seen how the sign vitality is concentrated within the low frequencies.
As soon as it was confirmed {that a} low-pass filter ought to be designed, we moved to the MATLAB’s Filter Designer App to decide on the most effective mannequin and regulate its coefficients. We chosen a Finite Impulse Response (FIR) filter with the design technique Least-squares. The move and cease frequencies have been set to 0.5 and 20 Hz respectively. The uncooked and filtered indicators are depicted within the following determine.

The speed and the place weren’t required to be filtered.

3. State classifier

The pace of the person, which will be obtained via MATLAB Cellular, is used to find out if they’re idle, strolling, operating or driving.However why do we have to know the state of the smartphone if we solely need to detect acceleration shocks? For a security motive: If we detect a 2g shock whereas the person is driving, it is going to be extra harmful than the identical shock when the person is idle or strolling. That motive makes this a part of the challenge essential as a result of if we all know the state of the person, we will decide how dangerous the detected shock has been.

To find out the state of the smartphone based mostly on its velocity, we’ve used a Ok-Nearest Neighbor Classifier Mannequin. Machine Studying and Deep Studying are very highly effective instruments and we needed to incorporate certainly one of these methods in our challenge. On, we discovered a dataset that contained the rate of the sensor and 4 completely different states to categorise: IDLE, WALKING, RUNNING and DRIVING. Our Ok-NN mannequin was skilled utilizing computerized hyperparameter optimization:

velocity = dataset.velocity;

mannequin = fitcknn(velocity,label,‘OptimizeHyperparameters’,‘auto’);

%% Compute the loss and get the confusion chart

predictions = predict(mannequin,velocity);




As soon as the mannequin is skilled, we’re able to predict the state of the person with the subsequent MATLAB operate:

operate state = getState(velocity,mannequin,interval,Fs)

% state -> State of the sensor based mostly in its velocity

% velocity -> Velocity magnitude time vector (m/s)

% mannequin -> Machine Studying mannequin used for prediction

% interval -> Interval used for computing the imply of velocity (s)

% Fs -> Sampling fee (Hz)

Vmean = imply(velocity(end-interval*Fs:finish));

state = char(predict(mannequin,Vmean));

4. Shock detection

To handle if the smartphone of the person has suffered an necessary acceleration shock, we simply compute the utmost acceleration in a pre-defined interval. Then, we examine this most with our threshold acceleration and if the brink is exceeded, we detect the shock with a Boolean variable:

operate [shock, Amax] = isShock(acceleration,threshold,interval,Fs)

% shock -> Signifies a shock within the acceleration knowledge (boolean)

% Amax -> Most acceleration within the samples (m/s2)

% acceleration -> Acceleration magnitude time vector (m/s2)

% threshold -> Acceleration above this worth might be thought of as shocks (m/s2)

% interval -> Interval used within the shock search (s)

% Fs -> Sampling fee (Hz)

Amax = max(acceleration(end-interval*Fs:finish));

5. Notification

SAM solely sends a notification if the acceleration threshold has been exceeded. Within the message we will see the magnitude of the shock (measured in g’s), the state of the person (IDLE, WALKING, RUNNING or DRIVING) and the situation of the influence in a map.

By way of a Telegram bot, the notification is shipped to an exterior gadget (emergency companies, kin, pals, …). The MATLAB-Telegram coupling was very straightforward with the Telegram Bot Toolbox API. We need to thank AlekseiKulkin for this work as a result of it allows you to to do helpful issues with a couple of traces of code. The operate that we used to inform a harmful shock is:

operate sendAlert(state,Amax,lat,lon)

% state -> State of the sensor based mostly in its velocity

% Amax -> Most acceleration of the shock (m/s2)




ShockBot = telegram_bot(BotToken);

mapsURL = [‘’ sprintf(‘%.7f’,lat)

‘,’ sprintf(‘%.7f’,lon) ‘/@’ sprintf(‘%.7f’,lat) ‘,’ sprintf(‘%.7f’,lon) ‘,16z?hl=es’];



geolimits([lat-0.001 lat+0.001],[lon-0.001 lon+0.001])


msg = [‘System detected a <b>’ sprintf(‘%.2f’,Amax/9.81) ‘g shock</b> while ‘

‘user was <b>’ state ‘</b>. Google Maps: ‘ mapsURL];

ShockBot.sendPhoto(ChatID, ‘photo_file’, ‘map.png’, % photograph

‘usepm’, true, % present progress monitor

‘caption’, msg,‘parse_mode’,‘HTML’); % caption of photograph


Combining each defined step within the earlier sections, we will now see how SAM works. To check and validate the mannequin, we implement a simulation surroundings in Simulink. To debug the mannequin in real-time, we choose as solver an ode1 (Euler) with a fixed-step (1 s) and a cease time of infinity. On this approach, the mannequin will be operating on a regular basis it’s wanted, and we will visualise and debug the leads to actual time. Within the following image we present the scheme of the Simulink mannequin, the place the main_class refers a to a MATLAB class which implements all of the features that have been described beforehand.

Now we use this mannequin to check SAM. First, we will see one member of the workforce hanging his smartphone. The acceleration measurements are under the brink (he isn’t shaking his telephone) so no shock is detected by SAM.

Then, he throws his telephone, and a giant acceleration spike is captured. When the sign is larger than the acceleration threshold, SAM detects a shock as we will see within the subsequent picture:

When the shock is detected, a message via a Telegram bot is shipped to a different gadget. The alert accommodates the utmost acceleration detected, the recognized state by the ML mannequin and the situation of the telephone in a map:

Key Takeaways

Participation in MATHACK 2022 has created a fantastic motivation and expertise for all of us. It not solely allowed us to enhance our teamwork expertise, but additionally to know the best way to work underneath strain, one thing that’s important in a hackathon. We additionally gained a fantastic perception in new purposes that MATLAB presents and acquired fingers on expertise in a real-life challenge, with all of the difficulties that it carries. Final however not least, we wish to congratulate the remainder of the MATHACK 2022 contributors. Their initiatives have been very attention-grabbing, and on the finish all of us must cooperate with a view to have efficient and optimistic contributions on the SDGs. The code, mannequin and datasets will be discovered on this repository.



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments