# Heart Rate Calculation

Discussion in 'Math' started by Art, Feb 12, 2008.

1. ### Art Thread Starter AAC Fanatic!

Sep 10, 2007
805
62
Hi Guys,
I'm trying to calculate heart rate in a programmatic manner,
and my head has hit the wall. I think it's relevant for this area...

Given the time in centiseconds (1/100ths of a second) between
any two individual heart beats, I need a formula to calculate the heart rate
in beats per minute.
As the heart rate increases, the number of centiseconds between heartbeats decreases.

Should be a snap for a maths whiz
Any help appreciated.
Cheers, Art.

2. ### Dave Retired Moderator

Nov 17, 2003
6,960
171
Wow, that is one fast heart rate!

The equation is simply:

(1/n)*60

Where n is the time in seconds. So for 1 beat every 100th of a second, n = 1/100, and you will get 6000 beats per minute - or in medical terms, you are dead!

Dave

3. ### Art Thread Starter AAC Fanatic!

Sep 10, 2007
805
62
1/100ths of a second was not an example rate, but a definition of a centisecond,
because until I Googled it, I thought that meant milliseconds which is incorrect,
and of course, silly

I'll give it a shot

edit, so for centseconds that would be (1/centicount)*0.6 ???
Pretend I'm an absolute moron, because I'm most of the way there where this is concerned.

( one divided by centisecondcount ) multiply that by 0.6 ? <=== baaap! guess again

( one divided by centisecondcount ) multiply that by 6000 ? <== I think we have a winner!

4. ### Dave Retired Moderator

Nov 17, 2003
6,960
171
If you want to convert the time into another base then you only need apply a multiplier to the equation, for example:

For time in centiseconds: (100/n)*60

For time in milliseconds: (1000/n)*60

You get the picture. Stick with seconds as your standard time unit, it is much easier.

Dave

5. ### Art Thread Starter AAC Fanatic!

Sep 10, 2007
805
62
Well I know you like videos of programs so here ya go:

That was done months ago, but if you look/listen carefully,
it is accurate, but the final heartrate is of a lower resolution than I'd desire
because of the low resolution timer I used based on the framerate at a low CPU speed.
that has been divided down to a centisecond timer, so the final readout won't make large jumps.

Thanks again Dave

6. ### Dave Retired Moderator

Nov 17, 2003
6,960
171
Good stuff, looks like an interesting little project.

Could you tell us a little about the equipment you are using?

Also, I didn't realise stroking a cat was so relaxing - down to 1 beat per minute at one point!

Dave

7. ### Art Thread Starter AAC Fanatic!

Sep 10, 2007
805
62
I had to go and check it out.... didn't hear it say "1" with the cat in the screen?
Readings with the cat in the screen are 77, 69, and then it cuts to the weight lift scene
in the middle of saying sixty-something. Please correct me if you're seeing something I'm not.

It's still a Sony PSP. The heart rate is obtained with a chest strap from a commercial unit
that comes with a wristwatch. I threw the watch part away. The chest strap emits a
few ms of electromagnetic noise that is received with a coil of wire connected to the
PSP's audio input line that was intended for a microphone, and the program looks for
peaks in the waveform.
The heartbeat you hear is just a sample of someone else's heartbeat played over and over.

Although done months ago, yesterday was the first time I took it to the gym, and it works
well speaking into my ear through headphones while playing mp3s as well .
but that visit prompted me to improve the resolution.
Cheers, Art.

8. ### Dave Retired Moderator

Nov 17, 2003
6,960
171
My bad, I meant to say 1 beat per second!

Sorry for getting you worried

Impressive arrangement. I'm amazed when you come up with these things, I don't know how you think of them!

Have you calibrated the device/arrangement against a commercial package to see how much, if any, error exists?

Still very enterprising. I await your next project.

Dave

9. ### Art Thread Starter AAC Fanatic!

Sep 10, 2007
805
62
Yes, when I was at the treadmill, their machinery worked on the signal from the chest strap too,
and it was accurate (I already knew that), but the display was better resolution, and climbed in
smaller increments. Hopefully tomorrow, that will be different.
It does make me wonder though, since the output of the chest strap is not coded in any way
like a more expensive unit,
how the gym machinery would behave if the person on the machine next to me had another of the same chest straps!
That feature,was born out of an audio recorder I wrote for the PSP I think...
I originally intended to use the commercial HRM "as is", but then wondered what would be
required in the watch to recieve the signal from the chest strap.

Have a look at my other vids on the same account

10. ### Dave Retired Moderator

Nov 17, 2003
6,960
171
I would assume the steady climb you observed may have something to do with an incremental value based on a reading history as oppose to sending the absolute reading to the display.

Why would you assume there would be an issue with an adjacent user on a machine?

I will do, thanks for the heads-up.

Dave

11. ### Art Thread Starter AAC Fanatic!

Sep 10, 2007
805
62
Because the chest strap (this model in particular, unlike some better ones) emits a burst of noise.
The signal is just rubbish that doesn't mean anything.
It can be interpreted as a heart beat because it arrives at the correct timing.
So as long as another machine is in range of the transmitter, I'd expect that to display
the same thing as my machine, and if two transmitters were in range of one treadmill,
I'd expect the treadmill to interpret every beat from both transmitter units.

I will know this tomorrow as well. I can at least stand between two treadmills with one transmitter,
and know what the range is.
Gotta go to bed.. I'll let you know the results of that.
Cheers

12. ### Dave Retired Moderator

Nov 17, 2003
6,960
171
Obviously a lot depends on a) machine proximity, and b) the noise sensitivity on the sensor. Without seeing this I could only speculate. Let us know how it goes.

Dave

13. ### Art Thread Starter AAC Fanatic!

Sep 10, 2007
805
62
Too lazy to go to the gym yesterday, but probably for the better since I only have two
free visits left, and the further developed this is, the better.

Thinking about something you said before, hysteresis, I think is the word?
I could, instead of displaying the instant heart rate, display the mean average of say,
the current rate and the last three or so rates, so that one bump doesn't cause a
dramatic error.

14. ### Dave Retired Moderator

Nov 17, 2003
6,960
171
Yes, I gestured to that earlier in this thread. Hysteresis is a form of memory in physical systems, but is probably wrong in this context. What you need to look at is averaging your readings over a sample and displaying, as you say, the (mean) average. There is an issue here, and that is outliers - spurious readings that may corrupt the average an cause non-real errors. Therefore you do one of three things to mitigate this:

1) Increase the sample range to minimise the effect of outliers, however there is the risk that the average value will be too slow in response to actual changes.

2) Use an averaging method known as the truncated mean, which omits outliers. Sadly truncated mean suffers from bias estimator issues.

3) Use the traditional arithmetic mean over a time sample (say 6-10 readings) and only utilise future readings that are within a given error percentage. This has the advantage of no responding to spurious errors, however will respond in a timely fashion to "real" dramatic changes in the readings.

IMO option 3 is the better option.

Dave

15. ### Art Thread Starter AAC Fanatic!

Sep 10, 2007
805
62
I have something like (3) working, on the display you see the mean of the
current rate, and the two prior rates that were read as heart rates
(not the last two averages that were displayed).
So if there is one error like a reading of 304 BPM, it's effect is not so great,
but it has
an effect on three displayed readings, and then is forgotten.

I think that's what you mean, but as further suggested by yourself,
I could ignore a reading of 304 BPM if it is very different from the reading before,
and not include it in the mean calculation because that silly number is disqualified.

Well I'm starting to feel like a programming slave but all for the best

I've got to get a pic at the gym today, and get this out for Valentine's Day.

What's this:
Code ( (Unknown Language)):
1.
2.  ^
3. /D\
4. 304
5.
A reading of 304 BPM sitting in a corner wearing a dunce hat for being disqualified.

16. ### Dave Retired Moderator

Nov 17, 2003
6,960
171
Yes that is what I am saying.

You can store an array of the last 6 readings, and before you go to update the display you can have a small routine that checks if the new reading is within a certain error of the mean average, for arguments sake say 15% change. This will remove the spurious one-off results. A final cavet is that instead of ignoring it you could increase by half, that way if the heart rate increases significantly there will be a small delay whilst the display catches up with the actual. How would that work:

Average = 250 (over 6 samples)
Add 280 as the latest reading - why? 280 is half way between 250 and 310
This will increase the average slightly

So if 310 is around the new real heart rate then the average is slowly creep up to the real value, but it is padded from the occasional spurious result.

Very artistic! I also most feel sorry for him/her!

Dave

17. ### Art Thread Starter AAC Fanatic!

Sep 10, 2007
805
62
Lol.
I have released it, but will still aim to impress for the next ver

18. ### Dave Retired Moderator

Nov 17, 2003
6,960
171
Very good. Keep me posted.

Dave

19. ### Art Thread Starter AAC Fanatic!

Sep 10, 2007
805
62
Hi again Dave,
I went yesterday, and my chest transmitter doesn't cause interference with other machines.
I think the recievers on the machines are in the handles which are right in front of you at waist height.
I'll bet I could make something (and so could you probably) that could interfere with them globally though!

The readings on the PSP and the treadmill are often different now.
You could say they are both correct, they equalise if the heart rate doesn't change much.
I would have to know how it comes up with the number it displays to copy it exactly.

Now someone is working on the Windows Session Performance Graph program,
where I save the start and finish time, and a byte for every heart rate inbetween
as a file that is loaded into a PC, and you see a fancy line!
Maybe it will even be red at the top, and green at 60BPM or lower

To top it off, I will probably write a seperate program for the PSP that does
the same thing, so you can see the graph at the gym. I will have to end a session
to see it though.
Cheers, Art.

20. ### Dave Retired Moderator

Nov 17, 2003
6,960
171
Tut-tut!

Yes, I would expect this behaviour. As long as the readings are around the same level, and when there is a dramatic change in the heart rate, one way or another, they both respond accordingly (given a small time lag of course), then we can assume there are giving equally accurate (or inaccurate as the case may be) readings.

Incidentally, how many sample readings are you basing your heart rate value on?

Wow, moving towards the PC market! I'd be interested in seeing how this phase of the project progresses. I await the YouTube video!

Dave