Base line removal for Raman input

i run this code, read txt file , get plot about the Raman spectrum, second about the baseline removal.
The first plot is perfect , at the second plot the variable p doesn't get a value, only Nah. i don't know why
Code:
T1 = readtable('20ul_nicotine_10mg_per_ml_20ul_AgNPs_45sx1.txt','PreserveVariableNames',true);
x = T1{:,4};
y = T1{:,8};
figure
plot(x,y)
grid
TF = islocalmin(y, 'MinProminence',15, 'MinSeparation',60);
hold on
plot(x(TF), y(TF), '.r')
hold off
xlim([175 3303])
legend('Signal','Points Used To Identify Baseline Trend', 'Location','best')
title('XRD Spectrum With Identified Baseline')
p = polyfit(x(TF), y(TF),4);
BL = polyval(p, x);
figure
plot(x, y-BL)
%xlim([175 3303])
grid
title('Baseline-Detrended Signal')

 Accepted Answer

One problem is that there are a lot of missing data in the chosen variables, so it was necessary to remove them first in order to process the signal.
Try this —
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/830055/20ul_nicotine_10mg_per_ml_20ul_AgNPs_45sx1.txt','PreserveVariableNames',true)
T1 = 2048×23 table
Pixel Wavelength Wavenumber Raman Shift Dark Reference Raw data #1 Dark Subtracted #1 %TR #1 Absorbance #1 Raw data #2 Dark Subtracted #2 %TR #2 Absorbance #2 Raw data #3 Dark Subtracted #3 %TR #3 Absorbance #3 Lamp FitCurve Irradiance Ratio Irradiance_1(uW/cm^2/nm) Irradiance_2(uW/cm^2/nm) Irradiance_3(uW/cm^2/nm) _____ __________ __________ ___________ ____ _________ ___________ __________________ ______ _____________ ___________ __________________ ______ _____________ ___________ __________________ ______ _____________ _____________ ________________ ________________________ ________________________ ________________________ 0 NaN NaN NaN 2294 65535 10971 8721.7 NaN NaN 10971 8721.7 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN NaN 1948 65535 13026 11095 NaN NaN 13026 11095 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN NaN 2039 65535 19181 17226 NaN NaN 19181 17226 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 3 NaN NaN NaN 1022 65535 33340 32284 NaN NaN 33340 32284 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 4 NaN NaN NaN 2069 65535 44743 42762 NaN NaN 44743 42762 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 5 NaN NaN NaN 2213 65535 39164 36966 NaN NaN 39164 36966 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 6 NaN NaN NaN 2877 65535 22028 19264 NaN NaN 22028 19264 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 7 NaN NaN NaN 1907 65535 7832 5859 NaN NaN 7832 5859 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 8 NaN NaN NaN 1197 65535 2261.7 1083 NaN NaN 2261.7 1083 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 9 NaN NaN NaN 2032 65535 2584.3 568.67 NaN NaN 2584.3 568.67 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 10 NaN NaN NaN 3600 65535 3986.7 338 NaN NaN 3986.7 338 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 11 NaN NaN NaN 2663 65535 2819.7 287.33 NaN NaN 2819.7 287.33 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 12 NaN NaN NaN 2884 65535 3110.7 174.67 NaN NaN 3110.7 174.67 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 13 NaN NaN NaN 899 65535 1115.3 80.667 NaN NaN 1115.3 80.667 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 14 NaN NaN NaN 1117 65535 1154.7 125.33 NaN NaN 1154.7 125.33 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 15 NaN NaN NaN 1089 65535 1261.3 58.333 NaN NaN 1261.3 58.333 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
% x = T1{:,4};
% y = T1{:,8};
xy = T1{:,[4 8]} % Get Data (Matrix)
xy = 2048×2
1.0e+04 * NaN 0.8722 NaN 1.1095 NaN 1.7226 NaN 3.2284 NaN 4.2762 NaN 3.6966 NaN 1.9264 NaN 0.5859 NaN 0.1083 NaN 0.0569
xyidx = ~any(ismissing(xy),2); % Rows To Keep
xy = xy(xyidx,:); % Re-Defined Variables
x = xy(:,1);
y = xy(:,2);
Fs = 1;
y = highpass(y, 0.01, Fs, 'ImpulseResponse','iir'); % Remove Wandering Baseline
figure
plot(x,y)
grid
TF = islocalmin(y, 'MinProminence',5, 'MinSeparation',50);
hold on
plot(x(TF), y(TF), '.r')
hold off
xlim([175 3303])
legend('Signal','Points Used To Identify Baseline Trend', 'Location','best')
title('XRD Spectrum With Identified Baseline')
p = polyfit(x(TF), y(TF),3);
BL = polyval(p, x);
figure
plot(x, y-BL)
%xlim([175 3303])
grid
title('Baseline-Detrended Signal')
This is the best I can do with this signal. It requires both highpass filtering and polynomial baseline correction to get a decent result.
I have no idea what this signal is, or what it should look like, so make appropriate changes to produce the desired result. It could be filtered further with a lowpass filter to reduce the high-frequency noise, however since I have no idea what is signal and what is noise, I am reluctant to do that here.
Also, use the re-defined variables in any subsequent analysis, since those are good data with no missing (NaN) values.
.

2 Comments

thank you so much!!!! is very good, is perfect
As always, my pleasure!
Thank you!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!