Introduction

Matlab Logo In this tutorial, you will learn how to compare dissimilar datasets (datasets with different sampling intervals and different number of data points). Usually, when you are comparing two data sets, you are looking for the difference between the two data sets. But what if the datasets don’t have the same sampling interval? How do you go about subtracting one dataset from the other?

The answer is to use the interp1 (or spline) command! If you don’t know how to use interp1 or spline, you should visit this tutorial first. Let’s do a quick example.

Example: Comparing two dissimilar data sets

  1. First, lets generate some sample data to work with. Notice that the input vector for these two data sets are different!

    t1 = 0.5:0.1:5; %input vector for data set 1
    y1 = exp(t1-4) + sin(2*t1) + t1.^.8; %output vector for data set 2
    plot(t1,y1) %plot the first data set
    grid on
     
    hold
    t2 = 0.5:0.03:5; %input vector for data set 2
    y2 = exp(t2-4) + cos(2*t2) + t2.^.3 ;%output vector for data set 2
    plot(t2,y2,'r') %plot the second data set
  2. You should get a plot that looks similar to the following:

    Sample Data

  3. If the two data sets had the same sampling interval, same number of data points, and the same startpoint and same endpoint, we could just simply do:

    %take the absolute value of the difference of the two data sets
    yDelta = abs(y2-y1);

    But since they don’t satisfy all the requirements, we need to do some interpolation before we can take the difference between these two data sets.

  4. The first thing we need to do is to align the two data sets so that the sampling interval, number of data points, and the startpoint and endpoint are the same. We can do this using the interp1 command. Since data set 2 has more data points, we should interpolate that data set to match data set 1. In addition, we should do a quick check to see that the interpolated data set looks the same as the original data set.

    t3 = 0.5:0.1:5;  %use the same sampling interval from data set 1
    y3 = interp1(t2,y2,t3,'spline') %interpolate the data using the spline method
    figure %create a new figure
     
    %plots the original data set and the interpolated data on same axis
    plot(t2,y2,'b',t3,y3,'g') 
    grid on
  5. You should see the following plot appear. Notice that the interpolated data set and the original data track each other quite well.

    Interpolated Data Set Vs the Original Data Set

  6. Next, we are ready to take the difference between the two data sets.

    %take the absolute value of the difference between the two data sets
    yDelta = abs(y1-y3); 
     
    figure %open a new figure
    plot(t3,yDelta) %plot the difference
    grid on
  7. You should see the following plot appear. Congratulations, you just took the difference between two dissimilar data sets!

    Difference between the Two Data Sets

This is the end of the tutorial.