Matlab - Interpolating Data using Interp1 and Spline
07 Jan 2008 Quan Quach 23 comments 9,428 views
Introduction
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
-
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
-
You should see the following graph appear

-
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
-
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.

-
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. -
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
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.