MATLAB: Background correction – msbackadj usage example

background correctionmsbackadj

This is less a question but the answer I was looking for but couldn't find.
I have seen that there are quite some questions on the usage of the msbackadj-function from the bioinformatics toolbox, a lot of them unanswered. I was also struggling to get it working with the information from the help menu as there are some pitfalls:
  1. You need to enter an array for your x-values (e.g. time in sec) and your y-values (e.g. intensities) of the same length.
  2. Both the x and the y-array need to be column vectors (n x 1) and not row vectors (1 x n). Transpose if necessary.
  3. The WindowSize refers to data points, so 'WindowSize',5 means every window contains 5 data points.
  4. The WindowSize can be a floating point number in the range from >0 to at least the number of data points in the x-array.
  5. The StepSize refers to the values of your x-array, so 'StepSize',1 means that the algorithm will calculate a window every 1 sec (or minute, or wavelength or whatever unit you have). This 1 sec can have more or less than 1 data point.
  6. The StepSize can be a floating point number in the range from >0 to at least the maximum value of your x-array.
  7. The Quantile value can be a floating point number in the range from 0 to 1.
I don't know about the other parameters as I did not use them. Below is an usage example with a sinus wave to make the behaviour of the algorithm more comprehensible.
Hope it helps someone to get this up and running faster than it took me.
% create example data set
x = 0:0.25:20; % this will create a row vector by default
y = sin(x-pi/2) + 2; % and this will also be a row vector
% plot of example data
figure();
plot(x,y, '-*');
ylim([-0.5, 4]);
for i = 2.5:21.5
xline(i, '-.', 'color', [150/255,150/255,150/255]); % this shows the x-values where a baseline point will be estimated
end
yline(0, 'color', [150/255,150/255,150/255]);
legend('raw data', 'values for baseline estimation');
% perform background correction
y2 = msbackadj(x', ... % transpose to column vector
y', ... % transpose to column vector
'WindowSize', 5, ...
'StepSize', 1, ...
'Quantile', 0, ...
'ShowPlot', 'yes');
% re-calculate vector of estimated baseline points
z = y - y2'; % transpose the output variable y2 so that it fits to the input variable y
% plot of result
figure();
plot(x,y, '-*');
hold on;
plot(x,z, '-*r');
plot(x,y2,'-*m'); % no need to transpose as plot does not care if a variable is a row or column vector
ylim([-0.5, 4]);
for i = 2.5:21.5
xline(i, '-.', 'color', [150/255,150/255,150/255]); % this shows the x-values where a baseline point was estimated
end
yline(0, 'color', [150/255,150/255,150/255]);
legend('raw data','regressed baseline', 'after background correction', 'values for baseline estimation');

Best Answer

Question = Answer in this case.