Exemplo n.º 1
0
    def test_max_iteration_flag(self):
        S = np.random.random(200)
        emd = EMD()
        emd.MAX_ITERATION = 10
        emd.FIXE = 20

        imfs = emd.emd(S)

        # There's not much to test, except that it doesn't fail.
        # With low MAX_ITERATION value for random signal it's
        # guaranteed to have at least 2 imfs.
        self.assertTrue(imfs.shape[0]>1)
Exemplo n.º 2
0
plt.savefig("ExecTime_%s.pdf" % dBase)

#%% Figs 6,7: EEG segment decomposition
# Decompostion figure parameters
Nmodes = [3, 3, 3, 4, 3]  #(EMD,EEMD, CEEMDAN, EWT,VMD)
#Nmodes = [2,4,2,5,6] #(EMD,EEMD, CEEMDAN, EWT,VMD)
#VMD parameters
alpha = 2000  #      % moderate bandwidth constraint
tau = 0  #     % noise-tolerance (no strict fidelity enforcement)
init = 1  #  % initialize omegas uniformly
tol = 1e-7  #

for kk, ind in zip(f, range(len(f))):
    #fig EMD
    emd = EMD()
    emd.MAX_ITERATION = 2000
    IMFs = emd.emd(f[kk], max_imf=Nmodes[0])
    plotModes(np.flip(IMFs.T, axis=1),
              Nmodes[0] + 1,
              tlimits=paramsData[dBase]["tlimits"])
    #plt.suptitle('EMD, %s signal'%list(f.keys())[ind])
    plt.savefig("DecEMD%d_%s_%s.pdf" % (Nmodes[0], dBase, kk))

    #EEMD
    if __name__ == "__main__":
        eemd = EEMD()
        eemd.MAX_ITERATION = 2000
        eIMFs = eemd(f[kk], max_imf=Nmodes[1])
    plotModes(np.flip(eIMFs.T, axis=1),
              Nmodes[1] + 1,
              tlimits=paramsData[dBase]["tlimits"])
Exemplo n.º 3
0
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