Showing results for 
Search instead for 
Did you mean: 

calculating BPM from pulse signal

I have built a circuit using a IR phototransistor and IR LED to acquire a signal of my pulse rate through the finger.  I am now trying to use LabView to calculate the BPM based on my signal. 

I managed to acquire the signal and save the data to an .lvm file.  I am trying to analyze the data that I have acquired but am having trouble calculating BPM from my signal. My sampling rate was 1000Hz and I selected 1000 samples to be read. I later want to be able to do this continuously and wanted to know if someone could please help me.

I am attaching my code (LV 8.0) and my .lvm file in a zipped folder

Any help would be appreciated.


0 Kudos
Message 1 of 15

Hello Priya,

I think that the VI you've made is definitely on the right track.  You should try increasing the threshold and width values to eliminate the lower peaks (noise) in your data.  You can read information on what each of these inputs do in the LabVIEW Help by pressing control+h and hovering over the Peak Detect VI.  If you need more information click "detailed help."

I tried playing with these value to get the correct number of peaks (I'm assuming you want to get 21 peaks from the data in your zip file), but I was unable to.  I think the issue is that you aren't writing enough digits to your data file.  If you take a look at your text file you can see that a number of consecutive x (time) values are identical.  This is likely messing up calculations that the Peak Detect VI is doing since it uses a polynomial fit to identify peaks.  I would assume that if you increase the digits you write to the file or use the signal directly after you read it without writing to a file then you should be able to adjust the threshold and width to correctly identify your peaks.

I hope this helps get you started.

Have a great day!


Message 2 of 15
See the attached vi, I modified yours a bit.  This should be a good starting point for you.
0 Kudos
Message 3 of 15
I have now managed to get the correct number of peaks that appear in my signal that I saved to an LVM file. I am still having trouble getting the correct beats per minute and wanted to know if I could get any advice. The sampling rate was 1000 Hz and samples read was 1000. 

I am also trying to write and implement a code that will acquire the signal and analyze it live to calculate BPM.  So far I am using the same code as I was for detecting peaks when reading the data file and having no success. I think I need to somehow indicate how much time to keep analyzing such as 10 seconds but am not sure how to go about this. Any help would be greatly appreciated.

I am attaching a zip folder which contains my files for reading/analyzing the file and the one for acquiring and analyzing the signal live. 

I would really appreciate any feedback I could get.


0 Kudos
Message 4 of 15

The heart rate (averaged over the amount of data in the file) is simply the number of beats divided by the duration of the dataset. I modified your VI to show this.

The dt control is set to the time between samples, 1 millisecond for a 1 kHz sampling rate. It appears that this data is not included in the .lvm file. I suggest that you modify the file recording program to include the sample rate data.

Rather than have the loop run as fast as possible and waste CPU cycles, I added an event structure so the HR calculation only updates when the Update HR Calc button is pressed or when width 2 or threshold 2 is changed.

I also moved parts which never change outside the loop.

0 Kudos
Message 5 of 15
Take the Locations output of the Peak Detector and get the derivative of it.  This will give you the number of samples between peaks.  You will have to drop the last point of the result.

Looking at your data, I am pretty sure you sampled at 100 sample/second not 1000 samples/second.

So now you have data in samples/beat.  Divide by 100 samples/second to get your datain seconds/beat.  Divide by 60 seconds/minute to get your data in minutes/beat.  Take the reciprocle to get the date in beats/minute.  Looking at this results convinced me that you sampled at 100 samples/second.

See attached.

Randall Pursley
Download All
0 Kudos
Message 6 of 15

I have modified my code as you have suggested but am still having little success when running the code live. I want the final device to be a "cardiorespiratory" device so if there is an abnormally high or low heart rate detected I want my device to sound/show an alarm which is why it's important that it run live and be able to continuously update itself. I have attached the code I have been trying to use. I have also again attached a sample file because my understanding is the same code should run to the data file or live but maybe I am mistaken?

The data file is a newer file with a lot better signal taken from one of my friends.

I would appreciate any help or guidance that you can give me.

0 Kudos
Message 7 of 15

Hello Priya,


You are correct that your analysis should return the same results regardless of whether or not the data is coming from a file or "live" from a DAQmx Read.  I think that the issue comes from the amount of data you're analyzing.  As your code is written you are acquiring the same number of samples as your sample rate in Hz, so each time you do a DAQmx read it represents 1 second of data.  On the other hand since you're file contained many seconds of data you have many peaks.  Since typical (human) heart rates are less than 2 beats/second I'd imagine that by only taking 1 second of data you have a number of sample sections that have only a single peak.  You're using a Waveform Chart to display your data so it retains the history, however, if you replace these with waveform graphs you'll see the data you're sending into your analysis each time--and likely find that it’s only a single peak.  This can cause inaccuracy's with the way you're calculating the BPM.  You could either increase the number of samples to read (by using the input on the DAQmx Timing VI as well as the DAQmx Read VI) to increase the number of beats you calculate over, or you can use a shift register to retain the position of the last peak you found and then take the difference between adjacent peaks to calculate the BPM.


You can create a shift register to pass the value of a wire to the next loop iteration by right clicking on the boarder of the loop and selecting "add shift register".  Another option would be to use a shift register to build an array of all your data.  This would allow you to calculate the average BPM of your data.



Message 8 of 15
I am still having trouble having a "real time" heart monitor that is continously updating and have written several different codes both of which I feel like should work but neither do.

I think one of the biggest problems I am having with LV 8.0 is the peak detector so my second code (heartmonitor_2) doesn't even use one but I wanted to know if someone could help me with it. With the second code I am able to get the number of samples above threshold but how would I modify it so it translates into heart rate?

Any help I can get editing my code to completely eliminate the peak detector would be great. I seem to keep getting some error about just that vi which is why I believe that is the problem I am having with my code when any sort of a peak detector is present.

The third code I am attaching is another simpler way for doing a read from measurement approach. I wanted to know if single tone extraction could also maybe work for the "real time" monitor and not only for my read from measurement files.  I tried doing it but it wouldn't work "real time" but suggestions would be appreciated. The third file was written using LV 8.5 evaluation version.

Thanks for your help and suggestions.

0 Kudos
Message 9 of 15
Before you apply peak detection on the signal, you might need to have some preprocessing such as removing lower frequency wondering base line caused by movement.  Otherwise, the simple threshold peak detection might deliver flase results. You can also try wavelet-based peak detection, which is more robust.
These application notes might be helpful to you:
Using LabVIEW for Heart Rate Variability Analysis:
Wavelet-based Peak Detection:
0 Kudos
Message 10 of 15