IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
peakdetector.m
Go to the documentation of this file.
1 %> @brief Peak Detector
2 %>
3 %> The algorithm is implemented in detect_peaks.m
4 %>
5 %> @sa detect_peaks.m
6 classdef peakdetector < irobj
7  properties
8  %> =0. Minimum vertical distance between peak and zero
9  minaltitude = 0;
10  %> =0. Minimum vertical distance between peak and closest trough
11  minheight = 0;
12  %> Minimum distance between peaks, expressed in number of points.
13  mindist;
14  %> Minimum distance between peaks, expressed in native units.
15  mindist_units;
16  %> =1. Whether minaltitude and minheight are expressed in percentage. If true, the actual minimum altitude and
17  %> minimum height will be calculated as a percentage of max(abs(y)).
18  flag_perc = 1;
19  %> =Inf. Maximum number of peaks to be returned. If used, peak are returned in ranked order of height.
20  no_max = Inf;
21  %> =1. Whether absolute value of the signal should be used. If 0, negative parts of the signal are made into
22  %> "lakes", i.e., replaced by zeroes
23  flag_abs = 1;
24  end;
25 
26  properties(SetAccess=protected)
27  %> Horizontal spacing between points, expressed in native units.
28  spacing;
29  minalt;
30  minhei;
31  flag_booted = 0;
32  end;
33 
34  methods
35  function o = peakdetector(o)
36  o.classtitle = 'Peak Detector';
37  o.color = [170, 189, 193]/255;
38  end;
39 
40  %> Detects peaks
41  %>
42  %> If the peak detector is going to be used many times, it is better to boot it first. Otherwise it will be internally booted
43  %> every time it is used.
44  %>
45  %> The peak detector will be internally booted when:
46  %> @arg @c x is not empty, or
47  %> @arg @c the peak detector hasn't been booted before
48  %>
49  %> @param x x-axis values. If not empty, the peak detector will be internally booted.
50  %> @param y curve to detect peaks from
51  function idxs = use(o, x, y)
52  y = y(:)';
53  if ~o.flag_booted || ~isempty(x)
54  if isempty(x)
55  x = 1:numel(y);
56  end;
57  x = x(:)';
58  o = o.boot(x, y);
59  end;
60 
61  if ~isempty(o.mindist)
62  mindist_ = o.mindist;
63  else
64  if ~isempty(o.mindist_units)
65  mindist_ = ceil(o.mindist_units/o.spacing);
66  else
67  irerror('Minimum distance between points not specified: specify either mindist or mindist_units');
68  end;
69  end;
70 
71  if o.flag_abs
72  y = abs(y);
73  else
74  y(y < 0) = 0;
75  end;
76  y(y == Inf) = realmax;
77 
78  idxs = detect_peaks(y, o.minalt, o.minhei, mindist_);
79 
80  if o.no_max > 0 && length(idxs) > o.no_max
81  [dummy, idx] = sort(y(idxs), 'descend');
82  idxs = idxs(idx(1:o.no_max));
83  end;
84  end;
85  end;
86 
87  methods
88  function o = boot(o, x, y)
89  o.spacing = abs(x(2)-x(1));
90 
91  if o.flag_perc
92  if o.minaltitude > 1 || o.minaltitude < 0
93  irerror('Invalid percentual minimum altitude!');
94  end;
95  if o.minheight > 1 || o.minheight < 0
96  irerror('Invalid percentual minimum height!');
97  end;
98  top = max(abs(y));
99  top(top == Inf) = realmax;
100  o.minalt = o.minaltitude*top;
101  o.minhei = o.minheight*top;
102  else
103  o.minalt = o.minaltitude;
104  o.minhei = o.minheight;
105  end;
106 
107  o.flag_booted = 1;
108  end;
109  end;
110 end
function irerror(in s)
Peak Detector.
Definition: peakdetector.m:6
function detect_peaks(in y, in minalt, in minhei, in mindist)
Analysis Session (AS) base class.
Definition: as.m:6
Base class.
Definition: irobj.m:33