{"id":4199,"date":"2020-06-24T00:07:55","date_gmt":"2020-06-23T22:07:55","guid":{"rendered":"https:\/\/blogs.mathworks.com\/student-lounge\/?p=4199"},"modified":"2020-06-24T00:07:55","modified_gmt":"2020-06-23T22:07:55","slug":"create-your-own-fitness-tracker-with-matlab-online-and-matlab-mobile","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/student-lounge\/2020\/06\/24\/create-your-own-fitness-tracker-with-matlab-online-and-matlab-mobile\/","title":{"rendered":"Create Your Own Fitness Tracker with MATLAB Online and MATLAB Mobile"},"content":{"rendered":"<p>Are you looking for something fun to try using MATLAB? Did you know you can use <a href=\"https:\/\/www.mathworks.com\/products\/matlab-mobile.html\">MATLAB Mobile<\/a> and <a href=\"https:\/\/www.mathworks.com\/products\/matlab-online.html\">MATLAB Online<\/a> to create your own fitness tracker? If you want to learn more you can <a href=\"https:\/\/github.com\/mathworks\/matlab-mobile-fitness-tracker\">download the code here<\/a>.<\/p>\n<p>Smartphones have sophisticated sensor suites onboard, including GPS, accelerometers, gyroscopes, etc. You can use this sensor data along with an algorithm or a mathematical model to extract metrics like calories burned, steps taken, or flights climbed, to inform someone about how effective their workout was. Your task is not only to figure out what metrics you want to extract but also the algorithm or mathematical model to help you transform the raw sensor data into your desired metrics.<\/p>\n<h2><strong>How to Get Started<\/strong><\/h2>\n<p>1. Use your MathWorks account to access <a href=\"https:\/\/matlab.mathworks.com\/\">MATLAB Online<\/a><\/p>\n<p>2. Download <a href=\"https:\/\/www.mathworks.com\/products\/matlab-mobile.html\">MATLAB Mobile<\/a> for iOS or Android<\/p>\n<ul>\n<li>Open app and select \u2018Connect to MathWorks Cloud\u2019<\/li>\n<li>Log in using MathWorks account<\/li>\n<\/ul>\n<p>3. Get familiar with the sensors in MATLAB Mobile<\/p>\n<ul>\n<li>Acceleration (m\/s^2)<\/li>\n<li>GPS Position\n<ul>\n<li>Latitude (degrees)<\/li>\n<li>Longitude (degrees)<\/li>\n<\/ul>\n<\/li>\n<li>Speed (m\/s)<\/li>\n<li>Altitude (m)<\/li>\n<li>Course\/Heading (degrees)<\/li>\n<li>Horizontal Accuracy (m)<\/li>\n<li>Orientation (degrees)<\/li>\n<li>Angular Velocity (rad\/s)<\/li>\n<\/ul>\n<p>To find out more about these sensors check out this <a href=\"https:\/\/www.mathworks.com\/help\/matlabmobile\/ug\/sensor-data-collection-with-matlab-mobile.html\">documentation page<\/a>.<\/p>\n<p>4. Research Fitness Models<\/p>\n<h2><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-4273 size-full\" src=\"https:\/\/blogs.mathworks.com\/racing-lounge\/files\/2020\/06\/image001-e1592848536361.png\" alt=\"workflow chart\" width=\"750\" height=\"452\" \/><\/h2>\n<p>The fitness model is the computational portion of your tracker; this algorithm will accept data from the sensors you choose, then compute and return the metrics you desire. The challenge here is understanding the mathematical operations needed to transform the raw sensor data you selected for the tracker into meaningful metrics. If you have downloaded the code from GitHub, <strong>ExampleModel.mlx<\/strong> contains a sample fitness tracker model to inspire you.<\/p>\n<h2><strong>Collect and Record the Data <\/strong><\/h2>\n<p>When recording data to build the model other than identifying the correct sensor data to use, you must also ensure that you have a variety of activity data to represent the anticipated activities, for e.g. if you only test your model on data collected while walking, your model will not be robust enough to track and reward more strenuous activities like running or cycling. If you want a robust model, you must account for as many activities as possible and collect the data appropriately. To record sensor data, use the MATLAB Mobile app:<\/p>\n<ol>\n<li>Navigate to the \u201cSensors\u201d screen by clicking on the three-lined icon on the top left<\/li>\n<li>Set the <strong>Stream to<\/strong> field to <strong>Log<\/strong> \u2013 This will log sensor data to a MAT-file and save it to your <a href=\"https:\/\/drive.mathworks.com\/\">MATLAB Drive<\/a> in a folder named \u2018MobileSensorData\u2019<\/li>\n<li>Select the sensors you wish to log data from<\/li>\n<li>Click Start to begin logging data and MOVE!!<\/li>\n<li>Click Stop to save the log to MATLAB Drive \u2013 Use the \u2018<strong>Acquire Data in Background<\/strong>\u2019 option in Sensor Settings to record data while your phone is locked<\/li>\n<\/ol>\n<p><strong>Android:<\/strong><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-4227 size-large\" src=\"https:\/\/blogs.mathworks.com\/racing-lounge\/files\/2020\/06\/img_3-1024x627.jpg\" alt=\"screenshot\" width=\"1024\" height=\"627\" \/><\/p>\n<p><strong><span class=\"TextRun SCXW55670112 BCX0\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW55670112 BCX0\">iPhone:<\/span><\/span><span class=\"EOP SCXW55670112 BCX0\" data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559731&quot;:720,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/strong><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-4229 size-large\" src=\"https:\/\/blogs.mathworks.com\/racing-lounge\/files\/2020\/06\/img_4-1024x524.jpg\" alt=\"screenshot\" width=\"1024\" height=\"524\" \/><\/p>\n<h2><strong>Accessing the Data<\/strong><\/h2>\n<p>When working with MATLAB Online, MATLAB Drive can be accessed from the left-hand plane labeled \u2018CURRENT FOLDER\u2019 as shown below.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-4259 size-full\" src=\"https:\/\/blogs.mathworks.com\/racing-lounge\/files\/2020\/06\/image009.png\" alt=\"screenshot\" width=\"506\" height=\"383\" \/><\/p>\n<p>Any logged sensor data is stored in the MobileSensorData folder in your MATLAB Drive; to load the data, double click on the appropriate MAT-file in that folder or use the following command:<\/p>\n<pre>\u00a0&gt;&gt; load('filename.mat'); % replace filename.mat with the appropriate file name<\/pre>\n<p>The data is loaded into the workspace as a <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/timetables.html\">timetable;<\/a>\u00a0a timetable is a type of table in MATLAB that associates a timestamp to each row. Use the following command to view the properties of the <strong>Position<\/strong> timetable.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-4203 size-full\" src=\"https:\/\/blogs.mathworks.com\/racing-lounge\/files\/2020\/06\/img_8.jpg\" alt=\"screenshot\" width=\"537\" height=\"97\" \/><\/p>\n<pre>&gt;&gt; Position.Properties<\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-4261 size-full\" src=\"https:\/\/blogs.mathworks.com\/racing-lounge\/files\/2020\/06\/image012-e1592848946623.png\" alt=\"code\" width=\"500\" height=\"296\" \/><\/p>\n<p>There are two dimensions held in the <strong>Position<\/strong> timetable: <strong>Variables<\/strong> &#8211;&gt;\u00a0the sensor data collected and <strong>Timestamp<\/strong> &#8211;&gt; the date and time of the corresponding sensor sample.<\/p>\n<p>Use the parenthesis notation as shown below to access a specific range of sensor data as shown below.<\/p>\n<pre>&gt;&gt; first6 = Position(1:6,:);<\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-4263 size-full\" src=\"https:\/\/blogs.mathworks.com\/racing-lounge\/files\/2020\/06\/image014-e1592848974902.png\" alt=\"code\" width=\"500\" height=\"153\" \/><\/p>\n<p>To extract the data as an array without the string elements of the timestamp and variable names, use the curly braces notation as shown below.<\/p>\n<pre>&gt;&gt; first6 = Position{1:6,:};<\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-4265 size-full\" src=\"https:\/\/blogs.mathworks.com\/racing-lounge\/files\/2020\/06\/image016-e1592848984562.png\" alt=\"code\" width=\"500\" height=\"152\" \/><\/p>\n<p>The second timetable dimension &#8211;&gt; <strong>Timestamp<\/strong> contains the year, month, day, hour all the way down to the millisecond at which that sample was taken; use the following command to access the timestamp information.<\/p>\n<pre>&gt;&gt; positionTime = Position.Timestamp;<\/pre>\n<p>The best way to examine data, is to plot it. MATLAB provides you with a variety of plots to help you gain insight into the data you are working with. One effective method to explore this data is to plot it against elapsed time in seconds. <strong>positionTime<\/strong> is a datetime array; to convert this to an array of elapsed time while still maintaining the temporal integrity of the sampled data use the <strong>timeElapsed<\/strong> function provided in the GitHub repository. Add the file to the MATLAB path or your current folder. Use the following command to run it.<\/p>\n<pre>&gt;&gt; positionTime = timeElapsed(positionTime);<\/pre>\n<h2>Develop the Model<\/h2>\n<p>One possible model that can achieve the goal is to convert GPS data to steps taken. Remember this is only one possible model and I encourage you to try other models that might be more accurate. This model calculates number of steps by dividing the <strong><em>total distance travelled<\/em><\/strong> by <strong><em>1 stride length<\/em><\/strong> or <strong><em>distance travelled by 1 step.<\/em><\/strong><\/p>\n<p style=\"text-align: center;\">$ \\Large Number\\;of\\;Steps = \\frac{Total\\;Distance\\;(ft)}{Stride\\;Length\\;(\\frac{ft}{step})} $<\/p>\n<p>In the equation above, for an average stride length or say 2.5 ft, <strong><em>totalDistance<\/em><\/strong> needs to be computed to use this model. A GPS sensor or a latitude and longitude sensor can help; take the start and end point and calculate the distance between them. This works well when the distance covered is over a straight line. What happens when the path taken to get from start to end is not a straight line? Consider the following scenario:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"300\" height=\"188\" class=\"alignnone size-medium wp-image-4213\" src=\"https:\/\/blogs.mathworks.com\/racing-lounge\/files\/2020\/06\/img_13-300x188.jpg\" alt=\"screenshot\" \/><\/p>\n<p>Using this equation the distance traveled would be 300ft for this scenario because the start and end position is 3 squares apart whereas in reality the person traveled much further. To counter this, use the model along every point on the path traveled or every sampled GPS point and add the distance between these points to calculate the total distance traveled so you must calculate the distance along the path using each data point. Now we get a distance traveled of 1500ft which is significantly different. Remember my recommendation to collect data or diverse situations?<\/p>\n<p>Latitude and Longitude data from the GPS sensor is in degrees, so the distance between latitude and longitude points as calculated from a GPS sensor is actually <strong><em>degreesTravelled<\/em><\/strong><em>;<\/em>\u00a0to convert this to <strong><em>distanceTravelled<\/em><\/strong> in feet, use the following relationship:<\/p>\n<p style=\"text-align: center;\">$ \\Large \\frac{Distance\\;Travelled\\;(ft)}{Earth&#8217;s\\;Circumference\\;(ft)}= \\frac{Degrees\\;Travelled\\;(degrees)}{Total\\;Earth&#8217;s\\;Degrees\\;(degrees)} $<\/p>\n<p>The MATLAB implementation of this model is shown below.<\/p>\n<pre><strong>%% Initialize Variables<\/strong>\r\n\r\nearthCirc = 24901*5280; % 24901 miles, convert to ft by multiplying by 5280\u00a0 \r\ntotaldis = 0; \r\nstride = 2.5; % feet\r\nlat = Position{1,:};\r\nlon = Position{2,:};\r\n\r\n<strong>%% Processing Loop<\/strong>\r\n\r\nfor i = 1:(length(lat)-1)\u00a0 % Loop through every data sample\r\n      lat1 = lat(i); % Latitude of the i\u2019th sample\r\n      lon1 = lon(i); % Longitude of the i\u2019th sample\r\n      lat2 = lat(i+1); % Latitude of the (i+1)\u2019th sample\r\n      lon2 = lon(i+1); % Latitude of the (i+1)\u2019th sample\r\n      diff = distance(lat1, lon1, lat2, lon2); % MATLAB function to compute \r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0        distance between 2 points on a\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0        sphere\r\n      dis = (diff\/360)*earthCirc; % convert degrees to feet\r\n      totaldis = totaldis +dis;\r\nend\r\n\r\nsteps = totaldis_ft\/stride;<\/pre>\n<h2><strong>Share your findings<\/strong><\/h2>\n<p>Why even bother developing a fitness tracker model if you don\u2019t make a pretty plot out of it right? Use MATLAB\u2019s <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/creating_plots\/types-of-matlab-plots.html\">plotting functionality<\/a> to present data to your future wearable tech clients and us!<\/p>\n<pre>plot(lat,lon);plot(lat, lon, '-r',lat(1),lon(1),'*g',lat(end),lon(end),'*b','LineWidth',3, 'MarkerSize',10 );\r\nhold on;\r\nlegend(\u2018Route\u2019,\u2019Start Point\u2019,\u2019End Point\u2019);\r\nxlabel(\u2018Latitude\u2019)\r\nylabel(\u2018Longitude\u2019);\r\ntitle(sprintf('Workout Summary : You took %0.0f steps and moved %0.3f miles',steps, totaldis_ft\/5280) );\r\nhold off<\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-4269 size-full\" src=\"https:\/\/blogs.mathworks.com\/racing-lounge\/files\/2020\/06\/plotPosition.png\" alt=\"screenshot\" width=\"557\" height=\"411\" \/><\/p>\n<p>In conclusion, here is a fun MATLAB project to work on, by using the cellphone in your pocket and MATLAB to explore the world of wearable technology. A group of students from <a href=\"https:\/\/www.franklinps.net\/fhs\">Franklin High School<\/a> in Massachusetts, <a href=\"https:\/\/blogs.mathworks.com\/racing-lounge\/2019\/06\/05\/fitness-trackers-stem-challenge\/\">designed a fitness tracker using the full cellphone sensor suite to calculate distance traveled, flights of stairs climbed, and calories burned<\/a>. Post your files on MATLAB Central\u2019s File Exchange or GitHub and send us a link to your repository! We would love to see what you are able to achieve!<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/student-lounge\/files\/2020\/06\/image001-e1592848536361.png\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"workflow chart\" decoding=\"async\" loading=\"lazy\" \/><\/div>\n<p>Are you looking for something fun to try using MATLAB? Did you know you can use MATLAB Mobile and MATLAB Online to create your own fitness tracker? If you want to learn more you can download the code&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/student-lounge\/2020\/06\/24\/create-your-own-fitness-tracker-with-matlab-online-and-matlab-mobile\/\">read more >><\/a><\/p>\n","protected":false},"author":163,"featured_media":4273,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[52,6],"tags":[281,415,411,413],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/student-lounge\/wp-json\/wp\/v2\/posts\/4199"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/student-lounge\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/student-lounge\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/student-lounge\/wp-json\/wp\/v2\/users\/163"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/student-lounge\/wp-json\/wp\/v2\/comments?post=4199"}],"version-history":[{"count":35,"href":"https:\/\/blogs.mathworks.com\/student-lounge\/wp-json\/wp\/v2\/posts\/4199\/revisions"}],"predecessor-version":[{"id":4317,"href":"https:\/\/blogs.mathworks.com\/student-lounge\/wp-json\/wp\/v2\/posts\/4199\/revisions\/4317"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/student-lounge\/wp-json\/wp\/v2\/media\/4273"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/student-lounge\/wp-json\/wp\/v2\/media?parent=4199"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/student-lounge\/wp-json\/wp\/v2\/categories?post=4199"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/student-lounge\/wp-json\/wp\/v2\/tags?post=4199"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}