IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
bc_rubber.m
Go to the documentation of this file.
1 %> @ingroup maths
2 %>@file
3 %>@brief Convex Polygonal Line baseline correction
4 %>
5 %> This was inspired on OPUS Rubberband baseline correction (RBBC) [1].
6 %>
7 %> Stretches a convex polygonal line whose vertices touch troughs of x
8 %> without crossing x (see below).
9 %>
10 %> This one is parameterless, whereas OPUS RBBC asks for a number of points.
11 %>
12 %> @image html rubberlike_explain.png
13 %>
14 %> <h3>References</h3>
15 %> [1] Bruker Optik GmbH, OPUS 5 Reference Manual. Ettlingen: Bruker, 2004.
16 %>
17 %> @sa demo_pre_bc_rubber.m
18 %
19 %> @param X [@ref no]x[@ref nf] matrix whose rows will be individually baseline-corrected
20 %>
21 %> @return @em [Y] or @em [Y, L] Where @em L are the baselines
22 function varargout = bc_rubber(X)
23 
24 msgstring = nargoutchk(1, 2, nargout);
25 if ~isempty(msgstring)
26  error(msgstring);
27 end;
28 
29 
30 [no, nf] = size(X);
31 
32 Y = zeros(no, nf);
33 L = zeros(no, nf);
34 
35 for i = 1:no
36  if nf > 0
37  l = [];
38  x = X(i, :);
39  if length(x) > 1
40  l2 = rubber(x);
41  else
42  l2 = [];
43  end;
44  l = [x(1) l2];
45 
46  Y(i, :) = x-l;
47  L(i, :) = l;
48  end;
49 end;
50 
51 
52 
53 if nargout == 1
54  varargout = {Y};
55 elseif nargout == 2
56  varargout = {Y, L};
57 end;
58 
59 %> @cond
60 %---------------------------------------------------------------------
61 % returns a "rubber" vector with one element less than the length of x
62 function y = rubber(x)
63 
64 nf = length(x); % number of points
65 
66 l = linspace(x(1), x(end), nf);
67 
68 xflat = x-l;
69 [val, idx] = min(xflat);
70 if ~isempty(val) && val < 0
71  y = [rubber(x(1:idx)), rubber(x(idx:end))];
72 else
73  y = l(2:end);
74 end;
75 %> @endcond
function bc_rubber(in X)