Hi all, I am not sure if this is of help for anyone. I wrote some code to find the relative maxima in a 1D array for my own purpose. Maybe someone is interested or even finds a bug *g*. I post the code here and appreciate any feedback. Even "stop spamming your buggy code" :-)
> from numpy import diff, sign, convolve, array, where, around, int32, alen > > def localmaxima_at(x): > '''Returns the indices of local maxima in the 1D array x. > > If several elements in x have the same value, then the > index of the element in the middle is returned. > > If there are two adjacent elements with the same value, > one of them is returned. > > x[0] and x[-1] are never returned as an index for the > local maximum. > > @Author: Samuel John > @copyright: http://creativecommons.org/licenses/by-nc-sa/3.0/ > @todo: unittests > ''' > assert len(x) > 2, "Length of x should be greater than two in order to > define a meaningful local maximum." > assert x.ndim == 1, "Expected 1D array." > #print 'x=\n',x > filled=sign(diff(x)).astype(int32) > # fill zeros: > has_zeros = (filled == 0).any() > last = 0 > if has_zeros: > for i in xrange(alen(filled)): > if filled[i] == 0: > filled[i] = last > else: > last = filled[i] > #print 'filled\n',filled > left = where( convolve( > filled, > array([-1,1]), mode='full' ) -2 == 0 )[0] > > if has_zeros: > filled=sign(diff(x)).astype(int32) > last = 0 > for i in reversed(xrange(len(filled))): > if filled[i] == 0: > filled[i] = last > else: > last = filled[i] > > right = where( convolve( > filled, > array([-1,1]), mode='full' ) -2 == 0 )[0] > #print 'left\n',left > #print 'right\n',right > return around( (left + right) / 2.0).astype(int32) > bests, Samuel _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion