def ecg_preprocess(sig): sig_sm1 = smooth(sig, window_len=20) sig_sm = smooth(sig_sm1, window_len=500) sig_sm = sig_sm1 - sig_sm sig_n = notchFilter(sig_sm, 60, 250, Q=5) sig_m = (sig_n - np.mean(sig_n)) / np.max(sig_n) return sig_m
def spectral_distance_corrected(signal, fs): """Computes the signal spectral distance. Distance of the signal's cumulative sum of the FFT elements to the respective linear regression. Parameters ---------- signal : nd-array Signal from which spectral distance is computed fs : int Sampling frequency Returns ------- float spectral distance """ f, fmag = calc_fftmulti(signal, fs) cum_fmag = np.cumsum(fmag, axis=1) # Computing the linear regression points_y = np.array([ np.linspace(0, cum_fmag_i[-1], np.shape(cum_fmag)[1]) for cum_fmag_i in cum_fmag ]) return smooth(np.sum(points_y - cum_fmag, axis=1), window_len=len(signal[0] / 10))
def make_noise_signals(full_paths, max_target_SNR=16, signal_dim=64): target_SNR_array = np.arange(max_target_SNR, max_target_SNR - 10, -1) N_SIGNALS, N_NOISE, N_SAMPLES = len(full_paths), len(target_SNR_array), 0 processed_noise_array = np.zeros( (N_NOISE, N_SIGNALS, len(sio.loadmat(full_paths[0])['val'][0]))) SNR = np.zeros(N_NOISE) SNRs = np.zeros(N_SIGNALS) for i, file_path in zip(range(len(full_paths)), full_paths): print("Processing " + file_path) signal = sio.loadmat(file_path)['val'][0] signal = remove_moving_std( remove_moving_avg((signal - np.mean(signal)) / np.std(signal))) smoothed_signal = smooth(signal) SNR = int(calculate_signal_to_noise_ratio(signal, smoothed_signal)) SNRs[i] = SNR # last_std = 0.0001 # j = 0 # for target_SNR in target_SNR_array: # signal_with_noise, last_std = make_noise_vectors(signal, smoothed_signal, target_SNR, last_std=last_std) # processed_noise_array[j, i, :] = process_dnn_signal(signal_with_noise, signal_dim, confidence=0.005)[:np.shape(processed_noise_array)[2]] # j += 1 print(np.mean(SNRs)) print(np.std(SNRs)) return processed_noise_array, target_SNR_array
def process_and_save_signals_2(filenames, core_names, Ns, indexes2process=None): if indexes2process is None: indexes2process = np.arange(len(filenames)) all_indexes = np.arange(len(filenames)) for x, filename, core_name in \ zip(all_indexes[indexes2process], np.array(filenames)[indexes2process], core_names[indexes2process]): print(x) # try: print("Loading signal {0}...".format(x)) if x < Ns[0]: fs = 360 else: fs = 128 original_signal = sio.loadmat(filename)['val'][0][3000:3000 + 3600 * fs] signal = smooth(original_signal, 10) signal = signal - smooth(signal, fs * 2) time = len(original_signal) / fs N = len(original_signal) iter_ = scp.interpolate.interp1d(np.arange(0, time, 1 / fs), signal) t = np.arange(0, time - 1 / 250, 1 / 250) signal = iter_(t) # moving_maximum = np.array(moving_max(signal, int(360 * 1.2))) # moving_maximum = smooth(moving_maximum, int(360)) # signal = signal / moving_maximum fs = 250 # for i in range(10): # signal -= np.array(moving_min(signal, int(fs * 8))) moving_maximum = np.array(moving_max(signal, int((3 / 2) * fs))) moving_minimum = np.array(moving_min(signal, int((3 / 2) * fs))) moving_maximum = smooth(moving_maximum - moving_minimum, int(fs * (3 / 2))) signal = signal / moving_maximum # signal -= np.array(moving_min(signal, int(fs*8))) signal = process_dnn_signal(signal, 256, confidence=0.01) new_path = '../data/processed/MIT/{0}[256].npz'.format(core_name) np.savez(new_path, signal=signal, core_name=core_name)
def process_and_save_fantasia(plot=False, signal_dim=64): """ insert noise into fantasia dataset :param plot: :param signal_dim: :return: """ signals_without_noise = [] signals_noise = [] SNR = [] full_paths = get_fantasia_full_paths(db.fantasia_ecgs[0].directory, list(range(1,21))) for file_path in full_paths: # try: print("Pre-processing signal - " + file_path) signal = sio.loadmat(file_path)['val'][0][:1256] # plt.plot(signal) processed = process_dnn_signal(signal, signal_dim) signals_without_noise.append(processed) if plot: plt.plot(processed) plt.show() plt.plot(signal[1000:5000]) fig, ax = plt.subplots() major_ticks = np.arange(0, 64) ax.set_yticks(major_ticks) plt.ylim([0, 15]) plt.xlim([0, 140]) ax.grid(True, which='both') plt.minorticks_on # ax.grid(which="minor", color='k') ax.set_ylabel('Class - k') ax.set_xlabel('Sample - n') plt.plot(signalx, label="Smoothed Signal", alpha=0.4) plt.plot(processed, label="Discretized Signal") # ticklines = ax.get_xticklines() + ax.get_yticklines() gridlines = ax.get_ygridlines() # + ax.get_ygridlines() ticklabels = ax.get_xticklabels() + ax.get_yticklabels() for line in gridlines: line.set_color('k') line.set_linestyle('-') line.set_linewidth(1) line.set_alpha(0.2) for label in ticklabels: label.set_color('r') label.set_fontsize('medium') plt.legend() plt.show() signalx = smooth(remove_moving_std(remove_moving_avg((signal - np.mean(signal)) / np.std(signal)))) print("Saving signals...") np.savez("signals_without_noise.npz", signals_without_noise=signals_without_noise)
def process_and_save_signals(filenames, core_names, Ns, indexes2process=None): if indexes2process is None: indexes2process = np.arange(len(filenames)) all_indexes = np.arange(len(filenames)) for x, filename, core_name in \ zip(all_indexes[indexes2process], np.array(filenames)[indexes2process], core_names[indexes2process]): print(x) try: print("Loading signal {0}...".format(x)) if x < Ns[0]: fs = 360 else: fs = 128 original_signal = sio.loadmat(filename)['val'][0][3000:3000 + 3600 * fs] signal = smooth(original_signal, 10) signal = signal - smooth(signal, fs) time = len(original_signal) / fs N = len(original_signal) iter_ = scp.interpolate.interp1d(np.arange(0, time, 1 / fs), signal) t = np.arange(0, time - 1 / 250, 1 / 250) signal = iter_(t) moving_maximum = np.array(moving_max(signal, int(360 * 1.2))) moving_maximum = smooth(moving_maximum, int(360)) signal = signal / moving_maximum signal = process_dnn_signal(signal, 256, window_rmavg=None, confidence=0.001) plt.plot(signal) plt.show() new_path = '../data/processed/MIT/{0}[256].npz'.format(core_name) np.savez(new_path, signal=signal, core_name=core_name) except: print("error") pass
def wii_smoother(wii_data, indexes): temp_1 = 0 temp_2 = 0 for i in range(0, len(wii_data)): wii_data[i] = smooth(np.array(wii_data[i]), indexes[temp_1]) temp_2 += 1 if temp_2 == 2: temp_1 += 1 temp_2 = 0 return wii_data
def smoothing(a): """novainstrumentation 100 smoothing Reshape safeguard used to work with column arrays TODO: Check causality """ is_reshaped = False if a.ndim != 1: shape = a.shape if shape[1] == 1: a = a.reshape(len(a)) is_reshaped = True return ni.smooth(a.reshape(len(a)),100).reshape(-1,1)
def plotSmooth(nbr_Burst, data, fig, axis1, axis2, axis3, figTitle, plot_title, axis1Title, units, font1, font2, smooth_factor): fig.suptitle(figTitle) # fig1.subplotpars(pars) # Final Stage of data manipulation (Removal of null [] entries). val_ent = np.where(np.array(data) != False)[0] nbr_Burst = np.array(nbr_Burst)[val_ent] data = np.array(data)[val_ent] axis1.plot(nbr_Burst, data) # limit from min and max frequencies # ax11.plot(nbr_Burst, MajorF, '.', linewidth=0) # limit from min and max frequencies axis1.set_title(plot_title, fontproperties=font2) axis1.set_ylabel(axis1Title + " " + "(" + units + ")", fontproperties=font1) axis1.set_xlabel("Burst/Set of Bursts", fontproperties=font1) axis1.set_xlim(0) # Plot of the smooth line. smooth_line = smooth(data, int(len(data) * smooth_factor)) axis1.plot(nbr_Burst, smooth_line) # Major Frequency Deviation from regression line. deviation = data - smooth_line absolute_deviation = np.absolute(deviation) axis2.plot(nbr_Burst, deviation) # limit from min and max frequencies # ax11.plot(nbr_Burst, MajorF, '.', linewidth=0) # limit from min and max frequencies axis2.set_title(plot_title + " " + 'Deviation', fontproperties=font2) axis2.set_ylabel("Deviation" + " " + "(" + units + ")", fontproperties=font1) axis2.set_xlabel("Burst/Set of Bursts", fontproperties=font1) axis2.set_xlim(0) axis3.plot(nbr_Burst, absolute_deviation) # limit from min and max frequencies # ax11.plot(nbr_Burst, MajorF, '.', linewidth=0) # limit from min and max frequencies axis3.set_title(plot_title + " " + 'Absolute Deviation', fontproperties=font2) axis3.set_ylabel("Absolute Deviation" + " " + "(" + units + ")", fontproperties=font1) axis3.set_xlabel("Burst/Set of Bursts", fontproperties=font1) axis3.set_xlim(0) if list(smooth_line) == list(data): FlagValidation = False else: FlagValidation = True return FlagValidation
def WindowStat(inputSignal, statTool, fs, window_len=50, window='hanning'): output = np.zeros(len(inputSignal)) win = eval('np.' + window + '(window_len)') if inputSignal.ndim != 1: raise ValueError("smooth only accepts 1 dimension arrays.") if inputSignal.size < window_len: raise ValueError("Input vector needs to be bigger than window size.") if window_len < 3: return inputSignal inputSignal = inputSignal - np.mean(inputSignal) WinRange = int(window_len / 2) sig = np.r_[inputSignal[WinRange:0:-1], inputSignal, inputSignal[-1:len(inputSignal) - WinRange:-1]] # windowing if (statTool is 'stn'): WinSize = window_len numSeg = int(len(inputSignal) / WinSize) SigTemp = np.zeros(numSeg) for i in range(1, numSeg): signal = inputSignal[(i - 1) * WinSize:i * WinSize] SigTemp[i] = sc.signaltonoise(signal) output = np.interp(np.linspace(0, len(SigTemp), len(output)), np.linspace(0, len(SigTemp), len(SigTemp)), SigTemp) elif (statTool is 'zcr'): # inputSignal = inputSignal - smooth(inputSignal, window_len=fs*4) # inputSignal = inputSignal - smooth(inputSignal, window_len=int(fs/10)) # sig = np.r_[inputSignal[WinRange:0:-1], inputSignal, inputSignal[-1:len(inputSignal) - WinRange:-1]] for i in range(int(WinRange), len(sig) - int(WinRange)): output[i - int(WinRange)] = ZeroCrossingRate( sig[i - WinRange:WinRange + i] * win) output = smooth(output, window_len=1024) elif (statTool is 'Azcr'): # inputSignal = inputSignal - smooth(inputSignal, window_len=fs*4) # inputSignal = inputSignal - smooth(inputSignal, window_len=int(fs/10)) # sig = np.r_[inputSignal[WinRange:0:-1], inputSignal, inputSignal[-1:len(inputSignal) - WinRange:-1]] for i in range(int(WinRange), len(sig) - int(WinRange)): A = np.max(sig[i - WinRange:WinRange + i]) output[i - int(WinRange)] = A * ZeroCrossingRate( sig[i - WinRange:WinRange + i] * win) output = smooth(output, window_len=1024) elif (statTool is 'std'): for i in range(int(WinRange), len(sig) - int(WinRange)): output[i - WinRange] = np.std(sig[i - WinRange:WinRange + i] * win) elif (statTool is 'mean'): for i in range(int(WinRange), len(sig) - int(WinRange)): output[i - WinRange] = np.mean(sig[i - WinRange:WinRange + i]) elif (statTool is 'subPks'): for i in range(int(WinRange), len(sig) - int(WinRange)): pks = [0] win_len = window_len while (len(pks) < 10): pks = detect_peaks( sig[i - int(win_len / 2):int(win_len / 2) + i], valley=False, mph=np.std(sig[i - int(win_len / 2):int(win_len / 2) + i])) if (len(pks) < 10): win_len += int(win_len / 5) sub_zero = pks[1] - pks[0] sub_end = pks[-1] - pks[-2] subPks = np.r_[sub_zero, (pks[1:-1] - pks[0:-2]), sub_end] win = eval('np.' + window + '(len(subPks))') output[i - int(WinRange)] = np.mean(subPks * win) elif (statTool is 'findPks'): for i in range(int(WinRange), len(sig) - int(WinRange)): pks = detect_peaks(sig[i - WinRange:WinRange + i], valley=False, mph=np.std(sig[i - WinRange:WinRange + i])) LenPks = len(pks) output[i - int(WinRange)] = LenPks elif (statTool is 'sum'): for i in range(int(WinRange), len(sig) - int(WinRange)): output[i - WinRange] = np.sum( abs(sig[i - WinRange:WinRange + i] * win)) elif (statTool is 'AmpDiff'): for i in range(int(WinRange), len(sig) - int(WinRange)): win_len = window_len tempSig = sig[i - int(win_len / 2):int(win_len / 2) + i] maxPks = detect_peaks(tempSig, valley=False, mph=np.std(tempSig)) minPks = detect_peaks(tempSig, valley=True, mph=np.std(tempSig)) AmpDiff = np.sum(tempSig[maxPks]) - np.sum(tempSig[minPks]) output[i - WinRange] = AmpDiff elif (statTool is 'MF'): for i in range(int(WinRange), len(sig) - int(WinRange)): f, Pxx = PowerSpectrum(inputSignal[i - WinRange:i + WinRange], fs=fs, nperseg=WinRange / 2) mf = MF_calculus(Pxx) output[i - WinRange] = mf elif (statTool is "SumPS"): for i in range(int(WinRange), len(sig) - int(WinRange)): f, Pxx = PowerSpectrum(inputSignal[i - WinRange:i + WinRange], fs=fs, nperseg=WinRange / 2) sps = SumPowerSpectrum(Pxx) output[i - WinRange] = sps elif (statTool is "AmpMean"): for i in range(int(WinRange), len(sig) - int(WinRange)): output[i - WinRange] = np.mean( abs(sig[i - WinRange:WinRange + i]) * win) elif (statTool is "Spikes1"): ss = 0.1 * max(sig) for i in range(int(WinRange), len(sig) - int(WinRange)): pkd, md = Spikes(sig[i - WinRange:WinRange + i] * win, mph=ss) output[i - WinRange] = pkd elif (statTool is "Spikes2"): ss = 0.1 * max(sig) for i in range(int(WinRange), len(sig) - int(WinRange)): pkd, md = Spikes(sig[i - WinRange:WinRange + i] * win, mph=ss) output[i - WinRange] = md elif (statTool is "Spikes3"): ss = 0.1 * max(sig) for i in range(int(WinRange), len(sig) - int(WinRange)): pkd, md = Spikes(abs(sig[i - WinRange:WinRange + i]) * win, mph=ss) output[i - WinRange] = md output = output - np.mean(output) output = output / max(output) #output = smooth(output, window_len=10) return output
def test_symmetric_window(): x = arange(1., 11.) sx = smooth(x, window_len=5, window='flat') assert_allclose(x, sx)
# print(lda_model[corpus_i]) return labels[:-ind_end] guide = CONFIG_PATH + "/Hui_SuperProject/MovDict.json" guide_dict = read_json(guide).to_dict() example_path = CONFIG_PATH + "/Hui_SuperProject/Data_Examples/" signal = loadH5(example_path + "arthrokinemat_2018_06_03_00_08_52.h5") fs = 1000 b = 4 acc1 = signal[b * fs:, 0] acc1_sm = mean_norm(acc1) acc1_sm = smooth(abs(acc1_sm), 1000) acc1_sm = acc1_sm[::50] # plt.plot(acc1_sm) # plt.show() acc_str = Connotation2(acc1_sm) print(acc_str) # lsa_labels = movingLSA(acc_str) lda_labels = movingLDA(acc_str, win_size=50) ax = plt.subplot(1, 1, 1) # plot_textcolorized(acc1_sm, acc_str, ax) plotScatterColors(acc1_sm, signal[b * fs:, -1], lda_labels,
import numpy as np import pandas as pd import novainstrumentation as ni from gmtools import * close('all') mapxy = np.array(pd.read_csv('./data/coord_demo3.txt', header=None)) x, y = mapxy[:, 0], mapxy[:, 1] nx, ny = ni.smooth(x, 70), ni.smooth(y, 70) dnx = np.diff(nx) dny = np.diff(ny) # Detection of straight segments idx = dnx != 0 idy = dny == 0 vals = (idx * idy) + (~idx * ~idy) figure() subplot(131) plot(nx, ny) scatter(nx[vals], ny[vals], color='r') # Looping detection maxs_ny = ni.peaks(ny) mins_ny = ni.peaks(-ny) maxs_nx = ni.peaks(nx) mins_nx = ni.peaks(-nx)
def emg_smoother(emg, window): return list(smooth(np.abs(np.array(emg)), window_len=window))
fs = f[Macs].attrs["sampling rate"] * 1.0 data1 = data_group["channel_4"][:, 0] data2 = data_group["channel_5"][:, 0] return data1, data2, fs bvp1, bvp2, fs = openH5("Exercise11/ctrx_0007803B4686_2017-04-11_12-16-22.h5") plt.plot(bvp2) plt.show() bvp2 = bvp2[:100000] bvp22 = filter.lowpass(bvp2, f=20, fs=fs, order=2) bvp22 = bvp22 - smooth(bvp22, window_len=2000) rising, falling, r, f, pks = R_F_Amp(bvp22, 0.3 * max(bvp22), 0.2 * max(bvp22)) # Quantization of the derivative ds1 = np.diff(bvp22) ds1 = np.clip(np.around(ds1 * 2), -1, 1).astype(int) ds1 = np.insert(ds1, 0, 0) x = np.empty(len(ds1), dtype='str') x[find(ds1 == -1)] = '-' x[find(ds1 == 0)] = '_' x[find(ds1 == 1)] = '+' # ds1 = medfilt(ds1, win_size) bvp22 = (bvp22 - mean(bvp22)) / max(bvp22) plt.plot(bvp22) plt.plot(rising) plt.show()
def EMG_diferential(data, patient): fig1 = plt.figure(1) fig2 = plt.figure(2) fig3 = plt.figure(3) fig4 = plt.figure(4) maxs = [0, 0, 0, 0] max_range = [0, 0] for i in range(0, len(data)): Acc_ = data[i].get_variable('open_signals_data_filtered')[5:8] EMGs = data[i].get_variable('open_signals_data_filtered')[1:5] wii = data[i].get_variable("wii_data") Acc = [] Acc.append(Acc_[2][100:-100]) Acc.append(Acc_[0][100:-100]) Acc[0] = list(np.array(Acc[0])) Acc[1] = list(np.array(Acc[1])) FR = list(smooth(np.abs(np.array(EMGs[0])), 500))[100:-100] FL = list(smooth(np.abs(np.array(EMGs[1])), 500))[100:-100] BL = list(smooth(np.abs(np.array(EMGs[2])), 500))[100:-100] BR = list(smooth(np.abs(np.array(EMGs[3])), 500))[100:-100] if max(FR)>maxs[0]: maxs[0] = max(FR) if max(FL) > maxs[1]: maxs[1] = max(FL) if max(BL) > maxs[2]: maxs[2] = max(BL) if max(BR) > maxs[3]: maxs[3] = max(BR) [FR, FL, BL, BR] = normalize([FR, FL, BL, BR]) Front = (np.array(FR) + np.array(FL)) Back = (np.array(BL) + np.array(BR)) Left = (np.array(FL) + np.array(BL)) Right = (np.array(FR) + np.array(BR)) x = list(np.array(Left)/2.0 - np.array(Right)/2.0) y = list(np.array(Front)/2.0 - np.array(Back)/2.0) [x, y] = normalize([x, y], zero_out=True) Acc = normalize(Acc) Cops=[] Cops.append(wii[6][0][10:-10]) Cops.append(wii[6][1][10:-10]) if (max(Cops[0]) - min(Cops[0])) > max_range[0]: max_range[0] = (max(Cops[0]) - min(Cops[0])) if (max(Cops[1]) - min(Cops[1])) > max_range[1]: max_range[1] = (max(Cops[1]) - min(Cops[1])) Cops = normalize(Cops) #SEPARAR plt.figure(1) t1 = np.linspace(0, len(Acc[1]), len(Cops[0])) plt.subplot(3, 4, i + 1) l1, = plt.plot(x, y, color='red') l2, = plt.plot(Cops[0], Cops[1], color='blue') l3, = plt.plot(Acc[0], Acc[1], color='black', alpha=0.2) plt.xlabel("Norm x") plt.ylabel("Norm y") plt.subplot(3, 4, i + 5) plt.plot(x, color='red') plt.plot(t1, Cops[0], color='blue') l4, = plt.plot(np.array(Left)/2.0, color='black', alpha=0.7) l5, = plt.plot(np.array(Right)/2.0, color='green', alpha=0.7) plt.xlim([0, len(Acc[0])]) plt.xlabel("Samples") plt.ylabel("Norm x") plt.title("X Axis") plt.subplot(3, 4, i + 9) plt.plot(y, color='red') plt.plot(t1, Cops[1], color='blue') l6, = plt.plot(np.array(Front)/2.0, color='black', alpha=0.7) l7, = plt.plot(np.array(Back)/2.0, color='green', alpha=0.7) plt.xlim([0, len(Acc[0])]) plt.xlabel("Samples") plt.ylabel("Norm y") plt.title("Y Axis") Acc[0] = list(smooth(np.array(Acc[0]), 1000))[100:-100] Acc[1] = list(smooth(np.array(Acc[1]), 1000))[100:-100] Cops[0] = list(smooth(np.array(Cops[0]), 100))[10:-10] Cops[1] = list(smooth(np.array(Cops[1]), 100))[10:-10] x = list(smooth(np.array(x), 1000))[100:-100] y = list(smooth(np.array(y), 1000))[100:-100] Left = list(smooth(np.array(Left), 1000))[100:-100] Right = list(smooth(np.array(Right), 1000))[100:-100] Front = list(smooth(np.array(Front), 1000))[100:-100] Back = list(smooth(np.array(Back), 1000))[100:-100] plt.figure(2) t1 = np.linspace(0, len(Acc[1]), len(Cops[0])) plt.subplot(3, 4, i + 1) l1, = plt.plot(x, y, color='red') l2, = plt.plot(Cops[0], Cops[1], color='blue') plt.xlabel("Norm x") plt.ylabel("Norm y") plt.subplot(3, 4, i + 5) plt.plot(x, color='red') plt.plot(t1, Cops[0], color='blue') plt.plot(np.array(Left)/2.0, color='black', alpha=0.7) plt.plot(np.array(Right)/2.0, color='green', alpha=0.7) plt.xlabel("Samples") plt.ylabel("Norm x") plt.title("X Axis") plt.subplot(3, 4, i + 9) plt.plot(y, color='red') plt.plot(t1, Cops[1], color='blue') plt.plot(np.array(Front)/2.0, color='black', alpha=0.7) plt.plot(np.array(Back)/2.0, color='green', alpha=0.7) plt.xlabel("Samples") plt.ylabel("Norm y") plt.title("Y Axis") for i in range(0, len(data)): Acc_ = data[i].get_variable('open_signals_data_filtered')[5:8] EMGs = data[i].get_variable('open_signals_data_filtered')[1:5] wii = data[i].get_variable("wii_data") Acc = [] Acc.append(Acc_[2][100:-100]) Acc.append(Acc_[0][100:-100]) Acc[0] = list(np.array(Acc[0])) Acc[1] = list(np.array(Acc[1])) FR = list(smooth(np.abs(np.array(EMGs[0])), 500))[100:-100] FL = list(smooth(np.abs(np.array(EMGs[1])), 500))[100:-100] BL = list(smooth(np.abs(np.array(EMGs[2])), 500))[100:-100] BR = list(smooth(np.abs(np.array(EMGs[3])), 500))[100:-100] [FR, FL, BL, BR] = normalize([FR, FL, BL, BR], currentMax=False, globalMax=maxs) Front = (np.array(FR) + np.array(FL)) Back = (np.array(BL) + np.array(BR)) Left = (np.array(FL) + np.array(BL)) Right = (np.array(FR) + np.array(BR)) x = list(np.array(Left)/2.0 - np.array(Right)/2.0) y = list(np.array(Front)/2.0 - np.array(Back)/2.0) [x, y] = normalize([x, y], zero_out=True) Acc = normalize(Acc) Cops = [] Cops.append(wii[6][0][10:-10]) Cops.append(wii[6][1][10:-10]) Cops = normalize(Cops, cop=True, cop_range=max_range, currentMax=False) # SEPARAR plt.figure(3) t1 = np.linspace(0, len(Acc[1]), len(Cops[0])) plt.subplot(3, 4, i + 1) l1, = plt.plot(x, y, color='red') l2, = plt.plot(Cops[0], Cops[1], color='blue') l3, = plt.plot(Acc[0], Acc[1], color='black', alpha=0.2) plt.xlabel("Norm x") plt.ylabel("Norm y") plt.subplot(3, 4, i + 5) plt.plot(x, color='red') plt.plot(t1, Cops[0], color='blue') l4, = plt.plot(np.array(Left)/2.0, color='black', alpha=0.7) l5, = plt.plot(np.array(Right)/2.0, color='green', alpha=0.7) plt.xlim([0, len(Acc[0])]) plt.xlabel("Samples") plt.ylabel("Norm x") plt.title("X Axis") plt.subplot(3, 4, i + 9) plt.plot(y, color='red') plt.plot(t1, Cops[1], color='blue') l6, = plt.plot(np.array(Front)/2.0, color='black', alpha=0.7) l7, = plt.plot(np.array(Back)/2.0, color='green', alpha=0.7) plt.xlim([0, len(Acc[0])]) plt.xlabel("Samples") plt.ylabel("Norm y") plt.title("Y Axis") Acc[0] = list(smooth(np.array(Acc[0]), 1000))[100:-100] Acc[1] = list(smooth(np.array(Acc[1]), 1000))[100:-100] Cops[0] = list(smooth(np.array(Cops[0]), 100))[10:-10] Cops[1] = list(smooth(np.array(Cops[1]), 100))[10:-10] x = list(smooth(np.array(x), 1000))[100:-100] y = list(smooth(np.array(y), 1000))[100:-100] Left = list(smooth(np.array(Left), 1000))[100:-100] Right = list(smooth(np.array(Right), 1000))[100:-100] Front = list(smooth(np.array(Front), 1000))[100:-100] Back = list(smooth(np.array(Back), 1000))[100:-100] plt.figure(4) t1 = np.linspace(0, len(Acc[1]), len(Cops[0])) plt.subplot(3, 4, i + 1) l1, = plt.plot(x, y, color='red') l2, = plt.plot(Cops[0], Cops[1], color='blue') plt.xlabel("Norm x") plt.ylabel("Norm y") plt.subplot(3, 4, i + 5) plt.plot(x, color='red') plt.plot(t1, Cops[0], color='blue') plt.plot(np.array(Left)/2.0, color='black', alpha=0.7) plt.plot(np.array(Right)/2.0, color='green', alpha=0.7) plt.xlabel("Samples") plt.ylabel("Norm x") plt.title("X Axis") plt.subplot(3, 4, i + 9) plt.plot(y, color='red') plt.plot(t1, Cops[1], color='blue') plt.plot(np.array(Front)/2.0, color='black', alpha=0.7) plt.plot(np.array(Back)/2.0, color='green', alpha=0.7) plt.xlabel("Samples") plt.ylabel("Norm y") plt.title("Y Axis") plt.figure(1) fig1.suptitle(patient, fontsize=20) plt.subplots_adjust(hspace=0.45, wspace=0.24, top=0.94, bottom=0.11, left=0.04, right=0.98) mng = plt.get_current_fig_manager() mng.window.showMaximized() fig1.legend((l1,l2, l3), ('Emg', 'Cop', 'Acc'), bbox_to_anchor=[0.5, 0.06], loc='center', ncol=3, labelspacing=0., fontsize=14) fig1.legend([l6, l7], ['Front', 'Back'], bbox_to_anchor=[0.5, 0.03], loc='center', ncol=2, labelspacing=0., fontsize=14) fig1.legend([l4, l5], ['Left', 'Right'], bbox_to_anchor=[0.5, 0.365], loc='center', ncol=2, labelspacing=0., fontsize=14) plt.figure(2) fig2.suptitle(patient, fontsize=20) fig2.legend((l1, l6, l2, l7), ('Emg', 'Front', 'Cop', 'Back'), loc='lower center', ncol=2, labelspacing=1., fontsize=14) fig2.legend([l4, l5], ['Left', 'Right'], bbox_to_anchor=[0.5, 0.365], loc='center', ncol=2, labelspacing=0., fontsize=14) plt.subplots_adjust(hspace=0.45, wspace=0.24, top=0.94, bottom=0.11, left=0.04, right=0.98) mng = plt.get_current_fig_manager() mng.window.showMaximized() plt.figure(3) fig3.suptitle(patient, fontsize=20) plt.subplots_adjust(hspace=0.45, wspace=0.24, top=0.94, bottom=0.11, left=0.04, right=0.98) mng = plt.get_current_fig_manager() mng.window.showMaximized() fig3.legend((l1, l2, l3), ('Emg', 'Cop', 'Acc'), bbox_to_anchor=[0.5, 0.06], loc='center', ncol=3, labelspacing=0., fontsize=14) fig3.legend([l6, l7], ['Front', 'Back'], bbox_to_anchor=[0.5, 0.03], loc='center', ncol=2, labelspacing=0., fontsize=14) fig3.legend([l4, l5], ['Left', 'Right'], bbox_to_anchor=[0.5, 0.365], loc='center', ncol=2, labelspacing=0., fontsize=14) plt.figure(4) fig4.suptitle(patient, fontsize=20) fig4.legend((l1, l6, l2, l7), ('Emg', 'Front', 'Cop', 'Back'), loc='lower center', ncol=2, labelspacing=1., fontsize=14) fig4.legend([l4, l5], ['Left', 'Right'], bbox_to_anchor=[0.5, 0.365], loc='center', ncol=2, labelspacing=0., fontsize=14) plt.subplots_adjust(hspace=0.45, wspace=0.24, top=0.94, bottom=0.11, left=0.04, right=0.98) mng = plt.get_current_fig_manager() mng.window.showMaximized() if not os.path.exists('../WiiBoard/DataProcessor/Images/Cops_gen_4/' + patient+'/'): os.makedirs('../WiiBoard/DataProcessor/Images/Cops_gen_4/'+ patient+'/') fig1.set_size_inches(20, 11.25) plt.figure(1) plt.savefig('../WiiBoard/DataProcessor/Images/Cops_gen_4/' + patient +'/cops1.png', dpi=300) fig2.set_size_inches(20, 11.25) plt.figure(2) plt.savefig('../WiiBoard/DataProcessor/Images/Cops_gen_4/' + patient + '/cops2.png', dpi=300) fig3.set_size_inches(20, 11.25) plt.figure(3) plt.savefig('../WiiBoard/DataProcessor/Images/Cops_gen_4/' + patient + '/cops3.png', dpi=300) fig4.set_size_inches(20, 11.25) plt.figure(4) plt.savefig('../WiiBoard/DataProcessor/Images/Cops_gen_4/' + patient + '/cops4.png', dpi=300) plt.show()
def ACC_Pre_P(acc, fs): # acc = filter.lowpass(acc, 15, 4, fs) acc = smooth(acc, window_len=fs // 10) return acc
def ReportClustering(Signal, NoiseSignal, fs, time, fileName, fileDir, clusters): #MainDir = "IWANTOFINDNOISE/TestSignals" # SignalKind = os.listdir(MainDir) # # for SignalFolder in SignalKind: # # SignalDir = MainDir + '/' + SignalFolder # SignalData = os.listdir(SignalDir) # # for signal in SignalData: # # if ".txt" in signal: # #---------------------------------------------------------------------------------------------------------------------- # Open Signal (and Pre-Process Data ???) #---------------------------------------------------------------------------------------------------------------------- #When I will need to take a signal: # fileName = "ecg1" # fileDir = "Signals2/ECG1/" # Signal = np.loadtxt(fileDir + fileName + ".txt") # NoiseSignal = np.loadtxt(fileDir + "Noise2790.txt") # fs = 100 # time = np.linspace(0, len(Signal)/fs, len(Signal)) # clusters = 4 print("Loading file " + fileName) #fileTest = "TestSignals/TestFiles/ecg1.txt" if(os.path.isdir(fileDir + "/" + "Report_Color_" + fileName) is False): os.makedirs(fileDir + "/" + "Report_Color_" + fileName) print("Creating Report File") pp, ReportTxt = pdf_report_creator(fileDir, "Report_Color_" + fileName) win = 512 # for win in windows: #NoiseTest = "TestSignals/TestFiles/Noise2790.txt" # open signal #signal = np.loadtxt(file) signal = Signal signalNoise = NoiseSignal osignal = Signal signal = signal - np.mean(signal) signal = signal/max(signal) #---------------------------------------------------------------------------------------------------------------------- # Extract Features #---------------------------------------------------------------------------------------------------------------------- print("Extracting features...") #1 - Std Window signalSTD = WindowStat(signal, fs=fs, statTool='std', window_len=(win*fs)/256) print("...feature 1 - STD") #2 - ZCR signalZCR64 = WindowStat(signal, fs=fs, statTool='zcr', window_len=(win*fs)/512) print("...feature 2 - ZCR") #3 - Sum signalSum64 = WindowStat(signal, fs=fs, statTool='sum', window_len=(win*fs)/256) signalSum128 = WindowStat(signal, fs=fs, statTool='sum', window_len=(win*fs)/100) print("...feature 3 - Sum") #4 - Number of Peaks above STD signalPKS = WindowStat(signal, fs=fs, statTool='findPks', window_len=(win*fs)/128) signalPKS2 = WindowStat(signal, fs=fs, statTool='findPks', window_len=(64 * fs) / 100) print("...feature 4 - Pks") #5 - Amplitude Difference between successive PKS signalADF32 = WindowStat(signal, fs=fs, statTool='AmpDiff', window_len=(win*fs)/128) # signalADF128 = WindowStat(signal, fs=fs, statTool='AmpDiff', window_len=(2*win*fs)/100) print("...feature 5 - AmpDif") #6 - Medium Frequency feature signalMF = WindowStat(signal, fs=fs, statTool='MF', window_len=(32*fs)/128) print("...feature 6 - MF") #7 - Frequency Spectrum over time Pxx, freqs, bins, im = SpectralComponents(osignal, fs, NFFT=129, show=False) #miss axes #Interp - to have the same number of points as the original signal signalPxx = np.interp(np.linspace(0, len(Pxx), len(signal)), np.linspace(0, len(Pxx), len(Pxx)), Pxx) signalPxx -= np.mean(signalPxx) #8 - Find Peak Distance dpks = findPeakDistance(signal, 2*np.std(signal), 0) #9 - Smooth signal smSignal = smooth(abs(signal), window_len=int(fs/2)) smSignal = smSignal/max(smSignal) smSignal2 = smooth(abs(signal), window_len=int(fs)) smSignal2 = smSignal2/ max(smSignal2) # # #8 - fractal analysis # SignalFract = WindowStat(signal, fs=fs, statTool='fractal', window_len=10) # print(SignalFract) print("Done with Features extraction...creating Matrix") # Create Matrix of Features------Standard features FeatureNames = ["Standard Deviation", 'Sum', "ZCR"] FeatureMatrix = np.array( [signalSTD, signalSum64, signalZCR64]).transpose() plotLinearData(time, FeatureMatrix, signal, FeatureNames, pp) print("Starting Clustering") X, y_pred, XPCA, params = MultiDimensionalClusteringAGG(FeatureMatrix, n_clusters=clusters, Linkage='ward', Affinity='euclidean') # ---------------------------------------------------------------------------------------------------------------------- # Create Classification Array (1- Noise) (0 - Non-Noise) # ---------------------------------------------------------------------------------------------------------------------- # find signal indexes - in this case i will assume that the signal is the majority of the signal print("Creating Predicted Array...") Indexiser = [] for i in range(0, clusters): s = len(y_pred[np.where(y_pred == i)[0]].tolist()) #s = np.std(signal[np.where(y_pred == i)[0]]) Indexiser.append(s) SigIndex = Indexiser.index(max(Indexiser)) Prediction = np.ones(np.size(y_pred.tolist())) Prediction[np.where(y_pred == SigIndex)[0]] = 0 print("Calculating Confusion matrix...") ResultEvents, Sens, Spec, CorrectPeriod, ErrorPeriod, RatioCorrectWrong, CorrectArray, ErrorArray, TP, TN, FP, FN = GetResults( signalNoise, Prediction) sizes = [TP, TN, FP, FN] # ---------------------------------------------------------------------------------------------------------------------- # Plot Things # ---------------------------------------------------------------------------------------------------------------------- Clusterfig = plotClusters(y_pred, signal, time, XPCA, clusters, pp) ClassFig = plotDistanceMetric(time, signal, signalNoise, Prediction, Spec, Sens, CorrectPeriod, sizes, pp) FeatureNames = ["Standard Deviation" ,"ZCR", "ADF128", "PKS"] FeatureMatrix = np.array( [signalSTD, signalZCR64, signalADF32, signalPKS]).transpose() clusters = 5 plotLinearData(time, FeatureMatrix, signal, FeatureNames, pp) print("Starting Clustering") X, y_pred, XPCA, params = MultiDimensionalClusteringAGG(FeatureMatrix, n_clusters=clusters, Linkage='ward', Affinity='euclidean') print("Plotting...") Clusterfig = plotClusters(y_pred, signal, time, XPCA, clusters, pp) # ---------------------------------------------------------------------------------------------------------------------- # Create Classification Array (1- Noise) (0 - Non-Noise) # ---------------------------------------------------------------------------------------------------------------------- # find signal indexes - in this case i will assume that the signal is the majority of the signal print("Creating Predicted Array...") Indexiser = [] for i in range(0, clusters): s = len(y_pred[np.where(y_pred == i)[0]].tolist()) # s = np.std(signal[np.where(y_pred == i)[0]]) Indexiser.append(s) SigIndex = Indexiser.index(max(Indexiser)) Prediction = np.ones(np.size(y_pred.tolist())) Prediction[np.where(y_pred == SigIndex)[0]] = 0 print("Calculating Confusion matrix...") ResultEvents, Sens, Spec, CorrectPeriod, ErrorPeriod, RatioCorrectWrong, CorrectArray, ErrorArray, TP, TN, FP, FN = GetResults( signalNoise, Prediction) sizes = [TP, TN, FP, FN] # ---------------------------------------------------------------------------------------------------------------------- # Plot Things # ---------------------------------------------------------------------------------------------------------------------- ClassFig = plotDistanceMetric(time, signal, signalNoise, Prediction, Spec, Sens, CorrectPeriod, sizes, pp) #Round4 clusters=5 FeatureNamesG = ["Standard Deviation", "std", "ADF128"] FeatureMatrixG = np.array( [signalSTD, signalSTD, signalADF32]).transpose() plotLinearData(time, FeatureMatrixG, signal, FeatureNamesG, pp) # ---------------------------------------------------------------------------------------------------------------------- # Execute Clustering Techniques # X, y_pred, XPCA, params, Z, xx, yy = MultiDimensionalClusteringKmeans(FeatureMatrix, time, signal, show=False, n_clusters=n_clusters) print("Starting Clustering") X, y_pred, XPCA, params = MultiDimensionalClusteringAGG(FeatureMatrixG, n_clusters=clusters, Linkage='ward', Affinity='euclidean') # X, y_pred, XPCA, params = MultiDimensionalClusteringKmeans(FeatureMatrix, n_clusters=clusters) # ---------------------------------------------------------------------------------------------------------------------- # Create Classification Array (1- Noise) (0 - Non-Noise) # ---------------------------------------------------------------------------------------------------------------------- # find signal indexes - in this case i will assume that the signal is the majority of the signal print("Creating Predicted Array...") Indexiser = [] for i in range(0, clusters): s = len(y_pred[np.where(y_pred == i)[0]].tolist()) #s = np.std(signal[np.where(y_pred == i)[0]]) Indexiser.append(s) SigIndex = Indexiser.index(max(Indexiser)) Prediction = np.ones(np.size(y_pred.tolist())) Prediction[np.where(y_pred == SigIndex)[0]] = 0 print("Calculating Confusion matrix...") ResultEvents, Sens, Spec, CorrectPeriod, ErrorPeriod, RatioCorrectWrong, CorrectArray, ErrorArray, TP, TN, FP, FN = GetResults( signalNoise, Prediction) sizes = [TP, TN, FP, FN] # ---------------------------------------------------------------------------------------------------------------------- # Plot Things # ---------------------------------------------------------------------------------------------------------------------- print("Plotting...") Clusterfig = plotClustersBW(y_pred, signal, time, XPCA, clusters, pp) ClassFig = plotDistanceMetric(time, signal, signalNoise, Prediction, Spec, Sens, CorrectPeriod, sizes, pp) clusters = 5 FeatureNamesG = ["Standard Deviation", "std", "ZCR"] FeatureMatrixG = np.array( [signalSTD, signalSTD, signalZCR64]).transpose() plotLinearData(time, FeatureMatrixG, signal, FeatureNamesG, pp) # ---------------------------------------------------------------------------------------------------------------------- # Execute Clustering Techniques # X, y_pred, XPCA, params, Z, xx, yy = MultiDimensionalClusteringKmeans(FeatureMatrix, time, signal, show=False, n_clusters=n_clusters) print("Starting Clustering") X, y_pred, XPCA, params = MultiDimensionalClusteringAGG(FeatureMatrixG, n_clusters=clusters, Linkage='ward', Affinity='euclidean') # X, y_pred, XPCA, params = MultiDimensionalClusteringKmeans(FeatureMatrix, n_clusters=clusters) # ---------------------------------------------------------------------------------------------------------------------- # Create Classification Array (1- Noise) (0 - Non-Noise) # ---------------------------------------------------------------------------------------------------------------------- # find signal indexes - in this case i will assume that the signal is the majority of the signal print("Creating Predicted Array...") Indexiser = [] for i in range(0, clusters): s = len(y_pred[np.where(y_pred == i)[0]].tolist()) # s = np.std(signal[np.where(y_pred == i)[0]]) Indexiser.append(s) SigIndex = Indexiser.index(max(Indexiser)) Prediction = np.ones(np.size(y_pred.tolist())) Prediction[np.where(y_pred == SigIndex)[0]] = 0 print("Calculating Confusion matrix...") ResultEvents, Sens, Spec, CorrectPeriod, ErrorPeriod, RatioCorrectWrong, CorrectArray, ErrorArray, TP, TN, FP, FN = GetResults( signalNoise, Prediction) sizes = [TP, TN, FP, FN] # ---------------------------------------------------------------------------------------------------------------------- # Plot Things # ---------------------------------------------------------------------------------------------------------------------- print("Plotting...") Clusterfig = plotClusters(y_pred, signal, time, XPCA, clusters, pp) ClassFig = plotDistanceMetric(time, signal, signalNoise, Prediction, Spec, Sens, CorrectPeriod, sizes, pp) #---------------------------------------------------------------------------------------------------------------------- # Save plots in PDF and relevant values on text file #---------------------------------------------------------------------------------------------------------------------- SaveReport(fileName, sizes, Sens, Spec, CorrectPeriod, ReportTxt) pdf_text_closer(ReportTxt, pp)
window_size = 256 signal_directory = 'NOISE_BIOMETRIC_ECGs_[{0}.{1}]'.format(batch_size, window_size) # get noise from signals: signals_without_noise = [] signals_noise = [] SNR = [] full_paths = get_fantasia_full_paths(db.fantasia_ecgs[0].directory, list(range(1,20))) for file_path in full_paths: signal = sio.loadmat(file_path)['val'][0] signal = (signal - np.mean(signal))/np.std(signal) signals_noise.append(np.array(signal) + np.random.normal(0, 0.8, len(signal))) signal = signals_noise[-1] signals_without_noise.append(signal) smoothed_signal = smooth(signal) noise = smoothed_signal - signals_without_noise[-1] print(np.std(signal)) SMOOTH_AMPLITUDE = np.max(smoothed_signal) - np.min(smoothed_signal) NOISE_AMPLITUDE = np.mean(np.abs(noise))*2 SNR.append(10 * np.log10(SMOOTH_AMPLITUDE/NOISE_AMPLITUDE)) # print(SNR[-1]*10, "-" + str(SMOOTH_AMPLITUDE/NOISE_AMPLITUDE)) # print(np.mean(SNR * 10)) plt.plot(noise[:1000],'r',smoothed_signal[:1000],'k')#, signals_noise[-1][:1000], 'b') plt.show() print(np.mean(np.array(SNR))) # plt.show()