matlabinterpolationsplinecubic-spline

Remove base line drift with peicewise cubic spline algorithm using MATLAB


I have a signal which I want to remove the basline drift using the picewise 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 cant see any basline removal..The orginal data is exactly on the interpolated one.Displayed plot

or in the other signal as we can see no base line is removed. plot2

The question is how I can "use peicewise cubic spline interpolation to remove basline drift" in MATLAB.

Thank you


Solution

  • I think you should reduce the number of points at which you calculate the spline fit (this avoids over-fitting) and successively 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. You 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