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)
def test_akima(self): dtype = np.float32 emd = EMD() emd.splineKind = 'akima' emd.DTYPE = dtype arr = lambda x: np.array(x) # Test error: len(X)!=len(Y) with self.assertRaises(ValueError): akima(arr([0]), arr([1, 2]), arr([0, 1, 2])) # Test error: any(dt) <= 0 with self.assertRaises(ValueError): akima(arr([1, 0, 2]), arr([1, 2]), arr([0, 1, 2])) with self.assertRaises(ValueError): akima(arr([0, 0, 2]), arr([1, 2]), arr([0, 1, 1])) # Test for correct responses T = np.array([0, 1, 2, 3, 4], dtype) S = np.array([0, 1, -1, -1, 5], dtype) t = np.array([i / 2. for i in range(9)], dtype) _t, s = emd.spline_points(t, np.array((T, S))) s_true = np.array([ S[0], 0.9125, S[1], 0.066666, S[2], -1.35416667, S[3], 1.0625, S[4] ], dtype) self.assertTrue(np.allclose(s_true, s), "Comparing akima with true") s_np = akima(np.array(T), np.array(S), np.array(t)) self.assertTrue(np.allclose(s, s_np), "Shouldn't matter if with numpy")
def test_unsupporter_spline(self): emd = EMD() emd.splineKind = "waterfall" S = np.random.random(20) with self.assertRaises(ValueError): emd.emd(S)
def test_wrong_extrema_detection_type(self): emd = EMD() emd.extrema_detection = "very_complicated" t = np.arange(10) s = np.array([-1, 0, 1, 0, -1, 0, 3, 0, -9, 0]) with self.assertRaises(ValueError): maxPos, maxVal, minPos, minVal, nz = emd.find_extrema(t, s)
def __init__(self): # Import libraries from PyEMD.EMD import EMD # Ensemble constants self.noise_width = 0.3 self.trials = 100 self.EMD = EMD() self.EMD.FIXE_H = 5
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")
def test_slinear(self): dtype = np.float64 emd = EMD() emd.splineKind = 'slinear' emd.DTYPE = dtype T = np.array([0, 1, 2, 3, 4], dtype=dtype) S = np.array([0, 1, -1, -1, 5], dtype=dtype) t = np.arange(9, dtype=dtype) / 2. _t, s = emd.spline_points(t, np.array((T, S))) s_true = np.array([S[0], 0.5, S[1], 0, \ S[2], -1, S[3], 2, S[4]], dtype=dtype) self.assertTrue(np.allclose(s, s_true), "Comparing SLinear")
def test_find_extrema_simple(self): """ Simple test for extrema. """ emd = EMD() emd.extrema_detection = "simple" t = np.arange(10) s = np.array([-1, 0, 1, 0, -1, 0, 3, 0, -9, 0]) expMaxPos = [2, 6] expMaxVal = [1, 3] expMinPos = [4, 8] expMinVal = [-1, -9] maxPos, maxVal, minPos, minVal, nz = emd.find_extrema(t, s) self.assertEqual(maxPos.tolist(), expMaxPos) self.assertEqual(maxVal.tolist(), expMaxVal) self.assertEqual(minPos.tolist(), expMinPos) self.assertEqual(minVal.tolist(), expMinVal)
def test_find_extrema_simple_repeat(self): """ Test what happens in /^^\ situation, i.e. when extremum is somewhere between two consecutive pts. """ emd = EMD() emd.extrema_detection = "simple" t = np.arange(2, 13) s = np.array([-1, 0, 1, 1, 0, -1, 0, 3, 0, -9, 0]) expMaxPos = [4, 9] expMaxVal = [1, 3] expMinPos = [7, 11] expMinVal = [-1, -9] maxPos, maxVal, minPos, minVal, nz = emd.find_extrema(t, s) self.assertEqual(maxPos.tolist(), expMaxPos) self.assertEqual(maxVal.tolist(), expMaxVal) self.assertEqual(minPos.tolist(), expMinPos) self.assertEqual(minVal.tolist(), expMinVal)
def test_cubic(self): dtype = np.float64 emd = EMD() emd.splineKind = 'cubic' emd.DTYPE = dtype T = np.array([0, 1, 2, 3, 4], dtype=dtype) S = np.array([0, 1, -1, -1, 5], dtype=dtype) t = np.arange(9, dtype=dtype) / 2. # TODO: Something weird with float32. # Seems to be SciPy problem. _t, s = emd.spline_points(t, np.array((T, S))) s_true = np.array([S[0], 1.203125, S[1], 0.046875, \ S[2], -1.515625, S[3], 1.015625, S[4]], dtype=dtype) self.assertTrue(np.allclose(s, s_true), "Comparing cubic") T = T[:-2].copy() S = S[:-2].copy() t = np.arange(5, dtype=dtype) / 2. _t, s3 = emd.spline_points(t, np.array((T, S))) s3_true = np.array([S[0], 0.78125, S[1], 0.28125, S[2]], dtype=dtype) self.assertTrue(np.allclose(s3, s3_true), "Compare cubic 3pts")
def test_single_imf(self): """ Input is IMF. Expecint single shifting. """ maxDiff = lambda a, b: np.max(np.abs(a - b)) emd = EMD() emd.FIXE_H = 5 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) IMF = emd.emd(S, t) self.assertEqual(IMF.shape[0], 1, "Expecting sin + trend") diff = np.allclose(IMF[0], c1) self.assertTrue( diff, "Expecting 1st IMF to be sin\nMaxDiff = " + str(maxDiff(IMF[0], c1))) # Input - linear function f(t) = siin(2Hz t) + 2*t c2 = 5 * (t + 2) S += c2.copy() IMF = emd.emd(S, t) self.assertEqual(IMF.shape[0], 2, "Expecting sin + trend") diff1 = np.allclose(IMF[0], c1, atol=0.2) self.assertTrue( diff1, "Expecting 1st IMF to be sin\nMaxDiff = " + str(maxDiff(IMF[0], c1))) diff2 = np.allclose(IMF[1], c2, atol=0.2) self.assertTrue( diff2, "Expecting 2nd IMF to be trend\nMaxDiff = " + str(maxDiff(IMF[1], c2)))
class EEMD: logger = logging.getLogger(__name__) def __init__(self): # Import libraries from PyEMD.EMD import EMD # Ensemble constants self.noise_width = 0.3 self.trials = 100 self.EMD = EMD() self.EMD.FIXE_H = 5 def eemd(self, S, T=None, max_imf=None): if T is None: T = np.arange(len(S), dtype=S.dtype) if max_imf is None: max_imf = -1 N = len(S) E_IMF = np.zeros((1,N)) for trial in range(self.trials): self.logger.debug("trial: "+str(trial)) noise = np.random.normal(loc=0, scale=self.noise_width, size=N) IMFs = self.emd(S+noise, T, max_imf) imfNo = IMFs.shape[0] while(E_IMF.shape[0] < imfNo): E_IMF = np.vstack((E_IMF, np.zeros(N))) E_IMF[:imfNo] += IMFs E_IMF /= self.trials return E_IMF def emd(self, S, T, max_imf=-1): return self.EMD.emd(S, T, max_imf)
def test_bound_extrapolation_simple(self): emd = EMD() emd.extrema_detection = "simple" emd.nbsym = 1 emd.DTYPE = np.int64 S = [0, -3, 1, 4, 3, 2, -2, 0, 1, 2, 1, 0, 1, 2, 5, 4, 0, -2, -1] S = np.array(S) T = np.arange(len(S)) pp = emd.prepare_points # There are 4 cases for both (L)eft and (R)ight ends. In case of left (L) bound: # L1) ,/ -- ext[0] is min, s[0] < ext[1] (1st max) # L2) / -- ext[0] is min, s[0] > ext[1] (1st max) # L3) ^. -- ext[0] is max, s[0] > ext[1] (1st min) # L4) \ -- ext[0] is max, s[0] < ext[1] (1st min) ## CASE 1 # L1, R1 -- no edge MIN & no edge MIN s = S.copy() t = T.copy() maxPos, maxVal, minPos, minVal, nz = emd.find_extrema(t, s) # Should extrapolate left and right bounds maxExtrema, minExtrema = pp(t, s, \ maxPos, maxVal, minPos, minVal) self.assertEqual([-1, 3, 9, 14, 20], maxExtrema[0].tolist()) self.assertEqual([4, 4, 2, 5, 5], maxExtrema[1].tolist()) self.assertEqual([-4, 1, 6, 11, 17, 23], minExtrema[0].tolist()) self.assertEqual([-2, -3, -2, 0, -2, 0], minExtrema[1].tolist()) ## CASE 2 # L2, R2 -- edge MIN, edge MIN s = S[1:-1].copy() t = T[1:-1].copy() maxPos, maxVal, minPos, minVal, nz = emd.find_extrema(t, s) # Should extrapolate left and right bounds maxExtrema, minExtrema = pp(t, s, \ maxPos, maxVal, minPos, minVal) self.assertEqual([-1, 3, 9, 14, 20], maxExtrema[0].tolist()) self.assertEqual([4, 4, 2, 5, 5], maxExtrema[1].tolist()) self.assertEqual([1, 6, 11, 17], minExtrema[0].tolist()) self.assertEqual([-3, -2, 0, -2], minExtrema[1].tolist()) ## CASE 3 # L3, R3 -- no edge MAX & no edge MAX s, t = S[2:-3], T[2:-3] maxPos, maxVal, minPos, minVal, nz = emd.find_extrema(t, s) # Should extrapolate left and right bounds maxExtrema, minExtrema = pp(t, s, \ maxPos, maxVal, minPos, minVal) self.assertEqual([-3, 3, 9, 14, 19], maxExtrema[0].tolist()) self.assertEqual([2, 4, 2, 5, 2], maxExtrema[1].tolist()) self.assertEqual([0, 6, 11, 17], minExtrema[0].tolist()) self.assertEqual([-2, -2, 0, 0], minExtrema[1].tolist()) ## CASE 4 # L4, R4 -- edge MAX & edge MAX s, t = S[3:-4], T[3:-4] maxPos, maxVal, minPos, minVal, nz = emd.find_extrema(t, s) # Should extrapolate left and right bounds maxExtrema, minExtrema = pp(t, s, \ maxPos, maxVal, minPos, minVal) self.assertEqual([3, 9, 14], maxExtrema[0].tolist()) self.assertEqual([4, 2, 5], maxExtrema[1].tolist()) self.assertEqual([0, 6, 11, 17], minExtrema[0].tolist()) self.assertEqual([-2, -2, 0, 0], minExtrema[1].tolist())
def test_bound_extrapolation_parabol(self): emd = EMD() emd.extrema_detection = "parabol" emd.nbsym = 1 emd.DTYPE = np.float64 S = [0, -3, 1, 4, 3, 2, -2, 0, 1, 2, 1, 0, 1, 2, 5, 4, 0, -2, -1] S = np.array(S) T = np.arange(len(S)) pp = emd.prepare_points # There are 4 cases for both (L)eft and (R)ight ends. In case of left (L) bound: # L1) ,/ -- ext[0] is min, s[0] < ext[1] (1st max) # L2) / -- ext[0] is min, s[0] > ext[1] (1st max) # L3) ^. -- ext[0] is max, s[0] > ext[1] (1st min) # L4) \ -- ext[0] is max, s[0] < ext[1] (1st min) ## CASE 1 # L1, R1 -- no edge MIN & no edge MIN s = S.copy() t = T.copy() maxPos, maxVal, minPos, minVal, nz = emd.find_extrema(t, s) # Should extrapolate left and right bounds maxExtrema, minExtrema = pp(t, s, \ maxPos, maxVal, minPos, minVal) maxExtrema = np.round(maxExtrema, decimals=3) minExtrema = np.round(minExtrema, decimals=3) self.assertEqual([-1.393, 3.25, 9, 14.25, 20.083], maxExtrema[0].tolist()) self.assertEqual([4.125, 4.125, 2, 5.125, 5.125], maxExtrema[1].tolist()) self.assertEqual([-4.31, 0.929, 6.167, 11, 17.167, 23.333], minExtrema[0].tolist()) self.assertEqual([-2.083, -3.018, -2.083, 0, -2.042, 0], minExtrema[1].tolist()) ## CASE 2 # L2, R2 -- edge MIN, edge MIN s = S[1:-1].copy() t = T[1:-1].copy() maxPos, maxVal, minPos, minVal, nz = emd.find_extrema(t, s) # Should extrapolate left and right bounds maxExtrema, minExtrema = pp(t, s, \ maxPos, maxVal, minPos, minVal) maxExtrema = np.round(maxExtrema, decimals=3) minExtrema = np.round(minExtrema, decimals=3) self.assertEqual([-1.25, 3.25, 9, 14.25, 19.75], maxExtrema[0].tolist()) self.assertEqual([4.125, 4.125, 2, 5.125, 5.125], maxExtrema[1].tolist()) self.assertEqual([1, 6.167, 11, 17], minExtrema[0].tolist()) self.assertEqual([-3, -2.083, 0, -2], minExtrema[1].tolist()) ## CASE 3 # L3, R3 -- no edge MAX & no edge MAX s = S[2:-3].copy() t = T[2:-3].copy() maxPos, maxVal, minPos, minVal, nz = emd.find_extrema(t, s) # Should extrapolate left and right bounds maxExtrema, minExtrema = pp(t, s, \ maxPos, maxVal, minPos, minVal) maxExtrema = np.round(maxExtrema, decimals=3) minExtrema = np.round(minExtrema, decimals=3) self.assertEqual([-2.5, 3.25, 9, 14.25, 19.5], maxExtrema[0].tolist()) self.assertEqual([2, 4.125, 2, 5.125, 2], maxExtrema[1].tolist()) self.assertEqual([0.333, 6.167, 11, 17.5], minExtrema[0].tolist()) self.assertEqual([-2.083, -2.083, 0, 0], minExtrema[1].tolist()) ## CASE 4 # L4, R4 -- edge MAX & edge MAX s = S[3:-4].copy() t = T[3:-4].copy() maxPos, maxVal, minPos, minVal, nz = emd.find_extrema(t, s) # Should extrapolate left and right bounds maxExtrema, minExtrema = pp(t, s, \ maxPos, maxVal, minPos, minVal) maxExtrema = np.round(maxExtrema, decimals=3) minExtrema = np.round(minExtrema, decimals=3) self.assertEqual([3, 9, 14], maxExtrema[0].tolist()) self.assertEqual([4, 2, 5], maxExtrema[1].tolist()) self.assertEqual([-0.167, 6.167, 11, 17], minExtrema[0].tolist()) self.assertEqual([-2.083, -2.083, 0, 0], minExtrema[1].tolist())
import numpy as np import pylab as plt from PyEMD.EMD import EMD t = np.linspace(0, 1, 200) tt = t * t s = np.cos(11 * 2 * np.pi * tt) + 6 * tt IMF = EMD().emd(s, t) N = IMF.shape[0] + 1 plt.subplot(N, 1, 1) plt.plot(t, s, 'r') plt.title("Input signal: $S(t)=cos(22\pi t^2) + 6t^2$") plt.xlabel("Time [s]") for n, imf in enumerate(IMF): plt.subplot(N, 1, n + 2) plt.plot(t, imf, 'g') plt.title("IMF " + str(n + 1)) plt.xlabel("Time [s]") plt.tight_layout() plt.savefig('simple_example') plt.show()
y_filtrada_dislex = butter_lowpass_filter(senial_indep_2500_muestras_dislexia, cutoff_l, fs, order) y_filtrada_dislex = butter_highpass_filter(y_filtrada_dislex, cutoff_h, fs, order) senial_indep_2500_muestras = y_filtrada senial_indep_2500_muestras_dislexia = y_filtrada_dislex # from PyEMD import EMD,visualisation from PyEMD.EMD import EMD from PyEMD.visualisation import Visualisation # Perform decomposition print("Performing decomposition... ") emd = EMD() emd.emd(senial_indep_2500_muestras, max_imf=5) emd_dis = EMD() emd_dis.emd(senial_indep_2500_muestras_dislexia, max_imf=5) imfs, res = emd.get_imfs_and_residue() imfs_dis, res_dis = emd_dis.get_imfs_and_residue() vis = Visualisation() print("Sujeto Normal") vis.plot_imfs(imfs=imfs, residue=res, t=time, include_residue=False) vis.plot_instant_freq(time, imfs=imfs) vis.show()