Example 2: Interpolating a Data Set using a Different Sampling Interval

Sometimes, we need to interpolate a data set using a different sampling interval from the original dataset. The interp1 and spline command gives us great flexibility in interpolating data and will allow us to do this. While we can use interp1 and specify the interpolation method as ’spline’ (which will give us the exact same result), I prefer to use spline for the most part due to the reasons listed in the introduction and because the command is a little shorter.

In the previous example, we used a sampling rate of 0.2 seconds from 0 to 3 seconds.

But what if we wanted to interpolate the data set so that we were sampling at 0.07 seconds from 0 to 3 seconds? Lets do a quick example.

  1. Let’s generate the sample data again:

    t = 0:0.2:3;     %time vector
    y = sin(t) + cos(3*t);  %output vector
    stem(t,y) %plot the data
  2. Sample Data

  3. Remember, in most cases, we won’t know the the output equation, so we must interpolate. So in this case, the data we have to work with are the t vector (input data), and the y vector (output data).

  4. Once again, we’re trying to interpolate the data using a different sampling interval. Instead of the following sampling interval

    t = 0:0.2:3;     %original sampling interval

    we want to use this one instead

    t3 = 0:0.07:3;     %new sampling interval
  5. The following code will create a vector that contains that newly interpolated data and will plot this new data onto the previous figure:

    hold %hold the plot from the previous set of commands
     
    %spline takes in three arguments
    %the first is the input vector of the original data set (t in this case)
    %the second is the output vector of the original data set (y in this case)
    %make sure that t and y are the same length
    %the third is the new sampling interval (t3 in this case)
    y3 = spline(t, y ,t3); %creates a the new interpolated dataset
     
     
    %we could have also used interp1 to get the same results:
    %y3 = interp1(t,y,t3,'spline')
     
    stem(t3,y3,'g')
  6. You should see the following plot. The original data set is plotted using the color blue, and the new data set is plotted used the color red. Notice the new sampling interval that has taken place!

    Sample Data interpolated using Spline

This is just a basic tutorial on how to interpolate data sets. The next tutorial will focus on applying these techniques to compare dissimilar data sets, or data sets that do not share the same sampling interval.

This is the end of the tutorial.

Pages: 1 2