Reputation: 49
I am using from scipy.signal import find_peaks
.
Is it possible to find all peaks greater than the specified threshold.
I am not completley certain if you can do this.
For example: indices = find_peaks(s_volts, threshold= 0.5*maxPeak)
I am trying to find all peaks that are greater than 50% of the max peak.
Upvotes: 0
Views: 3443
Reputation: 255
If you can define your threshold value beforehand you can use the height
argument. Borrowing from @omer-tuchfeld example:
from scipy.signal import find_peaks
import numpy as np
from matplotlib import pyplot as plt
x = np.array([1,2,3,2,1,2,3,2,1,2,3,4,3,2,1,2,3,4,7,4,3,2,1])
threshold = 0.5 * max(x)
peaks_indices = find_peaks(x, height=threshold)[0]
peaks_values = x[peaks_indices]
fig = plt.figure()
plt.plot(range(len(x)), x)
for index in peaks_indices:
plt.axvline(index)
plt.axhline(threshold)
plt.scatter(peaks_indices, peaks_values, s=200)
plt.show()
Upvotes: 1
Reputation: 3022
Don't think there's a built-in way to do that. Here's how you can go about it without scipy:
from scipy.signal import find_peaks
import numpy as np
x = np.array([1,2,3,2,1,2,3,2,1,2,3,4,3,2,1,2,3,4,7,4,3,2,1])
peaks_indices = find_peaks(x)[0]
peaks = np.array(list(zip(peaks_indices, x[peaks_indices])))
threshold = 0.5 * max(x[peaks_indices])
filtered_peaks = [(index, value) for index, value in peaks if value > threshold]
# If you just want the indices:
filtered_peaks_indices = [index for index, value in peaks if value > threshold]
# Or just want the values
filtered_peaks_values = [value for index, value in peaks if value > threshold]
# Visualize
from matplotlib import pyplot as plt
plt.plot(range(len(x)), x)
for index in peaks_indices:
plt.axvline(index)
plt.axhline(threshold)
plt.scatter(filtered_peaks_indices, filtered_peaks_values, s=200)
plt.show()
Upvotes: 2