Problem using interp1 with a sharp change in the data

Hello everyone.
I have a vector of data and I want to interpolate it using interp1. The problem is that there is a sharp increase in the values of the vector and the resulting graph does not reproduce the data propperly.
Is there a way to make the interpolated data reproduce the sharp jump propperly?
I will attach a figure where my issue is displayed.
Any answer is appreciated.
Thank you very much.
Jaime.

5 Comments

Hi, you can check out the documentation of the interp1 function https://www.mathworks.com/help/matlab/ref/interp1.html
If you choose to use the approach vq = interp1(x,v,xq,method) , you can define the method that best refects the type of interpolation you are interesed in.
Also xq is th enumber of query points, so the larger the number, the more the points that will be used to interpolate (you can think of it as using more points for averaging) and hence you might get a less 'sharp' interpolation.
Eg,
%%taken from documentation
x = 0:pi/4:2*pi;
v = sin(x);
xq = 0:pi/16:2*pi;
%Interpolate the function at the query points and plot the result.
figure
%making the interpolation smoother
xq=xq*10;
vq2 = interp1(x,v,xq,'spline');
plot(x,v,'o',xq,vq2,':.');
xlim([0 2*pi]);
title('Spline Interpolation');
Thanks for the answer, but I think that I might have expresed myself worng. In the attached figure of the original message you can see two lines, the blue (interpolated) and the orange (the original). I want to make the blue line fit the orange better. I have tested all the integrations avaiable inthe documentation between 'linear' and 'spline'. I have also tested increasing (and decreasing) the number of points as sugested but I this has worsened the problem. See atached figure to this coment.
Regards.
Jaime.
It's difficult to know what's wrong when we don't have the code you're using nor the input data, so please attach both (not screenshots which we can't use for testing/diagnostics).
Thanks for clarification. What will also help is a clear description of what you want the output to look like.
The code I use is simply
Tinterp=interp1(time,T,t,'spline');
being T a vector which values can be seen in the attached T.txt; the same thing happens with time.
What I do next is plot together T-time and Tinterp and t as
figure
plot(t, Tinterp)
hold on
plot(time, T)
legend('interpolated','original')
title('T Spline')
I hope this clarifies what I am attempting to do.

Sign in to comment.

Answers (1)

Try using ischange to detect these sharp changes in your data and interpolating using only data in the same "piece" of your curve (where pieces are delimited by the ends of your data sets and the change points) as the points for which you want the interpolated value.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!