-
Notifications
You must be signed in to change notification settings - Fork 0
/
FMRBaselineDriftCorrect.m
37 lines (26 loc) · 1.66 KB
/
FMRBaselineDriftCorrect.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
function y=FMRBaselineDriftCorrect(spectrum,varargin)
ends=0;
if nargin>1
if strcmp(varargin{1},'highend'), ends=1; elseif strcmp(varargin{1},'lowend'),ends=-1; end
end
y=spectrum;
smoothSpan=double(int32(0.03/((max(y.fields)-min(y.fields))/length(y.fields)))*2+1);
%slopes=smooth(diff(y.datDerivSmooth)./diff(y.fields),double(int32(smoothSpan/4)*2+1),'rlowess');
%slopes=.5*([slopes(1) ; slopes] + [slopes ; slopes(end)]);
%significantpoints=find((abs(y.datIntSmooth)/max(y.datIntSmooth)>0.0005) .* (abs(slopes)>400));
%significantpoints=find((abs(y.datIntSmooth)/max(y.datIntSmooth)>0.005))
significantpoints=15:(length(y.datInt)-14);
if ends == 1, significantpoints=sort(union([1:min(significantpoints)-1],significantpoints));
elseif ends==-1, significantpoints=sort(union(significantpoints,[significantpoints+1:length(y.datIntSmooth)])); end
[linefit,linefitgoodness]=fit(y.fields([1:(min(significantpoints)-1) (max(significantpoints)+1):end]),y.datDeriv([1:(min(significantpoints)-1) (max(significantpoints)+1):end]),'poly1')
%meanBaselineSlope=mean(slopes([1:min(significantpoints) max(significantpoints):end]))
%slopeRemovedSmooth=y.datDerivSmooth-meanBaselineSlope*y.fields;
%slopeRemoved=y.datDeriv-meanBaselineSlope*y.fields;
%meanIntercept=mean(slopeRemoved([1:5]))
if linefitgoodness.rsquare > 0.9, y.datDeriv=y.datDeriv-linefit(y.fields); end
%y.datDeriv=slopeRemoved - meanIntercept;
%y.derivInterp=fit(y.fields,y.datDeriv,'linearinterp');
y.datInt=cumtrapz(y.fields,y.datDeriv);
y.datIntSmooth=smooth(y.datInt,smoothSpan,'rlowess');
%y.intInterp=fit(y.fields,y.datIntSmooth,'linearinterp');
y.datDerivSmooth=smooth(y.datDeriv,double(int32(smoothSpan/4)*2+1),'rlowess');