Eemil Aittokoski

Accelerometer-based Heart Rate Monitor App

Description

I wrote a mobile app that measures resting heart rate through the smartphone’s accelerometer. The user lies down, and a measurement is taken by placing the phone on the chest for 10 seconds. During that time, the accelerometer collects data 100 times per second. Small vibrations of the chest are detected this way. Then such a frequency from the acceleration signal is determined that corresponds to the heart rate. The user is shown a beautiful visual representation of their heart rate.

The app also has basic functionality for storing measurements for later viewing, as well as deleting them. This is a hobby project, and probably won't be released.

GitHub repository

Keywords

JavaScript

Python

React Native

Mobile Development

Signal Processing

Fourier Analysis

Health Technology

App 0 App 0

The Mobile Development Part

My goal was to build a visually appealing app that has limited functionalities but implements them solidly. This means writing as clean code as I could. The app is around a thousand lines in total, which is my largest single project so far. I utilized the Eslint and Prettier projects to help me follow good practice.

Of course, some things could be done better. At least version control is a bit lazy. Also, I should write tests.

A reason I decided to use React Native is that I wanted to especially demonstrate my skills in the React framework.

The Signal Processing Part

The input: An array of objects, each consisting of a timestamp and acceleration values for x, y and z components.
The output: A heart rate.

Before implementing the math in a mobile JavaScript environment, I used Python to draw some graphs and examine the data on my computer. Let’s dive into the data.

Chart 0

First, I took a few 10-second measurements in good conditions (i.e. lying very still and holding breath). Some are plotted in the image above: total acceleration experienced by the phone (g = 9.81 m/s^2) versus time (seconds). For the sake of simplicity, I never analyzed the xyz components separately, instead just combined them into a total acceleration value. There’s clearly quite a clean pulse in each of the graphs. So how can we extract it?

The next step I took was calculate a moving average of the data (after mapping each data point to it’s distance from 1 g).

Chart 1

Starts to look even clearer. Each peak of the red line now corresponds to a heartbeat. So I tried to find ways to find the peaks. I tried a couple of simple algorithms for finding local maxima, but a problem persisted: in order to return all the actual maximums and nothing between (see the green dots), an algorithm needs to know approximately how often a maximum should be expected – a frequency. Which is what we’re trying to find in the first place!

I also tried using autocorrelation (the blue line) to find the peaks. And even though the blue wave seems to correspond to the red line, there were some problems––the biggest one being that I did not really grasp how the autocorrelation works, at least not enough that I could trust it.

So, in order to not make things too complicated, I settled to not needing to find individual peaks, just the average frequency. After some research, I came across the Fourier transform. I did some more research and managed to successfully implement it, which I’m proud of.

Chart 2

The raw acceleration data seemed to have too much noise, but when we instead transform the moving average to the frequency domain, the tallest spike (excluding the one at 0 Hz) is the frequency that we are looking for. Convert that to bpm and we have a heart rate! This is the method that I finally implemented in the app. It’s works robustly for any bpm, allows the user to breathe during the measurement, and discards noise and momentary unexpected inaccuracies in the signal. And the result is very precise.

Reflection

For a long time I’ve been fascinated by what could be done with a smartphone’s sensors. The idea of an accelerometer-based HR monitor came out of nowhere, but I started working on the project right away since it felt like a good problem: something that I knew I’d be capable of, but would still be challenging enough. It turned out be just that, and I’m really proud of the final product.

An improved version of the app could analyze heart rate variability. It would require identifying the individual heartbeats, which I now think could be achieved by combining Fourier transform with a peak finding algorithm. A measurement longer than 10 seconds could be used, so that it would contain enough inhales and exhales for finding how they are correlated to the distance between individual beats.

Another thing that should be implemented is quality control for the measurements. In other words, identifying when a measurement is too poor to be correctly analyzed. This would be a must for an app with real use.

This was also the first time I used AI tools in programming. Github Copilot turned out to be immensely helpful, at least after learning where it’s at its best and where not. However, there were problems that I wanted to work out myself. I didn’t want to ask Copilot to just give me a black box algorithm to find a heart rate from accelerometer data. That would take all the fun out of it––I’m a problem solver.