Exemplo n.º 1
0
def main_cropped_lms():
    sns.set_style('darkgrid')
    emg = load_emg_stack(
        r'C:\Users\hbkm9\Documents\Projects\CYB\Balint\CYB104\Data',
        task='Stand')
    emg = [norm_emg(cur_emg) for cur_emg in emg]
    targ = 5
    cropped = [
        crop_to_peaks(cur_emg[targ, :], cur_emg[7, :], 4, 0) for cur_emg in emg
    ]
    return
Exemplo n.º 2
0
def main_ecg():
    emg = load_emg_stack(
        r'C:\Users\hbkm9\Documents\Projects\CYB\Experiment2\CYB102\Data',
        task='Walk')
    sns.set_style('darkgrid')
    sns.set_context('poster')
    lat = np.empty((0, 300))
    ecg = np.empty((0, 300))
    for f in emg:
        pr = peak_regs(f[7, :], 100, 200, distance=800)
        lat = np.vstack((lat, f[4, pr]))
        ecg = np.vstack((ecg, f[7, pr]))

    to_plot = norm_emg(np.vstack([np.mean(ecg, axis=0), np.mean(lat, axis=0)]))

    fig: plt.Figure = plt.figure(constrained_layout=True)
    spec = GridSpec(ncols=2, nrows=2, figure=fig)
    ax = fig.add_subplot(spec[0, :])
    ax.plot(np.linspace(-50, 99, 300), to_plot[0, :], ls='--')
    ax.plot(np.linspace(-50, 99, 300), to_plot[1, :])
    ax.get_yaxis().set_ticklabels([" "])
    ax.set_ylabel("Arbitrary Units")
    ax.set_xlabel("Time Relative to R peak (ms)")
    plt.legend(["$\mathbb{E}\{V_{ECG}\}$", "$\mathbb{E}\{V_{Latissimus}\}$"])

    ax2 = fig.add_subplot(spec[1, 0])
    ax3 = fig.add_subplot(spec[1, 1])

    def my_spec(ts_in, ax):
        f, t, Sxx = sp.spectrogram(ts_in,
                                   2000,
                                   nperseg=256,
                                   noverlap=128,
                                   nfft=500)
        ax.pcolormesh(t, f[:50], np.log10(np.square(Sxx[:50, :])))
        ax.set_ylabel('Frequency [Hz]')
        ax.set_xlabel('Time [sec]')
        ax.set_ylim((0, 200))

    my_spec(emg[20][5, :], ax2)
    ax2.set_title('Latissimus Spectrogram')
    my_spec(emg[20][7, :], ax3)
    ax3.set_title('ECG Spectrogram')

    peaks, _ = sp.find_peaks(emg[20][7, :],
                             distance=800,
                             height=np.max(emg[20][7, :]) * 0.5)
    ax2.plot(peaks / 2000, np.ones_like(peaks) * 40, 'k+')
    ax3.plot(peaks / 2000, np.ones_like(peaks) * 40, 'k+')
    plt.show()
    print('yee')
Exemplo n.º 3
0
def main_spectrum_lms():
    emg = load_emg(
        r'C:\Users\hbkm9\Documents\Projects\CYB\Experiment1\CYB005\Data\005_Walk20.json'
    )
    emg = norm_emg(emg)
    step_dict = load_dict(
        r'C:\Users\hbkm9\Documents\Projects\CYB\Experiment1\CYB005\Data_p\005_ParamWalk20.json'
    )

    def consecutive(data, stepsize=1):
        return np.split(data, np.where(np.diff(data) != stepsize)[0] + 1)

    cl = np.array(step_dict["step_class"])
    step_idx = consecutive(np.where(cl == 0)[0])

    peaks, _ = sp.find_peaks(emg[7, :], distance=800, height=2.2)
    targ = 6

    plt.plot(emg[targ, :])
    plt.figure()
    n_bins = 50
    F = spectrum_lms(emg[targ, :], n_bins, gamma=0.01)
    #spec = norm_emg(np.abs(F.W[1:int(F.W.shape[0]/4), 3:]).T).T
    #spec = spec-np.min(spec)+1
    import seaborn as sns
    sns.set_style('darkgrid')
    sns.set_context('poster')
    spec = np.log10(np.square(np.abs(F.W[1:int(F.W.shape[0] / 8), 1::40])))
    plt.figure(figsize=(8, 5))
    plt.pcolormesh(np.linspace(0, spec.shape[1] / 50, spec.shape[1]),
                   np.arange(spec.shape[0]) / n_bins * 2000, spec)
    plt.xlabel('Time (s)')
    plt.ylabel('Frequency (Hz)')
    plt.tight_layout()
    plt.vlines([st[0] / 100 for st in step_idx],
               ymin=0,
               ymax=(spec.shape[0] - 1) / n_bins * 2000,
               label='Stride start')
    plt.vlines([p / 2000 for p in peaks],
               ymin=0,
               ymax=(spec.shape[0] - 1) / n_bins * 2000,
               lw=1,
               color='w',
               label='Heartbeat')
    plt.legend()
    plt.show()
    return
Exemplo n.º 4
0
def main_ar():
    sns.set_style('darkgrid')
    # emg = load_emg_stack(r'C:\Users\hbkm9\Documents\Projects\CYB\Experiment1\CYB004\Data', task='Walk')
    # targ = 0
    # x0 = np.array([cur_x[targ, i*400:(i+1)*400] for cur_x in emg for i in range(int(cur_x.shape[1]/400))])
    # from statsmodels.tsa.stattools import pacf
    # pacs = [pacf(cur_seg, method='ywm') for cur_seg in x0]
    # plt.stem(np.mean(pacs, axis=0), use_line_collection=True)
    # plt.axhline(1.96 / np.sqrt(x0.shape[1]), color='red', ls='--')
    # plt.axhline(-1.96 / np.sqrt(x0.shape[1]), color='red', ls='--')
    emg = norm_emg(
        load_emg(
            r'C:\Users\hbkm9\Documents\Projects\CYB\Experiment1\CYB004\Data\004_Walk12.json',
            task='Walk'))
    targ = 0
    ord = 4
    mu = 0.01
    x = emg[targ, :]
    X = ar_stack(x, ord)
    F = LMS(x_in=X[:, :-1], d_in=x[ord:], mu=mu)
    F.run()
    plt.plot(F.W.T)
    plt.show()
Exemplo n.º 5
0
def main_anc():
    sns.set_style('darkgrid')
    sns.set_context('poster')
    emg_stack = load_emg_stack(
        r'C:\Users\hbkm9\Documents\Projects\CYB\Experiment2\CYB102\Data',
        task='Stair')
    for i in range(4, 5):
        emg = emg_stack[i]
        emg = norm_emg(emg)
        s = emg[4, :]
        ref = emg[7, :]
        filt_ord = 4
        mu = 0.5
        F = anc(s=s,
                ref=ref,
                beta=1,
                order=filt_ord,
                mu=mu,
                eps=1 / mu,
                pad=True,
                rho=0,
                act='tanh',
                pretrain=(400, 3))
        plt.figure()

        plt.plot(np.linspace(0, len(s) / 2, len(s)), s * 4, lw=2)
        plt.plot(np.linspace(0, len(s) / 2, len(s)), ref, lw=2)
        plt.plot(np.linspace(0, len(s) / 2, len(s)), F.e * 4, lw=2)
        plt.xlabel('Time (ms)')
        plt.ylabel('Voltage (arbitrary units)')
        plt.legend(('Trapezius signal', 'ECG reference', 'Filtered signal'))
        plt.gca().get_yaxis().set_ticklabels([" "])
        plt.tight_layout()
        # plt.figure()
        # plt.plot(F.W.T)
    plt.show()
Exemplo n.º 6
0
def main_spectrum_visu():
    # region Loading and setup
    sns.set_style('darkgrid')
    sns.set_context('poster')
    emg = load_emg(
        r'C:\Users\hbkm9\Documents\Projects\CYB\Experiment1\CYB005\Data\005_Walk05.json',
        task='')
    emg = norm_emg(emg)
    fig, axes = plt.subplots(2, 4)
    names = [
        'L Internal Oblique', 'R Internal Oblique', 'L External Oblique',
        'R External Oblique', 'L Trapezius', 'R Trapezius', 'Erector Spinae',
        'ECG'
    ]
    for i in range(8):
        if i is not 7:
            axes.flatten()[i].plot(emg[7, :], alpha=0.6)
        axes.flatten()[i].plot(emg[i, :])
        if i is 0:
            axes.flatten()[i].legend(['ECG', 'EMG'])
        axes.flatten()[i].set_title(names[i])
    plt.show()
    ecg = emg[7, :]
    emg1 = emg[5, :]
    emg = emg[6, :]

    # endregion
    def plot_stuff():
        # region Spectral plotting
        fig, axs = plt.subplots(1, 3)

        def my_spec(ts_in, ax):
            f, t, Sxx = sp.spectrogram(ts_in,
                                       2000,
                                       nperseg=256,
                                       noverlap=128,
                                       nfft=2000)
            ax.pcolormesh(t, f, np.log(Sxx))
            ax.set_ylabel('Frequency [Hz]')
            ax.set_xlabel('Time [sec]')
            ax.set_ylim((0, 500))

        my_spec(emg, axs[0])
        axs[0].set_title('Trapezius Spectrum')
        my_spec(emg1, axs[1])
        axs[1].set_title('Erector Spinae Spectrum')
        my_spec(ecg, axs[2])
        axs[2].set_title('ECG Spectrum')

        # endregion

        # region ECG ARMA modelling

        peaks, _ = sp.find_peaks(ecg, distance=800, height=2.2)
        if min(peaks) < 59:
            peaks = peaks[1:]
        if max(peaks) > len(ecg) - 202:
            peaks = peaks[:-1]
        axs[2].plot(peaks / 2000, np.ones_like(peaks) * 100, 'k+')
        axs[0].plot(peaks / 2000, np.ones_like(peaks) * 100, 'k+')
        axs[1].plot(peaks / 2000, np.ones_like(peaks) * 100, 'k+')
        plt.figure()
        plt.plot(ecg)
        qrs = np.zeros((len(peaks) - 1, 260))
        for i in range(len(peaks) - 1):
            qrs[i, :] = ecg[peaks[i] - 59:peaks[i] + 201]
        plt.plot(peaks, np.ones_like(peaks) * 5, 'r+')
        # endregion
        from statsmodels.tsa.stattools import pacf
        pacs = [pacf(cur_ecg, method='ywm') for cur_ecg in qrs]
        plt.figure()
        plt.stem(np.mean(pacs, axis=0), use_line_collection=True)
        a = plt.axhline(1.96 / np.sqrt(qrs.shape[1]),
                        color='tab:red',
                        ls='--',
                        label='5% significance')
        plt.axhline(-1.96 / np.sqrt(qrs.shape[1]), color='tab:red', ls='--')
        plt.legend()
        plt.tight_layout()
        plt.xlabel('Delay (sample)')
        plt.ylabel('Partial Autocorrelation')
        plt.title('ECG Partial Autocorrelation Function')
        plt.show()

    plot_stuff()
    emg1 = np.clip(emg1, -8, 8)
    F = anc(emg1,
            ecg,
            5e-04,
            rho=1e-08,
            delay=0,
            order=4,
            mode='GASS',
            alg='A&F',
            alph=0.9)
    plt.plot(emg1, alpha=0.1)
    plt.plot(ecg / 4, alpha=0.1)
    plt.plot(np.squeeze(F.W).T)
    plt.show()
    return