Last active
March 11, 2020 19:10
-
-
Save ludflu/3b8ef71929b9a30a5e8d989eb3bb12d7 to your computer and use it in GitHub Desktop.
Find peaks in time series data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# I wanted to see if my naive find_peaks code would be faster than: https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.find_peaks.html | |
# So far the answer is that its about the same. Will run more benchmarks | |
def remove_repeats(data): | |
acc = [] | |
head, *tail = data | |
acc.append(head) | |
for (v,i) in tail: | |
(lastv,lasti) = acc[-1] | |
if (v != lastv): # only append if element is not the same as last | |
acc.append((v,i)) | |
return acc | |
def window(iterable, size): | |
iters = tee(iterable, size) | |
for i in range(1, size): | |
for each in iters[i:]: | |
next(each, None) | |
return zip(*iters) | |
def is_peak(x): | |
((left,_),(peak,_),(right,_)) = x | |
return (peak>left) & (peak>right) | |
def find_peaks(data): | |
data_indexed = zip(data, range(0,len(data))) | |
no_repeats = remove_repeats(data_indexed) | |
win = window(no_repeats,3) | |
peaks = filter(is_peak,win) | |
indxes = [i[1][1] for i in peaks] | |
return list(indxes) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment