The library for this code is given as :

from scipy import signal
from scipy.signal import find_peaks
import matplotlib.pyplot as plt
import numpy as np
from scipy.signal import savgol_filter
import pickle

[1] Load data

First, load the 2 data: angle data and EMG data

and set video frequency (video_fs) and EMG sensor frequency (emg_fs)]

if __name__=="__main__":
    data_path = '/home/hyuninlee/PycharmProjects/xcorps/data/data0622_differentsecond/lhi/convertdata/train/'
    savepath = '/home/hyuninlee/PycharmProjects/xcorps/seq2seq_attentionmodel/trainData/lhi/1sec/'
    filename_angles = 'angles_0622_exp_lhi_one_1sec_4.npy'
    filename_emg = 'emg_smooth_0622_exp_lhi_one_1sec_4.npy'

    filename = 'train_lhi_4'
    savename = filename +'.pkl'

    video_fs = 239.88
    emg_fs = 1260

Watch the video where this angle data was extracted and set start time and end time of the experiment as start_time_angle and end_time_angle

middle_time_list_angle is the time when a finger is in full flexsion (when a finger folds at a maximum finger)

resampling frequency (resample_fs) of angle and EMG data is 100

start_time_angle = 8
end_time_angle = 75
middle_time_list_angle = [9.3, 10.3, 11.3, 12.3, 13.3, 14.3, 15.3, 16.3, 17.3, 18.3, 19.3, 20.4, 21.4, 22.4, 23.3, 24.3, 25.3, 26.3, 27.3, 28.3, 29.4, 30.5, 31.5, 32.5, 33.4,34.3, 35.4, 36.4, 37.5, 38.5, 39.5, 40.5, 41.5, 42.5, 43.5, 44.5, 45.5, 46.6, 47.6, 48.6, 49.5, 50.5, 51.5, 52.5, 53.5, 54.5, 55.6, 56.6, 57.6, 58.6,59.6, 60.5, 61.5, 62.5, 63.5, 64.5, 65.5, 66.5, 67.5, 68.5, 69.6, 70.6, 71.6, 72.6, 73.6]
dataLength = 65
duration = 1

assert len(middle_time_list_angle) == dataLength

resample_fs = 100

Now, load the data.

#prepare data
input_data, output_data = dataprepare(data_path, filename_emg, filename_angles)

Function dataprepare is defined as follows :

def dataprepare(data_path,emg_file_name,angle_file_name) :
    path_emg = data_path + emg_file_name
    path_angle = data_path + angle_file_name

    import pickle
    with open(path_emg,'rb') as f :
        emgdata = pickle.load(f)
    with open(path_angle,'rb') as f :
        angledata = pickle.load(f)

    return emgdata , angledata

[2] Synchronize the start time of EMG data and Angle data

Synchronize the start time of EMG data (input_data) and Angle data (output_data)


emg_sync, angle_sync , _ , _ = synctime(input_data,output_data,start_time_angle,video_fs,emg_fs)