Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if stop > length:
stop = length
idx = []
while True:
sq = np.copy(signal[start:stop])
sq -= sq.mean()
# sq = sq[1:]
ss = 25 * ssf[start:stop]
sss = 100 * np.diff(ss)
sss[sss < 0] = 0
sss = sss - alpha * np.mean(sss)
# find maxima
pk, pv = st.find_extrema(signal=sss, mode='max')
pk = pk[np.nonzero(pv > 0)]
pk += wrange
dpidx = pk
# analyze between maxima of 2nd derivative of ss
detected = False
for i in range(1, len(dpidx) + 1):
try:
v, u = dpidx[i - 1], dpidx[i]
except IndexError:
v, u = dpidx[-1], -1
s = sq[v:u]
Mk, Mv = st.find_extrema(signal=s, mode='max')
mk, mv = st.find_extrema(signal=s, mode='min')
dx = np.abs(np.diff(filtered, 1) * sampling_rate)
# smoothing
dx, _ = st.smoother(signal=dx, kernel='hamming', size=sm_size, mirror=True)
# buffers
qrspeakbuffer = np.zeros(init_ecg)
noisepeakbuffer = np.zeros(init_ecg)
peak_idx_test = np.zeros(init_ecg)
noise_idx = np.zeros(init_ecg)
rrinterval = sampling_rate * np.ones(init_ecg)
a, b = 0, v1s
all_peaks, _ = st.find_extrema(signal=dx, mode='max')
for i in range(init_ecg):
peaks, values = st.find_extrema(signal=dx[a:b], mode='max')
try:
ind = np.argmax(values)
except ValueError:
pass
else:
# peak amplitude
qrspeakbuffer[i] = values[ind]
# peak location
peak_idx_test[i] = peaks[ind] + a
a += v1s
b += v1s
# thresholds
ANP = np.median(noisepeakbuffer)
AQRSP = np.median(qrspeakbuffer)
thres_ch = 0.85
adjacency = 0.05 * sampling_rate
for i in beats:
error = [False, False]
if i - lim < 0:
window = signal[0:i + lim]
add = 0
elif i + lim >= length:
window = signal[i - lim:length]
add = i - lim
else:
window = signal[i - lim:i + lim]
add = i - lim
# meanval = np.mean(window)
w_peaks, _ = st.find_extrema(signal=window, mode='max')
w_negpeaks, _ = st.find_extrema(signal=window, mode='min')
zerdiffs = np.where(np.diff(window) == 0)[0]
w_peaks = np.concatenate((w_peaks, zerdiffs))
w_negpeaks = np.concatenate((w_negpeaks, zerdiffs))
pospeaks = sorted(zip(window[w_peaks], w_peaks), reverse=True)
negpeaks = sorted(zip(window[w_negpeaks], w_negpeaks))
try:
twopeaks = [pospeaks[0]]
except IndexError:
twopeaks = []
try:
twonegpeaks = [negpeaks[0]]
except IndexError:
twonegpeaks = []
# find maxima
pk, pv = st.find_extrema(signal=sss, mode='max')
pk = pk[np.nonzero(pv > 0)]
pk += wrange
dpidx = pk
# analyze between maxima of 2nd derivative of ss
detected = False
for i in range(1, len(dpidx) + 1):
try:
v, u = dpidx[i - 1], dpidx[i]
except IndexError:
v, u = dpidx[-1], -1
s = sq[v:u]
Mk, Mv = st.find_extrema(signal=s, mode='max')
mk, mv = st.find_extrema(signal=s, mode='min')
try:
M = Mk[np.argmax(Mv)]
m = mk[np.argmax(mv)]
except ValueError:
continue
if (s[M] - s[m] > d1_th) and (m - M > d2_th):
idx += [v + start]
detected = True
# next round continues from previous detected beat
if detected:
start = idx[-1] + wrange
else:
pk, pv = st.find_extrema(signal=sss, mode='max')
pk = pk[np.nonzero(pv > 0)]
pk += wrange
dpidx = pk
# analyze between maxima of 2nd derivative of ss
detected = False
for i in range(1, len(dpidx) + 1):
try:
v, u = dpidx[i - 1], dpidx[i]
except IndexError:
v, u = dpidx[-1], -1
s = sq[v:u]
Mk, Mv = st.find_extrema(signal=s, mode='max')
mk, mv = st.find_extrema(signal=s, mode='min')
try:
M = Mk[np.argmax(Mv)]
m = mk[np.argmax(mv)]
except ValueError:
continue
if (s[M] - s[m] > d1_th) and (m - M > d2_th):
idx += [v + start]
detected = True
# next round continues from previous detected beat
if detected:
start = idx[-1] + wrange
else:
start += size
SCR pulse amplitudes.
References
----------
.. [Gamb08] Hugo Gamboa, "Multi-modal Behavioral Biometrics Based on HCI
and Electrophysiology", PhD thesis, Instituto Superior T{\'e}cnico, 2008
"""
# check inputs
if signal is None:
raise TypeError("Please specify an input signal.")
# find extrema
pi, _ = st.find_extrema(signal=signal, mode='max')
ni, _ = st.find_extrema(signal=signal, mode='min')
# sanity check
if len(pi) == 0 or len(ni) == 0:
raise ValueError("Could not find SCR pulses.")
# pair vectors
if ni[0] < pi[0]:
ni = ni[1:]
if pi[-1] > ni[-1]:
pi = pi[:-1]
if len(pi) > len(ni):
pi = pi[:-1]
li = min(len(pi), len(ni))
i1 = pi[:li]
i3 = ni[:li]