Example #1
0
def emd_plot(data):
    emd = EMD(data)
    imfs = emd.decompose()
    # ipdb.set_trace()
    plot_imfs(data,imfs)
    plt.legend('EMD')
    return imfs
Example #2
0
def testHHT(c):
    #x,y = dataset.loadDataset("longdata.txt", filterCondition=True, filterType="DC")
    #x,y = dataset.sortDataset(x, y, length=1, classes=[c])
    N_imfs = 3

    #xt = x[0][0]
    #length = len(xt)
    #xt = np.array(xt)
    #t = np.arange(0, length/glb.fs, 1.0/glb.fs)

    t = np.linspace(0, 1, 1000)

    modes = np.sin(2 * np.pi * 5 * t) + np.sin(2 * np.pi * 10 * t) + np.sin(
        2 * np.pi * 20 * t) + np.sin(2 * np.pi * 40 * t)
    xt = modes + t

    #t = np.linspace(0, 1, 1000)
    #print(x[0][0])

    #decomposer = EMD(xt, n_imfs=N_imfs, fixe=0, threshold_1=0.05, threshold_2=0.5, alpha=0.05)
    iterations = 10
    decomposer = EMD(xt, fixe=iterations)
    imfs = decomposer.decompose()

    seconddecomposer = EMD(imfs[-1, :], fixe=iterations)
    secondimfs = seconddecomposer.decompose()

    thirddecomposer = EMD(secondimfs[-1, :], fixe=iterations)
    thirdimfs = thirddecomposer.decompose()
    print imfs.shape
    print secondimfs.shape
    #imfs = np.concatenate(imfs[0], secondimfs[0])
    imfs = np.vstack((imfs[0], secondimfs[0]))
    print imfs.shape
    imfs = np.vstack((imfs, thirdimfs[0]))
    print imfs.shape
    imfs = np.vstack((imfs, thirdimfs[1]))
    print imfs.shape
    #imfs = np.concatenate(
    plot_imfs(xt, imfs, t)  #doctest: +SKIP
Example #3
0
def testWithIMFPrediction():
    t = time.time()
    df = loadTestData('table_bac.csv')
    plt.plot(df[5].values[:])
    #plt.show()
    close_prices = df[5].values[:]
    print(len(close_prices))
    close_prices = minmax_scale(close_prices)
    emd = EMD(close_prices, maxiter=3000)
    imf = emd.decompose()
    plot_imfs(close_prices, imf)
    plt.plot(hilbert(imf, axis=0).T)
    plt.show()
    svrlist = []
    predYVals = np.matrix([])
    for i in range(7, 8):
        x, y = rollingWindows(imf[i], 500, 0, 2500)
        if i == 7:
            svr = svm.SVR(C=0.1, cache_size=4000)
        else:
            svr = svm.SVR(c=10, cache_size=4000)
        svr.fit(x, y)
        svrlist.append(svr)
        testX, testY = rollingWindows(imf[i], 500, 3040, 3400)
        predY = np.matrix(svr.predict(testX)).T
        print(predY.shape)
        try:
            predYVals = np.concatenate([predYVals, predY], axis=1)
        except ValueError:
            predYVals = np.matrix(predY)
    svr = svm.SVR()
    svr.fit(imf[7:8, 0:3000].T, close_prices[0:3000])
    predPrices = svr.predict(predYVals)
    print(mean_squared_error(close_prices[3540:3900], predPrices))
    print(mean_squared_error(close_prices[3540:3900], close_prices[3539:3899]))
    print(time.time() - t)
"""
Created on Wed Apr 10 17:57:02 2019

@author: Administrator
"""

from pyhht.emd import EMD
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pyhht.visualization import plot_imfs

# 读取数据
#dataset = pd.read_csv('data_day.csv')
stock_dir='../dataset/AAPL.csv'
dataset = pd.read_csv(open(stock_dir),header=0)
dataset=dataset[::-1]
for col in dataset.columns:
    dataset=dataset['Open']
    data = dataset.values
    s = data.ravel()
    #emd
    decomposer = EMD(s)               
    IMF = decomposer.decompose()
    print(IMF.shape)
    imf_data = pd.DataFrame(IMF.T)
    imf_data.to_csv('../dataset/emd/emd_AAPL_'+str(col)+'.csv')
    #绘制分解图
    plot_imfs(s,IMF)

Example #5
0
@author: ly
@date Date: 2019年07月10日 12:45
@Description: 
@URL: https://github.com/jaidevd/pyhht/blob/dev/docs/examples/simple_emd.py
@version: V1.0
'''
import numpy as np
from numpy import pi, sin, linspace, sqrt
from pyhht.emd import EMD
from pyhht.visualization import plot_imfs
import matplotlib
import matplotlib.pyplot as plt

t = linspace(0, 1, 1000)
modes = sin(2 * pi * 5 * t) + sin(2 * pi * 10 * t) + sin(2 * pi * 3 * t)
x = modes + t
plt.plot(t, modes)
plt.plot(t, x)
plt.show()
decomposer = EMD(x)
imfs = decomposer.decompose()

plot_imfs(x, imfs, t)

sumValues = np.zeros_like(imfs[0])
for i in range(imfs.shape[0]):
    sumValues += imfs[i]

plt.plot(t, sumValues)
plt.show()
Example #6
0
from pyhht import EMD
from pyhht.visualization import plot_imfs
from scipy.signal import hilbert
from pyhht.utils import inst_freq
import matplotlib.pyplot as plt
import numpy as np

lon = []
lon = lon.crop(-0.5, 0.5)
signal = lon._data[5, 57, :]

decomposer = EMD(signal)
imfs = decomposer.decompose()
plot_imfs(signal, imfs, lon.times)
ht = hilbert(imfs)
plt.imshow(np.abs(ht), aspect='auto', origin='lower')


Example #7
0
# -*- coding: utf-8 -*-
"""
Created on Wed Jun  5 16:01:27 2019

@author: veepoo
"""

from pyhht.visualization import plot_imfs
import numpy as np
import pyhht
import math

t = np.linspace(0, 100, 1000)
fs1 = 4
fs2 = 10
modes = np.cos(math.radians(2 * math.pi * fs1 * t)) + np.cos(
    math.radians(2 * math.pi * fs2 * t))
x = modes + t
decomposer = pyhht.EMD(x)
imfs = decomposer.decompose()
plot_imfs(x, imfs, t)  #doctest: +SKIP
Example #8
0
Created on Wed Jun 20 10:58:43 2018

@author: pxr13258
"""

from pyhht.visualization import plot_imfs
import numpy as np
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import math
import csv
pi=3.14
t = np.linspace(0, 1, 1000)
modes = np.sin(2 * pi * 5 * t) + np.sin(2 * pi * 10 * t)
x = modes + t
decomposer = EMD(x)
imfs = decomposer.decompose()
plot_imfs(x, imfs, t) 


pathbcc='D:/newdata/BCCBTC_5T.csv'
bcc= pd.read_csv(pathbcc)
data=bcc['Close'].values
emdtest=[]
for i in range(10):
    emdtest.append(data[i*288:(i+1)*288])
emdtest=np.array(emdtest)
dec=EMD(emdtest[9])
imf=dec.decompose()
plot_imfs(emdtest[9], imf) 
Example #9
0
    IA0 = abs(ReImf0)
    IA1 = abs(ReImf1)
    instf = np.array([instf0, instf1, instf2])  # 频率均值
    AR0 = ar_least_square(IA0, 4)
    AR1 = ar_least_square(IA1, 4)
    f1 = instf.mean()
    return np.vstack((AR0, AR1, f1))  #纵向拼接


path = 'F:\\BaiduNetdiskDownload\\超声波数据\\1mm\\50.txt'
with open(path, 'r', encoding='utf-8') as f:
    for line in f:
        a = line.split(',')
        a = np.array(a).astype(float)
        x, y = signal.butter(1, 0.4, 'highpass')
        a = signal.filtfilt(x, y, a)
"""decomposer = pyhht.emd.EMD(a[5000:6000])
imfs = decomposer.decompose()      #获取到imfs数据。
plot_imfs(a[5000:6000], imfs) 
ReImf0 = hilbert(imfs[0])
ReImf1 = hilbert(imfs[1])
ReImf2 = hilbert(imfs[2])
ReImf3 = hilbert(imfs[3])
ReImf4 = hilbert(imfs[4])
ReImf = np.array([ReImf0, ReImf1, ReImf2, ReImf3, ReImf4])
amplitude = abs(ReImf0)     #计算幅值
instf, timestamps = tftb.processing.inst_freq(ReImf0)    #计算瞬时频率
plt.plot(timestamps, instf)     #画图看看效果
plt.show()"""
x = (cal(a[6000:7000]))
Example #10
0
plt.plot(y)
#ext1=y[argrelextrema(y, np.greater)[0]]
#min1=y[argrelextrema(y, np.less)[0]]
##f2 = interp1d(x, y, kind='cubic')
###for i in range(len(ext1)):
##
##
#cubicspline=CSP(x,y)
##plt.plot(f2);
#extarray=cubicspline(ext1);
#minarray=cubicspline(min1);
#larr=np.zeros(176);
#larr=larr+min1     ;
decomposer = EMD(y)
imfs = decomposer.decompose()
plot_imfs(y, imfs, x)
imf1 = []
for i in range(1000):
    imf1.append(imfs[0, i])

plt.subplot(2, 1, 1)
plt.plot(x, imf1)
plt.xlabel('IMF 1')
plt.ylabel('Amplitude')
plt.show()
imf2 = []
for i in range(1000):
    imf2.append(imfs[1, i])

plt.subplot(2, 1, 2)
plt.plot(x, imf2)
Example #11
0
@version: V1.0
'''
import numpy as np
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
from pyhht.emd import EMD
from pyhht.visualization import plot_imfs

column_names = [
    'term', 'date', 'red1', 'red2', 'red3', 'red4', 'red5', 'red6', 'blue',
    'appear1', 'appear2', 'appear3', 'appear4', 'appear5', 'appear6', 'prize',
    'Total', 'first_num', 'first_amount', 'second_num', 'second_amount',
    'third_num', 'third_amount', 'fourth_num', 'fourth_amount', 'fifth_num',
    'fifth_amount', 'sixth_num', 'sixth_amount'
]
data = pd.read_csv('F:/workspace/Tensorflow/src/ssq/ssq.txt',
                   sep=' ',
                   header=None,
                   names=column_names)
print(data.info())
plt.figure(figsize=(15, 5))
x = range(0, len(data['red1']), 1)
plt.plot(x, data['red1'])
plt.show()

decomposer = EMD(data['red1'])
imfs = decomposer.decompose()
print(imfs.shape)
plot_imfs(data['red2'].values, imfs, None)
Example #12
0
import numpy as np
import pandas as pd
from sklearn import datasets
import matplotlib.pyplot as plt
from pyhht.emd import EMD
from pyhht.visualization import plot_imfs

#载入时间序列数据
data = pd.read_csv('gold_data.csv',usecols=['settle'])
#EMD经验模态分解
decomposer = EMD(data[0])
imfs = decomposer.decompose()
#绘制分解图
plot_imfs(data[0],imfs,data.index)
#保存IMFs
arr = np.vstack((imfs,data[0]))
dataframe = pd.DataFrame(arr.T)
dataframe.to_csv('C:\imf.csv',index=None,columns=None)
Example #13
0
from pyhht.emd import EMD
import pandas as pd
import numpy as np
import time

start = time.clock()
from pyhht.visualization import plot_imfs

dta = pd.read_csv('C:/Users/dw/Desktop/Data1.csv')
seris = np.array(dta.iloc[:, 0])
decomposer = EMD(seris)
imfs = decomposer.decompose()
plot_imfs(seris, imfs)
arr = np.vstack((imfs, seris))
dataframe = pd.DataFrame(arr.T)
dataframe.to_csv("C:/Users/dw/Desktop/temp.csv")

end = time.clock()
print("final is in ", end - start)
Example #14
0
    plt.savefig("hs.png")
    plt.show()
    #############noise################

noise = np.zeros((1600, 3))
for i in range(3):
    noise[:, i] = wgn(data[:, i], 40)

data = data + noise

if if_imf:
    x = data[0:1600, 0]
    decomposer = EMD(x)
    imfs = decomposer.decompose()
    plot_imfs("imfs_noise", x, imfs, t)

else:
    plt.figure(figsize=(16, 12))

    x = data[0:1600, 0]
    decomposer = EMD(x)
    imfs = decomposer.decompose()

    plt.subplot(3, 5, 1)  # original signal
    plt.plot(t, data[0:1600, 0], 'y')
    plt.grid()

    hs0 = hilbert(imfs[0])
    hs1 = hilbert(imfs[1])
    for xx in range(1600):
Example #15
0
ener = ener / max(ener)
plt.plot(time[indxt], ener)
plt.show()
plt.plot(vtime, tmp)
plt.xlim(tmin, tmax)
plt.show()


# In[33]:

npt = len(trNfil[0].data)
emd = EMD(trNfil[0].data)
imfs = emd.decompose()
time = (np.linspace(1, npt, npt)) * dt
plt.rcParams["figure.figsize"] = (30.0, 50.0)
plot_imfs(trNfil[0].data, time, imfs)


# In[44]:

aa = trNfil[2].copy()
plotTrigger(aa, cft, 2.2, 0.5)
dm = len(cft)
item = [i for i in list(range(dm)) if cft[i] > 2.2]
print(min(item))


ene = [0] * dm
for i in xrange(1, dm):
    # ene[i] = ene[i-1] + envel[i] ** 2
    ene[i] = ene[i - 1] + trNfil[2].data[i] ** 2
Example #16
0
import matplotlib.pyplot as plt
from pyhht.visualization import plot_imfs

image = cv2.imread("dan.jpg")
image = cv2.resize(image, (108, 108))

image2 = cv2.imread('images.jpg')
image2 = cv2.resize(image2, (108, 108))


pi = 3.14
temp = image.flatten()
temp2 = image2.flatten()
t = np.linspace(0, 1, 34992)
# modes = np.sin(2 * pi * 5 * t) + np.sin(2 * pi * 10 * t)
# x = modes + t
temp = temp + t
temp2 = temp2 + t

hilbert = pyhht.EMD(temp)
imfs = hilbert.decompose()

hilbert2 = pyhht.EMD(temp2)
imfs2 = hilbert.decompose()


print(imfs.shape, imfs2.shape)

plot_imfs(temp, imfs, t)
plot_imfs(temp, imfs2, t)
Example #17
0
from pyhht.visualization import plot_imfs

N = 2001
T = np.arange(1, N + 1, step=4)
t = np.arange(1, N + 1)

p = N / 2

fmin1 = 1.0 / 64
fmax1 = 1.5 * 1.0 / 8
x1 = fmsin(N, fmin1, fmax1, p, N / 2, fmax1)[0]

fmin2 = 1.0 / 32
fmax2 = 1.5 * 1.0 / 4
x2 = fmsin(N, fmin2, fmax2, p, N / 2, fmax2)[0]

f0 = 1.5 * 1.0 / 16

x3 = amgauss(N, N / 2, N / 8) * fmconst(N, f0)[0]

a1 = 1
a2 = 1
a3 = 1

x = np.real(a1 * x1 + a2 * x2 + a3 * x3)
x = x / np.max(np.abs(x))

decomposer = EMD(x)
imf = decomposer.decompose()
plot_imfs(x, imf, t)
Example #18
0
    high_prices = df[3].values[:]
    encodings = np.array([
        longShortEncoding(i, close_prices, high_prices, low_prices, 5, 0.05)
        for i in range(3600)
    ])

    weights = sampleWeightsByUniqueness(encodings)
    print(weights)
    print(encodings[:, 0])
    print(encodings.shape)

    s = minmax_scale(close_prices)
    emd = EMD(s, maxiter=3000)
    imf = emd.decompose()

    plot_imfs(s, imf)

    predYVals = []
    for i in range(7, imf.shape[0]):
        x, y = rollingWindows(imf[i], 30, 0, 3000)
        nn = KNeighborsRegressor(n_neighbors=4)
        nn.fit(x, y)
        x, y = rollingWindows(imf[i], 30, 3030, 3400)
        predYNN = nn.predict(x)
        print(y)
        print(predYNN)
        predYVals.append(predYNN)

    clf2 = KNeighborsClassifier(n_neighbors=2)
    clf2.fit(
        np.matrix([imf[i][0:3000] for i in range(7, imf.shape[0])]).T,
Example #19
0
        for i in range(2, len(y)):
            q.append((y[i] - y[i - 1]) / (x[i] - x[i - 1]))
        q.append(0)
        return np.array(q)
    else:
        print("Error: Invalid shape. x and y must be the same length")


#computing the derivative of column 1 wrt time
differ = (diff(data[:, 1], data[:, 0]))
pl.plot(data[:, 0], differ)
pl.show()

decomposer = hht.EMD(data[:, 1] + data[:, 0])
imfs = decomposer.decompose()
vis.plot_imfs(data[:, 0] + data[:, 1], imfs, data[:, 0])

for blep in range(0, 27):
    #Determine where peaks are, change parameters for a change in what is defined as a "peak". In order, minimum height limit, vertical distance to neighbouring samples, horizontal distance to neighboring samples, how prominant a "peak" is, required width of peaks.
    ProtoPeaks = scipy.signal.find_peaks(data[:, blep],
                                         height=0,
                                         threshold=0,
                                         distance=1,
                                         prominence=70,
                                         width=0)
    Peaks = ProtoPeaks[0]
    print(
        "For the below graph, (graph  number", blep,
        ")the peaks as defined by the paramters on line 53 of the program are at"
    )
    print(Peaks)
Example #20
0
from pyhht.visualization import plot_imfs

import matplotlib.pyplot as plt

sys.path.append('data/')
from read_PLAID_data import read_source_data

source_dir = 'data/source/submetered_new_pured/source/'

with open(
        '/home/chaofan/powerknowledge/data/source/metadata_submetered2.1.json',
        'r',
        encoding='utf8') as load_meta:
    meta = json.load(load_meta)

length1 = 3000
length2 = 3000
t = range(3000)
csv_dir = os.listdir(source_dir)
for file in csv_dir:
    file_dir = source_dir + file
    Switch_V, Switch_I = read_source_data(file_dir, offset=0, length=length1)
    Stable_V, Stable_I = read_source_data(file_dir,
                                          offset=3000,
                                          length=length2)
    tem = np.array(Switch_I) - np.array(Stable_I)

    decomposer = EMD(tem, n_imfs=3)
    imfs = decomposer.decompose()
    plot_imfs(tem, imfs, t)
Example #21
0
import numpy as np
import pandas as pd
from sklearn import datasets
import matplotlib.pyplot as plt
from pyhht.emd import EMD
from pyhht.visualization import plot_imfs

#载入时间序列数据
data = pd.read_csv('gold_data_.csv', usecols=['settle'])
data = data['settle']
decomposer = EMD(data)
imfs = decomposer.decompose()
print(imfs)
#绘制分解图
plot_imfs(data, imfs, data.index)
#保存IMFs
arr = np.vstack((imfs, data))
dataframe = pd.DataFrame(arr.T)
dataframe.to_csv('imf.csv', index=None, columns=None)
print(imfs[0])
Example #22
0
import numpy as np
import pandas as pd
from sklearn import datasets
import matplotlib.pyplot as plt
from pyhht.emd import EMD
from pyhht.visualization import plot_imfs

#载入时间序列数据
data = pd.read_csv('gold_data.csv', usecols=['settle'])

#EMD经验模态分解
x = data['settle']
decomposer = EMD(x)
imfs = decomposer.decompose()

#绘制分解图
plot_imfs(x, imfs, data.index)
#保存IMFs
arr = np.vstack((imfs, x))
dataframe = pd.DataFrame(arr.T)
dataframe.to_csv('D:/imf.csv', index=None, columns=None)
Example #23
0
'''

this file contains object to extract the linear component of raw data
this method is based on the secondary decomposition using EMD and SSA

'''

# load SourceData#1.csv
dta = pd.read_csv('C:/Users/dw/Desktop/Data1.csv')
# read the wind speed series
ts = np.array(dta.iloc[:, 0])

# EMD decomposition
decomposer = EMD(ts)
imfs = decomposer.decompose()
plot_imfs(ts, imfs)
arr = np.vstack((imfs, ts))
dataframe = pd.DataFrame(arr.T)
arr1 = arr.T[:, 2:8]
# linear subseries: IMF3 to IMF7, and Res
linear_subseries = arr1.sum(1)
# nonlinear subseries: IMF1 and IMF2
arr2 = arr.T[:, 0:2]
nonlinear_subseries = arr2.sum(1)

# Secondary decomposition of nonlinear subseries using SSA
series = nonlinear_subseries - np.mean(nonlinear_subseries)
windowLen = 12
seriesLen = len(series)
K = seriesLen - windowLen + 1
X = np.zeros((windowLen, K))