Exemplo n.º 1
0
def get_imfs(signal):
    """"
    Return the imf of a signal
    """
    decomposer = EMD(signal)
    imf = decomposer.decompose()
    return imf
Exemplo n.º 2
0
def plot_imf(signal):
    decomposer = EMD(signal)
    imfs = decomposer.decompose()
    m = len(signal)
    x = np.linspace(0,1,m)
    pyhht.plot_imfs(x, imfs)
    plt.show()
Exemplo n.º 3
0
def get_imfs(signal):
	try:
		# decomposer_signal = EMD(signal, fixe=100, n_imfs=2)
		decomposer_signal = EMD(signal)
		imfs = decomposer_signal.decompose()
		if len(imfs) < 2:
			print("imfs {} +++++++++++++++++++++++++++++++++++++++".format(len(imfs)))
			raise ValueError("imfs {}".format(len(imfs)))
		return imfs[:2]
	except Exception as e:
		raise e
Exemplo n.º 4
0
def process_window(filename, eeg, stage, step):
    # plt.figure()
    s = np.zeros((5, 2501))
    print('step: ', step)
    # t = np.linspace(step*fs*30,step*fs*30+fs*10,fs*10)
    decomposer = EMD(eeg[step * fs * window_len:step * fs * window_len +
                         fs * sample_time])
    imfs = decomposer.decompose()
    # print(len(imfs))
    # plot_imfs(eeg[step*fs*30:step*fs*30+fs*10], imfs, t)
    for imf in imfs:
        analytic_signal = hilbert(imf)
        instantaneous_phase = np.unwrap(np.angle(analytic_signal))
        instantaneous_freq = (np.diff(instantaneous_phase) / (2.0 * np.pi) *
                              fs)
        # print(len(instantaneous_freq))
        for i in range(len(instantaneous_freq)):
            if instantaneous_freq[i] > 1 and instantaneous_freq[i] <= 4:
                s[0][i] += imf[i]
            elif instantaneous_freq[i] > 4 and instantaneous_freq[i] <= 8:
                s[1][i] += imf[i]
            elif instantaneous_freq[i] > 8 and instantaneous_freq[i] <= 12:
                s[2][i] += imf[i]
            elif instantaneous_freq[i] > 12 and instantaneous_freq[i] <= 30:
                s[3][i] += imf[i]
            elif instantaneous_freq[i] > 30 and instantaneous_freq[i] <= 60:
                s[4][i] += imf[i]

    for i in range(5):
        if stage == 'W':
            s[i][-1] = 0
        else:
            s[i][-1] = 1

    # save np array
    Path('./delta').mkdir(parents=True, exist_ok=True)
    Path('./theta').mkdir(parents=True, exist_ok=True)
    Path('./alpha').mkdir(parents=True, exist_ok=True)
    Path('./beta').mkdir(parents=True, exist_ok=True)
    Path('./gamma').mkdir(parents=True, exist_ok=True)

    np.save('./delta/' + filename + '_' + str(step) + '_delta', s[0])
    np.save('./theta/' + filename + '_' + str(step) + '_theta', s[1])
    np.save('./alpha/' + filename + '_' + str(step) + '_alpha', s[2])
    np.save('./beta/' + filename + '_' + str(step) + '_beta', s[3])
    np.save('./gamma/' + filename + '_' + str(step) + '_gamma', s[4])

    pass
Exemplo n.º 5
0
import numpy as np
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.ensemble import AdaBoostRegressor, BaggingRegressor
import xgboost as xgb

np.random.seed(123)

ticker = 'SPY'
stock = pdr.get_data_yahoo(ticker.upper(),
                           start='2009-01-01',
                           end=str(datetime.now())[0:11])
stock = stock.Close
returns = np.log(stock).diff().dropna()

decomposer = EMD(returns)
imfs = decomposer.decompose()
#t = np.linspace(0, 1, len(returns))
#plot_imfs(returns, imfs, t)

imfs = imfs.T
imfs = pd.DataFrame(imfs)

series = returns[len(returns) - 2000:]
series = np.array(series)
series = series.reshape(-1, 1)

D = 4
T = 1
N = 2000
series = series[500:]
lbls = np.zeros((N - 500 - T - (D - 1) * T, ))
def hht(signal):
    decomposer = EMD(signal)
    imfs = decomposer.decompose()
    ht = hilbert(imfs)
    return imfs, ht
Exemplo n.º 7
0
def get_imfs_emd(signal):
    decomposer_signal = EMD(signal)
    return decomposer_signal.decompose()