Skip to content

Commit fd267bd

Browse files
authored
Correct HRV and PCG module bugs (#55)
* Fix sphinx Scipy modules added to conf.py * Correct HRV and PCG module bugs - correct bug in outlier interpolation in HRV module - correct bug in PCG module related with pre-filtering of the signal
1 parent 12667df commit fd267bd

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

biosppy/signals/hrv.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,15 @@ def rri_correction(rri=None, threshold=250):
309309

310310
# find artifacts
311311
artifacts = np.abs(rri - rri_filt) > threshold
312+
313+
# before interpolating, check if the artifacts are at the beginning or end of the sequence
314+
if min(np.where(artifacts)[0]) < min(np.where(~artifacts)[0]):
315+
rri = rri[min(np.where(~artifacts)[0]):]
316+
artifacts = artifacts[min(np.where(~artifacts)[0]):]
317+
318+
if max(np.where(artifacts)[0]) > max(np.where(~artifacts)[0]):
319+
rri = rri[:max(np.where(~artifacts)[0])]
320+
artifacts = artifacts[:max(np.where(~artifacts)[0])]
312321

313322
# replace artifacts with cubic spline interpolation
314323
rri[artifacts] = interp1d(np.where(~artifacts)[0], rri[~artifacts],

biosppy/signals/pcg.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def pcg(signal=None, sampling_rate=1000., units=None, path=None, show=True):
8080
filtered, fs, params = st.filter_signal(signal, 'butter', 'bandpass', order, passBand, sampling_rate)
8181

8282
# find peaks
83-
peaks, envelope = find_peaks(signal=filtered, sampling_rate=sampling_rate)
83+
peaks, envelope = find_peaks(signal=filtered, sampling_rate=sampling_rate, filter=False)
8484

8585
# classify heart sounds
8686
hs, = identify_heart_sounds(beats=peaks, sampling_rate=sampling_rate)
@@ -119,7 +119,7 @@ def pcg(signal=None, sampling_rate=1000., units=None, path=None, show=True):
119119

120120
return utils.ReturnTuple(args, names)
121121

122-
def find_peaks(signal=None,sampling_rate=1000.):
122+
def find_peaks(signal=None,sampling_rate=1000.,filter=True):
123123

124124
"""Finds the peaks of the heart sounds from the homomorphic envelope
125125
@@ -140,7 +140,7 @@ def find_peaks(signal=None,sampling_rate=1000.):
140140
"""
141141

142142
# Compute homomorphic envelope
143-
envelope, = homomorphic_filter(signal,sampling_rate)
143+
envelope, = homomorphic_filter(signal,sampling_rate,filter=filter)
144144
envelope, = st.normalize(envelope)
145145

146146
# Find the prominent peaks of the envelope
@@ -152,7 +152,7 @@ def find_peaks(signal=None,sampling_rate=1000.):
152152
('peaks','homomorphic_envelope'))
153153

154154

155-
def homomorphic_filter(signal=None, sampling_rate=1000., f_LPF=8, order=2):
155+
def homomorphic_filter(signal=None, sampling_rate=1000., f_LPF=8, order=2, filter=True):
156156
"""Finds the homomorphic envelope of a signal.
157157
158158
Adapted to Python from original MATLAB code written by David Springer, 2016 (C), for
@@ -196,8 +196,12 @@ def homomorphic_filter(signal=None, sampling_rate=1000., f_LPF=8, order=2):
196196
# Filter Design
197197
passBand = np.array([25, 400])
198198

199+
if filter:
199200
# Band-Pass filtering of the PCG:
200-
signal, fs, params = st.filter_signal(signal, 'butter', 'bandpass', order, passBand, sampling_rate)
201+
signal, fs, params = st.filter_signal(signal, 'butter', 'bandpass', order, passBand, sampling_rate)
202+
203+
else:
204+
fs = sampling_rate
201205

202206
# LP-filter Design (to reject the oscillating component of the signal):
203207
b, a = ss.butter(order, 2 * f_LPF / fs, 'low')

0 commit comments

Comments
 (0)