Matlab - Comparing Dissimilar Data Sets using Interp1
08 Jan 2008 Quan Quach 2 comments 418 views
Introduction
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
-
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
-
You should get a plot that looks similar to the following:

-
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.
-
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
interp1command. 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
-
You should see the following plot appear. Notice that the interpolated data set and the original data track each other quite well.

-
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
-
You should see the following plot appear. Congratulations, you just took the difference between two dissimilar data sets!

This is the end of the tutorial.
2 Responses to “Matlab - Comparing Dissimilar Data Sets using Interp1”
Leave a Reply
Include MATLAB code in your comment by doing the following:
<pre lang="MATLAB">
%insert code here
</pre>


I like how you break these down into two smaller tutorials. It makes them more digestable, and if you ever need the resampling skill for another tutorial. it is easy enough to reference it.
Doug
Thank you so much for this information.