def vmd_extract(region_i, var_min=0.01, alpha=2000, tau=0.0, K=5, DC=0, init=1, tol=1e-7):
    r_sum = list(map(lambda x:np.sum(x),region_i))
    r_recon_all, _, _ = VMD(r_sum, alpha, tau, K, DC, init, tol)
    r_recon = list(r_recon_all[0,:])
    for mode in range(1,r_recon_all.shape[0]):
        if np.var(r_recon_all[mode,:])/np.sum(np.var(r_recon_all,axis=1)) > var_min:
            r_recon += r_recon_all[mode,:]
    return r_recon
예제 #2
0
def avaliateVMD(df):
    mape = []
    for i in range(2, 25):
        u_temp, u_hat, omega = VMD(df[LABEL_COLUMN][0:10000], 2000, 0., i, 0,
                                   1, 1e-7)
        mape_temp = 0
        for t in range(u_temp.shape[1]):
            if df[LABEL_COLUMN][t] < 1:
                continue
            sum1 = 0
            for k in range(u_temp.shape[0]):
                sum1 += u_temp[k][t]
            mape_temp += abs(
                (df[LABEL_COLUMN][t] - sum1) / df[LABEL_COLUMN][t])
        mape.append(mape_temp / len(u_temp[0]))
        print(
            str(i) + " wavelets, MAPE: " + str(round(mape[i - 2] * 100, 2)) +
            "%")
예제 #3
0
    #fig EWT
    ewt, _, _ = ewtpy.EWT1D(f[kk],
                            N=Nmodes[3],
                            log=0,
                            detect="locmax",
                            completion=0,
                            reg=FFTreg,
                            lengthFilter=FFTregLen,
                            sigmaFilter=gaussSigma)
    plotModes(ewt, Nmodes[3], tlimits=paramsData[dBase]["tlimits"])
    #plt.suptitle('EWT, %s signal'%list(f.keys())[ind])
    plt.savefig("DecEWT%d_%s_%s.pdf" % (Nmodes[3], dBase, kk))
    #fig VMD
    DC = np.mean(f[kk])  #          % no DC part imposed
    vmd, _, _ = VMD(f[kk], alpha, tau, Nmodes[4], DC, init, tol)
    plotModes(vmd.T, Nmodes[4], tlimits=paramsData[dBase]["tlimits"])
    #plt.suptitle('VMD, %s signal'%list(f.keys())[ind])
    plt.savefig("DecVMD%d_%s_%s.pdf" % (Nmodes[4], dBase, kk))
#
for fi in f:
    plt.figure(figsize=(5.4, 2))
    ax = plt.subplot(111)
    ax.plot(t, f[fi], 'k')
    ax.autoscale(enable=True, axis='x', tight=True)
    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)
    ax.spines['bottom'].set_visible(False)
    ax.get_xaxis().set_ticks([])
    plt.xlim(paramsData[dBase]["tlimits"])
    plt.locator_params(axis='y', nbins=4)
예제 #4
0
파일: VMD_tests.py 프로젝트: zhmxtr/vmdpy
# composite signal, including noise

f = v_1 + v_2 + v_3 + 0.1 * np.random.randn(v_1.size)
f_hat = np.fft.fftshift((np.fft.fft(f)))

# some sample parameters for VMD
alpha = 2000  # moderate bandwidth constraint
tau = 0.  # noise-tolerance (no strict fidelity enforcement)
K = 3  # 3 modes
DC = 0  # no DC part imposed
init = 1  # initialize omegas uniformly
tol = 1e-7

# Run actual VMD code

u, u_hat, omega = VMD(f, alpha, tau, K, DC, init, tol)

#%%
# Simple Visualization of decomposed modes

plt.figure()
plt.plot(u.T)
plt.title('Decomposed modes')

# For convenience here: Order omegas increasingly and reindex u/u_hat
sortIndex = np.argsort(omega[-1, :])
omega = omega[:, sortIndex]
u_hat = u_hat[:, sortIndex]
u = u[sortIndex, :]
linestyles = ['b', 'g', 'm', 'c', 'c', 'r', 'k']
예제 #5
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
예제 #6
0

dataset = pd.read_csv('Solcast_test_file.csv', usecols=['PeriodEnd', 'Ghi'])

dataset['PeriodEnd'] = pd.to_datetime(dataset['PeriodEnd'])
dataset.set_index('PeriodEnd')
dataset = dataset.sort_values(by='PeriodEnd')

# avaliateVMD(dataset)
order = 9
n = len(dataset)
len_train = int(n * 0.8)
train_original = dataset[LABEL_COLUMN][int(len_train * 0.92):len_train]
test_original = dataset[LABEL_COLUMN][len_train:int(1.05 * len_train)]

u_temp_train, u_hat_train, omega_train = VMD(train_original, 2000, 0., order,
                                             0, 1, 1e-7)
u_temp_test, u_hat_test, omega_test = VMD(
    dataset[LABEL_COLUMN][len_train:int(1.05 * len_train)], 2000, 0., order, 0,
    1, 1e-7)

split_train_x, split_train_y, split_train_baseline = SplitData(
    u_temp_train, dataset[LABEL_COLUMN][0:len_train])
split_test_x, split_test_y, split_test_baseline = SplitData(
    u_temp_test, dataset[LABEL_COLUMN][len_train:])

train_df = u_temp_train
test_df = u_temp_test

# ve max e min de cada um dos novos sinais
train_max = [element.max() for element in train_df]
train_min = [element.min() for element in train_df]