-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_peak_locations.m
47 lines (43 loc) · 1.07 KB
/
find_peak_locations.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
function peaks = find_peak_locations(x, y, mode)
peaks = [];
rising = 0;
curr = y(1);
if nargin == 2
mode = 'der';
end
switch mode
case 'direct'
for k = 2:length(newy)
if y(k)>= curr
rising = 1;
elseif rising
peaks(end+1).x = x(k-1);
peaks(end).y = y(k-1);
peaks(end).index = k-1;
rising = 0;
end
curr = y(k);
end
case 'der'
dy = zeros([1 length(y)-1]);
for k=1:length(dy)
dy(k) = y(k+1)-y(k);
end
dy = abs(dy);
falling = 0;
curr = dy(1);
for k = 2:length(dy)
if dy(k) <= curr
falling = 1;
elseif falling
peaks(end+1).x = x(k);
peaks(end).y = y(k);
peaks(end).index = k;
falling = 0;
end
curr = dy(k);
end
end
%Find two largest peaks and arrange acccording to left/right.
[p, ind] = sort([peaks.y], 'descend');
peaks = peaks(ind);