Пример #1
0
def sift(x, ts):
    """One sifting iteration."""
    tmin, tmax, xmin, xmax = boundary_conditions(x, ts)
    tck = splrep(tmin, xmin)
    lower_envelop = splev(ts, tck)
    tck = splrep(tmax, xmax)
    upper_envelop = splev(ts, tck)
    mean_amplitude = np.abs(upper_envelop - lower_envelop) / 2
    local_mean = (upper_envelop + lower_envelop) / 2
    amplitude_error = np.abs(local_mean) / mean_amplitude
    return x - local_mean, amplitude_error.sum()
Пример #2
0
def emd(x, ts, n_imfs):
    imfs = np.zeros((n_imfs + 1, x.shape[0]))
    for i in range(n_imfs):
        ix = 0
        mode = x - imfs.sum(0)
        nmin, nmax, nzero = map(len, extr(mode))
        tmin, tmax, xmin, xmax = boundary_conditions(mode, ts)
        #             while abs((nmin + nmax) - nzero) > 1 and (not (utils.judge_stop(mode))):
        #                 mode, amplitude_error = sift(mode, ts)
        #                 if amplitude_error <= tol or (utils.judge_stop(mode)):
        #                     break
        if abs((nmin + nmax) - nzero) > 1 and (not (
                utils.judge_stop(mode))) and len(tmin) > 3 and len(tmax) > 3:
            mode, amplitude_error = sift(mode, ts)
        imfs[i, :] = mode
    imfs[-1, :] = x - imfs.sum(0)
    return imfs
    def mean_and_amplitude(self, m):

        if self.is_bivariate:
            if self.bivariate_mode == 'centroid':
                nem = []
                nzm = []
                envmin = np.zeros((self.ndirs, len(self.t)))
                envmax = np.zeros((self.ndirs, len(self.t)))
                for k in range(self.ndirs):
                    phi = k * pi / self.ndirs
                    y = np.real(np.exp(-1j * phi) * m)
                    indmin, indmax, indzer = extr(y)
                    nem.append(len(indmin) + len(indmax))
                    nzm.append(len(indzer))
                    if self.nbsym:
                        tmin, tmax, zmin, zmax = boundary_conditions(
                            y, self.t, m, self.nbsym)
                    else:
                        tmin = np.r_[self.t[0], self.t[indmin], self.t[-1]]
                        tmax = np.r_[self.t[0], self.t[indmax], self.t[-1]]
                        zmin, zmax = m[tmin], m[tmax]

                    f = splrep(tmin, zmin)
                    spl = splev(self.t, f)
                    envmin[k, :] = spl

                    f = splrep(tmax, zmax)
                    spl = splev(self.t, f)
                    envmax[k, :] = spl

                envmoy = np.mean((envmin + envmax) / 2, axis=0)
                amp = np.mean(abs(envmax - envmin), axis=0) / 2

            elif self.bivariate_mode == 'bbox_center':
                nem = []
                nzm = []
                envmin = np.zeros((self.ndirs, len(self.t)), dtype=complex)
                envmax = np.zeros((self.ndirs, len(self.t)), dtype=complex)
                for k in range(self.ndirs):
                    phi = k * pi / self.ndirs
                    y = np.real(np.exp(-1j * phi) * m)
                    indmin, indmax, indzer = extr(y)
                    nem.append(len(indmin) + len(indmax))
                    nzm.append(len(indzer))
                    if self.nbsym:
                        tmin, tmax, zmin, zmax = boundary_conditions(
                            y, self.t, m, self.nbsym)
                    else:
                        tmin = np.r_[self.t[0], self.t[indmin], self.t[-1]]
                        tmax = np.r_[self.t[0], self.t[indmax], self.t[-1]]
                        zmin, zmax = m[tmin], m[tmax]
                    f = splrep(tmin, zmin)
                    spl = splev(self.t, f)
                    envmin[k, ] = np.exp(1j * phi) * spl

                    f = splrep(tmax, zmax)
                    spl = splev(self.t, f)
                    envmax[k, ] = np.exp(1j * phi) * spl

                envmoy = np.mean((envmin + envmax), axis=0)
                amp = np.mean(abs(envmax - envmin), axis=0) / 2

        else:
            indmin, indmax, indzer = extr(m)
            nem = len(indmin) + len(indmax)
            nzm = len(indzer)
            if self.nbsym:
                tmin, tmax, mmin, mmax = boundary_conditions(
                    m, self.t, m, self.nbsym)
            else:
                tmin = np.r_[self.t[0], self.t[indmin], self.t[-1]]
                tmax = np.r_[self.t[0], self.t[indmax], self.t[-1]]
                mmin, mmax = m[tmin], m[tmax]

            f = splrep(tmin, mmin)
            envmin = splev(self.t, f)

            f = splrep(tmax, mmax)
            envmax = splev(self.t, f)

            envmoy = (envmin + envmax) / 2
            amp = np.abs(envmax - envmin) / 2.0
        if self.is_bivariate:
            nem = np.array(nem)
            nzm = np.array(nzm)

        return envmoy, nem, nzm, amp
Пример #4
0
    def decompose(self):
        if self.display_sifting:
            fig_h = plt.figure()

        A = not(self.stop_EMD())
        B = (self.k<self.MAXMODES+1 or self.MAXMODES==0)
        C = not(np.any(self.mask))

        while (A and B and C):

            # current mode
            m = self.r

            # mode at previous iteration
            mp = m.copy()

            # computing mean and stopping criterion
            if self.FIXE:
                stop_sift, moyenne = self.stop_sifting_fixe()
            elif self.FIXE_H:
                stop_count = 0
                stop_sift, moyenne = self.stop_sifting_fixe_h()
            else:
                stop_sift, moyenne, _ = self.stop_sifting(m)

            # in case current mode is small enough to cause spurious extrema
            if np.max(np.abs(m)) < (1e-10)*np.max(np.abs(self.x)):
                if not stop_sift:
                    warnings.warn("EMD Warning: Amplitude too small, stopping.")
                else:
                    print "Force stopping EMD: amplitude too small."
                return

            # SIFTING LOOP:
            while not(stop_sift) and (self.nbit<self.MAXITERATIONS):
                print "iteration {}".format(self.nbit)

                if (not(self.MODE_COMPLEX) and (self.nbit>self.MAXITERATIONS/5) \
                    and self.nbit%np.floor(self.MAXITERATIONS/10)==0 and \
                    not(self.FIXE) and self.nbit > 100):
                    print "Mode "+str(self.k) + ", Iteration " + str(self.nbit)
                    im, iM, _ = extr(m)
                    print str(np.sum(m[im]>0)) + " minima > 0; " + \
                          str(np.sum(m[im]<0)) + " maxima < 0."

                # Sifting
                m = m - moyenne

                # Computing mean and stopping criterion
                if self.FIXE:
                    stop_sift, moyenne = self.stop_sifting_fixe()
                elif self.FIXE_H:
                    stop_sift, moyenne, stop_count = self.stop_sifting_fixe_h()
                else:
                    stop_sift, moyenne, s = self.stop_sifting(m)

                # Display
                if self.display_sifting and self.MODE_COMPLEX:
                    indmin, indmax, _ = extr(m)
                    tmin, tmax, mmin, mmax = boundary_conditions(indmin,
                                                                 indmax,
                                                                 self.t, mp,
                                                                 mp,
                                                                 self.nbsym)

                    f = splrep(tmin,mmin)
                    envminp = splev(self.t,f)
                    f = splrep(tmax,mmax)
                    envmaxp = splev(self.t,f)

                    envmoyp = (envminp+envmaxp)/2;

                    if self.FIXE or self.FIXE_H:
                        self.display_emd_fixe(mp, envminp, envmaxp, envmoyp)
                    else:
                        sxp = 2*(np.abs(envmoyp))/np.abs(envmaxp-envminp)
                        sp = np.mean(sxp)
                        self.display_emd(mp, envminp, envmaxp, envmoyp, sp, sxp)

                mp = m
                self.nbit += 1
                self.NbIt += 1

                if (self.nbit==(self.MAXITERATIONS-1)) and not(self.FIXE) and (self.nbit>100):
                    warnings.warn("Emd:warning, Forced stop of sifting - "+
                                  "too many iterations")

            self.imf.append(m)
            if self.display_sifting:
                print "mode "+str(self.k)+ " stored"

            self.nbits.append(self.nbit)
            self.k += 1

            self.r = self.r - m
            ort = self.io()

            self.ort = ort
            return np.array(self.imf)
Пример #5
0
    def mean_and_amplitude(self, m):
        """ Computes the mean of the envelopes and the mode amplitudes."""
        # FIXME: The spline interpolation may not be identical with the MATLAB
        # implementation. Needs further investigation.
        if self.MODE_COMPLEX:
            if self.MODE_COMPLEX == 1:
                nem = []
                nzm = []
                envmin = np.zeros((self.ndirs,len(self.t)))
                envmax = np.zeros((self.ndirs,len(self.t)))
                for k in range(self.ndirs):
                    phi = k*pi/self.ndirs
                    y = np.real(np.exp(-1j*phi)*m)
                    indmin, indmax, indzer = extr(y)
                    nem.append(len(indmin)+len(indmax))
                    nzm.append(len(indzer))
                    tmin, tmax, zmin, zmax = boundary_conditions(indmin,
                                                                 indmax,
                                                                 self.t, y, m,
                                                                 self.nbsym)

                    f = splrep(tmin,zmin)
                    spl = splev(self.t,f)
                    envmin[k,:] = spl

                    f = splrep(tmax,zmax)
                    spl = splev(self.t,f)
                    envmax[k,:] = spl

                envmoy = np.mean((envmin+envmax)/2,axis=0)
                amp = np.mean(abs(envmax-envmin),axis=0)/2

            elif self.MODE_COMPLEX == 2:
                nem = []
                nzm = []
                envmin = np.zeros((self.ndirs,len(self.t)))
                envmax = np.zeros((self.ndirs,len(self.t)))
                for k in range(self.ndirs):
                    phi = k*pi/self.ndirs
                    y = np.real(np.exp(-1j*phi)*m)
                    indmin, indmax, indzer = extr(y)
                    nem.append(len(indmin)+len(indmax))
                    nzm.append(len(indzer))
                    tmin, tmax, zmin, zmax = boundary_conditions(indmin,
                                                                 indmax,
                                                                 self.t, y, m,
                                                                 self.nbsym)
                    f = splrep(tmin, zmin)
                    spl = splev(self.t,f)
                    envmin[k,:] = np.exp(1j*phi)*spl

                    f = splrep(tmax, zmax)
                    spl = splev(self.t,f)
                    envmax[k,:] = np.exp(1j*phi)*spl

                envmoy = np.mean((envmin+envmax),axis=0)
                amp = np.mean(abs(envmax-envmin),axis=0)/2

        else:
            indmin, indmax, indzer = extr(m)
            nem = len(indmin)+len(indmax)
            nzm = len(indzer)
            tmin, tmax, mmin, mmax = boundary_conditions(indmin, indmax,
                                                         self.t, m, m,
                                                         self.nbsym)

            f = splrep(tmin, mmin)
            envmin = splev(self.t,f)

            f = splrep(tmax, mmax)
            envmax = splev(self.t,f);

            envmoy = (envmin+envmax)/2;
            amp = np.abs(envmax-envmin)/2.0

        return envmoy, nem, nzm, amp
Пример #6
0
    def mean_and_amplitude(self, m):
        """ Computes the mean of the envelopes and the mode amplitudes."""
        # FIXME: The spline interpolation may not be identical with the MATLAB
        # implementation. Needs further investigation.
        if self.is_mode_complex:
            if self.is_mode_complex == 1:
                nem = []
                nzm = []
                envmin = np.zeros((self.ndirs, len(self.t)))
                envmax = np.zeros((self.ndirs, len(self.t)))
                for k in range(self.ndirs):
                    phi = k * pi / self.ndirs
                    y = np.real(np.exp(-1j * phi) * m)
                    indmin, indmax, indzer = extr(y)
                    nem.append(len(indmin) + len(indmax))
                    nzm.append(len(indzer))
                    tmin, tmax, zmin, zmax = boundary_conditions(
                        y, self.t, m, self.nbsym)

                    f = splrep(tmin, zmin)
                    spl = splev(self.t, f)
                    envmin[k, :] = spl

                    f = splrep(tmax, zmax)
                    spl = splev(self.t, f)
                    envmax[k, :] = spl

                envmoy = np.mean((envmin + envmax) / 2, axis=0)
                amp = np.mean(abs(envmax - envmin), axis=0) / 2

            elif self.is_mode_complex == 2:
                nem = []
                nzm = []
                envmin = np.zeros((self.ndirs, len(self.t)))
                envmax = np.zeros((self.ndirs, len(self.t)))
                for k in range(self.ndirs):
                    phi = k * pi / self.ndirs
                    y = np.real(np.exp(-1j * phi) * m)
                    indmin, indmax, indzer = extr(y)
                    nem.append(len(indmin) + len(indmax))
                    nzm.append(len(indzer))
                    tmin, tmax, zmin, zmax = boundary_conditions(
                        y, self.t, m, self.nbsym)
                    f = splrep(tmin, zmin)
                    spl = splev(self.t, f)
                    envmin[k, ] = np.exp(1j * phi) * spl

                    f = splrep(tmax, zmax)
                    spl = splev(self.t, f)
                    envmax[k, ] = np.exp(1j * phi) * spl

                envmoy = np.mean((envmin + envmax), axis=0)
                amp = np.mean(abs(envmax - envmin), axis=0) / 2

        else:
            indmin, indmax, indzer = extr(m)
            nem = len(indmin) + len(indmax)
            nzm = len(indzer)
            tmin, tmax, mmin, mmax = boundary_conditions(
                m, self.t, m, self.nbsym)

            f = splrep(tmin, mmin)
            envmin = splev(self.t, f)

            f = splrep(tmax, mmax)
            envmax = splev(self.t, f)

            envmoy = (envmin + envmax) / 2
            amp = np.abs(envmax - envmin) / 2.0

        return envmoy, nem, nzm, amp