Nathan Thomas February 2016
### 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.

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]
```

MSeifert February 2016

If you can exclude maxima at the edges of the arrays you can always check if one elements is bigger than each of it's neighbors by checking:

```
import numpy as np
array = 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])
# Check that it is bigger than either of it's neighbors exluding edges:
max = (array[1:-1] > array[:-2]) & (array[1:-1] > array[2:])
# Print these values
print(array[1:-1][max])
# Locations of the maxima
print(np.arange(1, array.size-1)[max])
```

ebarr February 2016

If your original data is noisy, then using statistical methods is preferable, as not all peaks are going to be significant. For your `a`

array, a possible solution is to use double differentials:

```
peaks = a[1:-1][np.diff(np.diff(a)) < 0]
# peaks = array([5, 3, 6])
```

Cleb February 2016

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
maxInd = argrelextrema(a, np.greater)
# get the actual values using these indices
r = a[maxInd] # array([5, 3, 6])
```

That gives you the desired output for `r`

.

Asked in February 2016

Viewed 1,560 times

Voted 8

Answered 3 times

Viewed 1,560 times

Voted 8

Answered 3 times