Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
- Dominique Makowski (https://github.com/DominiqueMakowski)
*Dependencies*
- biosppy
*See Also*
- BioSPPY: https://github.com/PIA-Group/BioSPPy
"""
# Compute gradient (sort of derivative)
gradient = np.gradient(signal)
# Find zero-crossings
zeros, = biosppy.tools.zero_cross(signal=gradient, detrend=True)
# Find respiratory phases
phases_indices = []
for i in zeros:
if gradient[i+1] > gradient[i-1]:
phases_indices.append("Inspiration")
else:
phases_indices.append("Expiration")
# Select cycles (inspiration) and expiration onsets
inspiration_onsets = []
expiration_onsets = []
for index, onset in enumerate(zeros):
if phases_indices[index] == "Inspiration":
inspiration_onsets.append(onset)
if phases_indices[index] == "Expiration":
References
-----------
- Kim, K. H., Bang, S. W., & Kim, S. R. (2004). Emotion recognition system using short-term monitoring of physiological signals. Medical and biological engineering and computing, 42(3), 419-427.
- Gamboa, H. (2008). Multi-Modal Behavioral Biometrics Based on HCI and Electrophysiology (Doctoral dissertation, PhD thesis, Universidade Técnica de Lisboa, Instituto Superior Técnico).
"""
# Processing
# ===========
if method == "slow":
# Compute gradient (sort of derivative)
gradient = np.gradient(signal)
# Smoothing
size = int(0.1 * sampling_rate)
smooth, _ = biosppy.tools.smoother(signal=gradient, kernel='bartlett', size=size, mirror=True)
# Find zero-crossings
zeros, = biosppy.tools.zero_cross(signal=smooth, detrend=True)
# Separate onsets and peaks
onsets = []
peaks = []
for i in zeros:
if smooth[i+1] > smooth[i-1]:
onsets.append(i)
else:
peaks.append(i)
else:
# find extrema
peaks, _ = biosppy.tools.find_extrema(signal=signal, mode='max')
onsets, _ = biosppy.tools.find_extrema(signal=signal, mode='min')
# Keep only pairs
size = int(0.1 * sampling_rate)
smooth, _ = biosppy.tools.smoother(signal=gradient, kernel='bartlett', size=size, mirror=True)
# Find zero-crossings
zeros, = biosppy.tools.zero_cross(signal=smooth, detrend=True)
# Separate onsets and peaks
onsets = []
peaks = []
for i in zeros:
if smooth[i+1] > smooth[i-1]:
onsets.append(i)
else:
peaks.append(i)
else:
# find extrema
peaks, _ = biosppy.tools.find_extrema(signal=signal, mode='max')
onsets, _ = biosppy.tools.find_extrema(signal=signal, mode='min')
# Keep only pairs
peaks = peaks[peaks > onsets[0]]
onsets = onsets[onsets < peaks[-1]]
# Artifact Treatment
# ====================
# Compute rising times
risingtimes = peaks-onsets
risingtimes = risingtimes/sampling_rate*1000
peaks = peaks[risingtimes > 100]
onsets = onsets[risingtimes > 100]