Last active
November 23, 2023 21:21
-
-
Save ben741/d8c70b608d96d9f7ed231086b237ba6b to your computer and use it in GitHub Desktop.
Find indices of local minima and maxima of a numpy array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this is best one till now