Skip to content
This repository was archived by the owner on Aug 14, 2021. It is now read-only.

Commit c4ee162

Browse files
committed
Added more robust rolling-ball baseline correction. Added ability tweak
baseline to avoid negative values.
1 parent 47f46d1 commit c4ee162

File tree

4 files changed

+150
-3
lines changed

4 files changed

+150
-3
lines changed

fix_spectra/fix_click_menu.m

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ function fix_click_menu(hObject, eventdata, handles) %#ok<INUSD>
3333
%
3434
% Dan Homer ????
3535
%
36-
% Paul Anderson ????
36+
% % Paul Anderson ????
3737
%
3838
% Eric Moyer 2011-2012 (eric_moyer@yahoo.com)
3939

4040
str = {'Get collection(s)','Load collections', ...
4141
...%'','Set noise regions', ...
4242
'','Load regions','Create signal map','Create new region', ...
43-
'Edit region','Delete region','Clear regions','Fix baseline', 'Fix baseline (rolling ball)', ...
43+
'Edit region','Delete region','Clear regions','Fix baseline', 'Rolling ball baseline', 'Fix negative points to 0', ...
4444
'','Crop','Set reference','Normalize to reference','Zero regions', 'Sum normalize','Normalize to weight',...
4545
'','Save regions','Finalize','Save collections', ...
4646
'Merge processing logs','Merge locally' ...
@@ -116,8 +116,10 @@ function fix_click_menu(hObject, eventdata, handles) %#ok<INUSD>
116116
set_noise_regions;
117117
elseif strcmp(str{s},'Fix baseline')
118118
fix_baseline
119-
elseif strcmp(str{s},'Fix baseline (rolling ball)')
119+
elseif strcmp(str{s},'Rolling ball baseline')
120120
fix_rolling_ball_baseline
121+
elseif strcmp(str{s},'Fix negative points to 0')
122+
fix_negative_points
121123
elseif strcmp(str{s},'Edit region')
122124
set_edit
123125
elseif strcmp(str{s},'Create new region')

fix_spectra/fix_negative_points.m

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function fix_negative_points
2+
%fix_negative_points adjust baseline so that negative points are set to 0.
3+
collections = getappdata(gcf,'collections');
4+
for c = 1:length(collections)
5+
for s = 1:collections{c}.num_samples
6+
inds = collections{c}.Y_fixed(:,s) < 0;
7+
collections{c}.Y_fixed(inds,s) = 0;
8+
collections{c}.Y_baseline(inds,s) = collections{c}.Y(inds,s) - collections{c}.Y_fixed(inds,s);
9+
end
10+
end
11+
setappdata(gcf,'collections',collections);
12+
plot_all
13+
end
14+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
function fix_rolling_ball_baseline
2+
%rolling_ball_baseline Summary of this function goes here
3+
% Detailed explanation goes here
4+
prompt={'Enter min/max window:', 'Enter smoothing window:'};
5+
name='Rolling ball';
6+
answer = inputdlg(prompt, name, [1, 35], {'55', '35'});
7+
8+
if(isempty(answer))
9+
return
10+
end
11+
12+
wm = str2double(answer{1});
13+
ws = str2double(answer{2});
14+
15+
collections = getappdata(gcf,'collections');
16+
for c = 1:length(collections)
17+
for s = 1:collections{c}.num_samples
18+
y = collections{c}.Y(:,s);
19+
baseline = rolling_ball_baseline(y, wm, ws);
20+
collections{c}.Y_baseline(:,s) = baseline;
21+
collections{c}.Y_fixed(:,s) = y - baseline;
22+
end
23+
end
24+
%setappdata(gcf,'add_processing_log','Fixed baseline.');
25+
setappdata(gcf,'add_processing_log',sprintf('Fix baseline (rolling ball, wm: %f, ws: %f).',wm,ws));
26+
setappdata(gcf,'temp_suffix','_fixed_baseline');
27+
setappdata(gcf,'collections',collections);
28+
29+
plot_all
30+
end
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
function baseline = rolling_ball_baseline(y, wm, ws)
2+
%rolling_ball_baseline Summary of this function goes here
3+
% y is a column vector representing a spectrum
4+
% wm is the width of min/max window
5+
% ws is the width of the smoothing window
6+
% Based on the rolling ball baseline implemented in the R package baseline
7+
[m, ~] = size(y);
8+
T1 = zeros(size(y)); % minimizers
9+
T2 = zeros(size(y)); % maximizers
10+
baseline = zeros(size(y));
11+
12+
% minimize
13+
u1 = ceil((wm+1) / 2) + 1;
14+
T1(1) = min(y(1:u1));
15+
% start of spectrum
16+
for i=2:wm
17+
u2 = u1 + 1 + mod(i, 2);
18+
T1(i) = min(min(y(u1+1:u2)), T1(i-1));
19+
u1 = u2;
20+
end
21+
% middle of spectrum
22+
for i = wm+1:m-wm
23+
if ((y(u1+1) <= T1(i-1)) && (y(u1-wm) ~= T1(i-1)))
24+
T1(i) = y(u1+1); % next is smaller
25+
else
26+
T1(i) = min(y(i-wm:i+wm));
27+
end
28+
u1 = u1 + 1;
29+
end
30+
% end of spectrum
31+
u1 = m - 2*wm - 1;
32+
for i = m-wm+1:m
33+
u2 = u1 + 1 + mod(i+1, 2);
34+
if min(y(u1:u2-1)) > T1(i-1)
35+
T1(i) = T1(i-1); % removed is larger
36+
else
37+
T1(i) = min(y(u2:m));
38+
end
39+
u1 = u2;
40+
end
41+
42+
% maximize
43+
u1 = ceil((wm+1)/2) + 1;
44+
% start of spectrum
45+
T2(1) = max(T1(1:u1));
46+
for i = 2:wm
47+
u2 = u1 + 1 + mod(i, 2);
48+
T2(i) = max(max(T1(u1+1:u2)), T2(i-1));
49+
u1 = u2;
50+
end
51+
% middle of spectrum
52+
for i = wm+1:m-wm
53+
if (T1(u1+1) >= T2(i-1)) && (T1(u1-wm) ~= T2(i-1))
54+
T2(i) = T1(u1+1);
55+
else
56+
T2(i) = max(T1(i-wm:i+wm));
57+
end
58+
u1 = u1 + 1;
59+
end
60+
% end of spectrum
61+
u1 = m - 2*wm - 1;
62+
for i = m-wm+1:m
63+
u2 = u1 + 1 + mod(i+1, 2);
64+
if max(T1(u1:u2-1)) < T2(i-1)
65+
T2(i) = T2(i-1);
66+
else
67+
T2(i) = max(T1(u2:m));
68+
end
69+
u1 = u2;
70+
end
71+
72+
% Smoothing
73+
u1 = ceil(ws/2);
74+
% start of spectrum
75+
v = sum(T2(1:u1));
76+
for i = 1:ws
77+
u2 = u1 + 1 + mod(i, 2);
78+
v = v + sum(T2(u1+1:u2));
79+
baseline(i) = v/u2;
80+
u1 = u2;
81+
end
82+
% middle of spectrum
83+
v = sum(T2(1:2*ws+1));
84+
baseline(ws+1) = v/(2*ws+1);
85+
for i = ws+2:m-ws
86+
v = v - T2(i-ws-1) + T2(i+ws);
87+
baseline(i) = v/(2*ws+1);
88+
end
89+
u1 = m - 2*ws+1;
90+
v = v - T2(u1); % sum so far
91+
baseline(m-ws+1) = v/(2*ws); % mean so far
92+
% end of spectrum
93+
for i = m-ws+2:m
94+
u2 = u1+ 1 + mod(i+1,2);
95+
v = v - sum(T2(u1:u2-1));
96+
baseline(i) = v/(m-u2+1);
97+
u1 = u2;
98+
end
99+
100+
end
101+

0 commit comments

Comments
 (0)