Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
sampling_rate = 1000
eda = nk.eda_simulate(
duration=30, sampling_rate=sampling_rate, scr_number=6, noise=0.01, drift=0.01, random_state=42
)
clean = nk.eda_clean(eda, sampling_rate=sampling_rate)
assert len(clean) == len(eda)
# Comparison to biosppy (https://github.com/PIA-Group/BioSPPy/blob/master/biosppy/signals/eda.py)
eda_biosppy = nk.eda_clean(eda, sampling_rate=sampling_rate, method="biosppy")
original, _, _ = biosppy.tools.filter_signal(
signal=eda, ftype="butter", band="lowpass", order=4, frequency=5, sampling_rate=sampling_rate
)
original, _ = biosppy.tools.smoother(signal=original, kernel="boxzen", size=int(0.75 * sampling_rate), mirror=True)
# pd.DataFrame({"our":eda_biosppy, "biosppy":original}).plot()
assert np.allclose((eda_biosppy - original).mean(), 0, atol=1e-5)
band='lowpass',
order=4,
frequency=25.,
sampling_rate=sampling_rate)
filtered, _, _ = biosppy.tools.filter_signal(signal=filtered,
ftype='butter',
band='highpass',
order=4,
frequency=3.,
sampling_rate=sampling_rate)
# diff
dx = np.abs(np.diff(filtered, 1) * sampling_rate)
# smoothing
dx, _ = biosppy.tools.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, _ = biosppy.tools.find_extrema(signal=dx, mode='max')
for i in range(init_ecg):
peaks, values = biosppy.tools.find_extrema(signal=dx[a:b], mode='max')
try:
ind = np.argmax(values)
except ValueError:
pass
- BioSPPy: https://github.com/PIA-Group/BioSPPy
References
-----------
- Kim, K. H., Bang, S. W., & Kim, S. R. (2004). Emotion recognition system using short-term monitoring of physiological signals. Medical and biological engineering and computing, 42(3), 419-427.
- Gamboa, H. (2008). Multi-Modal Behavioral Biometrics Based on HCI and Electrophysiology (Doctoral dissertation, PhD thesis, Universidade Técnica de Lisboa, Instituto Superior Técnico).
"""
# Processing
# ===========
if method == "slow":
# Compute gradient (sort of derivative)
gradient = np.gradient(signal)
# Smoothing
size = int(0.1 * sampling_rate)
smooth, _ = biosppy.tools.smoother(signal=gradient, kernel='bartlett', size=size, mirror=True)
# Find zero-crossings
zeros, = biosppy.tools.zero_cross(signal=smooth, detrend=True)
# Separate onsets and peaks
onsets = []
peaks = []
for i in zeros:
if smooth[i+1] > smooth[i-1]:
onsets.append(i)
else:
peaks.append(i)
else:
# find extrema
peaks, _ = biosppy.tools.find_extrema(signal=signal, mode='max')
onsets, _ = biosppy.tools.find_extrema(signal=signal, mode='min')
# Initialization
eda = np.array(eda)
eda_df = pd.DataFrame({"EDA_Raw": np.array(eda)})
# Preprocessing
# ===================
# Filtering
filtered, _, _ = biosppy.tools.filter_signal(signal=eda,
ftype='butter',
band='lowpass',
order=4,
frequency=5,
sampling_rate=sampling_rate)
# Smoothing
filtered, _ = biosppy.tools.smoother(signal=filtered,
kernel='boxzen',
size=int(0.75 * sampling_rate),
mirror=True)
eda_df["EDA_Filtered"] = filtered
# Derive Phasic and Tonic
try:
tonic, phasic = cvxEDA(eda, sampling_rate=sampling_rate, alpha=alpha, gamma=gamma)
eda_df["EDA_Phasic"] = phasic
eda_df["EDA_Tonic"] = tonic
signal = phasic
except:
print("NeuroKit Warning: eda_process(): Error in cvxEDA algorithm, couldn't extract phasic and tonic components. Using raw signal.")
signal = eda
# Skin-Conductance Responses