MATLAB - Interpolation



Interpolation is a method used in mathematics and computer science to estimate unknown values that lie between known data points. It is commonly used in various fields, including signal processing, computer graphics, and numerical analysis, to create a smooth curve or surface that approximates the data.

In MATLAB, interpolation is implemented using functions like interp1() for 1D interpolation and interp2() for 2D interpolation. These functions take the known data points (x and y coordinates) and a set of points where interpolation is desired. The interpolation method can be specified, such as linear interpolation ('linear'), spline interpolation ('spline'), or nearest neighbor interpolation ('nearest'). The chosen method determines how the curve or surface is constructed between the known data points.

By using interpolation, you can fill in missing data, create smoother representations of data, and generate new data points for visualization or analysis. MATLAB's interpolation functions provide a powerful tool for working with data that is not regularly sampled or for generating interpolated surfaces from scattered data points.

Syntax

y = interp(x,r)
y = interp(x,r,n,cutoff)
[y,b] = interp(x,r,n,cutoff)

The explanation for the syntax mentioned above is as follows −

y = interp(x,r) − takes a signal x and increases its sample rate by a factor of r.

y = interp(x,r,n,cutoff) − here we have two additions which are n and cutoff.n is half the number of original sample values used to interpolate the expanded signal.

cutoff is the normalized cutoff frequency of the input signal, specified as a fraction of the Nyquist frequency.

[y,b] = interp(x,r,n,cutoff) − gives back the filter coefficients used for the interpolation.

Example 1: Using y = interp(x,r)

The code we have is −

x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
r = 2;
y = interp(x, r);
subplot(2, 1, 1);
stem(x, 'b', 'DisplayName', 'Original Signal');
xlabel('Sample');
ylabel('Value');
title('Original and Interpolated Signals');
legend('show');
subplot(2, 1, 2);
stem(y, 'r', 'DisplayName', 'Interpolated Signal');
xlabel('Sample');
ylabel('Value');
legend('show');

This code creates a subplot with two plots: one for the original signal (x) and one for the interpolated signal (y). The stem function is used to plot the signals as stem plots, where each data point is represented as a vertical line. The subplot(2, 1, 1) command divides the plot area into two rows and one column, and selects the first subplot for the original signal. The subplot(2, 1, 2) command selects the second subplot for the interpolated signal.

interpolated signal

Example 2: Interpolating a Signal with Additional Parameters like n and cutoff

The code we have is −

n = 5;
x = 1:20;
r = 2;
cutoff = 0.5;
y = interp(x, r, n, cutoff);
subplot(2, 1, 1);
stem(x, 'b', 'DisplayName', 'Original Signal');
xlabel('Sample');
ylabel('Value');
title('Original and Interpolated Signals');
legend('show');

subplot(2, 1, 2);
stem(y, 'r', 'DisplayName', 'Interpolated Signal');
xlabel('Sample');
ylabel('Value');
legend('show');

In the example above −

  • The filter length n is set to 5, which determines the length of the filter used for interpolation. It is half the number of original sample values used to interpolate the expanded signal.The original signal x is defined as 1:20, which creates a vector of integers from 1 to 20.
  • The sample rate is increased by a factor of 2, specified by r = 2.The normalized cutoff frequency cutoff is set to 0.5, which is a fraction of the Nyquist frequency. It determines the frequency at which the filter attenuates to half power.
  • The interp() function is used to interpolate the original signal x using the specified parameters (r, n, cutoff).The interpolation process increases the sample rate of the original signal, resulting in a new interpolated signal y.
  • Two subplots are created using the subplot() function to display the original and interpolated signals.The stem() function is used to plot the signals as stem plots, where each data point is represented as a vertical line.The first subplot shows the original signal (x) in blue, and the second subplot shows the interpolated signal (y) in red.The x-axis represents the sample number, and the y-axis represents the signal value.

When the code is executed in matlab command window the output is −

Signal additional parameters

Example 3: Displaying filter coefficients using [y,b] = interp(x,r,n,cutoff)

The code we have is −

x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
r = 2;
n = 5;
cutoff = 0.5;
[y, b] = interp(x, r, n, cutoff);

% Plot the original and interpolated signals
subplot(2, 1, 1);
stem(x, 'b', 'DisplayName', 'Original Signal');
xlabel('Sample');
ylabel('Value');
title('Original and Interpolated Signals');
legend('show');

subplot(2, 1, 2);
stem(y, 'r', 'DisplayName', 'Interpolated Signal');
xlabel('Sample');
ylabel('Value');
legend('show');

% Display the filter coefficients
disp('Filter Coefficients:');
disp(b);

In this example, the interp() function is used to interpolate the original signal x with the specified parameters (r, n, cutoff). The interpolated signal y is obtained, and the filter coefficients b used for the interpolation are also returned. The filter coefficients are displayed using disp(b) at the end of the example.

When the code is executed the output is as follows −

Filter Coefficients:
    0.0000
    0.0020
   -0.0000
   -0.0135
    0.0000
    0.0511
   -0.0000
   -0.1546
    0.0000
    0.6150
    1.0000
    0.6150
    0.0000
   -0.1546
   -0.0000
    0.0511
    0.0000
   -0.0135
   -0.0000
    0.0020
    0.0000
filter coefficients

Interpolating Gridded Data

When working with gridded data, interpolation is often used to estimate values between grid points. MATLAB provides several functions for interpolating gridded data, such as interp2 for 2D data and interp3 for 3D data. These functions can be used to create smoother representations of the data or to extract values at specific points within the grid.

Example 1: Using interp2 to interpolate 2D gridded data

The code we have is −

[x, y] = meshgrid(-2:0.5:2, -2:0.5:2);

% Define the function z = f(x, y) = x^2 + y^2
z = x.^2 + y.^2;

% Create a finer grid for interpolation
[xq, yq] = meshgrid(-2:0.1:2, -2:0.1:2);

% Perform 2D interpolation
zq = interp2(x, y, z, xq, yq, 'cubic');

% Plot the original and interpolated data
figure;
surf(x, y, z);
hold on;
surf(xq, yq, zq);
legend('Original Data', 'Interpolated Data');
xlabel('X');
ylabel('Y');
zlabel('Z');
title('2D Interpolation of Gridded Data');

In this example, we first create a grid of x and y values using meshgrid. We then define a function z = f(x, y) = x^2 + y^2 and calculate the corresponding z values on the grid. Next, we create a finer grid for interpolation using meshgrid again. Finally, we use interp2 with the cubic method to interpolate the z values at the new grid points xq and yq. The interpolated data is then plotted alongside the original data for comparison.

On execution the output is −

2d gridded data

Example 2: Using interp3 to interpolate 3D gridded data

The code we have is −

[x, y, z] = meshgrid(-2:0.5:2, -2:0.5:2, -2:0.5:2);


w = x.^2 + y.^2 + z.^2;

% Create a finer grid for interpolation
[xq, yq, zq] = meshgrid(-2:0.1:2, -2:0.1:2, -2:0.1:2);

% Perform 3D interpolation
wq = interp3(x, y, z, w, xq, yq, zq, 'cubic');

% Plot the original and interpolated data
figure;
slice(x, y, z, w, [0], [0], [0]); % plot slices of the original data
hold on;
slice(xq, yq, zq, wq, [0], [0], [0]); % plot slices of the interpolated data
xlabel('X');
ylabel('Y');
zlabel('Z');
title('3D Interpolation of Gridded Data');
legend('Original Data', 'Interpolated Data');

In this example, we first create a grid of x, y, and z values using meshgrid. We then define a function w = f(x, y, z) = x^2 + y^2 + z^2 and calculate the corresponding w values on the grid. Next, we create a finer grid for interpolation using meshgrid again. Finally, we use interp3 with the cubic method to interpolate the w values at the new grid points xq, yq, and zq. The interpolated data is then plotted alongside the original data for comparison.

On execution we have −

3d gridded data

Interpolating Scattered Data

Scattered data in MATLAB refers to a set of data points that are not arranged on a regular grid. This type of data is common in many scientific and engineering applications where measurements are taken at arbitrary locations. Interpolating scattered data involves estimating the values between these points to create a smooth surface or function.

In MATLAB, the griddata function is often used for interpolating scattered data. It uses various interpolation methods, such as linear, nearest neighbor, cubic, and spline, to estimate values at points not explicitly given in the dataset.

Example

The code we have is −

x = rand(100,1)*4 - 2;
y = rand(100,1)*4 - 2;
z = peaks(x,y);

% Define a regular grid for interpolation
[Xq,Yq] = meshgrid(-2:0.1:2, -2:0.1:2);

% Interpolate scattered data to the regular grid using griddata
Zq = griddata(x,y,z,Xq,Yq,'cubic');

% Plot the scattered data and the interpolated surface
figure;
scatter3(x, y, z, 'filled'); % Plot scattered data points
hold on;
surf(Xq, Yq, Zq); % Plot interpolated surface
hold off;
xlabel('X');
ylabel('Y');
zlabel('Z');
title('Interpolating Scattered Data');
legend('Scattered Data', 'Interpolated Surface');

In the example −

  • We generate random scattered data points (x, y) and calculate corresponding z values using the peaks function.
  • We define a regular grid (Xq, Yq) using meshgrid over the same range as the scattered data.
  • We use griddata to interpolate the scattered data onto the regular grid. The 'cubic' method is used for interpolation, but you can also use 'linear', 'nearest', or other methods.
  • Finally, we plot the scattered data points and the interpolated surface to visualize the interpolation.

The output on execution is −

interpolating scattered data
Advertisements