Interpolation doesn't reproduce the character of original curve

Using the Matlab function notation for interp1: x, v, xq, and vq {vq = interp1(x, v, xq, 'method')}, I am trying to create a 1-d vector vq as a function of 1-d vector xq to recreate the shape of the curve v(x). I am trying to attach images that show the x & xq on one plot, and v & the resultant vq on another plot.
I have tried various 'methods', and none reproduces the original character of the curve v(x). I don't understand why and what I need to do (other than laborious self-computation) to get there. Appreciate pointers.

3 Comments

I am trying to attach images that show the x & xq on one plot, and v & the resultant vq on another plot.
Why attach images? Why not attach a .mat file with all the variables and Run your code for us.
That way we can not only see the result, but play with the data as well?
EXACTLY. Without your data, we cannot help you that much. A picture of your data might be interesting, but it does not tell us enough. Attach a .mat file to a comment, or to your original question by editting the question.
I will note that since xq is NOT monotonic, then I would expect vq to also not be monotonic.

Sign in to comment.

 Accepted Answer

Perhaps I misunderstood how this works. I expected v to be scaled (compressed as it were in this case) and be reproduced over the new range xq
The vector v is just a list of values. It doesn't have a unique domain x. If the goal is to compress the plot, you just need to redefine v's domain. It doesn't require interpolation,e.g.,
x=linspace(0,2*pi,100);
v=sin(x);
xc=linspace(0,pi,100);
plot(x,v,xc,v,'x'); legend('Original','Compressed')

5 Comments

I can't redefine x. x is what I have. 1 value of v is mapped to each value of x - and thus I regard it a function of x. I need to reproduce the same "mapping" for vq vs. xq - and xq is also already defined. I don't have a closed-form function to use for v vs. x. Anyway, I realized that I can do this:
  • Find scaling factor for xq vs. x
  • For each xq, apply inverse scaling factor and determine nearest v(x) for that x
  • Assign vq = this value of v
  • Perhaps smooth the result with some low-span moving avg. filter or something of this sort if necessary
If you apply my example to your case, all you have to do is,
plot(x,v,xq,v,'x')
No computation is required.
Not sure I understand. Firstly, x and xq don't have the same number of points. So we can't just use v for vq. A new vector has to be generated it seems to me. Instead I do this (sorry about the for loop):
scaling = (max(xq)-min(xq))/(max(x)-min(x));
for k=1:length(xq)
eqx = xq(k,1)/scaling;
[mval, idx] = min(abs(eqx - x));
vq(k,1) = v(idx,1);
end
To be smoothed, but result looks good and I now have a vector vq to work with. Makes sense? Not the most efficient way I'm sure. I don't know why I was using interpolation.
Easier to do,
xq=linspace(min(xq), max(xq), numel(v));
plot(x,v,xq,v,'x')
This may be workable, but here we are coming up with a new xq and keeping v. In my case, I don't think I can change xq. I would need to keep xq and come up with a new v. Anyway, we have a workable solution one way or another.

Sign in to comment.

More Answers (1)

I don't know how you are plotting, but it should look like this:
plot(x,v,xq,vq,'x--')

7 Comments

I was just plotting them separately. Here is a picture with them overlaid - and yes it is insightful. vq seems to follow v accurately, but only over a small range. v is in blue & vq is in red with 'x' marker.
I hesitate to post code (from work), although I may be able to pare-down & attach only the relevant section.
but only over a small range
Yes, but that's the range covered by your xq. So, this is the expected result.
Perhaps I misunderstood how this works. I expected v to be scaled (compressed as it were in this case) and be reproduced over the new range xq. Is there some other relatively simple way to do that?
I am surprised, as I seem to have found this to work when the range of xq > range of x. Is that true?
As I poiinted out in my comment to your question, if xq is not monotonic, then vq will also not be monotonic. But we really need to have your data to offer a good explanation. You do not need to post the code that produced it.
Perhaps I misunderstood how this works. I expected v to be scaled (compressed as it were in this case) and be reproduced over the new range xq.
You did misunderstand. Interpolation is like table lookup. It will evaluate v(x) at one or more xq, treating x as the domain of v. So if xq spans a particular interval a<=xq<=b, interp1 will evaluate v(xq) over that interval.
I would appreciate understanding what it does when the span of xq > the span of x. How does table lookup work here?
interp1 will need to extrapolate outside the range of x. It will do that according to the extrapolation settings that you give to interp1:

Sign in to comment.

Categories

Asked:

AR
on 10 Dec 2021

Commented:

on 10 Dec 2021

Community Treasure Hunt

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

Start Hunting!