-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpeakdet.m
57 lines (49 loc) · 1.1 KB
/
peakdet.m
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
% [maxtab, mintab]=peakdet(v, delta, x);
%
% this function Detects peaks in a vector
%
% [INPUTS]
% v: input vector.
% delta: dt.
% nn: arriaval times
%
% [OUTPUTS]
% maxtab: maximum.
% mintab: minimum
% -------------------------------------------------------------------------
% Mostafa Mousavi, [email protected]
% Last modify: Oct 2, 2016
% -------------------------------------------------------------------------
function [maxtab, mintab]=peakdet(v, delta, x)
maxtab = [];
mintab = [];
v = v(:);
if nargin < 3
x = (1:length(v))';
else
x = x(:);
if length(v)~= length(x)
error('Input vectors v and x must have same length');
end
end
mn = Inf; mx = -Inf;
mnpos = NaN; mxpos = NaN;
lookformax = 1;
for i=1:length(v)
this = v(i);
if this > mx, mx = this; mxpos = x(i); end
if this < mn, mn = this; mnpos = x(i); end
if lookformax
if this < mx-delta
maxtab = [maxtab ; mxpos mx];
mn = this; mnpos = x(i);
lookformax = 0;
end
else
if this > mn+delta
mintab = [mintab ; mnpos mn];
mx = this; mxpos = x(i);
lookformax = 1;
end
end
end