## Introduction

Welcome back to Vibrations All Around Us! In this blog post, we are going to be building an algorithm for a wearable device to rank your activity levels throughout the day. Let’s get into it.

When we think of vibrations, we typically think of high frequency movement occurring over a short period of time, but we can also observe vibrations over the course of minutes or hours. When it comes to looking at activity levels, we are interested in vibrations that occur over a very long period of time. I’m going to hold the Nicla in my hand to emulate a wrist-worn wearable device and perform a series of tasks over 2 minutes. These tasks will go from highly active to low activity. During the experiment, I will do the following for 30 seconds each:

- Sit-Ups
- Pretend to play a racing game on my phone
- Browse social media laying down
- Lay completely still (fake nap)

Let’s take a look at the time series to see what we’re working with. For this blog, we’ll be using the resultant acceleration (look at Blog 2 for a refresher on this) to perform our analysis, but for a more complex system you would preserve and analyze individual components of the sensor. Below is plot of the resultant where I’ve added red lines to mark the points where I switched activities in the experiment:

In moments of high activity, we as humans are likely to experience large impacts, such as when running and moving heavy objects. Because of this, we can characterize active moments in time as time periods with large spikes of acceleration, or periods where acceleration has a high *variance*. Variance is a measure of dispersion of values from their mean. The formula is as follows:

S^{2} = \frac{1}{n-1}\sum_{i=1}^{n}{(x_i}-\bar{x})^2

where *S ^{2}* is the sample variance, x̄ is the mean of the sample, n is the number of samples in the set, and

*x*

_{i}is i’th sample. Here is a simple algorithm for assigning an activity score over the course of the experiment:

- Choose variance bins to correspond with activity levels
- Window the last X seconds of acceleration data and take the variance of the resultant over the window
- Assign to an activity level based on the variance bin values
- Wait for X seconds, return to 2.

First, let’s decide on how often we want to generate an activity score – I’ll set a parameter to generate one for every second and then create a 5 second window to calculate variance from. This might be familiar to you from our previous lesson, where we applied another FIR filter, the moving average filter. This will be much like like the MAF from the previous lesson, except we won’t be calculating an output at every time step to save memory and because we don’t need an activity score at millisecond resolution throughout the day (I don’t need this level of scrutiny in my life).

After visually inspecting the variance changes between stages, we can use the following variance values compared against a threshold to assign an activity score from the variance values. Let’s say that an activity score of 1 is being completely still, and an activity score of 4 is actively exercising. I’ve chosen the following values

Score | Variance Range [(m/s^2)^2] |

4 | 3e-3 < X ≤ ∞ |

3 | 4e-4 < X ≤ 3e-3 |

2 | 4e-5 < X ≤ 4e-4 |

1 | 0 < X ≤ 4e-5 |

This looks OK, let’s make the variance calculation non-causal, centering the values that the variance is calculated at to remove any delays, and then let’s add in hysteresis to remove any jittering effects.

Not too shabby! Keep in mind that the chosen variance range values will need to be tuned depending on the location of your wearable device. Of course, in the real world, users are not running a nice controlled experiment, so other algorithms which *learn* the user’s behavior would be much more successful. Think Circuits would be happy to build those into your next activity tracking product!

See you next week, where we’ll be doing an important lesson on the Frequency domain and the Fast-Fourier Transform.