wosker4yan
wosker4yan

Reputation: 323

How to find maximum peak location, index with scipy?

I want to find the location of the maximum peak how can I do it? I am using scipy.signal for finding peaks. I want the code to return the location (in ums) for the peak.

enter image description here

Upvotes: 0

Views: 7216

Answers (1)

VRehnberg
VRehnberg

Reputation: 592

If you want to find the highest of the peaks identified by scipy.signal.find_peaks then you can do the following:

import numpy as np
from scipy.signal import find_peaks
import matplotlib.pyplot as plt

# Example data
x = np.linspace(-4000, 4000)  # equal spacing needed for find_peaks
y = np.sin(x / 1000) + 0.1 * np.random.rand(*x.shape)

# Find peaks
i_peaks, _ = find_peaks(y)

# Find the index from the maximum peak
i_max_peak = i_peaks[np.argmax(y[i_peaks])]

# Find the x value from that index
x_max = x[i_max_peak]

# Plot the figure
plt.plot(x, y)
plt.plot(x[i_peaks], y[i_peaks], 'x')
plt.axvline(x=x_max, ls='--', color="k")
plt.show()

enter image description here

If you just want the highest point then do just use argmax as Sembei suggests.

Upvotes: 5

Related Questions