31 lines
730 B
Python
31 lines
730 B
Python
def find_maxima(x):
|
|
"""Find local maxima of x.
|
|
|
|
Input arguments:
|
|
x -- 1D list of real numbers
|
|
|
|
Output:
|
|
idx -- list of indices of the local maxima in x
|
|
"""
|
|
idx = []
|
|
# catch the case when input is an empty list
|
|
if x == []:
|
|
return idx
|
|
|
|
# catch the case where a maximum is at the edge
|
|
if len(x) > 1:
|
|
if x[0] > x[1]:
|
|
idx.append(0)
|
|
if x[-1] > x[-2]:
|
|
idx.append(len(x)-1)
|
|
|
|
# if we only have two values, we are done
|
|
if len(x) == 2:
|
|
return idx
|
|
|
|
# other wise for everything not on the edge:
|
|
for i in range(1, len(x)-1):
|
|
if (x[i] > x[i-1]) & (x[i] > x[i+1]):
|
|
idx.append(i)
|
|
|
|
return sorted(idx)
|