[SciPy-user] Extrema finding

John Hunter jdh2358@gmail....
Thu Oct 9 07:21:47 CDT 2008


On Thu, Oct 9, 2008 at 6:44 AM, iCy-fLaME <icy.flame.gm@gmail.com> wrote:
> I am trying to find a list of all maxima and minima, each for a given
> (1D) numpy array. Anyone know of a quick way to do it?


>
> Ideally the function will return the extrema values and their
> positions. Relatively simple function to implement in Python, but that
> would be painfully slow. The typical data array I am looking at, has
> approximately 500k elements of double precision float.

I use the following to find the indices of extrema:


def extrema_sampling(x, withend=False):
    """
    return the indices into the local maxima or minima of x
    if withend, include the 0 and end points in the sampling
    """
    d = np.diff(x)
    d2 = d[:-1]*d[1:]
    ind = []
    if withend:
        ind.append(0)
    ind.extend(np.nonzero(d2<0)[0]+1)
    if withend and ind[-1]!=len(x)-1:
        ind.append(len(x)-1)
    return np.array(ind)


More information about the SciPy-user mailing list