Skip to content

Instantly share code, notes, and snippets.

View ben741's full-sized avatar

Benjamin Schmidt ben741

View GitHub Profile
@ben741
ben741 / local_maxima.py
Last active November 23, 2023 21:21
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