Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
-------
onsets : array
Indices of EMG pulse onsets.
"""
# check inputs
if signal is None:
raise TypeError("Please specify an input signal.")
# full-wave rectification
fwlo = np.abs(signal)
# smooth
size = int(sampling_rate * size)
mvgav, _ = st.smoother(signal=fwlo,
kernel='boxzen',
size=size,
mirror=True)
# threshold
if threshold is None:
aux = np.abs(mvgav)
threshold = 1.2 * np.mean(aux) + 2.0 * np.std(aux, ddof=1)
# find onsets
length = len(signal)
start = np.nonzero(mvgav > threshold)[0]
stop = np.nonzero(mvgav <= threshold)[0]
onsets = np.union1d(np.intersect1d(start - 1, stop),
np.intersect1d(start + 1, stop))
.. [KiBK04] K.H. Kim, S.W. Bang, and S.R. Kim, "Emotion recognition
system using short-term monitoring of physiological signals",
Med. Biol. Eng. Comput., vol. 42, pp. 419-427, 2004
"""
# check inputs
if signal is None:
raise TypeError("Please specify an input signal.")
# differentiation
df = np.diff(signal)
# smooth
size = int(1. * sampling_rate)
df, _ = st.smoother(signal=df, kernel='bartlett', size=size, mirror=True)
# zero crosses
zeros, = st.zero_cross(signal=df, detrend=False)
if np.all(df[:zeros[0]] > 0):
zeros = zeros[1:]
if np.all(df[zeros[-1]:] > 0):
zeros = zeros[:-1]
# exclude SCRs with small amplitude
thr = min_amplitude * np.max(df)
scrs, amps, ZC, pks = [], [], [], []
for i in range(0, len(zeros) - 1, 2):
scrs += [df[zeros[i]:zeros[i + 1]]]
aux = scrs[-1].max()
if aux > thr:
sm_size = 0.25 if not sm_size else sm_size
sm_size = int(sm_size * sampling_rate)
size = 5 if not size else size
size = int(size * sampling_rate)
wrange = 0.1 if not wrange else wrange
wrange = int(wrange * sampling_rate)
d2_th = 0.15 if not d2_th else d2_th
d2_th = int(d2_th * sampling_rate)
length = len(signal)
# slope sum function
dy = np.diff(signal)
dy[dy < 0] = 0
ssf, _ = st.smoother(signal=dy, kernel='boxcar', size=sm_size, mirror=True)
# main loop
start = 0
stop = size
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
fcn_kwargs = {'pairs': pairs, 'N': N}
index, values = st.windower(signal=signal,
size=size,
step=step,
kernel='hann',
fcn=_plf_features,
fcn_kwargs=fcn_kwargs)
# median filter
md_size = int(0.625 * sampling_rate / float(step))
if md_size % 2 == 0:
# must be odd
md_size += 1
for i in range(nb):
values[:, i], _ = st.smoother(signal=values[:, i],
kernel='median',
size=md_size)
# convert indices to seconds
ts = index.astype('float') / sampling_rate
# output
args = (ts, pairs, values)
names = ('ts', 'plf_pairs', 'plf')
return utils.ReturnTuple(args, names)
index, values = st.windower(signal=signal,
size=size,
step=step,
kernel='hann',
fcn=_power_features,
fcn_kwargs=fcn_kwargs)
# median filter
md_size = int(0.625 * sampling_rate / float(step))
if md_size % 2 == 0:
# must be odd
md_size += 1
for i in range(nb):
for j in range(nch):
values[:, i, j], _ = st.smoother(signal=values[:, i, j],
kernel='median',
size=md_size)
# extract individual bands
theta = values[:, 0, :]
alpha_low = values[:, 1, :]
alpha_high = values[:, 2, :]
beta = values[:, 3, :]
gamma = values[:, 4, :]
# convert indices to seconds
ts = index.astype('float') / sampling_rate
# output
args = (ts, theta, alpha_low, alpha_high, beta, gamma)
names = ('ts', 'theta', 'alpha_low', 'alpha_high', 'beta', 'gamma')
if len(beats) < 2:
rate_idx = []
rate = []
else:
# compute respiration rate
rate_idx = beats[1:]
rate = sampling_rate * (1. / np.diff(beats))
# physiological limits
indx = np.nonzero(rate <= 0.35)
rate_idx = rate_idx[indx]
rate = rate[indx]
# smooth with moving average
size = 3
rate, _ = st.smoother(signal=rate,
kernel='boxcar',
size=size,
mirror=True)
# get time vectors
length = len(signal)
T = (length - 1) / sampling_rate
ts = np.linspace(0, T, length, endpoint=True)
ts_rate = ts[rate_idx]
# plot
if show:
plotting.plot_resp(ts=ts,
raw=signal,
filtered=filtered,
zeros=zeros,