Guest User

Untitled

a guest
Aug 18th, 2019
67
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import hyperspy.api as hs
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4.  
  5. def Gauss2D(X, Y, A=1, cx=0, cy=0, sx=1, sy=1):
  6. return A*np.exp(-( ((X - cx)**2)/(2*sx**2) + ((Y - cy)**2)/(2*sy**2)))
  7.  
  8. def Gauss(x, A=1, c=0, s=1):
  9. return A*np.exp(-((x - c)**2)/(2*s**2))
  10.  
  11. # Create some moving gaussians
  12. np.random.seed(0)
  13. X,Y = np.meshgrid(np.arange(20), np.arange(20))
  14. g2d = Gauss2D(X,Y, sx=3, sy=4, cx=7, cy=7)
  15. g2d /= g2d.max()
  16. g2d += 0.5
  17. g2d *= 15
  18.  
  19. g2db = Gauss2D(X,Y, sx=3, sy=4, cx=15, cy=9)
  20. g2db /= g2db.max()
  21. g2db += 0.5
  22. g2db *= 15
  23.  
  24. g2dc = Gauss2D(X,Y, sx=3.5, sy=5, cx=15, cy=9)
  25. g2dc /= g2dc.max()
  26. g2dc += 1
  27. g2dc *= 40
  28.  
  29. x = np.linspace(0, 100, 1000)
  30. data = np.zeros(g2d.shape + x.shape)
  31.  
  32. for index, val in np.ndenumerate(g2d):
  33. i,j = index
  34. c = g2d[i,j]
  35. a2 = g2db[i,j]
  36. c2 = g2dc[i,j]
  37. data[i,j] += Gauss(x, A=c, c=c, s=3)
  38. data[i,j] += Gauss(x, A=a2/2, c=c2, s=3)
  39.  
  40. # add a lot of noise
  41. noise = 5 * (np.random.random(data.shape) - 0.5)
  42. data += noise
  43.  
  44. s = hs.signals.Signal1D(data)
  45. s.axes_manager[-1].scale = np.diff(x)[0]
  46.  
  47. # Create and fit normal model
  48. m = s.create_model()
  49. g1 = hs.model.components1D.Gaussian(centre=8.)
  50. g2 = hs.model.components1D.Gaussian(centre=40.)
  51. g1.name = 'g1'
  52. g2.name = 'g2'
  53. m.extend([g1,g2])
  54.  
  55. m.multifit()
  56. normal = g1.A.as_signal() + g2.A.as_signal()
  57.  
  58. # Create and fit zigzag model
  59. m = s.create_model()
  60. g1 = hs.model.components1D.Gaussian(centre=8.)
  61. g2 = hs.model.components1D.Gaussian(centre=40.)
  62. m.extend([g1,g2])
  63.  
  64. m.multifit(zigzag=True)
  65. zigzag = g1.A.as_signal() + g2.A.as_signal()
  66.  
  67. hs.plot.plot_images([s.T.sum(), normal, zigzag], axes_decor=None, label=['Raw Signal', 'Normal', 'Zigzag'])
RAW Paste Data