MATLAB Answers

# How to remove outlier of time series signal data by using cubic spline interpolation?

10 views (last 30 days)
Yared Daniel on 13 May 2021
Commented: Yared Daniel on 13 May 2021
Hello everybody am new for Matlab and need an algorithm for my project that computes the following problems.
I have a time series 1D signal data like this:
x=[180 142 213 78 90 192 40 38 105 162 270 262 211 170 72 28 40 55 90 201]
and
L = numel(x),
fs=4
t=0:1/fs:(L-1)/fs;
I want to detect and remove outliers which is less than 50 and greater than 200 by using cubic spline interpolation.
Thank you so much in advance for all your contribution
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

DGM on 13 May 2021
If you want to use normal tools,
x=[180 142 213 78 90 192 40 38 105 162 270 262 211 170 72 28 40 55 90 201]
L = numel(x);
fs=4
t=0:1/fs:(L-1)/fs; % [0 4.75]
limits = [50 200];
% find valid data locations
mask = ~(x<limits(1) | x>limits(2));
% select only x,t corresponding to good samples
tc = t(mask);
xc = x(mask);
% interpolate back to original timebase
xr = interp1(tc,xc,t,'spline');
% or perhaps use a finer timebase?
tf = linspace(min(t),max(t),100);
xf = interp1(tc,xc,tf,'spline');
plot(t,x,'k:'); % all original samples
hold on; grid on
plot(tc,xc,'ko') % valid samples
plot(t,xr,'g') % same timebase
plot(tf,xf,'b') % finer timebase
Of course, there's nothing stopping the interpolated result from projecting values beyond the limits again, because that's where they would be using a spline. If this is some sort of homework where you have to write a cubic spline interpolation routine, that's a different story.
##### 2 CommentsShowHide 1 older comment
DGM on 13 May 2021
I'm confused. That description seems to be what this:
% interpolate back to original timebase
xr = interp1(tc,xc,t,'spline');
part would be (as opposed to xf). That was the complete removal of said samples and cubic spline interpolation on the given timebase.
If it's not quite what you want, you can always unaccept the answer. Accepting an answer before you're satisfied makes it much less likely that others might try to offer different solutions.

Sign in to comment.

### More Answers (1)

Steven Lord on 13 May 2021
Use > and < or the isoutlier function to determine where the outliers are located.
Use filloutliers specifying 'OutlierLocations' with the vector of outlier locations determined in the first step as that argument's value. Specify 'spline', 'pchip', or perhaps 'makima' as the fillmethod input.
##### 2 CommentsShowHide 1 older comment
Yared Daniel on 13 May 2021
Thanks this worked well

Sign in to comment.

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!