-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocalMaximum.m
81 lines (72 loc) · 2.64 KB
/
localMaximum.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
function varargout = localMaximum(x,minDist, exculdeEqualPoints)
% function varargout = localMaximum(x,minDist, exculdeEqualPoints)
%
% This function returns the indexes\subscripts of local maximum in the data x.
% x can be a vector or a matrix of any dimension
%
% minDist is the minimum distance between two peaks (local maxima)
% minDist should be a vector in which each argument corresponds to it's
% relevant dimension OR a number which is the minimum distance for all
% dimensions
%
% exculdeEqualPoints - is a boolean definning either to recognize points with the same value as peaks or not
% x = [1 2 3 4 4 4 4 4 4 3 3 3 2 1];
% will the program return all the '4' as peaks or not - defined by the 'exculdeEqualPoints'
% localMaximum(x,3)
% ans =
% 4 5 6 7 8 9 11 12
%
% localMaximum(x,3,true)
% ans =
% 4 7 12
%
%
% Example:
% a = randn(100,30,10);
% minDist = [10 3 5];
% peaks = localMaximum(a,minDist);
%
% To recieve the subscript instead of the index use:
% [xIn yIn zIn] = localMaximum(a,minDist);
%
% To find local minimum call the function with minus the variable:
% valleys = localMaximum(-a,minDist);
if nargin < 3
exculdeEqualPoints = false;
if nargin < 2
minDist = size(x)/10;
end
end
if isempty(minDist)
minDist = size(x)/10;
end
dimX = length ( size(x) );
if length(minDist) ~= dimX
% In case minimum distance isn't defined for all of x dimensions
% I use the first value as the default for all of the dimensions
minDist = minDist( ones(dimX,1) );
end
% validity checks
minDist = ceil(minDist);
minDist = max( [minDist(:)' ; ones(1,length(minDist))] );
minDist = min( [minDist ; size(x)] );
% ---------------------------------------------------------------------
if exculdeEqualPoints
% this section comes to solve the problem of a plato
% without this code, points with the same hight will be recognized as peaks
y = sort(x(:));
dY = diff(y);
% finding the minimum step in the data
minimumDiff = min( dY(dY ~= 0) );
%adding noise which won't affect the peaks
x = x + rand(size(x))*minimumDiff;
end
% ---------------------------------------------------------------------
se = ones(minDist);
X = imdilate(x,se);
f = find(x == X);
if nargout
[varargout{1:nargout}] = ind2sub( size(x), f );
else
varargout{1} = f;
end