Linear Interpolation on missing data

I have a time series and i tried using linear interpolation on that data. But linear interpolation seems to change the original data too (not significantly). Is there a way I can keep the values for original data same. I think i need to use a loop but not sure how to use it in linear interpolation.

1 Comment

Please don't add a new answer every time you wish to make a comment. Use the comments. That is why links are provided for comments. USE THEM.

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 26 Sep 2015
We need to see your code. The interp1 function has not (at least in my experience) changed any of the original data. Don’t use a loop if you can use interp1 or another appropriate interpolation function.

4 Comments

Interp1 will NEVER change the original data, if used properly.
However, it is possible that the predicted values at the original data points may be off by something on the order of eps. This is due to floating point arithmetic, and not by design or intention.
Mathematically, interp1 will be always exact at the data points. At the same time, as I have often said, MATLAB (and any floating point computational tool) is NOT truly mathematics. It just looks that way. Close, but not truly so.
Moved from an answer by ammara into a comment.
"This the code I am using
dornq= interp1(Qdorn(:,1),Qdorn(:,2),interpQ); A friend told me i need piecewise interpolation instead of linear regression to find the missing values between two given points. I have a time series with a lot of data.So Phosphorus concentration is missing for some days and I want to interpolate phosphorus concentration for missing dates and want to generate daily time series.
He wrote a code for piecewise interpolation
m=diff(y)./diff(x);
values=cell(length(x)-1);
for i=1:length(x)-1
plot(x(i:i+1),y(i:i+1));
hold on;
temp=y(i)+m(i)*(0:(x(i+1)-x(i)));
%values(i)=temp;
end
I can run the code. It is giving me a plot. I want the output x values for x. I am new to matlab and don't know how to do it."
That code looks as though it should work. If you end up extrapolating beyond your actual data (that will produce NaN output from interp1), you have to change your interp1 call to:
dornq = interp1(Qdorn(:,1),Qdorn(:,2),interpQ, 'linear', 'extrap');
This will work even if you don’t extrapolate.
How did you determine the x values stored in interpQ? If you find that the y values, dornq, at the same x value are different, then either your interpQ values don't match up with the Qdorn(:,1) values, or you're not looking at the right index.
The first case would be like your original x values are 1,2,3,5 and you're specifying interpolated x locations of 1.5, 2.5, 3.5, 4.5, and 5.5. Of course the y value at 1 is not going to be the same as the y value at 1.5
In the second case, if your original x values are 1,2,3,5, and your interpolated x values are 1,2,3,4,5, then your original y(4) will not match your new interpolated y(4) because the original y(4) was the value for x=5 while the new y(4) is the value for x=4.
Make sense? Perhaps is that what is happening?

Sign in to comment.

Categories

Find more on Interpolation in Help Center and File Exchange

Asked:

on 26 Sep 2015

Commented:

on 26 Sep 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!