Exemplo n.º 1
0
 def estimate_trend(self, time_series_x: np.ndarray,
                    time_series_y: np.ndarray):
     # Extract imfs and residue
     emd = EMD()
     emd.emd(time_series_y)
     imfs, res = emd.get_imfs_and_residue()
     return imfs[-1]
Exemplo n.º 2
0
    def test_single_imf(self):
        """
        Input is IMF. Expecint single shifting.
        """

        max_diff = lambda a,b: np.max(np.abs(a-b))

        emd = EMD()
        emd.FIXE_H = 2

        t = np.arange(0, 1, 0.001)
        c1 = np.cos(4*2*np.pi*t) # 2 Hz
        S = c1.copy()

        # Input - linear function f(t) = sin(2Hz t)
        # import pdb; pdb.set_trace()
        imfs = emd.emd(S, t)
        self.assertEqual(imfs.shape[0], 1, "Expecting sin + trend")

        diff = np.allclose(imfs[0], c1)
        self.assertTrue(diff, "Expecting 1st IMF to be sin\nMaxDiff = " + str(max_diff(imfs[0],c1)))

        # Input - linear function f(t) = siin(2Hz t) + 2*t
        c2 = 5*(t+2)
        S += c2.copy()
        imfs = emd.emd(S, t)

        self.assertEqual(imfs.shape[0], 2, "Expecting sin + trend")
        diff1 = np.allclose(imfs[0], c1, atol=0.2)
        self.assertTrue(diff1, "Expecting 1st IMF to be sin\nMaxDiff = " + str(max_diff(imfs[0],c1)))
        diff2 = np.allclose(imfs[1], c2, atol=0.2)
        self.assertTrue(diff2, "Expecting 2nd IMF to be trend\nMaxDiff = " + str(max_diff(imfs[1],c2)))
Exemplo n.º 3
0
    def test_different_length_input(self):
        T = np.arange(20)
        S = np.random.random(len(T)+7)

        emd = EMD()
        with self.assertRaises(ValueError):
            emd.emd(S, T)
Exemplo n.º 4
0
    def test_different_length_input(self):
        T = np.arange(20)
        S = np.random.random(len(T) + 7)

        emd = EMD()
        with self.assertRaises(ValueError):
            emd.emd(S, T)
Exemplo n.º 5
0
    def test_unsupporter_spline(self):
        emd = EMD()
        emd.spline_kind = "waterfall"

        S = np.random.random(20)

        with self.assertRaises(ValueError):
            emd.emd(S)
Exemplo n.º 6
0
    def test_unsupporter_spline(self):
        emd = EMD()
        emd.spline_kind = "waterfall"

        S = np.random.random(20)

        with self.assertRaises(ValueError):
            emd.emd(S)
Exemplo n.º 7
0
 def test_instantiation2(self):
     t = np.linspace(0, 1, 50)
     S = t + np.cos(np.cos(4. * t**2))
     emd = EMD()
     emd.emd(S, t)
     imfs, res = emd.get_imfs_and_residue()
     vis = Visualisation(emd)
     assert (vis.imfs == imfs).all()
     assert (vis.residue == res).all()
Exemplo n.º 8
0
def Inst_freq(peeled_seq, fs=4000):
    t = np.arange(0, 1, 1 / fs)
    # print(len(t))
    emd = EMD()
    emd.emd(peeled_seq)
    imfs, res = emd.get_imfs_and_residue()
    vis = Visualisation(emd)
    imfs_inst_freqs = vis._calc_inst_freq(imfs, t, order=False, alpha=None)
    return imfs_inst_freqs
Exemplo n.º 9
0
 def test_check_imfs5(self):
     t = np.linspace(0, 1, 50)
     S = t + np.cos(np.cos(4. * t**2))
     emd = EMD()
     emd.emd(S, t)
     imfs, res = emd.get_imfs_and_residue()
     vis = Visualisation(emd)
     imfs2, res2 = vis._check_imfs(imfs, res, False)
     assert (imfs == imfs2).all()
     assert (res == res2).all()
def get_same_shaped_imfs(signal):
    emd = EMD()
    try:
        imfs_signal = emd.emd(signal.numpy(), max_imf=12)
    except AttributeError:
        imfs_signal = emd.emd(signal, max_imf=12)
    if imfs_signal.shape[0] > 12:
        imfs_signal = imfs_signal[:12, :]
    new_imfs = np.zeros((12, signal.shape[0]))
    new_imfs[:imfs_signal.shape[0], :] = imfs_signal
    return new_imfs
Exemplo n.º 11
0
 def emd_decompose(a, t):
     emd = EMD()
     emd.emd(a)
     imfs, res = emd.get_imfs_and_residue()
     plt.plot(t, a)
     plt.title('origin sequence')
     vis = Visualisation(emd)
     vis.plot_imfs(t=t)
     vis.plot_instant_freq(t)
     vis.show()
     plt.show()
     return imfs, res
Exemplo n.º 12
0
def Visualing(peeled_seq, fs=4000):
    t = np.arange(0, 1, 1 / fs)
    # print(len(t))
    emd = EMD()
    emd.emd(peeled_seq)
    imfs, res = emd.get_imfs_and_residue()
    vis = Visualisation(emd)
    # Create a plot with all IMFs and residue
    vis.plot_imfs(imfs=imfs, residue=res, t=t, include_residue=True)
    # Create a plot with instantaneous frequency of all IMFs
    vis.plot_instant_freq(t, imfs=imfs)
    # Show both plots
    vis.show()
def empirical_mode_decomposition(date_ini, ts_ini):
    """
    :param ts_ini: the raw data to be decomposed
    :return: the decomposed time series
    """
    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//the wavelet tranform //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    # A2, D2, D1 = pywt.wavedec(stress_ini.ravel(), 'db5', level=2)
    # print(stress_ini.shape)
    # print(D2.shape, D1.shape)
    # cow = [A2, D2, D1]
    # raw_data = pywt.waverec(cow, 'db5')
    # plt.style.use('seaborn-pastel')
    # fig = plt.figure(figsize=(8, 6))
    # ax = fig.add_subplot(111)
    # ax.plot(stress_ini)
    # ax.plot(A2)
    # ax.plot(D2)
    # ax.plot(D1)
    # ax.plot(raw_data+0.1)
    # plt.show()
    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//the empirical mode decomposition //~~~~~~~~~~~~~~~~~~~~~
    emd = EMD()
    emd.emd(ts_ini.ravel())
    imfs, res = emd.get_imfs_and_residue()
    # Plot results
    # N = imfs.shape[0] + 2
    # plt.style.use('seaborn-pastel')
    # plt.figure(figsize=(15, 20))
    # plt.subplot(N, 1, 1)
    # plt.plot(date_ini, ts_ini, 'r')
    # plt.title("Raw data")
    # plt.xlabel('shear stress, ' + r'$\tau$(MPa)')
    #
    # for n, imf in enumerate(imfs):
    #     plt.subplot(N, 1, n + 2)
    #     plt.plot(date_ini, imf, 'g')
    #     plt.title("IMF " + str(n + 1))
    #     plt.xlabel("Time [0.1s]")
    #
    # plt.subplot(N, 1, N)
    # plt.plot(date_ini, res, 'b')
    # plt.title("residue")
    # plt.xlabel('shear stress, ' + r'$\tau$(MPa)')
    # plt.tight_layout()
    # plt.savefig('simple_example', dpi=600, bbox_inches='tight')
    # plt.show()
    return imfs, res
Exemplo n.º 14
0
class EmdTransformer(object):
    """Empirical Mode Decomposition"""
    def __init__(self, ensemble=False, **kwargs):
        self.ensemble = ensemble
        if ensemble:
            self.emd_obj = EEMD(**kwargs)
        else:
            self.emd_obj = EMD(**kwargs)

    def fit_transform(self, data, **kwargs):

        if isinstance(data, pd.DataFrame):
            data = data.values
        else:
            assert isinstance(data, np.ndarray)

        assert len(data.shape) == 2

        imfs = []
        for col in range(data.shape[1]):

            if self.ensemble:
                IMFs = self.emd_obj.eemd(data[:, col], **kwargs)
            else:
                IMFs = self.emd_obj.emd(data[:, col], **kwargs)

            imfs.append(IMFs.T)

        return np.concatenate(imfs, axis=1)

    def inverse_transform(self, **kwargs):
        raise NotImplementedError
Exemplo n.º 15
0
    def test_emd_FIXE(self):
        T = np.linspace(0, 1, 100)
        c = np.sin(9 * 2 * np.pi * T)
        offset = 4
        S = c + offset

        emd = EMD()

        # Default state: converge
        self.assertTrue(emd.FIXE == 0)
        self.assertTrue(emd.FIXE_H == 0)

        # Set 1 iteration per each sift,
        # same as removing offset
        FIXE = 1
        emd.FIXE = FIXE

        # Check flags correctness
        self.assertTrue(emd.FIXE == FIXE)
        self.assertTrue(emd.FIXE_H == 0)

        # Extract IMFs
        IMFs = emd.emd(S)

        # Check that IMFs are correct
        self.assertTrue(np.allclose(IMFs[0], c))
        self.assertTrue(np.allclose(IMFs[1], offset))
Exemplo n.º 16
0
    def test_emd_FIXE(self):
        T = np.linspace(0, 1, 100)
        c = np.sin(9*2*np.pi*T)
        offset = 4
        S = c + offset

        emd = EMD()

        # Default state: converge
        self.assertTrue(emd.FIXE==0)
        self.assertTrue(emd.FIXE_H==0)

        # Set 1 iteration per each sift,
        # same as removing offset
        FIXE = 1
        emd.FIXE = FIXE

        # Check flags correctness
        self.assertTrue(emd.FIXE==FIXE)
        self.assertTrue(emd.FIXE_H==0)

        # Extract IMFs
        IMFs = emd.emd(S)

        # Check that IMFs are correct
        self.assertTrue(np.allclose(IMFs[0], c))
        self.assertTrue(np.allclose(IMFs[1], offset))
Exemplo n.º 17
0
 def test_calc_instant_phase2(self):
     t = np.linspace(0, 1, 50)
     S = t + np.cos(np.cos(4. * t**2))
     emd = EMD()
     imfs = emd.emd(S, t)
     vis = Visualisation()
     phase = vis._calc_inst_phase(imfs, 0.4)
     assert len(imfs) == len(phase)
Exemplo n.º 18
0
 def test_calc_instant_phase3(self):
     t = np.linspace(0, 1, 50)
     S = t + np.cos(np.cos(4. * t**2))
     emd = EMD()
     imfs = emd.emd(S, t)
     vis = Visualisation()
     with self.assertRaises(AssertionError):
         phase = vis._calc_inst_phase(imfs, 0.8)
Exemplo n.º 19
0
 def test_calc_instant_freq(self):
     t = np.linspace(0, 1, 50)
     S = t + np.cos(np.cos(4. * t**2))
     emd = EMD()
     imfs = emd.emd(S, t)
     vis = Visualisation()
     freqs = vis._calc_inst_freq(imfs, t, False, None)
     assert imfs.shape == freqs.shape
Exemplo n.º 20
0
 def IMFs(self, win):
     temp = self.data_freq * int(1 / self.gap)
     self.price = self.PriceList[-60 / int(self.gap):-1]
     s = self.price
     emd = EMD()
     IMFs = emd.emd(s)
     r = IMFs[-1]
     b = np.std(s - r)
     self.r = math.log(b / a)
 def imff(self):
     temp = self.data_freq * int(1 / self.gap)
     self.price = self.PriceList[-int(self.data_freq / self.gap):-1]
     s = np.array(self.price)
     emd = EMD()
     IMFs = emd.emd(s)
     r = IMFs[-1]
     a = np.std(r)
     b = np.std(s - r)
     self.r = math.log(b / a)
Exemplo n.º 22
0
def dme_function(lista_para_ejecutar):
    nueva_list = lista_para_ejecutar
    new_data =[]
    for item in nueva_list:
        new_data.append(float(item))
    #x = np.linspace(0, 10, len(lista_para_ejecutar))
    signal = np.array(new_data)
    emd = EMD()
    IMFS = emd.emd(signal)
    return IMFS
Exemplo n.º 23
0
def hes2(args):
    n = 10000
    t = np.arange(0, n/args.fs, 1/args.fs)
    S = args.singled_out[-1, 0:n]
    args.temp_add = 'emd_raw'
    show_signal(S, args)
    emd = EMD()
    emd.emd(S)
    imfs, res = emd.get_imfs_and_residue()

    # In general:
    # components = EEMD()(S)
    # imfs, res = components[:-1], components[-1]

    vis = Visualisation()
    vis.plot_imfs(imfs=imfs, residue=res, t=t, include_residue=True)
    vis.plot_instant_freq(t, imfs=imfs)

    vis.show()
    return 0
Exemplo n.º 24
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.º 25
0
    def test_trend(self):
        """
        Input is trend. Expeting no shifting process.
        """
        emd = EMD()

        t = np.arange(0, 1, 0.01)
        S = 2*t

        # Input - linear function f(t) = 2*t
        imfs = emd.emd(S, t)
        self.assertEqual(imfs.shape[0], 1, "Expecting single IMF")
        self.assertTrue(np.allclose(S, imfs[0]))
Exemplo n.º 26
0
    def test_trend(self):
        """
        Input is trend. Expeting no shifting process.
        """
        emd = EMD()

        t = np.arange(0, 1, 0.01)
        S = 2 * t

        # Input - linear function f(t) = 2*t
        IMF = emd.emd(S, t)
        self.assertEqual(IMF.shape[0], 1, "Expecting single IMF")
        self.assertTrue(np.allclose(S, IMF[0]))
Exemplo n.º 27
0
        def ver_dme():
            array1year = []
            array1month = []
            array2 = []
            #limpieza de datos
            for line in open(path):
                array1year.append(line[0:4])
                array1month.append(line[4:6])
                if line[7] == '-':
                    array2.append(line[7:])

                else:
                    array2.append(line[7:])

            lista = []
            lista2 = []
            listaGeneral = []
            for valor in array2:
                lista.append(valor.rstrip('\n'))
                #print(lista)
            for sal in range(len(lista)):
                lista2.append(array1year[sal] + '-' + array1month[sal])
                #print(lista2)
            #creación del dataframe

            listaGeneral = dict(zip(lista2[1:], lista[1:]))
            tupla = listaGeneral.items()
            df = pd.DataFrame(list(tupla))
            df.columns = ["fecha", "valor"]
            global my_graph
            my_graph = df["valor"].astype(float)

            global new_list
            new_list = lista[1:]

            dataoni = []
            for item in new_list:
                dataoni.append(float(item))
            signal = np.array(dataoni)
            emd = EMD()
            IMFS = emd.emd(signal)
            funcion = tk.StringVar(framedme)
            funcion.set("Seleccione la Descomposición")

            opciones = []
            for line in range(len(IMFS)):
                opciones.append(line)
                #print (line)
            global dropselect
            dropselect = ttk.OptionMenu(framedme, funcion, *opciones)
            dropselect.pack(padx=5, pady=5, ipadx=5, ipady=5)
Exemplo n.º 28
0
    def get_data(self):
        x_train, y_train = [], []
        for k in self.list_npz_files():
            with np.load(k) as f:
                data = f["x"]
                labels = f["y"]
                labels = np.expand_dims(labels,axis=1)
                for i in range(len(data)):
                    a = np.zeros((5,))
                    a[labels[i]] = 1 
                    a = np.reshape(a, (None,5))
                    x_train.append(EMD.emd(data[i]))
                    y_train.append(a)

        return np.asarray(x_train), np.asarray(y_train)
Exemplo n.º 29
0
def emd_filter(raw_time_series, threshold=0.5):
    """
    This method is required by all the bar types and is used to create the desired bars.
    :params
    1. raw_time_series: (time series in numpy array)
    2. threshold:       (float number)
    it denotes how much low-frequency components will be kept. It should be in the range of [0, 1]

    :return: the same-length data
    """
    # initalize the EMD
    emd = EMD()
    # apply the EMD decomposition
    imfs = emd.emd(raw_time_series)
    # save the low frequent components
    new_time_series = _filter_imfs(imfs, threshold)
    return new_time_series
Exemplo n.º 30
0
def emdLSTM(dataset):
    emd = EMD()
    IMFs = emd.emd(dataset)
    print(type(IMFs))
    [rows, columns] = IMFs.shape
    yhatResult = 0
    for n, imf in enumerate(IMFs):
        tempDataSet = imf
        #print('--------------------------------------')
        #myDataSet, myScalar, myMinValue = normalizeData(tempDataSet)
        X_train, y_train, X_test, y_test = getTrainAndTest(tempDataSet)
        yhat = trainAndPred(X_train, y_train, X_test)
        #pred_y, _ = returnNormal(myScalar, myMinValue, yhat, y_test)
        #print(yhat)
        yhatResult = yhat + yhatResult
    print(yhatResult)
    return yhatResult
Exemplo n.º 31
0
    def test_emd_default(self):
        T = np.linspace(0, 2, 200)
        c1 = 1 * np.sin(11 * 2 * np.pi * T + 0.1)
        c2 = 11 * np.sin(1 * 2 * np.pi * T + 0.1)
        offset = 9
        S = c1 + c2 + offset

        emd = EMD(spline_kind='akima')
        imfs = emd.emd(S, T)
        self.assertTrue(imfs.shape[0] == 3)

        close_imfs1 = np.allclose(c1[2:-2], imfs[0, 2:-2], atol=0.2)
        self.assertTrue(close_imfs1)

        close_imfs2 = np.allclose(c2[2:-2], imfs[1, 2:-2], atol=0.21)
        self.assertTrue(close_imfs2)

        close_offset = np.allclose(offset, imfs[2, 1:-1], atol=0.5)
        self.assertTrue(close_offset)
Exemplo n.º 32
0
    def test_emd_default(self):
        T = np.linspace(0, 2, 200)
        c1 = 1 * np.sin(11 * 2 * np.pi * T + 0.1)
        c2 = 11 * np.sin(1 * 2 * np.pi * T + 0.1)
        offset = 9
        S = c1 + c2 + offset

        emd = EMD()
        IMFs = emd.emd(S, T)

        self.assertTrue(IMFs.shape[0] == 3)

        closeIMF1 = np.allclose(c1[1:-1], IMFs[0, 1:-1], atol=0.5)
        self.assertTrue(closeIMF1)

        closeIMF2 = np.allclose(c2[1:-1], IMFs[1, 1:-1], atol=0.5)
        self.assertTrue(closeIMF2)

        closeOffset = np.allclose(offset, IMFs[2, 1:-1], atol=0.5)
        self.assertTrue(closeOffset)
Exemplo n.º 33
0
    def test_emd_default(self):
        T = np.linspace(0, 2, 200)
        c1 = 1*np.sin(11*2*np.pi*T+0.1)
        c2 = 11*np.sin(1*2*np.pi*T+0.1)
        offset = 9
        S = c1 + c2 + offset

        emd = EMD(spline_kind='akima')
        imfs = emd.emd(S, T)
        self.assertTrue(imfs.shape[0]==3)

        close_imfs1 = np.allclose(c1[2:-2], imfs[0,2:-2], atol=0.21)
        self.assertTrue(close_imfs1)

        close_imfs2 = np.allclose(c2[2:-2], imfs[1,2:-2], atol=0.24)
        diff = np.abs(c2[2:-2] - imfs[1,2:-2])
        self.assertTrue(close_imfs2)

        close_offset = np.allclose(offset, imfs[2,1:-1], atol=0.5)
        self.assertTrue(close_offset)
Exemplo n.º 34
0
    def test_emd_FIXEH(self):
        T = np.linspace(0, 2, 200)
        c1 = 1 * np.sin(11 * 2 * np.pi * T + 0.1)
        c2 = 11 * np.sin(1 * 2 * np.pi * T + 0.1)
        offset = 9
        S = c1 + c2 + offset

        emd = EMD()

        # Default state: converge
        self.assertTrue(emd.FIXE == 0)
        self.assertTrue(emd.FIXE_H == 0)

        # Set 5 iterations per each protoIMF
        FIXE_H = 6
        emd.FIXE_H = FIXE_H

        # Check flags correctness
        self.assertTrue(emd.FIXE == 0)
        self.assertTrue(emd.FIXE_H == FIXE_H)

        # Extract IMFs
        IMFs = emd.emd(S)

        # Check that IMFs are correct
        self.assertTrue(IMFs.shape[0] == 3)

        closeIMF1 = np.allclose(c1[1:-1], IMFs[0, 1:-1], atol=0.5)
        self.assertTrue(closeIMF1)
        self.assertTrue(np.allclose(c1, IMFs[0], atol=1.))

        closeIMF2 = np.allclose(c2[1:-1], IMFs[1, 1:-1], atol=0.5)
        self.assertTrue(closeIMF2)
        self.assertTrue(np.allclose(c2, IMFs[1], atol=1.))

        closeOffset = np.allclose(offset, IMFs[2, 1:-1], atol=0.5)
        self.assertTrue(closeOffset)
        self.assertTrue(np.allclose(IMFs[1, 1:-1], c2[1:-1], atol=0.5))

        closeOffset = np.allclose(offset, IMFs[2, 1:-1], atol=0.5)
        self.assertTrue(closeOffset)
Exemplo n.º 35
0
    def test_emd_FIXEH(self):
        T = np.linspace(0, 2, 200)
        c1 = 1*np.sin(11*2*np.pi*T+0.1)
        c2 = 11*np.sin(1*2*np.pi*T+0.1)
        offset = 9
        S = c1 + c2 + offset

        emd = EMD()

        # Default state: converge
        self.assertTrue(emd.FIXE==0)
        self.assertTrue(emd.FIXE_H==0)

        # Set 5 iterations per each protoIMF
        FIXE_H = 6
        emd.FIXE_H = FIXE_H

        # Check flags correctness
        self.assertTrue(emd.FIXE==0)
        self.assertTrue(emd.FIXE_H==FIXE_H)

        # Extract IMFs
        imfs = emd.emd(S)

        # Check that IMFs are correct
        self.assertTrue(imfs.shape[0]==3)

        close_imf1 = np.allclose(c1[2:-2], imfs[0,2:-2], atol=0.2)
        self.assertTrue(close_imf1)
        self.assertTrue(np.allclose(c1, imfs[0], atol=1.))

        close_imf2 = np.allclose(c2[2:-2], imfs[1,2:-2], atol=0.21)
        self.assertTrue(close_imf2)
        self.assertTrue(np.allclose(c2, imfs[1], atol=1.))

        close_offset = np.allclose(offset, imfs[2,2:-2], atol=0.1)
        self.assertTrue(close_offset)

        close_offset = np.allclose(offset, imfs[2,1:-1], atol=0.5)
        self.assertTrue(close_offset)
Exemplo n.º 36
0
def EMDfilterData(data,
                  samplingRate,
                  lowerFreqCutoff=0.28,
                  upperFreqCutoff=20):
    '''
    Inputs:
        Data: numpy arrray with one-dimensional data
    Outputs:
        filteredData: numpy array of data after filtering
    '''
    emd = EMD()
    IMFs = emd.emd(data)
    outputSignal = numpy.zeros_like(data)
    for imf in range(IMFs.shape[0]):
        imfFrequency = getFreqDataOfIMF(IMFs[imf, :])
        if imfFrequency > upperFreqCutoff:
            pass
        elif imfFrequency < lowerFreqCutoff:
            pass
        else:
            outputSignal += IMFs[imf, :]
    return (outputSignal)