-
Notifications
You must be signed in to change notification settings - Fork 0
/
peakdetect.py
284 lines (223 loc) · 10 KB
/
peakdetect.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import numpy as np
def peakdetect(y_axis, x_axis = None, lookahead = 500, delta = 0):
"""
Converted from/based on a MATLAB script at http://billauer.co.il/peakdet.html
Algorithm for detecting local maximas and minmias in a signal.
Discovers peaks by searching for values which are surrounded by lower
or larger values for maximas and minimas respectively
keyword arguments:
y_axis -- A list containg the signal over which to find peaks
x_axis -- A x-axis whose values correspond to the 'y_axis' list and is used
in the return to specify the postion of the peaks. If omitted the index
of the y_axis is used. (default: None)
lookahead -- (optional) distance to look ahead from a peak candidate to
determine if it is the actual peak (default: 500)
'(sample / period) / f' where '4 >= f >= 1.25' might be a good value
delta -- (optional) this specifies a minimum difference between a peak and
the following points, before a peak may be considered a peak. Useful
to hinder the algorithm from picking up false peaks towards to end of
the signal. To work well delta should be set to 'delta >= RMSnoise * 5'.
(default: 0)
Delta function causes a 20% decrease in speed, when omitted
Correctly used it can double the speed of the algorithm
return -- two lists [maxtab, mintab] containing the positive and negative
peaks respectively. Each cell of the lists contains a tupple of:
(position, peak_value)
to get the average peak value do 'np.mean(maxtab, 0)[1]' on the results
"""
maxtab = []
mintab = []
dump = [] #Used to pop the first hit which always if false
length = len(y_axis)
if x_axis is None:
x_axis = range(length)
#perform some checks
if length != len(x_axis):
raise ValueError, "Input vectors y_axis and x_axis must have same length"
if lookahead < 1:
raise ValueError, "Lookahead must be above '1' in value"
if not (np.isscalar(delta) and delta >= 0):
raise ValueError, "delta must be a positive number"
#needs to be a numpy array
y_axis = np.asarray(y_axis)
#maxima and minima candidates are temporarily stored in
#mx and mn respectively
mn, mx = np.Inf, -np.Inf
#Only detect peak if there is 'lookahead' amount of points after it
for index, (x, y) in enumerate(zip(x_axis[:-lookahead], y_axis[:-lookahead])):
if y > mx:
mx = y
mxpos = x
if y < mn:
mn = y
mnpos = x
####look for max####
if y < mx-delta and mx != np.Inf:
#Maxima peak candidate found
#look ahead in signal to ensure that this is a peak and not jitter
if y_axis[index:index+lookahead].max() < mx:
maxtab.append((mxpos, mx))
dump.append(True)
#set algorithm to only find minima now
mx = np.Inf
mn = np.Inf
####look for min####
if y > mn+delta and mn != -np.Inf:
#Minima peak candidate found
#look ahead in signal to ensure that this is a peak and not jitter
if y_axis[index:index+lookahead].min() > mn:
mintab.append((mnpos, mn))
dump.append(False)
#set algorithm to only find maxima now
mn = -np.Inf
mx = -np.Inf
#Remove the false hit on the first value of the y_axis
try:
if dump[0]:
maxtab.pop(0)
#print "pop max"
else:
mintab.pop(0)
#print "pop min"
del dump
except IndexError:
#no peaks were found, should the function return empty lists?
pass
return maxtab, mintab
def peakdetect_zero_crossing(y_axis, x_axis = None, window = 49):
"""
Algorithm for detecting local maximas and minmias in a signal.
Discovers peaks by dividing the signal into bins and retrieving the
maximum and minimum value of each the even and odd bins respectively.
Division into bins is performed by smoothing the curve and finding the
zero crossings.
Suitable for repeatable sinusoidal signals with some amount of RMS noise
tolerable. Excecutes faster than 'peakdetect', although this function will
break if the offset of the signal is too large. It should also be noted
that the first and last peak will probably not be found, as this algorithm
only can find peaks between the first and last zero crossing.
keyword arguments:
y_axis -- A list containg the signal over which to find peaks
x_axis -- A x-axis whose values correspond to the 'y_axis' list and is used
in the return to specify the postion of the peaks. If omitted the index
of the y_axis is used. (default: None)
window -- the dimension of the smoothing window; should be an odd integer
(default: 49)
return -- two lists [maxtab, mintab] containing the positive and negative
peaks respectively. Each cell of the lists contains a tupple of:
(position, peak_value)
to get the average peak value do 'np.mean(maxtab, 0)[1]' on the results
"""
if x_axis is None:
x_axis = range(len(y_axis))
length = len(y_axis)
if length != len(x_axis):
raise ValueError, 'Input vectors y_axis and x_axis must have same length'
#needs to be a numpy array
y_axis = np.asarray(y_axis)
zero_indices = zero_crossings(y_axis, window = window)
period_lengths = np.diff(zero_indices)
bins = [y_axis[indice:indice+diff] for indice, diff in
zip(zero_indices, period_lengths)]
even_bins = bins[::2]
odd_bins = bins[1::2]
#check if even bin contains maxima
if even_bins[0].max() > abs(even_bins[0].min()):
hi_peaks = [bin.max() for bin in even_bins]
lo_peaks = [bin.min() for bin in odd_bins]
else:
hi_peaks = [bin.max() for bin in odd_bins]
lo_peaks = [bin.min() for bin in even_bins]
hi_peaks_x = [x_axis[np.where(y_axis==peak)[0]] for peak in hi_peaks]
lo_peaks_x = [x_axis[np.where(y_axis==peak)[0]] for peak in lo_peaks]
maxtab = [(x,y) for x,y in zip(hi_peaks, hi_peaks_x)]
mintab = [(x,y) for x,y in zip(lo_peaks, lo_peaks_x)]
return maxtab, mintab
def smooth(x,window_len=11,window='hanning'):
"""
smooth the data using a window with requested size.
This method is based on the convolution of a scaled window with the signal.
The signal is prepared by introducing reflected copies of the signal
(with the window size) in both ends so that transient parts are minimized
in the begining and end part of the output signal.
input:
x: the input signal
window_len: the dimension of the smoothing window; should be an odd integer
window: the type of window from 'flat', 'hanning', 'hamming', 'bartlett', 'blackman'
flat window will produce a moving average smoothing.
output:
the smoothed signal
example:
t=linspace(-2,2,0.1)
x=sin(t)+randn(len(t))*0.1
y=smooth(x)
see also:
numpy.hanning, numpy.hamming, numpy.bartlett, numpy.blackman, numpy.convolve
scipy.signal.lfilter
TODO: the window parameter could be the window itself if an array instead of a string
"""
if x.ndim != 1:
raise ValueError, "smooth only accepts 1 dimension arrays."
if x.size < window_len:
raise ValueError, "Input vector needs to be bigger than window size."
if window_len<3:
return x
if not window in ['flat', 'hanning', 'hamming', 'bartlett', 'blackman']:
raise ValueError, "Window is on of 'flat', 'hanning', 'hamming', 'bartlett', 'blackman'"
s=np.r_[x[window_len-1:0:-1],x,x[-1:-window_len:-1]]
#print(len(s))
if window == 'flat': #moving average
w=np.ones(window_len,'d')
else:
w=eval('np.'+window+'(window_len)')
y=np.convolve(w/w.sum(),s,mode='valid')
return y
def zero_crossings(y_axis, x_axis = None, window = 49):
"""
Algorithm to find zero crossings. Smoothens the curve and finds the
zero-crossings by looking for a sign change.
keyword arguments:
y_axis -- A list containg the signal over which to find zero-crossings
x_axis -- A x-axis whose values correspond to the 'y_axis' list and is used
in the return to specify the postion of the zero-crossings. If omitted
then the indice of the y_axis is used. (default: None)
window -- the dimension of the smoothing window; should be an odd integer
(default: 49)
return -- the x_axis value or the indice for each zero-crossing
"""
#smooth the curve
length = len(y_axis)
if x_axis == None:
x_axis = range(length)
x_axis = np.asarray(x_axis)
y_axis = smooth(y_axis, window)[:length]
zero_crossings = np.where(np.diff(np.sign(y_axis)))[0]
times = [x_axis[indice] for indice in zero_crossings]
#check if zero-crossings are valid
diff = np.diff(times)
if diff.std() / diff.mean() > 0.1:
raise ValueError, "smoothing window too small, false zero-crossings found"
return times
if __name__=="__main__":
import matplotlib.pyplot as plt
from math import pi
# i = 10000
# x = np.linspace(0,3.7*pi,i)
# y = 0.3*np.sin(x) + np.sin(1.3*x) + 0.9*np.sin(4.2*x) + 0.06*np.random.randn(i)
# y *= -1
# x = range(i)
y = union_norm
_max, _min = peakdetect(y,lookahead=75, delta=0.5)
# xm = [p[0] for p in _max]
# ym = [p[1] for p in _max]
xn = [p[0] for p in _min]
yn = [p[1] for p in _min]
pt_no = np.linspace(1,len(xn), len(xn)).astype(np.uint8)
fig = plt.figure(figsize=(30,6))
ax = fig.add_subplot(111)
plt.plot(y)
plt.scatter(xn, yn, color = 'red')
for i, txt in enumerate(pt_no):
ax.annotate(txt, (xn[i],max(0, yn[i]-5)))
plt.savefig('figs/union_minimas_' + str_version + '.jpg')
plt.close()