2021-06-22 18:31:16 +00:00
|
|
|
import numpy as np
|
2021-06-23 09:17:59 +00:00
|
|
|
import matplotlib.pyplot as plt
|
2021-06-23 13:56:35 +00:00
|
|
|
from pandas.core.frame import DataFrame
|
2021-06-24 07:48:30 +00:00
|
|
|
from scipy.fft import fft, fftfreq
|
2021-06-24 10:02:49 +00:00
|
|
|
import pywt
|
|
|
|
from scipy.signal import wavelets
|
2021-06-24 12:17:31 +00:00
|
|
|
#import pyyawt
|
2021-06-22 18:31:16 +00:00
|
|
|
|
2021-06-23 13:56:35 +00:00
|
|
|
import Handle_emg_data as Handler
|
2021-06-23 11:26:17 +00:00
|
|
|
|
2021-06-23 13:56:35 +00:00
|
|
|
SAMPLE_RATE = 200
|
2021-06-23 11:26:17 +00:00
|
|
|
|
|
|
|
def load_user_emg_data():
|
2021-06-23 12:17:12 +00:00
|
|
|
|
2021-06-23 11:30:05 +00:00
|
|
|
# CSV data from subject 1
|
2021-06-23 11:26:17 +00:00
|
|
|
file1_subject1_left = "/Exp20201205_2myo_hardTypePP/HaluskaMarek_20201207_1810/myoLeftEmg.csv"
|
|
|
|
file2_subject1_left = "/Exp20201205_2myo_hardTypePP/HaluskaMarek_20201207_1830/myoLeftEmg.csv"
|
|
|
|
file3_subject1_left = "/Exp20201205_2myo_hardTypePP/HaluskaMarek_20201207_1845/myoLeftEmg.csv"
|
|
|
|
file4_subject1_left = "/Exp20201205_2myo_hardTypePP/HaluskaMarek_20201207_1855/myoLeftEmg.csv"
|
2021-06-23 12:17:12 +00:00
|
|
|
subject1_left_files = [file1_subject1_left, file2_subject1_left, file3_subject1_left, file4_subject1_left]
|
2021-06-23 11:26:17 +00:00
|
|
|
file1_subject1_rigth = "/Exp20201205_2myo_hardTypePP/HaluskaMarek_20201207_1810/myoRightEmg.csv"
|
|
|
|
file2_subject1_rigth = "/Exp20201205_2myo_hardTypePP/HaluskaMarek_20201207_1830/myoRightEmg.csv"
|
|
|
|
file3_subject1_rigth = "/Exp20201205_2myo_hardTypePP/HaluskaMarek_20201207_1845/myoRightEmg.csv"
|
|
|
|
file4_subject1_rigth = "/Exp20201205_2myo_hardTypePP/HaluskaMarek_20201207_1855/myoRightEmg.csv"
|
2021-06-23 12:17:12 +00:00
|
|
|
subject1_right_files = [file1_subject1_rigth, file2_subject1_rigth, file3_subject1_rigth, file4_subject1_rigth]
|
2021-06-23 11:26:17 +00:00
|
|
|
|
2021-06-23 11:30:05 +00:00
|
|
|
# CSV data from subject 2
|
2021-06-23 11:26:17 +00:00
|
|
|
file1_subject2_left = "/Exp20201205_2myo_hardTypePP/HaluskaMaros_20201205_2010/myoLeftEmg.csv"
|
|
|
|
file2_subject2_left = "/Exp20201205_2myo_hardTypePP/HaluskaMaros_20201205_2025/myoLeftEmg.csv"
|
|
|
|
file3_subject2_left = "/Exp20201205_2myo_hardTypePP/HaluskaMaros_20201205_2035/myoLeftEmg.csv"
|
|
|
|
file4_subject2_left = "/Exp20201205_2myo_hardTypePP/HaluskaMaros_20201205_2045/myoLeftEmg.csv"
|
2021-06-23 12:17:12 +00:00
|
|
|
subject2_left_files = [file1_subject2_left, file2_subject2_left, file3_subject2_left, file4_subject2_left]
|
2021-06-23 11:26:17 +00:00
|
|
|
file1_subject2_rigth = "/Exp20201205_2myo_hardTypePP/HaluskaMaros_20201205_2010/myoRightEmg.csv"
|
|
|
|
file2_subject2_rigth = "/Exp20201205_2myo_hardTypePP/HaluskaMaros_20201205_2025/myoRightEmg.csv"
|
|
|
|
file3_subject2_rigth = "/Exp20201205_2myo_hardTypePP/HaluskaMaros_20201205_2035/myoRightEmg.csv"
|
|
|
|
file4_subject2_rigth = "/Exp20201205_2myo_hardTypePP/HaluskaMaros_20201205_2045/myoRightEmg.csv"
|
2021-06-23 12:17:12 +00:00
|
|
|
subject2_right_files = [file1_subject2_rigth, file2_subject2_rigth, file3_subject2_rigth, file4_subject2_rigth]
|
2021-06-23 11:26:17 +00:00
|
|
|
|
2021-06-23 11:30:05 +00:00
|
|
|
# CSV data from subject 3
|
2021-06-23 11:26:17 +00:00
|
|
|
file1_subject3_left = "/Exp20201205_2myo_hardTypePP/HaluskovaBeata_20201205_1700/myoLeftEmg.csv"
|
|
|
|
file2_subject3_left = "/Exp20201205_2myo_hardTypePP/HaluskovaBeata_20201205_1715/myoLeftEmg.csv"
|
|
|
|
file3_subject3_left = "/Exp20201205_2myo_hardTypePP/HaluskovaBeata_20201205_1725/myoLeftEmg.csv"
|
|
|
|
file4_subject3_left = "/Exp20201205_2myo_hardTypePP/HaluskovaBeata_20201205_1735/myoLeftEmg.csv"
|
2021-06-23 12:17:12 +00:00
|
|
|
subject3_left_files = [file1_subject3_left, file2_subject3_left, file3_subject3_left, file4_subject3_left]
|
2021-06-23 11:26:17 +00:00
|
|
|
file1_subject3_rigth = "/Exp20201205_2myo_hardTypePP/HaluskovaBeata_20201205_1700/myoRightEmg.csv"
|
|
|
|
file2_subject3_rigth = "/Exp20201205_2myo_hardTypePP/HaluskovaBeata_20201205_1715/myoRightEmg.csv"
|
|
|
|
file3_subject3_rigth = "/Exp20201205_2myo_hardTypePP/HaluskovaBeata_20201205_1725/myoRightEmg.csv"
|
|
|
|
file4_subject3_rigth = "/Exp20201205_2myo_hardTypePP/HaluskovaBeata_20201205_1735/myoRightEmg.csv"
|
2021-06-23 12:17:12 +00:00
|
|
|
subject3_right_files = [file1_subject3_rigth, file2_subject3_rigth, file3_subject3_rigth, file4_subject3_rigth]
|
2021-06-23 11:26:17 +00:00
|
|
|
|
2021-06-23 11:30:05 +00:00
|
|
|
# CSV data from subject 4
|
2021-06-23 11:26:17 +00:00
|
|
|
file1_subject4_left = "/Exp20201205_2myo_hardTypePP/KelisekDavid_20201209_1900/myoLeftEmg.csv"
|
|
|
|
file2_subject4_left = "/Exp20201205_2myo_hardTypePP/KelisekDavid_20201209_1915/myoLeftEmg.csv"
|
|
|
|
file3_subject4_left = "/Exp20201205_2myo_hardTypePP/KelisekDavid_20201209_1925/myoLeftEmg.csv"
|
|
|
|
file4_subject4_left = "/Exp20201205_2myo_hardTypePP/KelisekDavid_20201209_1935/myoLeftEmg.csv"
|
2021-06-23 12:17:12 +00:00
|
|
|
subject4_left_files = [file1_subject4_left, file2_subject4_left, file3_subject4_left, file4_subject4_left]
|
2021-06-23 11:26:17 +00:00
|
|
|
file1_subject4_rigth = "/Exp20201205_2myo_hardTypePP/KelisekDavid_20201209_1900/myoRightEmg.csv"
|
|
|
|
file2_subject4_rigth = "/Exp20201205_2myo_hardTypePP/KelisekDavid_20201209_1915/myoRightEmg.csv"
|
|
|
|
file3_subject4_rigth = "/Exp20201205_2myo_hardTypePP/KelisekDavid_20201209_1925/myoRightEmg.csv"
|
|
|
|
file4_subject4_rigth = "/Exp20201205_2myo_hardTypePP/KelisekDavid_20201209_1935/myoRightEmg.csv"
|
2021-06-23 12:17:12 +00:00
|
|
|
subject4_right_files = [file1_subject4_rigth, file2_subject4_rigth, file3_subject4_rigth, file4_subject4_rigth]
|
|
|
|
|
2021-06-23 11:26:17 +00:00
|
|
|
|
2021-06-23 11:30:05 +00:00
|
|
|
# CSV data from subject 5
|
2021-06-23 11:26:17 +00:00
|
|
|
file1_subject5_left = "/Exp20201205_2myo_hardTypePP/KelisekRichard_20201209_2030/myoLeftEmg.csv"
|
|
|
|
file2_subject5_left = "/Exp20201205_2myo_hardTypePP/KelisekRichard_20201209_2040/myoLeftEmg.csv"
|
|
|
|
file3_subject5_left = "/Exp20201205_2myo_hardTypePP/KelisekRichard_20201209_2050/myoLeftEmg.csv"
|
|
|
|
file4_subject5_left = "/Exp20201205_2myo_hardTypePP/KelisekRichard_20201209_2100/myoLeftEmg.csv"
|
2021-06-23 12:17:12 +00:00
|
|
|
subject5_left_files = [file1_subject5_left, file2_subject5_left, file3_subject5_left, file4_subject5_left]
|
2021-06-23 11:26:17 +00:00
|
|
|
file1_subject5_rigth = "/Exp20201205_2myo_hardTypePP/KelisekRichard_20201209_2030/myoRightEmg.csv"
|
|
|
|
file2_subject5_rigth = "/Exp20201205_2myo_hardTypePP/KelisekRichard_20201209_2040/myoRightEmg.csv"
|
|
|
|
file3_subject5_rigth = "/Exp20201205_2myo_hardTypePP/KelisekRichard_20201209_2050/myoRightEmg.csv"
|
|
|
|
file4_subject5_rigth = "/Exp20201205_2myo_hardTypePP/KelisekRichard_20201209_2100/myoRightEmg.csv"
|
2021-06-23 12:17:12 +00:00
|
|
|
subject5_right_files = [file1_subject5_rigth, file2_subject5_rigth, file3_subject5_rigth, file4_subject5_rigth]
|
2021-06-23 11:26:17 +00:00
|
|
|
|
2021-06-23 12:17:12 +00:00
|
|
|
left_list = [subject1_left_files, subject2_left_files, subject3_left_files, subject4_left_files, subject5_left_files]
|
|
|
|
right_list = [subject1_right_files, subject2_right_files, subject3_right_files, subject4_right_files, subject5_right_files]
|
2021-06-23 11:30:05 +00:00
|
|
|
|
2021-06-24 08:06:01 +00:00
|
|
|
csv_handler = Handler.CSV_handler
|
2021-06-23 12:17:12 +00:00
|
|
|
|
2021-06-24 08:06:01 +00:00
|
|
|
subject1_data_container = Handler.Data_container(1, 'HaluskaMarek')
|
|
|
|
subject2_data_container = Handler.Data_container(1, 'HaluskaMaros')
|
|
|
|
subject3_data_container = Handler.Data_container(1, 'HaluskovaBeata')
|
|
|
|
subject4_data_container = Handler.Data_container(1, 'KelisekDavid')
|
|
|
|
subject5_data_container = Handler.Data_container(1, 'KelisekRichard')
|
2021-06-23 12:17:12 +00:00
|
|
|
subject_data_container_list = [subject1_data_container, subject2_data_container, subject3_data_container,
|
|
|
|
subject4_data_container, subject5_data_container]
|
2021-06-23 11:30:05 +00:00
|
|
|
|
2021-06-23 11:26:17 +00:00
|
|
|
|
2021-06-23 12:17:12 +00:00
|
|
|
for subject_nr in range(5):
|
|
|
|
# left variant proccessed here
|
|
|
|
for round in range(4):
|
|
|
|
for emg_nr in range(8):
|
2021-06-23 12:37:17 +00:00
|
|
|
csv_handler.store_df(left_list[subject_nr][round], emg_nr+1, 'left', subject_data_container_list[subject_nr])
|
2021-06-23 12:17:12 +00:00
|
|
|
# right variant proccessed here
|
|
|
|
for round in range(4):
|
|
|
|
for emg_nr in range(8):
|
2021-06-23 12:37:17 +00:00
|
|
|
csv_handler.store_df(left_list[subject_nr][round], emg_nr+1, 'right', subject_data_container_list[subject_nr])
|
2021-06-23 12:17:12 +00:00
|
|
|
|
|
|
|
return csv_handler.data_container_dict
|
2021-06-22 18:31:16 +00:00
|
|
|
|
2021-06-24 07:48:30 +00:00
|
|
|
# Takes in a df and outputs np arrays for x and y values
|
2021-06-24 10:02:49 +00:00
|
|
|
def prep_df(df:DataFrame):
|
2021-06-23 13:56:35 +00:00
|
|
|
min, duration = Handler.get_min_max_timestamp(df)
|
2021-06-24 08:29:11 +00:00
|
|
|
y = df.iloc[:,1].to_numpy()
|
2021-06-24 10:02:49 +00:00
|
|
|
return y, duration
|
2021-06-22 13:32:51 +00:00
|
|
|
|
2021-06-24 11:30:32 +00:00
|
|
|
# Normalizes a ndarray of a signal to the scale of int16(32767)
|
2021-06-23 13:56:35 +00:00
|
|
|
def normalize_wave(y_values):
|
2021-06-24 07:40:30 +00:00
|
|
|
y = np.int16((y_values / y_values.max()) * 32767)
|
|
|
|
return y
|
2021-06-23 13:56:35 +00:00
|
|
|
|
2021-06-24 11:30:32 +00:00
|
|
|
# Takes the FFT of a DataFrame object
|
2021-06-24 10:02:49 +00:00
|
|
|
def fft_of_df(df:DataFrame):
|
|
|
|
y_values, duration = prep_df(df)
|
|
|
|
N = y_values.size
|
2021-06-24 07:48:30 +00:00
|
|
|
norm = normalize_wave(y_values)
|
2021-06-24 12:24:58 +00:00
|
|
|
N_trans = fftfreq(N, 1 / SAMPLE_RATE)
|
2021-06-24 07:48:30 +00:00
|
|
|
y_f = fft(norm)
|
2021-06-24 12:24:58 +00:00
|
|
|
return N_trans, y_f, duration
|
2021-06-24 10:02:49 +00:00
|
|
|
|
2021-06-24 11:30:32 +00:00
|
|
|
# Removes noise with db4 wavelet function
|
2021-06-24 12:24:58 +00:00
|
|
|
def wavelet_db4_denoising(df:DataFrame):
|
2021-06-24 10:02:49 +00:00
|
|
|
y_values, duration = prep_df(df)
|
2021-06-24 11:30:32 +00:00
|
|
|
#y_values = normalize_wave(y_values)
|
2021-06-24 10:02:49 +00:00
|
|
|
wavelet = pywt.Wavelet('db4')
|
2021-06-24 11:30:32 +00:00
|
|
|
cA, cD = pywt.dwt(y_values, wavelet)
|
2021-06-24 12:24:58 +00:00
|
|
|
N_trans = np.array(range(int(np.floor((y_values.size + wavelet.dec_len - 1) / 2))))
|
|
|
|
return N_trans, cA, cD
|
2021-06-24 11:30:32 +00:00
|
|
|
|
2021-06-24 12:12:21 +00:00
|
|
|
# Filters signal accordning to Stein's Unbiased Risk Estimate(SURE)
|
|
|
|
def sure_threshold_filter(cA, cD):
|
2021-06-24 12:35:37 +00:00
|
|
|
cA_filt = pyyawt.theselect(cA, 'rigrsure')
|
|
|
|
cD_filt = cD
|
|
|
|
return cA_filt, cD_filt
|
2021-06-24 12:12:21 +00:00
|
|
|
|
2021-06-24 12:35:37 +00:00
|
|
|
# soft filtering of wavelet trans with 0.25 lower percent
|
2021-06-24 12:12:21 +00:00
|
|
|
def soft_threshold_filter(cA, cD):
|
2021-06-24 12:35:37 +00:00
|
|
|
cA_filt = pywt.threshold(cA, 0.25 * cA.max())
|
|
|
|
cD_filt = cD
|
|
|
|
return cA_filt, cD_filt
|
|
|
|
|
|
|
|
# Inverse dwt for brining denoise signal back to the time domain
|
|
|
|
def inverse_wavelet(cA_filt, cD_filt):
|
|
|
|
wavelet = pywt.Wavelet('db4')
|
|
|
|
y_new_values = pywt.idwt(cA_filt, cD_filt, wavelet)
|
|
|
|
return y_new_values
|
2021-06-24 11:30:32 +00:00
|
|
|
|
|
|
|
# Plots DataFrame objects
|
2021-06-23 13:56:35 +00:00
|
|
|
def plot_df(df:DataFrame):
|
|
|
|
lines = df.plot.line(x='timestamp')
|
|
|
|
plt.show()
|
|
|
|
|
2021-06-24 11:30:32 +00:00
|
|
|
# Plots ndarrays after transformations
|
2021-06-24 13:02:21 +00:00
|
|
|
def plot_arrays(N, y):
|
|
|
|
plt.plot(N, np.abs(y))
|
2021-06-24 07:51:14 +00:00
|
|
|
plt.show()
|
|
|
|
|
2021-06-24 12:17:31 +00:00
|
|
|
|
2021-06-24 08:29:11 +00:00
|
|
|
#'''
|
2021-06-24 08:06:01 +00:00
|
|
|
handler = Handler.CSV_handler()
|
2021-06-23 13:56:35 +00:00
|
|
|
file = "/Exp20201205_2myo_hardTypePP/HaluskaMarek_20201207_1810/myoLeftEmg.csv"
|
|
|
|
df = handler.get_time_emg_table(file, 1)
|
2021-06-24 13:02:21 +00:00
|
|
|
N = np.array(range(int(df.iloc[:,1].size + 1)))
|
|
|
|
plot_df(df)
|
2021-06-24 12:24:58 +00:00
|
|
|
x, cA, cD = wavelet_db4_denoising(df)
|
2021-06-24 13:02:21 +00:00
|
|
|
plot_arrays(x, cA)
|
|
|
|
cA_filt, cD_filt = soft_threshold_filter(cA, cD)
|
|
|
|
plot_arrays(x, cA_filt)
|
|
|
|
y_new_values = inverse_wavelet(cA, cD)
|
|
|
|
plot_arrays(N, y_new_values)
|