Advertisement
Guest User

Untitled

a guest
May 25th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. x = np.linspace (0, 50, 1000)
  5. y = 0.75 * np.sin(x)
  6.  
  7. peaks = np.where((y[1:-1] > y[0:-2]) * (y[1:-1] > y[2:]))[0] + 1
  8. dips = np.where((y[1:-1] < y[0:-2]) * (y[1:-1] < y[2:]))[0] + 1
  9.  
  10. # The above makes a list of all indices where the value of y[i] is greater than both of its neighbours
  11. # It does not check the endpoints, which only have one neighbour each
  12. # The extra +1 at the end is necessary because where finds the indices within the slice y[1:-1],
  13. # not the full array y. The [0] is necessary because where returns a tuple of arrays, where the first element
  14. # is the array we want.
  15.  
  16. plt.plot (x, y)
  17. plt.plot (x[peaks], y[peaks], 'o')
  18. plt.plot (x[dips], y[dips], 'o')
  19.  
  20. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement