[SciPy-User] computing local extrema

alan@ajackso... alan@ajackso...
Thu Sep 15 20:00:19 CDT 2011

Here's what I wrote for finding extrema of a 1D array

def extrema(signal, hilow="Hi"):
    ''' hilow = "Hi", "Low", or "Both" depending on whether you want 
        upper or lower extrema, or all of them.
    a = np.sign(np.diff(signal))
    zerolocs = np.transpose(np.where( (a[1:]+a[0:-1]==0.) + (a==0.)[0:-1] )).flatten() + 1
    zerolocs = zerolocs[zerolocs>=a.argmax()] # remove leading zeros
    if zerolocs[0] < 1:
        zerolocs = zerolocs[1:]
    if zerolocs[-1]>len(a)-1:
        zerolocs = zerolocs[0:-1]
    if hilow == "Low" :
        return zerolocs[np.where(a[zerolocs] >0)]
    elif hilow == "Hi" :
        return zerolocs[np.where(a[zerolocs] <=0)]
    else :
        return zerolocs

>You could create multiple thresholds along one axis of the array, and
>get all elements above each threshold value (=region). You build a
>region-stack from this; a 2D/3D boolean array that says which elements
>are above the threshold value for each threshold you used. If a region
>contains a region in a higher threshold value level (higher up in the
>stack) that means you haven't found the local maximum yet; if it
>doesn't, that is your top.
>This method would allow flats to be detected as well.
>The last thing you have to do is: for every remaining region that is a
>local maximum, you compute the coordinates and value as the mean of
>all coordinates or values contained in the region.
>Hope this helps
>2009/8/26 fred <fredmfp@gmail.com>:
>> Hi,
>> I would like to compute local extrema of an array (2D/3D),
>> ie get a list of points (coords + value).
>> How could I do this?
>> Any hint?
>> TIA.
>> Cheers,
>> --
>> Fred
>> _______________________________________________
>> SciPy-User mailing list
>> SciPy-User@scipy.org
>> http://mail.scipy.org/mailman/listinfo/scipy-user
>SciPy-User mailing list

| Alan K. Jackson            | To see a World in a Grain of Sand      |
| alan@ajackson.org          | And a Heaven in a Wild Flower,         |
| www.ajackson.org           | Hold Infinity in the palm of your hand |
| Houston, Texas             | And Eternity in an hour. - Blake       |

More information about the SciPy-User mailing list