Skip to content

Commit 2773cd1

Browse files
committed
Add Simple peak detection.
1 parent f7fe48a commit 2773cd1

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

tests/test_processing.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,15 @@ def test_3(self):
3939
assert x.shape[0] == sig.shape[0]
4040
assert numpy.min(x) >= lb
4141
assert numpy.max(x) <= ub
42+
43+
def test_4(self):
44+
x = []
45+
hp, sp = wfdb.processing.find_peaks(x)
46+
assert hp.shape == (0,)
47+
assert sp.shape == (0,)
48+
49+
def test_5(self):
50+
x = [0, 2, 1, 0, -10, -15, -15, -15, 9, 8, 0, 0, 1, 2, 10]
51+
hp, sp = wfdb.processing.find_peaks(x)
52+
assert numpy.array_equal(hp, [1, 8])
53+
assert numpy.array_equal(sp, [6, 10])

wfdb/processing/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
from .basic import resample_ann, resample_sig, resample_singlechan, resample_multichan, normalize
2+
from .peaks import find_peaks

wfdb/processing/peaks.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import numpy
2+
3+
4+
def find_peaks(x):
5+
# Definitions:
6+
# * Hard peak: a peak that is either /\ or \/
7+
# * Soft peak: a peak that is either /-*\ or \-*/ (In that cas we define the middle of it as the peak)
8+
9+
# Returns two numpy arrays:
10+
# * hard_peaks contains the indexes of the Hard peaks
11+
# * soft_peaks contains the indexes of the Soft peaks
12+
13+
if len(x) == 0:
14+
return numpy.empty([0]), numpy.empty([0])
15+
16+
tmp = x[1:]
17+
tmp = numpy.append(tmp, [x[-1]])
18+
tmp = x-tmp
19+
tmp[numpy.where(tmp>0)] = +1
20+
tmp[numpy.where(tmp==0)] = 0
21+
tmp[numpy.where(tmp<0)] = -1
22+
tmp2 = tmp[1:]
23+
tmp2 = numpy.append(tmp2, [0])
24+
tmp = tmp-tmp2
25+
hard_peaks = numpy.where(numpy.logical_or(tmp==-2,tmp==+2))[0]+1
26+
soft_peaks = []
27+
for iv in numpy.where(numpy.logical_or(tmp==-1,tmp==+1))[0]:
28+
t = tmp[iv]
29+
i = iv+1
30+
while True:
31+
if i==len(tmp) or tmp[i] == -t or tmp[i] == -2 or tmp[i] == 2:
32+
break
33+
if tmp[i] == t:
34+
soft_peaks.append(int(iv+(i-iv)/2))
35+
break
36+
i += 1
37+
soft_peaks = numpy.asarray(soft_peaks)+1
38+
return hard_peaks, soft_peaks

0 commit comments

Comments
 (0)