Ejemplo n.º 1
0
 def stop_EMD(self):
     """Check if there are enough extrema (3) to continue sifting."""
     if self.is_mode_complex:
         ner = []
         for k in range(self.ndirs):
             phi = k * pi / self.ndirs
             indmin, indmax, _ = extr(
                 np.real(np.exp(1j * phi) * self.residue))
             ner.append(len(indmin) + len(indmax))
         stop = np.any(ner < 3)
     else:
         indmin, indmax, _ = extr(self.residue)
         ner = len(indmin) + len(indmax)
         stop = ner < 3
     return stop
Ejemplo n.º 2
0
 def test_zerocrossings_random(self):
     """
     Test if the zero crossings are accurate for a trended sinusoid.
     """
     _, _, indzer = utils.extr(self.random_data)
     neighbours = np.zeros((indzer.shape[0], 2))
     neighbours[:, 0] = self.random_data[indzer - 1]
     neighbours[:, 1] = self.random_data[indzer + 1]
     p = np.prod(neighbours, axis=1)
     self.assertTrue(np.all(p < 0))
Ejemplo n.º 3
0
 def test_zerocrossings_random(self):
     """
     Test if the zero crossings are accurate for a trended sinusoid.
     """
     _, _, indzer = utils.extr(self.random_data)
     neighbours = np.zeros((indzer.shape[0], 2))
     neighbours[:, 0] = self.random_data[indzer - 1]
     neighbours[:, 1] = self.random_data[indzer + 1]
     p = np.prod(neighbours, axis=1)
     self.assertTrue(np.all(p < 0))
Ejemplo n.º 4
0
 def test_extrema_random(self):
     """
     Test if local extrema are detected properly for random data.
     """
     indmin, indmax, _ = utils.extr(self.random_data)
     min_neighbours = np.zeros((indmin.shape[0], 2))
     max_neighbours = np.zeros((indmax.shape[0], 2))
     min_neighbours[:, 0] = self.random_data[indmin - 1]
     min_neighbours[:, 1] = self.random_data[indmin + 1]
     max_neighbours[:, 0] = self.random_data[indmax - 1]
     max_neighbours[:, 1] = self.random_data[indmax + 1]
     minima = self.random_data[indmin].reshape(indmin.shape[0], 1)
     self.assertTrue(np.all(min_neighbours >= minima))
     maxima = self.random_data[indmax].reshape(indmax.shape[0], 1)
     self.assertTrue(np.all(max_neighbours <= maxima))
Ejemplo n.º 5
0
 def test_extrema_sinusoid(self):
     """
     Test if local extrema are detected properly for a trended sinusoid.
     """
     indmin, indmax, _ = utils.extr(self.sinusoid)
     min_neighbours = np.zeros((indmin.shape[0], 2))
     max_neighbours = np.zeros((indmax.shape[0], 2))
     min_neighbours[:, 0] = self.sinusoid[indmin - 1]
     min_neighbours[:, 1] = self.sinusoid[indmin + 1]
     max_neighbours[:, 0] = self.sinusoid[indmax - 1]
     max_neighbours[:, 1] = self.sinusoid[indmax + 1]
     minima = self.sinusoid[indmin].reshape(indmin.shape[0], 1)
     self.assertTrue(np.all(min_neighbours >= minima))
     maxima = self.sinusoid[indmax].reshape(indmax.shape[0], 1)
     self.assertTrue(np.all(max_neighbours <= maxima))
Ejemplo n.º 6
0
    def stop_EMD(self):
        """Check if there are enough extrema (3) to continue sifting.

        Returns
        -------
        bool
            Whether to stop further cubic spline interpolation for lack of
            local extrema.

        """
        if self.is_bivariate:
            stop = False
            for k in range(self.ndirs):
                phi = k * pi / self.ndirs
                indmin, indmax, _ = extr(
                    np.real(np.exp(1j * phi) * self.residue))
                if len(indmin) + len(indmax) < 3:
                    stop = True
                    break
        else:
            indmin, indmax, _ = extr(self.residue)
            ner = len(indmin) + len(indmax)
            stop = ner < 3
        return stop
Ejemplo n.º 7
0
 def test_extrema_random(self):
     """
     Test if local extrema are detected properly for random data.
     """
     indmin, indmax, _ = utils.extr(self.random_data)
     min_neighbours = np.zeros((indmin.shape[0], 2))
     max_neighbours = np.zeros((indmax.shape[0], 2))
     min_neighbours[:, 0] = self.random_data[indmin - 1]
     min_neighbours[:, 1] = self.random_data[indmin + 1]
     max_neighbours[:, 0] = self.random_data[indmax - 1]
     max_neighbours[:, 1] = self.random_data[indmax + 1]
     minima = self.random_data[indmin].reshape(indmin.shape[0], 1)
     self.assertTrue(np.all(min_neighbours >= minima))
     maxima = self.random_data[indmax].reshape(indmax.shape[0], 1)
     self.assertTrue(np.all(max_neighbours <= maxima))
Ejemplo n.º 8
0
 def test_extrema_sinusoid(self):
     """
     Test if local extrema are detected properly for a trended sinusoid.
     """
     indmin, indmax, _ = utils.extr(self.sinusoid)
     min_neighbours = np.zeros((indmin.shape[0], 2))
     max_neighbours = np.zeros((indmax.shape[0], 2))
     min_neighbours[:, 0] = self.sinusoid[indmin - 1]
     min_neighbours[:, 1] = self.sinusoid[indmin + 1]
     max_neighbours[:, 0] = self.sinusoid[indmax - 1]
     max_neighbours[:, 1] = self.sinusoid[indmax + 1]
     minima = self.sinusoid[indmin].reshape(indmin.shape[0], 1)
     self.assertTrue(np.all(min_neighbours >= minima))
     maxima = self.sinusoid[indmax].reshape(indmax.shape[0], 1)
     self.assertTrue(np.all(max_neighbours <= maxima))
Ejemplo n.º 9
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))
                    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.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))
                    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]

            if len(tmin) <= 3:
                f = splrep(tmin, mmin, k=2)
            else:
                f = splrep(
                    tmin,
                    mmin)  #this can cause an error, fixed with a horrible hack

            envmin = splev(self.t, f)

            if len(tmax) <= 3:
                f = splrep(tmax, mmax, k=2)
            else:
                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
Ejemplo n.º 10
0
    def mean_and_amplitude(self, m):
        """ Compute the mean of the envelopes and the mode amplitudes.

        Parameters
        ----------
        m : array-like, shape (n_samples,)
            The input array or an itermediate value of the sifting process.

        Returns
        -------
        tuple
            A tuple containing the mean of the envelopes, the number of
            extrema, the number of zero crosssing and the estimate of the
            amplitude of themode.
        """
        # FIXME: The spline interpolation may not be identical with the MATLAB
        # implementation. Needs further investigation.
        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