Introduction

Matlab Logo In this tutorial, you will learn how to interpolate a data set using the interp1 and spline command. Interpolation is a method of constructing new data points from a discrete set of known data points. This can be helpful when you are presented with a data set that does not have the desired resolution. Interpolating a data set can also give the effect of “smoothing” out a data set.

There are many ways to interpolate a dataset, but I prefer to use spline. A spline is a special function defined piecewise by polynomials. In interpolating problems, spline interpolation is often preferred to polynomial interpolation because it yields similar results, even when using low degree polynomials, while avoiding Runge’s phenomenon for higher degrees (taken from Wikipedia). For more information on the different interpolation methods and their pros and cons, click here.

In this tutorial, we will go over a two simple examples on how to interpolate the data. The first example will use interp1 and the second example will use spline.

Example 1: Interpolating a Data Set by a Factor of 2 using Interp1

  1. First, lets generate some sample data using the following matlab code. In general, you will NOT know the output equation, or else interpolating would be unnecessary. Copy and paste this code into the command prompt:

    t = 0:0.2:3;     %time vector
    y = sin(t) + cos(3*t);  %output vector
    stem(t,y) %plot the data
  2. You should see the following graph appear

    Sample Data

  3. Next, we want to increase the sampling rate of this data set by 2.

    t2 = 0:0.2/2:3; %increase the sampling rate by two, which will
                        %give us double the data points
     
    %interp1 takes in 3 arguments
    %the first is the input vector, t
    %the second is the output vector, y
    %make sure t and y are the same length
    %the third is the new sampling interval
     
    y2=interp1(t,y,t2);  %interpolates the data
    stem(t2,y2); %plot the new interpolated data
  4. And thats all there is to it. You should see the following graph appear. Notice that there are twice as many data points now, which fills out the plot much better.

    Interpolated Sample Data

  5. By default, the interp1 command uses linear interpolation. If you wanted to use a different type of interpolation, you can specify it as the fourth argument. From the matlab help file:

    ‘nearest’ - nearest neighbor interpolation
    ‘linear’ - linear interpolation
    ’spline’ - piecewise cubic spline interpolation (SPLINE)
    ‘pchip’ - shape-preserving piecewise cubic interpolation
    ‘cubic’ - same as ‘pchip’
    ‘v5cubic’ - the cubic interpolation from MATLAB 5, which does not
    extrapolate and uses ’spline’ if X is not equally
    spaced.

  6. So in the previous example, if I wanted to do a pchip interpolation instead of a linear interpolation, I would have used the following code instead:

    t2 = 0:0.2/2:3; %increase the sampling rate by two, which will
                        %give us double the data points
    y2=interp1(t,y,t2,'pchip');  %interpolate the data using the pchip method
    stem(t2,y2); %plot the data

Pages: 1 2