Advertisement
Guest User

mahmud

a guest
Nov 14th, 2019
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as pl
  3. from scipy.signal import argrelextrema
  4. from scipy.interpolate import CubicSpline
  5. import sys
  6. sys.setrecursionlimit(10000)
  7.  
  8.  
  9. def findExtremum(sgnl):
  10.  
  11. extPoints = np.array([(x,y) for x in argrelextrema(sgnl, np.less) for y in argrelextrema(sgnl, np.greater) ])
  12. return extPoints;
  13.  
  14. def getEnvelopeMean(sgnl):
  15. extremePoints = findExtremum(sgnl)
  16. #define lower envelope Xs
  17. lx = extremePoints[0,0]
  18. lx = np.insert(lx,0,0)
  19. lx = np.append(lx,(len(sgnl)-1))
  20.  
  21. #define lower envelope Ys
  22. ly = np.array([(sgnl[x]) for x in lx])
  23.  
  24. #define upper envelope Xs
  25. ux = extremePoints[0,1]
  26. ux = np.insert(ux,0,0)
  27. ux = np.append(ux,(len(sgnl)-1))
  28.  
  29. #define upper envelope Ys
  30. uy = np.array([(sgnl[x]) for x in ux])
  31. #FIND LOWER ENVELOPE using cubic spline interpolation
  32. y1 = CubicSpline(lx,ly)
  33. x1 = np.linspace(lx.min(), lx.max(), sgnl.size)
  34.  
  35. #FIND UPPER ENVELOPE using cubic spline interpolation
  36. y2 = CubicSpline(ux,uy)
  37. x2 = np.linspace(ux.min(), ux.max(), sgnl.size)
  38.  
  39. #returns mean envelope signal
  40. return np.array(y1(x1)+y2(x2)/2.0)
  41.  
  42. def sdCondition(sgnl):
  43. #Returns true if stopping condition of standart deviation is true
  44. return np.std(sgnl)<0.1
  45.  
  46. def isMonotonic(sgnl):
  47. dx = np.diff(sgnl) #dx is an array. np.diff(sgnl) calculates discrete difference along given axis
  48. return np.all(dx <= 0) or np.all(dx >= 0) #returns true if array is either increasing or decreasing
  49.  
  50. def isSignalZero(sgnl):
  51. return (not np.any(sgnl)) #returns true if all elements of array is zero. np.any tests whether any array element along a given axis evaluates to True.
  52.  
  53.  
  54. def isImf(sgnl):
  55. extremas = findExtremum(sgnl)
  56.  
  57. #counts how many times signal crosses 0 by counting sign changes
  58. zero_crossings = (np.diff(np.sign(sgnl)) != 0).sum()
  59.  
  60. #count number of optimas (minima count + maxima count)
  61. extremaCount = extremas[0,0].size + extremas[0,1].size
  62.  
  63. #returns true if conditions of IMF are true. Those are number of zero crossings and extremas must be equal or differ by 1, and mean envelope should be zero
  64. return (((zero_crossings==extremaCount)or(np.abs(zero_crossings-extremaCount))==1) and isSignalZero(np.around(getEnvelopeMean(sgnl),decimals=1)))
  65.  
  66.  
  67.  
  68. sumc = 0
  69. k= 0
  70. c = np.zeros(100000,dtype=object)
  71. r = np.zeros(100000,dtype=object)
  72. residue = np.array([])
  73.  
  74.  
  75.  
  76. #copied EMG signal filtering based on Empirical Mode Decomposition
  77.  
  78. '''
  79. (a) x (an auxiliary variable) is set to the signal S(t) to be
  80. analysed, and a variable k, which is the number of estimated
  81. IMFs, is set to zero.
  82. (b) Splines are fitted to the upper extrema and the lower
  83. extrema. This will define the lower (LE) and upper
  84. envelopes (UE).
  85. (c) The average envelope, m, is calculated as the arithmetic
  86. mean between UE and LE.
  87. (d) A candidate IMF, h, is estimated as the difference between x
  88. and m.
  89. (e) If h does not fulfill the criteria defining an IMF, it is assigned
  90. to the variable x and the steps (b)–(d) are repeated.
  91. Otherwise, if h is an IMF then the procedure moves to step
  92. (f).
  93. (f) If h is an IMF it is saved as ck, where k is the kth component.
  94. (g) The mean squared error, mse, between two consecutive IMFs
  95. Cki and ck, is calculated, and this value is compared to a
  96. stopping condition (usually a very small value, i.e. 105
  97. ).
  98. (h) If the stopping condition is not reached, the partial residue,
  99. rk, is estimated as the difference between a previous partial
  100. residue rk1 and ck, and its content is assigned to the dummy
  101. variable x and the steps of (b)–(d) are repeated.
  102. (i) If the stopping condition is reached then the sifting process
  103. is finalized and the final residue rfinal can be estimated as the
  104. difference between S(t) and the sum of all IMFs.
  105.  
  106. '''
  107.  
  108.  
  109. def emd(x,orgs):
  110. global k, c, r, residue, sumc
  111. m = getEnvelopeMean(x)
  112. h= x-m
  113. if(not isImf(h)):
  114. x = h
  115. emd(x,orgs)
  116. else:
  117. k+=1
  118. c[k] = h
  119. c[0] = c[1]
  120. if(not sdCondition(c[k])):
  121. r[0] = orgs
  122. r[k] = r[k-1] - c[k]
  123. x = r[k]
  124. emd(x,orgs)
  125. else:
  126. for i in range(1,k+1):
  127. sumc += c[i]
  128. residue = orgs - sumc
  129.  
  130. #Draws Original signal, IMFS, and Residue
  131. def drawIMF(x,y):
  132. global k , c, residue
  133. pl.subplot(k+1,1,1)
  134. pl.plot(x,y,'b-',label = "Original signal")
  135. pl.legend()
  136. for i in range(2,k+1):
  137. pl.subplot(k+1,1,i)
  138. pl.plot(x,c[i-1],'r-',label = "IMF"+str(i-1))
  139. pl.legend()
  140. pl.subplot(k+1,1,k+1)
  141. pl.plot(x,residue,'g-', label = "Residue")
  142. pl.legend()
  143.  
  144. x= np.arange(0,1,0.001)
  145. y = np.cos(4*np.pi*x)+np.sin(4*np.pi*x)
  146.  
  147.  
  148. emd(y,y)
  149. drawIMF(x,y)
  150. pl.tight_layout()
  151. pl.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement