@@ -130,3 +130,54 @@ def slide_function(func, x, dt, kernel, *args, stride=1, pass_weights=False, **k
130130 weight_sum [window ] += w # save sum of weights for normalization at the end
131131
132132 return x_hat / weight_sum , dxdt_hat / weight_sum
133+
134+
135+ def peakdet (x , delta , t = None ):
136+ """Find peaks and valleys of 1D array. A point is considered a maximum peak if it has the maximal
137+ value, and was preceded (to the left) by a value lower by delta. Converted from MATLAB script at
138+ http://billauer.co.il/peakdet.html Eli Billauer, 3.4.05 (Explicitly not copyrighted). This function
139+ is released to the public domain; Any use is allowed.
140+
141+ :param np.array[float] x: array for which to find peaks and valleys
142+ :param float delta: threshold for finding peaks and valleys. A point is considered a maximum peak
143+ if it has the maximal value, and was preceded (to the left) by a value lower by delta.
144+ :param np.array[float] t: optional domain points where data comes from, to make indices into locations
145+
146+ :return: tuple[np.array, np.array] of\n
147+ - **maxtab** -- indices or locations (column 1) and values (column 2) of maxima
148+ - **mintab** -- indices or locations (column 1) and values (column 2) of minima
149+ """
150+ maxtab = []
151+ mintab = []
152+ if t is None :
153+ t = np .arange (len (x ))
154+ elif len (x ) != len (t ):
155+ raise ValueError ('Input vectors x and t must have same length' )
156+ if not (np .isscalar (delta ) and delta > 0 ):
157+ raise ValueError ('Input argument delta must be a positive scalar' )
158+
159+ mn , mx = np .inf , - 1 * np .inf
160+ mnpos , mxpos = np .nan , np .nan
161+ lookformax = True
162+ for i in np .arange (len (x )):
163+ this = x [i ]
164+ if this > mx :
165+ mx = this
166+ mxpos = t [i ]
167+ if this < mn :
168+ mn = this
169+ mnpos = t [i ]
170+ if lookformax :
171+ if this < mx - delta :
172+ maxtab .append ((mxpos , mx ))
173+ mn = this
174+ mnpos = t [i ]
175+ lookformax = False # now searching for a min
176+ else :
177+ if this > mn + delta :
178+ mintab .append ((mnpos , mn ))
179+ mx = this
180+ mxpos = t [i ]
181+ lookformax = True # now searching for a max
182+
183+ return np .array (maxtab ), np .array (mintab )
0 commit comments