else: f = data #f = f - np.mean(f) N = 3 #number of supports detect = "locmax" #detection mode: locmax, locmaxmin, locmaxminf reg = 'none' #spectrum regularization - it is smoothed with an average (or gaussian) filter lengthFilter = 0 #length or average or gaussian filter sigmaFilter = 0 #sigma of gaussian filter Fs = 1 #sampling frequency, in Hz (if unknown, set 1) ewt, mfb ,boundaries = ewtpy.EWT1D(f, N = N, log = 0, detect = detect, completion = 0, reg = reg, lengthFilter = lengthFilter, sigmaFilter = sigmaFilter) #plot original signal and decomposed modes plt.figure() plt.subplot(211) plt.plot(f) plt.title('Original signal %s'%signal) plt.subplot(212) plt.plot(ewt) plt.title('EWT modes') #%% show boundaries ff = np.fft.fft(f)
import numpy as np import matplotlib.pyplot as plt import ewtpy T = 1000 t = np.arange(1,T+1)/T f = np.cos(2*np.pi*0.8*t) + 2*np.cos(2*np.pi*10*t)+0.8*np.cos(2*np.pi*100*t) ewt, mfb ,boundaries = ewtpy.EWT1D(f, N = 3) plt.plot(f) plt.plot(ewt) plt.show()
#%% Fig 4: EWT spectrum segmentation Fs = 173.61 #EEG sampling frequency #EWT parameters FFTreg = 'gaussian' FFTregLen = 25 gaussSigma = 5 plt.rcParams.update({'font.size': 8}) f1 = np.loadtxt("BonnDataset/data/S/S013.txt") b, a = signal.butter(4, 40 / (0.5 * Fs), btype='low', analog=False) f1 = signal.filtfilt(b, a, f1) f1 = f1 - np.mean(f1) ewt, mfb, boundaries = ewtpy.EWT1D(f1, N=5, log=0, detect="locmax", completion=0, reg=FFTreg, lengthFilter=FFTregLen, sigmaFilter=gaussSigma) ff = np.fft.fft(f1) ff = abs(ff[:ff.size // 2]) #one-sided magnitude regFilter = np.zeros(FFTregLen) regFilter[regFilter.size // 2] = 1 #prefer odd filter lengths - otherwise the gaussian is skewed presig = np.convolve(ff, gaussian_filter(regFilter, gaussSigma), mode='same') fftFreq = np.fft.fftfreq(f1.size, 1 / Fs) fftFreq = fftFreq[:fftFreq.size // 2] plt.figure(figsize=(3.54, 2.5)) ax = plt.subplot(111) #ax.plot(fftFreq,ff) ax.plot(fftFreq, presig, 'k')
def fun_loadExFeats(dSet, ri, idx, item, Fs, LPcutoff, Nmodes, FFTregLen=25, gaussSigma=5, FFTreg='gaussian'): #load eeg featNames = ["Group", "decTime"] featsTuple = { "EMD": 0, "EEMD": 0, "CEEMDAN": 0, "EWT": 0, "VMD": 0, "Orig": 0 } if dSet == "NSC_ND": fLoad = loadmat("%s/data/%s/%s%d" % (dSet, item, item, ri + 1)) f = fLoad[item][:, 0] ltemp = int(np.ceil(f.size / 2)) #to behave the same as matlab's round fMirr = np.append(np.flip(f[0:ltemp - 1], axis=0), f) fMirr = np.append(fMirr, np.flip(f[-ltemp - 1:-1], axis=0)) f = np.copy(fMirr) if dSet == "BonnDataset": f = np.loadtxt("%s/data/%s/%s%.3d.txt" % (dSet, item, item, ri + 1)) #preprocessing - LP filter and remove DC f = f - np.mean(f) b, a = signal.butter(4, LPcutoff / (0.5 * Fs), btype='low', analog=False) fp = signal.filtfilt(b, a, f) #% EMD features tic = time.time() emd = EMD() emd.MAX_ITERATION = 2000 IMFs = emd.emd(fp, max_imf=Nmodes) toc = time.time() featsTuple["EMD"] = toc - tic #execution time (decomposition) if Nmodes != IMFs.shape[0] - 1: print("\nCheck number of EMD modes: %s%.3d" % (item, ri + 1)) for mi in range(IMFs.shape[0]): featOut, labelTemp = featExtract(IMFs[mi, :], Fs, welchWin=1024) featsTuple["EMD"] = np.append(featsTuple["EMD"], featOut) #write feature name header if ri == 0 and idx == 0: for ii in labelTemp: featNames = np.append(featNames, "%s%d" % (ii, mi)) if IMFs.shape[0] < Nmodes + 1: featsTuple["EMD"] = np.append( featsTuple["EMD"], np.zeros(Nfeats * (Nmodes + 1 - IMFs.shape[0]))) #% EEMD - Ensemble Empirical Mode Decomposition tic = time.time() if __name__ == "__main__": eemd = EEMD(trials=200) eemd.MAX_ITERATION = 2000 eIMFs = eemd(fp, max_imf=Nmodes) toc = time.time() featsTuple["EEMD"] = toc - tic #execution time (decomposition ) if Nmodes != eIMFs.shape[0] - 1: print("\nCheck number of EEMD modes: %s%.3d" % (item, ri + 1)) #for each mode, extract features for mi in range(eIMFs.shape[0]): featOut, labelTemp = featExtract(eIMFs[mi, :], Fs, welchWin=1024) featsTuple["EEMD"] = np.append(featsTuple["EEMD"], featOut) if eIMFs.shape[0] < Nmodes + 1: featsTuple["EEMD"] = np.append( featsTuple["EEMD"], np.zeros(Nfeats * (Nmodes + 1 - eIMFs.shape[0]))) #% CEEMDAN - Complete Ensemble Empirical Mode Decomposition with Adaptive Noise tic = time.time() if __name__ == "__main__": ceemdan = CEEMDAN() ceIMFs = ceemdan(fp, max_imf=Nmodes) toc = time.time() featsTuple["CEEMDAN"] = toc - tic #execution time (decomposition ) if Nmodes != ceIMFs.shape[0] - 1: print("\nCheck number of CEEMDAN modes: %s%.3d" % (item, ri + 1)) #for each mode, extract features for mi in range(ceIMFs.shape[0]): featOut, labelTemp = featExtract(ceIMFs[mi, :], Fs, welchWin=1024) featsTuple["CEEMDAN"] = np.append(featsTuple["CEEMDAN"], featOut) if ceIMFs.shape[0] < Nmodes + 1: featsTuple["CEEMDAN"] = np.append( featsTuple["CEEMDAN"], np.zeros(Nfeats * (Nmodes + 1 - ceIMFs.shape[0]))) #%EWT features tic = time.time() ewt, _, _ = ewtpy.EWT1D(fp, N=Nmodes, log=0, detect="locmax", completion=0, reg=FFTreg, lengthFilter=FFTregLen, sigmaFilter=gaussSigma) toc = time.time() featsTuple["EWT"] = toc - tic #execution time (decomposition ) if Nmodes != ewt.shape[1]: print("\nCheck number of EWT modes: %s%.3d" % (item, ri + 1)) #for each mode, extract features for mi in range(Nmodes): featOut, labelTemp = featExtract(ewt[:, mi], Fs, welchWin=1024) featsTuple["EWT"] = np.append(featsTuple["EWT"], featOut) #% VMD features DC = np.mean(fp) # no DC part imposed tic = time.time() vmd, _, _ = VMD(fp, alpha, tau, Nmodes, DC, init, tol) toc = time.time() featsTuple["VMD"] = toc - tic #execution time (decomposition ) if Nmodes != vmd.shape[0]: print("\nCheck number of VMD modes: %s%.3d" % (item, ri + 1)) #for each mode, extract features for mi in range(Nmodes): featOut, labelTemp = featExtract(vmd[mi, :], Fs, welchWin=1024) featsTuple["VMD"] = np.append(featsTuple["VMD"], featOut) #% Original non-decomposed signal features tic = time.time() featOut, labelTemp = featExtract(fp, Fs, welchWin=1024) toc = time.time() featsTuple["Orig"] = np.append(toc - tic, featOut) return item, featsTuple