2021-06-28 09:28:53 +00:00
|
|
|
from logging import error
|
2021-06-28 13:22:10 +00:00
|
|
|
|
|
|
|
from matplotlib.cbook import get_sample_data
|
2021-06-25 14:09:51 +00:00
|
|
|
from Handle_emg_data import *
|
|
|
|
from Signal_prep import *
|
2021-06-28 08:57:08 +00:00
|
|
|
import matplotlib.pyplot as plt
|
2021-06-28 13:22:10 +00:00
|
|
|
from matplotlib import cm
|
|
|
|
import matplotlib.ticker as ticker
|
2021-06-28 12:44:00 +00:00
|
|
|
|
2021-06-28 17:24:17 +00:00
|
|
|
# Global variables for MFCC
|
2021-06-28 12:44:00 +00:00
|
|
|
mfcc_stepsize = 0.5
|
|
|
|
mfcc_windowsize = 2
|
2021-06-25 14:09:51 +00:00
|
|
|
|
2021-06-28 17:24:17 +00:00
|
|
|
|
|
|
|
# PLOT FUNCTIONS --------------------------------------------------------------:
|
2021-06-25 14:09:51 +00:00
|
|
|
|
|
|
|
# Plots DataFrame objects
|
|
|
|
def plot_df(df:DataFrame):
|
|
|
|
lines = df.plot.line(x='timestamp')
|
|
|
|
plt.show()
|
|
|
|
|
|
|
|
# Plots ndarrays after transformations
|
2021-06-28 08:57:08 +00:00
|
|
|
def plot_array(N, y):
|
2021-06-25 14:09:51 +00:00
|
|
|
plt.plot(N, np.abs(y))
|
|
|
|
plt.show()
|
|
|
|
|
2021-06-28 07:59:01 +00:00
|
|
|
def plot_compare_two_df(df_old, old_name, df_new, new_name):
|
2021-06-25 14:35:35 +00:00
|
|
|
x = get_xory_from_df('x', df_old)
|
|
|
|
y1 = get_xory_from_df('y', df_old)
|
|
|
|
y2 = get_xory_from_df('y', df_new)
|
2021-06-25 14:09:51 +00:00
|
|
|
|
2021-06-25 14:35:35 +00:00
|
|
|
figure, axis = plt.subplots(1, 2)
|
|
|
|
axis[0].plot(x, y1)
|
2021-06-28 07:59:01 +00:00
|
|
|
axis[0].set_title(old_name)
|
2021-06-25 14:35:35 +00:00
|
|
|
axis[1].plot(x, y2)
|
2021-06-28 07:59:01 +00:00
|
|
|
axis[1].set_title(new_name)
|
2021-06-25 14:35:35 +00:00
|
|
|
plt.show()
|
2021-06-25 14:09:51 +00:00
|
|
|
|
2021-06-28 17:24:17 +00:00
|
|
|
def plot_mfcc(mfcc_data, data_label:str):
|
2021-06-28 08:57:08 +00:00
|
|
|
fig, ax = plt.subplots()
|
|
|
|
mfcc_data= np.swapaxes(mfcc_data, 0 ,1)
|
2021-06-28 17:24:17 +00:00
|
|
|
|
|
|
|
ticks_x = ticker.FuncFormatter(lambda x, pos: '{0:g}'.format(x * mfcc_stepsize))
|
|
|
|
ax.xaxis.set_major_formatter(ticks_x)
|
|
|
|
|
2021-06-28 12:44:00 +00:00
|
|
|
ax.imshow(mfcc_data, interpolation='nearest', cmap=cm.coolwarm, origin='lower')
|
2021-06-28 17:24:17 +00:00
|
|
|
ax.set_title('MFCC:' + data_label)
|
|
|
|
ax.set_ylabel('Cepstral coefficients')
|
|
|
|
ax.set_xlabel('Time(s)')
|
2021-06-28 08:57:08 +00:00
|
|
|
plt.show()
|
2021-06-25 14:09:51 +00:00
|
|
|
|
2021-06-28 17:24:17 +00:00
|
|
|
# DATA FUNCTIONS: --------------------------------------------------------------:
|
2021-06-25 14:35:35 +00:00
|
|
|
|
|
|
|
# The CSV_handler takes in data_type, but only for visuals.
|
|
|
|
# E.g. handler = CSV_handler('soft')
|
|
|
|
|
|
|
|
# Loads in data. Choose data_type: hard, hardPP, soft og softPP as str. Returns None
|
|
|
|
def load_data(csv_handler:CSV_handler, data_type):
|
2021-06-28 09:28:53 +00:00
|
|
|
if data_type == 'hard':
|
|
|
|
csv_handler.load_hard_original_emg_data()
|
|
|
|
elif data_type == 'hardPP':
|
|
|
|
csv_handler.load_hard_PP_emg_data()
|
|
|
|
elif data_type == 'soft':
|
|
|
|
csv_handler.load_soft_original_emg_data()
|
|
|
|
elif data_type == 'softPP':
|
|
|
|
csv_handler.load_soft_PP_emg_data()
|
|
|
|
else:
|
|
|
|
raise Exception('Wrong input')
|
2021-06-25 14:35:35 +00:00
|
|
|
|
2021-06-28 17:24:17 +00:00
|
|
|
# Retrieved data. Send in loaded csv_handler and data detailes you want. Returns DataFrame and samplerate
|
2021-06-25 14:35:35 +00:00
|
|
|
def get_data(csv_handler:CSV_handler, subject_nr, which_arm, session, emg_nr):
|
|
|
|
data_frame = csv_handler.get_df_from_data_dict(subject_nr, which_arm, session, emg_nr)
|
2021-06-28 17:24:17 +00:00
|
|
|
samplerate = get_samplerate(data_frame)
|
|
|
|
return data_frame, samplerate
|
2021-06-25 14:35:35 +00:00
|
|
|
|
2021-06-28 07:59:01 +00:00
|
|
|
#Takes in handler and detailes to denoise. Returns arrays and df
|
|
|
|
def denoice_dataset(handler:Handler.CSV_handler, subject_nr, which_arm, round, emg_nr):
|
|
|
|
df = handler.get_df_from_data_dict(subject_nr, which_arm, round, emg_nr)
|
|
|
|
|
|
|
|
N = get_xory_from_df('x', df)
|
|
|
|
N_trans, cA, cD = wavelet_db4(df)
|
|
|
|
cA_filt, cD_filt = soft_threshold_filter(cA, cD)
|
|
|
|
y_values = inverse_wavelet(df, cA_filt, cD_filt)
|
|
|
|
|
|
|
|
df_new = Handler.make_df_from_xandy(N, y_values, emg_nr)
|
|
|
|
return df_new
|
|
|
|
|
2021-06-28 13:22:10 +00:00
|
|
|
def mfcc_custom(df:DataFrame, samplesize, windowsize, stepsize):
|
|
|
|
N = get_xory_from_df('x', df)
|
|
|
|
y = get_xory_from_df('y', df)
|
|
|
|
return N, base.mfcc(y, samplesize, windowsize, stepsize)
|
2021-06-28 07:59:01 +00:00
|
|
|
|
2021-06-28 17:24:17 +00:00
|
|
|
# CASE FUNTIONS ----------------------------------------------------------------:
|
2021-06-28 08:57:08 +00:00
|
|
|
|
2021-06-28 17:24:17 +00:00
|
|
|
def compare_with_wavelet_filter(data_frame):
|
2021-06-28 08:57:08 +00:00
|
|
|
N_trans, cA, cD = wavelet_db4(data_frame)
|
|
|
|
data_frame_freq = make_df_from_xandy(N_trans, cA, 1)
|
|
|
|
|
|
|
|
cA_filt, cD_filt = soft_threshold_filter(cA, cD)
|
|
|
|
data_frame_freq_filt = make_df_from_xandy(N_trans, cD_filt, 1)
|
|
|
|
|
|
|
|
plot_compare_two_df(data_frame_freq, 'Original data', data_frame_freq_filt, 'Analyzed data')
|
|
|
|
|
2021-06-25 14:09:51 +00:00
|
|
|
|
2021-06-28 17:24:17 +00:00
|
|
|
# MAIN: ------------------------------------------------------------------------:
|
2021-06-25 14:09:51 +00:00
|
|
|
|
|
|
|
def main():
|
|
|
|
|
2021-06-28 09:28:53 +00:00
|
|
|
csv_handler = CSV_handler()
|
2021-06-25 14:35:35 +00:00
|
|
|
load_data(csv_handler, 'hard')
|
2021-06-28 17:24:17 +00:00
|
|
|
data_frame, samplerate = get_data(csv_handler, 2, 'left', 1, 1)
|
2021-06-28 12:44:00 +00:00
|
|
|
|
2021-06-28 17:24:17 +00:00
|
|
|
N, mfcc_feat = mfcc_custom(data_frame[:5000], samplerate, mfcc_windowsize, mfcc_stepsize)
|
2021-06-28 12:44:00 +00:00
|
|
|
plot_mfcc(mfcc_feat)
|
2021-06-28 17:24:17 +00:00
|
|
|
#print(mfcc_feat)
|
|
|
|
|
2021-06-28 13:22:10 +00:00
|
|
|
|
2021-06-25 14:09:51 +00:00
|
|
|
return None
|
2021-06-25 14:35:35 +00:00
|
|
|
|
|
|
|
main()
|