5

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

Juliette
  • 341
  • 1
  • 11
  • Is the data you want small fluctuations along that larger curve? Is the spline fitting the baseline drift? If so, then you have to subtract the spline fit from your original data. – gnovice Aug 30 '17 at 20:46
  • Thank you for your reply. But I did not realized what you said. I am sorry. Could you please explain further? Yes, the original data fits splined one. If I subtract these two the result will be zero as they are exactly fit. – Juliette Aug 30 '17 at 20:49
  • Maybe the spline is fitting too well. What component of the signal are you considering baseline drift and what are you considering the actual signal of interest? – gnovice Aug 30 '17 at 20:53
  • Trend of the signal is "basline drift" which is part of signal device thermal heat effect. – Juliette Aug 30 '17 at 20:57
  • You may want a lower order fit. Splines is an interpolation technique which fits a different polynomial between each pair of samples. You probably want to use `polyfit` which finds the best single polynomial to fit the data (in a least squares sense). – jodag Aug 30 '17 at 21:01

2 Answers2

5

It seems likely that you are looking to fit a polynomial to your data to estimate baseline drift due to thermal variations. The problem with spline is that it will always perfectly fit your data (similar to pchip) because it is an interpolation technique. You probably want a courser fit which you can get using polyfit. The following code sample shows how you could use polyfit to estimate the drift. In this case I fit a 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 the curve fitting toolbox you can fit a cubic spline with an extra smoothing constraint. In the example above you could 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 being completely linear and 1 giving the same results as spline.

enter image description here

jodag
  • 19,885
  • 5
  • 47
  • 66
  • Thank you for your answer. In most of the scientific papers that I have read till now to analyse these type of data ( such as what you can see in the second figure in the question), they mentioend that they used " peicewise cubic splined interpolation to remove baseline drift". Therefore I would like to replicate and understand fully what they did... I would be very pleased if you can help me to do that considering that the data is very long (30 minutes) I dont think I can use poly fit for such a long data if I am not mistaken. – Juliette Aug 30 '17 at 21:49
  • Sorry about 30 minutes I am wrong and I could see how we can remove basline drift using polyfit but I strongly need to work with peice wise cubic spline algorithm. Thanks – Juliette Aug 30 '17 at 21:58
  • @Juliette Updated. – jodag Aug 31 '17 at 03:44
  • @jodah Thanks a lot – Juliette Aug 31 '17 at 12:59
  • @jodah You helped a lot. Thanks a lot..But still a tiny part of my question is not answered. Many papers claims that they used cubic spline algorithm in peicewise way where "each period" of the signal starts. shall I apply this function in each peiod?or it will do it automatically? – Juliette Aug 31 '17 at 17:03
  • 1
    @Juliette `csaps` is performing piecewise cubic interpolation between each sample, not each period. It fits a different polynomial between each sample, however, it uses an additional smoothness constraint that penalizes the solution for not being smooth enough. This is why the result won't doesn't exactly match each sample. If you know how length of a period of your periodic signal is then you can apply `csaps` or `spline` to a subset of samples (one sample per period) as demonstrated in shamalia's answer which results in a piecewise per-period spline. – jodag Sep 01 '17 at 15:43
  • @Juliette Have you checked any of the papers you mention to see if they cite the detrend method they use? – jodag Sep 01 '17 at 15:49
  • Thanks..as I can see they did not cite it they just mentioend use a MATLAB toolbox for this purpose. – Juliette Sep 01 '17 at 21:33
  • @Juliette: Don't forget, you can [mark an answer as accepted](https://stackoverflow.com/help/accepted-answer) when it solves your problem. – gnovice Sep 05 '17 at 15:34
3

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

shamalaia
  • 2,282
  • 3
  • 23
  • 35