# HR algorithm: how does it work?

Posted on
• Hi,

I have some students developing an algorithm to detect HR from raw PPG. We are trying to make the algorithm somewhat hardware indipendent, so the outcome could be applicable to Bangle as well (if good enough!). But first I wanted to understand how the current implementation works.

I know you used to compute autocorrelation at some point, but the current algorithm seems more like a peak detector? As the code is not greatly commented, can anybody (Gordon?) explain a little what is the idea behind?

Thanks a lot,
Dario

• I looked at the algorithm some time ago:
http://forum.espruino.com/conversations/­372681/#16388080

So as I understand its really not that complicated: bandpass filter and peak recognition as the main steps and a median filter to remove outliers and estimate confidence.

When I looked into the data, the peak detector seems to be not that reliable. I found many peaks easily detectable by the eye after the bandpass, but missed by the peak detector. So to find a robust peak detector seems to be a valuable task.
The other obvious thing is to include accelerometer readings to suppress movement artifacts.

• Just posted some summery that I wrote some time ago, put didn't post so far:
http://forum.espruino.com/conversations/­372681/?offset=50#16454552

Happy to have more helping hands on this topic :-)
No promise here (!), but my plan was to have a basic test harness ready this weekend (maybe including a rewrite of the algorithm in python).

• thanks for the explanation, now it's more or less clear.

As for testing, I have created a repo for testing the algos of BangleJS. Currently it contains only the step counter, but we could happily add also the heart rate. The idea is to upload some test data with reference (maybe coming from another commercial sensor) and benchmark different algorithms.

• Doing some work on the HRM would be amazing - @Mi's explanation sounds pretty much spot on.

Hopefully the code itself is in its own file so should be reasonably easy to compile without Espruino with a few simple stubs.

So to find a robust peak detector seems to be a valuable task.

Yes - it's worth looking in GitHub history as I did previously use a more robust detector, but at least in my testing I had found the current one performed better.

One thing to add is the hardware itself auto-adjusts exposure, and it's actually possible that during high accelerations it actually needs modifying to stop doing that.

• Post a reply

### HR algorithm: how does it work?

Posted by @user107850