Search code examples
pythonmeanheartrate

Python: How to calculate the mean heart rate during a specific time frame of my data?


I am trying to measure the changes in heart rate throughout the continuous resting and exercise periods of my experiment. How can I calculate the mean heart rate of just the resting period, from approximately 0 to 120 seconds?

For reference, this is the filtered heart rate vs time graph.

To supply some background, I started from an ECG analog signal, found the R-wave peaks and calculated the RR interval. Using the RR interval, I was able to calculate heart rate. I filtered out the outliers of the raw heart rate plot, and thus came up with the graph that I linked. Thanks in advance!


Solution

  • I take you have a list of values behind that graph?

    import numpy
    numpy.mean([1,2,3])
    
    import statistics
    statistics.mean([1,2,3])
    

    Beware that statistics is much slower than numpy - albeit fractionally more accurate (talking 12th decimal point more accurate or something like that!).

    If you have adjacent timeframes (as you must for plotting your x-values), then you can index your window start and end via (you define the <start_time> and <end_time>):

    iStart = timeList.index(<start_time>)
    iEnd = timeList.index(<end_time>)
    

    and do your mean across that

    meanRate = numpy.mean(rateList[iStart:iEnd])