-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
93 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
import numpy as np | ||
from vector import vector, plot_peaks | ||
from libs.findpeaks import findpeaks | ||
|
||
print('Detect peaks without any filters.') | ||
indexes = findpeaks(np.array(vector), spacing=1) | ||
print('Peaks are: %s' % (indexes)) | ||
plot_peaks(np.array(vector), np.array(indexes), | ||
algorithm='findpeaks from Janko Slavic') | ||
|
||
print('Detect peaks with distance and height filters.') | ||
indexes = findpeaks(np.array(vector), spacing=2, limit=7) | ||
print('Peaks are: %s' % (indexes)) | ||
plot_peaks(np.array(vector), np.array(indexes), mph=7, mpd=2, | ||
algorithm='findpeaks from Janko Slavic') |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
""" Searches for peaks in data | ||
History: | ||
-nov 2015: Janko Slavic, update | ||
-mar 2013: [email protected] | ||
""" | ||
|
||
import numpy as np | ||
|
||
def findpeaks(data, spacing=10, limit=None): | ||
"""Finds peaks in `data` which are of `spacing` width and >=`limit`. | ||
:param data: values | ||
:param spacing: minimum width for single peak | ||
:param limit: peaks should have value greater or equal | ||
:return: | ||
""" | ||
x = np.zeros(len(data)+2*spacing) | ||
x[:spacing] = data[0] | ||
x[-spacing:] = data[-1] | ||
x[spacing:-spacing] = data | ||
peak_candidate = np.zeros(x.size - 2*spacing - 2) | ||
peak_candidate[:] = True | ||
for s in range(spacing): | ||
h_b = x[s:-2 * spacing + s - 2] # before | ||
h_c = x[spacing: -spacing - 2] # central | ||
h_a = x[spacing + s + 1: -spacing + s - 1] # after | ||
peak_candidate = np.logical_and(peak_candidate, np.logical_and(h_c > h_b, h_c > h_a)) | ||
|
||
ind = np.argwhere(peak_candidate) | ||
ind = ind.reshape(ind.size) | ||
if limit is not None: | ||
ind = ind[data[ind] > limit] | ||
return ind | ||
|
||
|
||
if __name__ == '__main__': | ||
import matplotlib.pyplot as plt | ||
|
||
n = 1000 | ||
m = 20 | ||
t = np.linspace(0., 1, n) | ||
x = np.zeros(n) | ||
np.random.seed(0) | ||
phase = 2 * np.pi * np.random.random(m) | ||
for i in range(m): | ||
x += np.sin(phase[i] + 2 * np.pi * t * i) | ||
|
||
peaks = findpeaks(x, spacing=100, limit=4.) | ||
plt.plot(t, x) | ||
plt.axhline(4, color='r') | ||
plt.plot(t[peaks], x[peaks], 'ro') | ||
plt.title('Peaks: minimum value 4., minimum width 100 points') | ||
plt.show() |