How to remove a baseline without using sgolayfilt
15 views (last 30 days)
Show older comments
I have around 1000 files, see attached graphs, and I want them to all be on the same baseline for direct comparison. I have tried the sgolayfilt with code below, however that removes a lot of features in my data as it is essentially a damping technique aswell. I was wondering if there was a baseline function that let you manually input points for fitting?
windowWidth = 101
polynomialOrder = 3
baselineY = sgolayfilt(y, polynomialOrder, windowWidth);
figure(1)
plot(x, baselineY,'r');
title('Baseline graph');
xlabel('Wavenumber (cm-1)');
ylabel('Absorbance (a.u)');
0 Comments
Answers (2)
Star Strider
on 5 Sep 2017
Edited: Star Strider
on 6 Sep 2017
It depends on what you intend by ‘baseline’ (that you did not define).
The detrend function could be what you want.
EDIT —
To illustrate detrend:
x = linspace(900, 1900);
y1 = randn(size(x)) + cumsum(ones(size(x)))*0.3 + 1;
y2 = randn(size(x)) + cumsum(ones(size(x)))*0.9 + 2;
y1d = detrend(y1);
y2d = detrend(y2);
figure(1)
subplot(2,1,1)
plot(x, y1, x, y2)
title('Original')
grid
subplot(2,1,2)
plot(x, y1d, x, y2d)
title('Detrended')
grid
0 Comments
Luuk van Oosten
on 5 Sep 2017
As far as I know, sgolayfilt does not subtract the background signal (what I think you mean when you refer to 'same baseline'), but just smooths the data (sometimes also referred to as "noise reduction").
If you want an easy plug-and-play function to correct the baseline of your signals I would suggest the following :
Y_subtracted = msbackadj(X_yoursignal,Y_yoursignal);
4 Comments
Image Analyst
on 6 Sep 2017
Use this code to ask your user to manually/interactively enter the 2 y values you need.
% Ask user for two floating point numbers.
defaultValue = {'45.67', '78.91'};
titleBar = 'Enter a value';
userPrompt = {'Enter y1 : ', 'Enter y2: '};
caUserInput = inputdlg(userPrompt, titleBar, 1, defaultValue);
if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.
% Convert to floating point from string.
usersValue1 = str2double(caUserInput{1})
usersValue2 = str2double(caUserInput{2})
% Check usersValue1 for validity.
if isnan(usersValue1)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
% Convert the default from a string and stick that into usersValue1.
usersValue1 = str2double(defaultValue{1});
message = sprintf('I said it had to be a number.\nTry replacing the user.\nI will use %.2f and continue.', usersValue1);
uiwait(warndlg(message));
end
% Do the same for usersValue2
% Check usersValue2 for validity.
if isnan(usersValue2)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
% Convert the default from a string and stick that into usersValue2.
usersValue2 = str2double(defaultValue{2});
message = sprintf('I said it had to be a number.\nTry replacing the user.\nI will use %.2f and continue.', usersValue2);
uiwait(warndlg(message));
end
Adapt as needed.
See Also
Categories
Find more on Acoustics, Noise and Vibration in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!