Skip to content

Commit

Permalink
resampling
Browse files Browse the repository at this point in the history
  • Loading branch information
ChilloutCharles committed Aug 14, 2024
1 parent 25df73b commit abe6020
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions logic/biometrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from brainflow.board_shim import BoardShim, BrainFlowPresets
from brainflow.data_filter import DataFilter, FilterTypes, DetrendOperations
from scipy.signal import find_peaks
from scipy.signal import find_peaks, resample

import numpy as np
import utils
Expand Down Expand Up @@ -32,7 +32,10 @@ def __init__(self, board, supported=True, window_seconds=10, ema_decay=0.025):
self.lowcut = 30 / 60
self.highcut = 240 / 60
self.order = 4
self.min_distance = 1 / self.highcut * self.ppg_sampling_rate

self.resample_rate = int(self.highcut * 2 + 0.5) # nyquist
self.resample_size = self.resample_rate * self.window_seconds
self.min_distance = 1 / self.highcut * self.resample_rate

# ema smoothing variables
self.current_values = None
Expand All @@ -49,20 +52,24 @@ def estimate_heart_rate(self, ppg_ir, ppg_red, ppg_ambient):
# detrend and filter down to possible heart rates
DataFilter.perform_bandpass(ppg_red, self.ppg_sampling_rate, self.lowcut, self.highcut, self.order, FilterTypes.BUTTERWORTH, 0)
DataFilter.perform_bandpass(ppg_ir, self.ppg_sampling_rate, self.lowcut, self.highcut, self.order, FilterTypes.BUTTERWORTH, 0)

ppg_red = resample(ppg_red, self.resample_size)
ppg_ir = resample(ppg_red, self.resample_size)

DataFilter.detrend(ppg_red, DetrendOperations.LINEAR)
DataFilter.detrend(ppg_ir, DetrendOperations.LINEAR)

# find peaks in signal
ppg_red = DataFilter.detect_peaks_z_score(ppg_red)
ppg_ir = DataFilter.detect_peaks_z_score(ppg_ir)
ppg_red = DataFilter.detect_peaks_z_score(ppg_red, threshold=3)
ppg_ir = DataFilter.detect_peaks_z_score(ppg_ir, threshold=3)
red_peaks, _ = find_peaks(ppg_red, distance=self.min_distance)
ir_peaks, _ = find_peaks(ppg_ir, distance=self.min_distance)

# get inter-peak sample intervals
sample_ipis = np.concatenate((np.diff(red_peaks), np.diff(ir_peaks)))

# get bpm from mean inter-peak sample interval
average_ipi = np.mean(sample_ipis) / self.ppg_sampling_rate
average_ipi = np.mean(sample_ipis) / self.resample_rate
heart_bpm = 60 / average_ipi

return heart_bpm
Expand Down

0 comments on commit abe6020

Please sign in to comment.