% Example of the use the mathematical technique of interpolation to change
% the number of points or to equalize unequally-spaced x-axis intervals of
% signals; the results are only approximate but often close enough in
% practice. Matlab and Octave has built in functions for linear, cubic
% spline, Modified Akima piecewise cubic Hermite (Makima), Piecewise Cubic
% Hermite Interpolating Polynomial interpolation (pchip) interpolation.
% This script compares all four methods for an undersampled pair of
% gaussian peaks
%
x = 0:1:20;
xx=0:.01:18;
y=exp(-(x-6.4).^2) + exp(-(x-12.5).^2);
xx=0:.1:20;
yy=exp(-(xx-6.4).^2) + exp(-(xx-12.5).^2);
Xq = 0:.25:20;
Spliney = spline(x,y,Xq);
Lineary = interp1(x,y,Xq);
pchipy=pchip(x,y,Xq);
Makimay=makima(x,y,Xq);
clf
plot(x,y,'ok',xx,yy,':',Xq,Lineary,':g',Xq,Spliney,Xq,pchipy,Xq,Makimay,'linewidth',2)
axis([min(x) max(x) -.2 1.4])
title('Comparison of Linear and Cubic Spline interpolation to change the x-axis spacing')
xlabel('x')
ylabel('y')
legend('Data points','underlying signal','Linear interpolation','Spline interpolation','Pchip interpolation','Makima interpolation')