Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find local maximums in numpy array

I am looking to find the peaks in some gaussian smoothed data that I have. I have looked at some of the peak detection methods available but they require an input range over which to search and I want this to be more automated than that. These methods are also designed for non-smoothed data. As my data is already smoothed I require a much more simple way of retrieving the peaks. My raw and smoothed data is in the graph below.

enter image description here

Essentially, is there a pythonic way of retrieving the max values from the array of smoothed data such that an array like

    a = [1,2,3,4,5,4,3,2,1,2,3,2,1,2,3,4,5,6,5,4,3,2,1]

would return:

    r = [5,3,6]
like image 715
Nathan Thomas Avatar asked Feb 09 '16 01:02

Nathan Thomas


People also ask

How do you find the local maxima of a numpy array?

Approach: The idea is to iterate over the given array arr[] and check if each element of the array is smallest or greatest among their adjacent element. If it is smallest then it is local minima and if it is greatest then it is local maxima.

How do you find the local maxima in Python?

How do you find the local maxima and minima in Python? Use scipy. signal. argrelextrema() to find the local minima and maxima.

What is local maximum in array?

An element in an array is a local maxima if it greater than the element after it, and the element before it. For the elements at the extreme end only one check is required, that is, the element following the first element or the element before the last element.

How do you find the max and min value of a numpy array?

amax() will find the max value in an array, and numpy. amin() does the same for the min value.


2 Answers

There exists a bulit-in function argrelextrema that gets this task done:

import numpy as np
from scipy.signal import argrelextrema
    
a = np.array([1,2,3,4,5,4,3,2,1,2,3,2,1,2,3,4,5,6,5,4,3,2,1])

# determine the indices of the local maxima
max_ind = argrelextrema(a, np.greater)

# get the actual values using these indices
r = a[max_ind]  # array([5, 3, 6])

That gives you the desired output for r.

As of SciPy version 1.1, you can also use find_peaks. Below are two examples taken from the documentation itself.

Using the height argument, one can select all maxima above a certain threshold (in this example, all non-negative maxima; this can be very useful if one has to deal with a noisy baseline; if you want to find minima, just multiply you input by -1):

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

x = electrocardiogram()[2000:4000]
peaks, _ = find_peaks(x, height=0)
plt.plot(x)
plt.plot(peaks, x[peaks], "x")
plt.plot(np.zeros_like(x), "--", color="gray")
plt.show()

enter image description here

Another extremely helpful argument is distance, which defines the minimum distance between two peaks:

peaks, _ = find_peaks(x, distance=150)
# difference between peaks is >= 150
print(np.diff(peaks))
# prints [186 180 177 171 177 169 167 164 158 162 172]

plt.plot(x)
plt.plot(peaks, x[peaks], "x")
plt.show()

enter image description here

like image 136
Cleb Avatar answered Oct 19 '22 15:10

Cleb


>> import numpy as np
>> from scipy.signal import argrelextrema
>> a = np.array([1,2,3,4,5,4,3,2,1,2,3,2,1,2,3,4,5,6,5,4,3,2,1])
>> argrelextrema(a, np.greater)
array([ 4, 10, 17]),)
>> a[argrelextrema(a, np.greater)]
array([5, 3, 6])

If your input represents a noisy distribution, you can try smoothing it with NumPy convolve function.

like image 29
CodeCode Avatar answered Oct 19 '22 14:10

CodeCode