# Inconsistant Pulse Measure for BPM

## Recommended Posts

Hi, was wondering if you could suggest a method to convert rhythmic Analog data pulses (the voltage range is not valid logic levels) and extract an average BPM (beats per min).

I was thinking along the lines of a 20X/sec sample for maybe 3 seconds and then add all the times the value is >X, then multiply by 15... what would be the best and efficient way to continuously do this and have the result (BPM) as a variable I can use for other sequence processes?

##### Share on other sites

Your method would only work if each beat was a peak with a single point, otherwise you have to find a trough between each beat. Anyhow, the method depends on whether you need to do this in real time, or you are going to take some data and then can run some code to analyze the data. If the second, then you could actually search for the troughs. It'd be something like this:

```private count = 0
private index = 0
while(index != -1)
// find a peak
index = search(signal &gt; hiThresh, index+1)
if (index == -1)
break
endif
count++
// find a trough
index = search(signal &lt; loThresh, index+1)
endwhile```

This code would actually execute quite quickly, but not fast enough, say, for use in a conversion.

##### Share on other sites

Thank you for the feedback, I do need near real-time information and to be right, I would likely need to determine that the most common beat is say X samples duration of across the sample period. Sometimes is a 2 sample duration beat or 4 samples per beat. So, to count the number of most common sample duration groups would be required... I think. Finding a trough between each beat group is going to help, but how to do it quickly?

I see only 2 usable signal levels in the analog data, and then the duration, so it ends up being just 1 and 0 in the end. Is doing a >X test slower or faster than find a peak(as in your example)? How to capture, group and compare the pulse duration sets is what I am lost on.

##### Share on other sites

> X is faster, but you are still going to have to count out groups, and that requires search. The code is identical, except that you can store signal > x in a private variable outside the loop and then just do search(y, index+1) and search(!y, index+1) where y = signal > x. You can determine the samples across the peak by using simple math once you find the through. You'll need another variable is all, setting the first index to that variable, and then you can subtract the second index from it. So you get something like this:

```private count = 0
private peaklengths
private index = 0
private y = signal &gt; x
private lastpeak
while(index != -1)
// find a peak
index = search(y, index+1)
if (index == -1)
break
endif
count++
lastpeak = index
// find next trough
index = search(!y, index+1)
peaklengths[count-1] = index - lastpeak
endwhile```

On a newer computer, this code probably wouldn't take more than 50ms to run. How long it takes is largely dependent on the number of iterations, therefore, the number of peaks it is finding.