使用MATLAB通过p​​eicewise立方样条算法去除基线漂移

Jul*_*tte 5 matlab interpolation spline cubic-spline

我有一个信号,我想在MATLAB中使用picewise三次样条算法去除基线漂移.

 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')
Run Code Online (Sandbox Code Playgroud)

但我看不到任何基线去除..原始数据完全在插值数据上.显示的情节

或者在另一个信号中,我们可以看到没有删除基线. plot2

问题是如何在MATLAB中"使用peicewise立方样条插值去除基线漂移".

谢谢

jod*_*dag 5

您可能希望将多项式拟合到数据中,以估算由于热变化引起的基线漂移.问题spline在于它总是完全适合您的数据(类似pchip),因为它是一种插值技术.你可能想要一个可以使用的courser fit polyfit.以下代码示例显示了如何使用它polyfit来估算漂移.在这种情况下,我拟合三阶多项式.

% 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');
Run Code Online (Sandbox Code Playgroud)

更新

如果您有曲线拟合工具箱,则可以使用额外的平滑约束拟合三次样条曲线.在上面的示例中,您可以使用

trend_est = fnval(csaps(t,x,0.01),t);
Run Code Online (Sandbox Code Playgroud)

而不是polyfitpolyval.您将必须使用平滑参数,0表示完全线性,1表示相同的结果spline.

在此输入图像描述


sha*_*aia 3

我认为您应该减少计算样条拟合的点数(这可以避免过度拟合)并连续在原始 x 数据上插入拟合。

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')
Run Code Online (Sandbox Code Playgroud)

结果