ATHeartRate icon indicating copy to clipboard operation
ATHeartRate copied to clipboard

Heart Rate keeps incrementing

Open ashwingattani opened this issue 5 years ago • 0 comments

Hi, I was using your code gist to calculate the heart rate from iPhone camera but it seems I am missing some essential part here.

I am setting frame rate at 30fps and making all calculations as per the frame rate, however, the heart rate value keeps on increasing with each call. Initially, my app was crashing because the percentage value comes <1 and we cannot divide by zero let heartRate = peakCount / percentage

I added a check for percentage, and execute the above code only if percentage >0. But it seems it affects the overall result.

Trying to achieve this in Swift 5, here is my code snippet:

if (self.hueDataSet.count % framePerSeconds == 0) {
                let displaySeconds = hueDataSet.count / framePerSeconds
                let bandpassFilteredItems = self.butterworthBandpassFilter(inputData: self.hueDataSet)
                let smoothedBandpassItems = self.medianSmoothing(inputData: bandpassFilteredItems)
                let peakCount = self.peakCount(inputData: smoothedBandpassItems)
                
                let secondsPassed = smoothedBandpassItems.count / framePerSeconds
                let percentage = secondsPassed / 60
                
                if percentage > 0 {
                    let heartRate = peakCount / percentage
                    print("heart rate: ", heartRate, "in seconds: ", displaySeconds)
                }
            }

Also, attaching log here:

heart rate: 139 in seconds: 60 heart rate: 142 in seconds: 61 heart rate: 145 in seconds: 62 heart rate: 147 in seconds: 63 heart rate: 149 in seconds: 64 heart rate: 151 in seconds: 65 heart rate: 153 in seconds: 66 heart rate: 155 in seconds: 67 heart rate: 158 in seconds: 68 heart rate: 160 in seconds: 69 heart rate: 161 in seconds: 70 heart rate: 163 in seconds: 71 heart rate: 165 in seconds: 72 heart rate: 167 in seconds: 73 heart rate: 169 in seconds: 74 heart rate: 171 in seconds: 75 heart rate: 173 in seconds: 76 heart rate: 175 in seconds: 77 heart rate: 178 in seconds: 78 heart rate: 179 in seconds: 79 heart rate: 182 in seconds: 80 heart rate: 185 in seconds: 81 heart rate: 187 in seconds: 82 heart rate: 189 in seconds: 83 heart rate: 193 in seconds: 84 heart rate: 196 in seconds: 85 heart rate: 198 in seconds: 86

I am not sure which place to debug the code. Any help is would be helpful thanks 👍🏼

ashwingattani avatar Sep 21 '19 11:09 ashwingattani