Esempio n. 1
0
def test_signal_distort():
    signal = nk.signal_simulate(duration=10, frequency=0.5, sampling_rate=10)

    # Warning for nyquist criterion
    with pytest.warns(
        nk.misc.NeuroKitWarning,
        match=r"Skipping requested noise frequency.*cannot be resolved.*"
    ):
        nk.signal_distort(signal, sampling_rate=10, noise_amplitude=1, silent=False)

    # Warning for period duration
    with pytest.warns(
        nk.misc.NeuroKitWarning,
        match=r"Skipping requested noise frequency.*since its period of.*"
    ):
        signal = nk.signal_simulate(duration=1, frequency=1, sampling_rate=10)
        nk.signal_distort(signal, noise_amplitude=1, noise_frequency=0.1, silent=False)
Esempio n. 2
0
def test_rsp_clean():

    sampling_rate = 100
    duration = 120
    rsp = nk.rsp_simulate(duration=duration,
                          sampling_rate=sampling_rate,
                          respiratory_rate=15,
                          noise=0.1,
                          random_state=42)
    # Add linear drift (to test baseline removal).
    rsp += nk.signal_distort(rsp,
                             sampling_rate=sampling_rate,
                             linear_drift=True)

    khodadad2018 = nk.rsp_clean(rsp,
                                sampling_rate=sampling_rate,
                                method="khodadad2018")
    assert len(rsp) == len(khodadad2018)

    rsp_biosppy = nk.rsp_clean(rsp,
                               sampling_rate=sampling_rate,
                               method="biosppy")
    assert len(rsp) == len(rsp_biosppy)

    # Check if filter was applied.
    fft_raw = np.abs(np.fft.rfft(rsp))
    fft_khodadad2018 = np.abs(np.fft.rfft(khodadad2018))
    fft_biosppy = np.abs(np.fft.rfft(rsp_biosppy))

    freqs = np.fft.rfftfreq(len(rsp), 1 / sampling_rate)

    assert np.sum(fft_raw[freqs > 3]) > np.sum(fft_khodadad2018[freqs > 3])
    assert np.sum(fft_raw[freqs < 0.05]) > np.sum(
        fft_khodadad2018[freqs < 0.05])
    assert np.sum(fft_raw[freqs > 0.35]) > np.sum(fft_biosppy[freqs > 0.35])
    assert np.sum(fft_raw[freqs < 0.1]) > np.sum(fft_biosppy[freqs < 0.1])

    # Comparison to biosppy (https://github.com/PIA-Group/BioSPPy/blob/master/biosppy/signals/resp.py#L62)
    rsp_biosppy = nk.rsp_clean(rsp,
                               sampling_rate=sampling_rate,
                               method="biosppy")
    original, _, _ = biosppy.tools.filter_signal(signal=rsp,
                                                 ftype="butter",
                                                 band="bandpass",
                                                 order=2,
                                                 frequency=[0.1, 0.35],
                                                 sampling_rate=sampling_rate)
    original = nk.signal_detrend(original, order=0)
    assert np.allclose((rsp_biosppy - original).mean(), 0, atol=1e-6)
Esempio n. 3
0
plot = nk.eog_plot(signals, peaks=info, sampling_rate=100)
plot.set_size_inches(10, 6, forward=True)
plot.savefig("README_eog.png", dpi=300, h_pad=3)

# =============================================================================
# Signal Processing
# =============================================================================

# Generate original signal
original = nk.signal_simulate(duration=6, frequency=1)

# Distort the signal (add noise, linear trend, artifacts etc.)
distorted = nk.signal_distort(original,
                              noise_amplitude=0.1,
                              noise_frequency=[5, 10, 20],
                              powerline_amplitude=0.05,
                              artifacts_amplitude=0.3,
                              artifacts_number=3,
                              linear_drift=0.5)

# Clean (filter and detrend)
cleaned = nk.signal_detrend(distorted)
cleaned = nk.signal_filter(cleaned, lowcut=0.5, highcut=1.5)

# Compare the 3 signals
plot = nk.signal_plot([original, distorted, cleaned])

# Save plot
fig = plt.gcf()
fig.set_size_inches(10, 6)
fig.savefig("README_signalprocessing.png", dpi=300, h_pad=3)