Advertisement
SamirBaghirzada

EMD_Code_Signal_Processing

Nov 15th, 2019
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 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.3
  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. global N
  56. extremas = findExtremum(sgnl)
  57.  
  58. #counts how many times signal crosses 0 by counting sign changes
  59. zero_crossings = (np.diff(np.sign(sgnl)) != 0).sum()
  60.  
  61. #count number of optimas (minima count + maxima count)
  62. extremaCount = extremas[0,0].size + extremas[0,1].size
  63.  
  64. #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
  65. return (((zero_crossings==extremaCount)or(np.abs(zero_crossings-extremaCount))==1) and isSignalZero(np.around(getEnvelopeMean(sgnl),decimals=N)))
  66.  
  67.  
  68.  
  69. sumc = 0
  70. k= 0
  71. c = np.zeros(100000,dtype=object)
  72. r = np.zeros(100000,dtype=object)
  73. residue = np.array([])
  74.  
  75.  
  76.  
  77. def emd(x,orgs):
  78. global k, c, r, residue, sumc
  79. m = getEnvelopeMean(x)
  80. h= x-m
  81. if(not isImf(h)):
  82. x = h
  83. emd(x,orgs)
  84. else:
  85. k+=1
  86. c[k] = h
  87. c[0] = c[1]
  88. if(not sdCondition(c[k])):
  89. r[0] = orgs
  90. r[k] = r[k-1] - c[k]
  91. x = r[k]
  92. emd(x,orgs)
  93. else:
  94. for i in range(1,k+1):
  95. sumc += c[i]
  96. residue = orgs - sumc
  97.  
  98. #Draws Original signal, IMFS, and Residue
  99. def drawIMF(x,y):
  100. global k , c, residue
  101. pl.subplot(k+1,1,1).set_title("Original signal")
  102. pl.plot(x,y,'b-')
  103. for i in range(2,k+1):
  104. pl.subplot(k+1,1,i).set_title("IMF"+str(i-1))
  105. pl.plot(x,c[i-1],'r-')
  106.  
  107. pl.subplot(k+1,1,k+1).set_title("Residue")
  108. pl.plot(x,residue,'g-')
  109. print(k)
  110.  
  111.  
  112. x= np.arange(0,1,0.01)
  113. y = np.sin(1020*np.pi*x)+np.sin(np.pi*440*x)
  114. N = 2 #decimals to round mean (higher number means more precision)
  115.  
  116. emd(y,y)
  117. drawIMF(x,y)
  118. pl.tight_layout()
  119. pl.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement