Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions biosppy/signals/hrv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
14 changes: 9 additions & 5 deletions biosppy/signals/pcg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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')
Expand Down