[SciPy-user] Extract local minima

Nils Wagner nwagner at iam.uni-stuttgart.de
Mon Jun 19 13:33:50 CDT 2006


On Mon, 19 Jun 2006 11:07:17 -0500
  Robert Kern <robert.kern at gmail.com> wrote:
> 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]).
> 
> (untested)
> 
> 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
> 
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.net
> http://www.scipy.net/mailman/listinfo/scipy-user

Hi Robert,

Thank you very much !
The result is promising.

from scipy import *
from pylab import plot, show, scatter
x = rand(100)
xp = x[2:]
xm = x[:-2]
x0 = x[1:-1]
minima=where((xp > x0) & (xm > x0))[0]+1
plot(arange(0,len(x)),x,'r-')
scatter(minima,x[minima],s=4)
show()

Nils



More information about the SciPy-user mailing list