diff --git a/biosppy/signals/hrv.py b/biosppy/signals/hrv.py index ce50e787..acab33ae 100644 --- a/biosppy/signals/hrv.py +++ b/biosppy/signals/hrv.py @@ -309,6 +309,15 @@ def rri_correction(rri=None, threshold=250): # find artifacts artifacts = np.abs(rri - rri_filt) > threshold + + # before interpolating, check if the artifacts are at the beginning or end of the sequence + if min(np.where(artifacts)[0]) < min(np.where(~artifacts)[0]): + rri = rri[min(np.where(~artifacts)[0]):] + artifacts = artifacts[min(np.where(~artifacts)[0]):] + + if max(np.where(artifacts)[0]) > max(np.where(~artifacts)[0]): + rri = rri[:max(np.where(~artifacts)[0])] + artifacts = artifacts[:max(np.where(~artifacts)[0])] # replace artifacts with cubic spline interpolation rri[artifacts] = interp1d(np.where(~artifacts)[0], rri[~artifacts], diff --git a/biosppy/signals/pcg.py b/biosppy/signals/pcg.py index 6fb20f49..36349d04 100644 --- a/biosppy/signals/pcg.py +++ b/biosppy/signals/pcg.py @@ -80,7 +80,7 @@ def pcg(signal=None, sampling_rate=1000., units=None, path=None, show=True): filtered, fs, params = st.filter_signal(signal, 'butter', 'bandpass', order, passBand, sampling_rate) # find peaks - peaks, envelope = find_peaks(signal=filtered, sampling_rate=sampling_rate) + peaks, envelope = find_peaks(signal=filtered, sampling_rate=sampling_rate, filter=False) # classify heart sounds 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): return utils.ReturnTuple(args, names) -def find_peaks(signal=None,sampling_rate=1000.): +def find_peaks(signal=None,sampling_rate=1000.,filter=True): """Finds the peaks of the heart sounds from the homomorphic envelope @@ -140,7 +140,7 @@ def find_peaks(signal=None,sampling_rate=1000.): """ # Compute homomorphic envelope - envelope, = homomorphic_filter(signal,sampling_rate) + envelope, = homomorphic_filter(signal,sampling_rate,filter=filter) envelope, = st.normalize(envelope) # Find the prominent peaks of the envelope @@ -152,7 +152,7 @@ def find_peaks(signal=None,sampling_rate=1000.): ('peaks','homomorphic_envelope')) -def homomorphic_filter(signal=None, sampling_rate=1000., f_LPF=8, order=2): +def homomorphic_filter(signal=None, sampling_rate=1000., f_LPF=8, order=2, filter=True): """Finds the homomorphic envelope of a signal. 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): # Filter Design passBand = np.array([25, 400]) + if filter: # Band-Pass filtering of the PCG: - signal, fs, params = st.filter_signal(signal, 'butter', 'bandpass', order, passBand, sampling_rate) + signal, fs, params = st.filter_signal(signal, 'butter', 'bandpass', order, passBand, sampling_rate) + + else: + fs = sampling_rate # LP-filter Design (to reject the oscillating component of the signal): b, a = ss.butter(order, 2 * f_LPF / fs, 'low')