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
|
|
|
|
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 13:45:33 +00:00
|
|
|
def get_xory_from_df(x_or_y, df:DataFrame):
|
|
|
|
swither = {
|
|
|
|
'x': df.iloc[:,0].to_numpy(),
|
|
|
|
'y': df.iloc[:,1].to_numpy()
|
|
|
|
}
|
|
|
|
return swither.get(x_or_y, 0)
|
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):
|
2021-06-24 13:45:33 +00:00
|
|
|
y_values = get_xory_from_df('y', df)
|
2021-06-24 10:02:49 +00:00
|
|
|
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 13:45:33 +00:00
|
|
|
y_values = get_xory_from_df('y', 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-25 07:12:12 +00:00
|
|
|
# soft filtering of wavelet trans with the 40% lowest removed
|
2021-06-24 12:12:21 +00:00
|
|
|
def soft_threshold_filter(cA, cD):
|
2021-06-25 07:05:10 +00:00
|
|
|
cA_filt = pywt.threshold(cA, 0.4 * cA.max())
|
2021-06-24 12:35:37 +00:00
|
|
|
cD_filt = cD
|
|
|
|
return cA_filt, cD_filt
|
|
|
|
|
2021-06-24 13:45:33 +00:00
|
|
|
# Inverse dwt for brining denoise signal back to the time domainfi
|
|
|
|
def inverse_wavelet(df, cA_filt, cD_filt):
|
2021-06-24 12:35:37 +00:00
|
|
|
wavelet = pywt.Wavelet('db4')
|
|
|
|
y_new_values = pywt.idwt(cA_filt, cD_filt, wavelet)
|
2021-06-24 13:45:33 +00:00
|
|
|
new_len = len(y_new_values)
|
|
|
|
old_len = len(get_xory_from_df('y', df))
|
|
|
|
if new_len > old_len:
|
|
|
|
while new_len > old_len:
|
|
|
|
y_new_values = y_new_values[:-1]
|
|
|
|
new_len = len(y_new_values)
|
|
|
|
old_len = len(get_xory_from_df('y', df))
|
2021-06-24 12:35:37 +00:00
|
|
|
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()
|
|
|
|
|