Remove baseline drift using cubic spline algorithm using MATLAB

I have a signal that I want to remove the baseline drift using the cubic spline algorithm in MATLAB.

d=load(file, '-mat'); t=1:length(a); xq1=1:0.01:length(a); p = pchip(t,a,xq1); s = spline(t,a,xq1); % figure, hold on, plot(a, 'g'), plot(t,a,'o',xq1,p,'-',xq1,s,'-.') legend('Sample Points','pchip','spline','Location','SouthEast') 

But I do not see any removal of the baseline. The source data is precisely on the interpolated. Plot displayed

or in another signal, as we see that the baseline is not removed. plot2

The question is how can I "use cubic spline interpolation in the form of peicewise to remove baseline drift" in MATLAB.

thanks

+5
source share
2 answers

I think you should reduce the number of points at which you calculate the spline image (this avoids reinstallation) and sequentially interpolate the fit on the original x-data.

 t = 0:60; trend = 0.003*t.^2; x = trend + sin(0.1*2*pi*t) + randn(1,numel(t))*0.5; figure;hold on plot(t,x,'.-') %coarser x-data t2=[1:10:max(t) t(end)]; %%quick and dirty. I probably wanna do better than this %spline fit here p = pchip(t,x,t2); s = spline(t,x,t2); plot(t2,s,'-.','color' ,'g') %interpolate back trend=interp1(t2,s,t); %remove the trend plot(t,x-trend,'-.','color' ,'c') 

result

+3
source

It seems likely that you want to fit the polynomial to your data to estimate baseline drift due to temperature changes. The problem with spline is that it will always perfectly match your data (similar to pchip ), because it is an interpolation method. You probably want the polyfit to use the cursor. The following code example shows how you can use polyfit to estimate drift. In this case, I come to the 3rd order polynomial.

 % generate some fake data t = 0:60; trend = 0.003*t.^2; x = trend + sin(0.1*2*pi*t) + randn(1,numel(t))*0.5; % estimate trend using polyfit p_est = polyfit(t,x,3); trend_est = polyval(p_est,t); % plot results plot(t,x,t,trend,t,trend_est,t,x-trend_est); legend('data','trend','estimated trend','trend removed','Location','NorthWest'); 

Update

If you have a toolbar for curve fitting, you can place a cubic spline with an additional smoothing constraint. In the above example you can use

 trend_est = fnval(csaps(t,x,0.01),t); 

instead of polyfit and polyval . You will have to play with the smoothing parameter, 0 - completely linear, and 1 - with the same results as spline .

enter image description here

+5
source

Source: https://habr.com/ru/post/1271374/


All Articles