MATLAB - Interpolation
MATLAB - Data Interpolation Techniques
Section titled “MATLAB - Data Interpolation Techniques”Interpolation is the process of estimating unknown values that fall between known data points. It is a fundamental tool in data analysis, signal processing, and computer graphics for tasks like resampling, smoothing, and filling in missing data. MATLAB provides a comprehensive set of functions for performing interpolation on 1D, 2D, and 3D datasets, as well as on scattered data.
This tutorial focuses on the modern, recommended functions: interp1 for 1D data, interp2 for 2D gridded data, interp3 for 3D gridded data, and griddedInterpolant / scatteredInterpolant for more advanced, performance-oriented workflows.
1D Interpolation with interp1
Section titled “1D Interpolation with interp1”The interp1 function is the primary tool for one-dimensional interpolation. Given a set of sample points (x, v), interp1 finds the value vq at a set of new query points xq.
Syntax
Section titled “Syntax”vq = interp1(x, v, xq)vq = interp1(x, v, xq, method)vq = interp1(..., 'extrap')Where method can be 'linear' (default), 'nearest', 'next', 'previous', 'pchip', 'cubic', 'spline', or 'makima'. The choice of method determines how the curve is constructed between the known data points.
Example 1: Basic Linear and Spline Interpolation
Section titled “Example 1: Basic Linear and Spline Interpolation”% 1. Define the original, coarse sample datax = 0:pi/2:2*pi;v = sin(x);
% 2. Define the query points for a finer gridxq = 0:pi/16:2*pi;
% 3. Perform interpolationvq_linear = interp1(x, v, xq, 'linear'); % Linear interpolationvq_spline = interp1(x, v, xq, 'spline'); % Smoother spline interpolation
% 4. Plot the results for comparisonfigure;plot(x, v, 'o', 'MarkerSize', 10); % Original sample pointshold on;plot(xq, vq_linear, '.-'); % Linear resultplot(xq, vq_spline, '--'); % Spline resulthold off;
title('1D Interpolation of sin(x)');xlabel('X');ylabel('Value');legend('Original Samples', 'Linear Interpolation', 'Spline Interpolation', 'Location', 'southwest');The output plot shows the original coarse sample points as large circles. The blue line with dots shows the ‘connect-the-dots’ result of linear interpolation. The orange dashed line shows the much smoother curve produced by spline interpolation, which often provides a more physically plausible fit.
2D and 3D Gridded Interpolation
Section titled “2D and 3D Gridded Interpolation”For data defined on a regular grid (like an image or a 3D volume), interp2 and interp3 are used. The syntax is analogous to interp1.
Example 2: Interpolating a 2D Grid with interp2
Section titled “Example 2: Interpolating a 2D Grid with interp2”% 1. Create a coarse 2D grid and data[X, Y] = meshgrid(-3:1:3); % Coarse gridZ = peaks(X, Y); % Z-values from the 'peaks' function
% 2. Create a finer grid for querying[Xq, Yq] = meshgrid(-3:0.25:3); % Fine grid
% 3. Perform 2D interpolation using the 'cubic' methodZq = interp2(X, Y, Z, Xq, Yq, 'cubic');
% 4. Visualize the comparisonfigure;subplot(1, 2, 1);surf(X, Y, Z);title('Original Coarse Surface');
subplot(1, 2, 2);surf(Xq, Yq, Zq);title('Interpolated Fine Surface');The output shows two subplots. The left plot displays a blocky surface based on the original coarse data. The right plot shows a much smoother, higher-resolution surface created by interpolating values onto the finer grid. The ‘cubic’ method ensures the resulting surface is smooth.
Interpolating Scattered Data
Section titled “Interpolating Scattered Data”When data points (x, y, z) are not on a regular grid, they are called scattered data. The griddata function or, for better performance, the scatteredInterpolant object is used.
Example 3: Using scatteredInterpolant for Efficiency
Section titled “Example 3: Using scatteredInterpolant for Efficiency”The scatteredInterpolant object is ideal when you need to query the same scattered dataset multiple times, as it performs the expensive initial triangulation only once.
% 1. Generate random scattered data pointsx = rand(100, 1) * 4 - 2; % 100 random points in [-2, 2]y = rand(100, 1) * 4 - 2;z = x .* exp(-x.^2 - y.^2);
% 2. Create the interpolant objectF = scatteredInterpolant(x, y, z, 'natural');
% 3. Define a regular grid to query the interpolant[Xq, Yq] = meshgrid(-2:0.1:2);Zq = F(Xq, Yq); % Evaluate the interpolant on the grid
% 4. Plot the resultsfigure;surf(Xq, Yq, Zq); % Plot interpolated surfacehold on;plot3(x, y, z, 'o', 'MarkerFaceColor', 'r', 'MarkerEdgeColor', 'k'); % Overlay original pointshold off;
title('Interpolating Scattered Data');legend('Interpolated Surface', 'Original Scattered Points', 'Location', 'northwest');The resulting plot shows a smooth surface constructed from the scattered data. The original 100 data points are overlaid as red circles, demonstrating how the surface passes through them.
Best Practices and Choosing a Method
Section titled “Best Practices and Choosing a Method”- Linear: Fast and simple. Good for densely sampled data or when speed is critical.
- Spline/Pchip/Makima: Produce smooth curves.
'pchip'and'makima'are good general-purpose choices as they avoid the overshoots that'spline'can sometimes create. - Nearest: Useful for categorical data or when you need to preserve original values without creating new ones.
- Performance: For repeated interpolations on the same dataset, use
griddedInterpolantorscatteredInterpolantto significantly improve performance.