[SciPy-user] Extract local minima

Robert Kern robert.kern at gmail.com
Mon Jun 19 11:07:17 CDT 2006

Nils Wagner wrote:
>  Hi all,
> Is there a way to extract local minima from the column vector stored in
> data.mtx ?

The general scheme would be to look for elements x[i] such that (x[i-1] > x[i])
& (x[i+1] > x[i]).


import numpy as np

x = ...

xp = x[2:]
xm = x[:-2]
x0 = x[1:-1]
minima = np.where((xp > x0) & (xm > x0))[0] + 1

There are some additional things you will want to do to make it robust, like
checking the endpoints and looking places where there are equal values. These
are left as an exercise for the reader.

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

More information about the SciPy-user mailing list