I am attempting to write a simple set of instructions to return the index of a peak in a set of data. In my actual data set I have multiple peaks (so simply finding the max or min values is not sufficient). For my purposes, I can define a peak as any value that is less than the value before in and also less than the value following in (I am looking for negative peaks).
I have a sort of working example but it fails if there are two identical numbers in the list.
mylist = [10, 8, 5, 3, 1, 3, 4]
min =[]
for i in mylist[1:-1]: # the pre and post comparison means I have to start from item 2 to second last item
pre = mylist.index(i)-1
post = mylist.index(i)+1
print('current i', i)
print('index', pre, post)
print('values pre post', mylist[pre], mylist[post])
if i < mylist[pre] and i< mylist[post]:
print('true')
min.append(mylist.index(i))
print('min=', min)
This seems to work until it gets to the second '3' (at position 5) in the list in which case it evaluates it against the values either side of the first '3'
....
current i 1
index 3 5
values pre post 3 3
true
current i 3
index 2 4
values pre post 5 1
min= [4]
As you can see it correctly finds that the value after '1' is '3' but I think its basically saying the value at index 5 = 3 so what values are either side of 3 and then reading the first 3. In my head this seems like it should be trivial, but am flummoxed. My searches and the suggested questions when writing this didn't flag any duplicates, but I would be amazed if I'm the first person this has happened to...
(also for explanation, I was using scipy find_peaks but it doesn't work for my purposes. If the final data points are rising it identifies the last data point as a peak. e.g. [... 11, 12, 13, 14, 15]
it would identify '15' as a peak, which it may not be).