Matlab - Interpolating Data using Interp1 and Spline
07 Jan 2008 Quan Quach 23 comments 9,428 views
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.
-
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
-
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).
-
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
-
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')
-
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!


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
23 Responses to “Matlab - Interpolating Data using Interp1 and Spline”
Leave a Reply
Include MATLAB code in your comment by doing the following:
<pre lang="MATLAB">
%insert code here
</pre>


i need a matlab code for linear interpolation and nearest neighbourhood also.can u pls send me that.i am writting the code for filtered back projection algorithm and my next step is to implement the interpolation.for time being i am using shepp logan phantoms that is head phantoms.and now applying the real ct datas.output is not having clarity.also i need a matlab code for ramlak filter too.pls help me out
Hello Rani,
I think what you are asking is beyond the scope of our abilities.
Hi,
could you show me how to code interp1 when given a vector of x values, a vector of y values and a vector of new_x values for which we want ot find new_y values for?
Keerthi,
Try this:
I am able to obtain value of curve given a value of x-axis i.e. xx
I want to find value of of xx given a value of yy. Your help will be highly appreciated.
% xx = linspace(1,10,5);
% pp = spline(xx,[1 0.93 0.55 0.322 .231]);
% yy=ppval(pp,4);
Ambitious,
This looks like you would just switch the data order in the spline function.
Given two vectors, x and y, you want to be able to find xx for the new vector yy (also given). So it’s the same process, but you just swap the variables.
Ambitious,
There are many ways in getting the value of xx, but it depends on the context of your problem.
1) Do you need it automated?
2) Do you do it once, or multiple times?
3) Is there a tolerance value?
One approach is to simply plot(xx,yy), and then plot(yval,’ro’), and see where it intersects by eyeballing… this is solving by graphing.. quick and easy… you can always use this
the numerical approach is to increase your xx = linspace(1,10,100) so that you have more resolution… then call i = find(yy>=yval,1,’first’) [you can play with the parameters to meet your needs by using ==, longer arry, or 'last']… the function will output the index i … then you simply call xx(i) to get your i value.
But do note that you need some sort of tolerance value, since your linspace might not be in a fine enough resolution.. it depends on the context of your problem
hey
how do we generate a square root raised cosin pulse?
Dear Quan Quach,
I have a graph from ground obstacles and I’ve found the edge’s points of that. I want to make them smooth by splines. I’ve tried to use pchip function but it doesn’t work and I got this error : “The data abscissae should be distinct.”
could you please help me,
thanks
Dear Quan Quach,
I want to interpolate a data set with spline of higher order ( say 5th order ) since spline command can only do cubic spline inerpolation, How can i do this?
Regards,
Sundar.
Dear Quan Quach,
Thank for your useful interpolation example.
I just want to know how to interpolate for a certain range only since the interested data at certain particular point?
For example, if my interested point at 1.5seconds, how to interpolate from 1 - 2 seconds only of your example1.
thank you
-Nizam-
hi i need to know internal meaning of interp function –
Hi Quan,
I have a question regarding interpolation. I have a graph of multiple heart beats. There are many peaks. I have been able to identify the position of all the peaks and also the difference on the x axis between subsequent peaks. Now, the differences are not the same because every heart beat varies with time. How am I supposed to interpolate the graph so that the difference on the x axis between peak to peak is the same for all?
hi i want to find the rise time of a certain second order system
i use interp1 to search the table for the value when it is 90% of the final value and when it is 10% of the final value i.e.
what does that mean? n how do i solve this problem
Hi,
I want to interpolate missing values in the excel sheet.Basically excel sheet contains the financial data of different stock exchanges. Can anyone tell me which one method is best for financial data and how to interpolate the data…
Hi there,
i am working on a similar project to the one that rani was talking about.
Can you send me his email-adress to contact him?
regards
dirk
Hi,
I’m trying to do a 2D interpolation from a grid of uneven dimensions to one of even dimensions. I’ve been encountering problems with interp2 and griddata since I can’t figure out how to get the functions to accept anything that is not of the same dimension. In other words, I’m trying to interpolate a function on a mesh of dimensions imax*jmax where imax ~= jmax onto a mesh of the size nmax*nmax.
Any help would be greatly appreciated.
Thanks,
Anonymous
Hi,
I have a Matlab code which works with interpolation. Write now I am using the following lines.
yy=interp1(x,y,xx,’pchip’)….. and it interpolates just fine.
The problem is that now I have to change the interpolation method to spline, and I did exactaly the same thing but with the other method:
yy=spline(x,y,xx)….
when I do this change, the following problem accurs:
‘Segmentation violation detected at Mon Nov 02 15:35:42 2009′
and I have to close matlab. It comes a erro dialog box saying the the program has to be closed, but I have the option to keep the program running just to save my datas, but it is recomended to close.
Does anyone have a clue on what is going on???
Thanks a lot
ps: I am using Matlab 7.8.0
Hi,
I want to interpolate values from two sets already given. Eg. : For time series 0:2:20, Y1 = [1 2 ... (11 valuse)] … Y2 = [3 4 ..(11 values)] . Now i want to find a series Y3 having values between Y1 nad Y2 for same time series. How to do that in matlab??
Hi,
I have 3 signals with 109, 105 and 91 samples respectively. I want to normalize all of them to 120 samples. How can I do it?
Your help will be appreciated
Thanks,
Raaj
how do i change real gdp with one constant price to another constant price
Hi,
Can someone tell me what algorithm is used by interp1 function for linear interpolation, i mean when we give the method as ‘linear’ ?. I need this as i am trying to implement the same functionality if interp1 in java.