7.2. Data InterpolationΒΆ

MATLAB has two functions that will interpolate between known data samples to estimate unknown sample points. The difference between the two functions relates just to the purpose for calling them. The fillmissing function is used when values that should be in the data are missing – often replaced with the NaN symbol. The interp1 function is used when you wish to add data points not contained in the data.

See the documentation for the usage information. They offer different methods of interpolation. The default is 'linear', which is good for slowly changing data or when there are a lot of sample points. The methods 'pchip' and 'spline' yield very good results. The spline method requires more computation than pchip. The spline method uses a matrix computation for each point added to determine polynomial coefficients that not only match that data, but maintain constant first and second derivatives at each data point added, which makes the curves between data points smooth.

f = @(x) 1 + x.^2 - x.^3 + 20*sin(x);
x1 = (-3:1.5:3)';  % Limited data points
y1 = f(x1);
x2 = (-3:0.5:3)';  % More data points
plot(x1,y1,'o',x2,interp1(x1,y1,x2,'nearest'),'*-'), title('nearest')
plot(x1,y1,'o',x2,interp1(x1,y1,x2,'linear'),'*-'), title('Linear')
plot(x1,y1,'o',x2,interp1(x1,y1,x2,'pchip'),'*-'), title('pchip')
plot(x1,y1,'o',x2,interp1(x1,y1,x2,'spline'),'*-'), title('spline')