feat: add db4 wavelet func

This commit is contained in:
Skudalen 2021-06-24 12:02:49 +02:00
parent 2afad7f87c
commit fecf1aaae2
3 changed files with 21 additions and 12 deletions

View File

@ -44,7 +44,7 @@ def get_emg_str(emg_nr):
def get_min_max_timestamp(df:DataFrame):
min = int(np.floor(df['timestamp'].min()))
max = int(np.ceil(df['timestamp'].max()))
max = df['timestamp'].max()
return min, max

View File

@ -2,6 +2,8 @@ import numpy as np
import matplotlib.pyplot as plt
from pandas.core.frame import DataFrame
from scipy.fft import fft, fftfreq
import pywt
from scipy.signal import wavelets
import Handle_emg_data as Handler
@ -97,31 +99,38 @@ def load_user_emg_data():
return csv_handler.data_container_dict
# Takes in a df and outputs np arrays for x and y values
def prep_df_for_trans(df:DataFrame):
sample_rate = SAMPLE_RATE
def prep_df(df:DataFrame):
min, duration = Handler.get_min_max_timestamp(df)
x = np.linspace(0, duration, SAMPLE_RATE * duration, endpoint=False)
y = df.iloc[:,1].to_numpy()
return x, y, duration
return y, duration
def normalize_wave(y_values):
y = np.int16((y_values / y_values.max()) * 32767)
return y
def transformed_df(df:DataFrame):
x_values, y_values, duration = prep_df_for_trans(df)
N = SAMPLE_RATE * duration
def fft_of_df(df:DataFrame):
y_values, duration = prep_df(df)
N = y_values.size
norm = normalize_wave(y_values)
x_f = fftfreq(N, 1 / SAMPLE_RATE)
y_f = fft(norm)
return x_f, y_f
return x_f, y_f, duration
def denoise_signal_pywt(df:DataFrame):
y_values, duration = prep_df(df)
norm = normalize_wave(y_values)
wavelet = pywt.Wavelet('db4')
cA, cD = pywt.dwt(norm, wavelet)
x = np.array(range(int(np.floor((y_values.size + wavelet.dec_len - 1) / 2))))
print(x)
return x, cA
def plot_df(df:DataFrame):
lines = df.plot.line(x='timestamp')
plt.show()
def plot_fft(x_f, y_f):
def plot_transformed(x_f, y_f):
plt.plot(x_f, np.abs(y_f))
plt.show()
@ -130,7 +139,7 @@ handler = Handler.CSV_handler()
file = "/Exp20201205_2myo_hardTypePP/HaluskaMarek_20201207_1810/myoLeftEmg.csv"
df = handler.get_time_emg_table(file, 1)
#plot_df(df)
trans_df = DataFrame(transformed_df(df))
x_f, y_f = denoise_signal_pywt(df)
#print(trans_df.info)
plot_fft(trans_df)
plot_transformed(x_f, y_f)
#'''