Skip to content

Instantly share code, notes, and snippets.

@ben741
Last active November 23, 2023 21:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save ben741/d8c70b608d96d9f7ed231086b237ba6b to your computer and use it in GitHub Desktop.
Save ben741/d8c70b608d96d9f7ed231086b237ba6b to your computer and use it in GitHub Desktop.
Find indices of local minima and maxima of a numpy array
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace (0, 50, 1000)
y = 0.75 * np.sin(x)
peaks = np.where((y[1:-1] > y[0:-2]) * (y[1:-1] > y[2:]))[0] + 1
dips = np.where((y[1:-1] < y[0:-2]) * (y[1:-1] < y[2:]))[0] + 1
# The above makes a list of all indices where the value of y[i] is greater than both of its neighbours
# It does not check the endpoints, which only have one neighbour each
# The extra +1 at the end is necessary because where finds the indices within the slice y[1:-1],
# not the full array y. The [0] is necessary because where returns a tuple of arrays, where the first element
# is the array we want.
plt.plot (x, y)
plt.plot (x[peaks], y[peaks], 'o')
plt.plot (x[dips], y[dips], 'o')
plt.show()
@divyesh-mistry
Copy link

this is best one till now

@rotorrest
Copy link

=)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment