Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
interp1 problem node number or distance?

Subject: interp1 problem node number or distance?

From: Nora

Date: 21 Apr, 2011 22:52:05

Message: 1 of 5

Hi,
I would like to ask a question about interp1. I have a series of x,y data which lie on a contour. And I want to find the xi,yi where it is on a defined distance from starting of this contour (that contour forms with [X]=x1,x2,x3,..xn; [Y]=y1,y2,y3...yn.
when I implement
xi=interp1(X,distance) ;
yi=interp1(Y,distance);
 it works with number of nodes (if distance is 2 in this case, it finds the 2nd node on this contour, but I want to find the xi,yi values of 2 m).
Could you help me please on this issue?
Thanks in advance

Subject: interp1 problem node number or distance?

From: John D'Errico

Date: 21 Apr, 2011 23:35:04

Message: 2 of 5

"Nora" wrote in message <ioqcel$9dr$1@fred.mathworks.com>...
> Hi,
> I would like to ask a question about interp1. I have a series of x,y data which lie on a contour. And I want to find the xi,yi where it is on a defined distance from starting of this contour (that contour forms with [X]=x1,x2,x3,..xn; [Y]=y1,y2,y3...yn.
> when I implement
> xi=interp1(X,distance) ;
> yi=interp1(Y,distance);
> it works with number of nodes (if distance is 2 in this case, it finds the 2nd node on this contour, but I want to find the xi,yi values of 2 m).
> Could you help me please on this issue?
> Thanks in advance

You may find my interparc code of utility here, as
well as arclength.

arclength allows you to compute the total arclength
of the curve. Then interparc lets you interpolate a
point at any fractional distance along the curve. You
may use a linear interpolation, or one based on a
spline or pchip interpolant.

http://www.mathworks.com/matlabcentral/fileexchange/27096-interparc
http://www.mathworks.com/matlabcentral/fileexchange/26848-arclength

HTH,
John

Subject: interp1 problem node number or distance?

From: ImageAnalyst

Date: 21 Apr, 2011 23:51:59

Message: 3 of 5

Very cool John. I have the case where I want the user to trace out a
curve and then I'm going to take a series of intensity profiles
perpendicular to the curve, to get the average intensity cross section
along the curve. imfreehand (or whatever I used) gives a contiguous
chain of pixel coordinates. To get the cross sectional line I have to
get the slope (m) and then get (-1/m) and go out a certain distance to
get the endpoints of the line that will be the cross section. The
problem with this is that if you try to get the slope over, say, 3
pixels before and after your point on the curve, then the slope can
oscillate very wildly. For example

1 1 1 1 1
             1
             1
             1
              1 1 1
As it rounds those corners the line will flip vertical/horizontal.
For a general purpose curve, it just acts crazy. So I use polyfit to
get a smoothed version with a bigger number, say 9 points ahead and
behind. This will smooth out the slope fluctuations but every once in
a while when the slope flips, say from -0.1 to 0.1, point one and
point 2 flip to the other side of the curve and now my cross sectional
profile is reversed (point 1 is not always on the left of the cross
section - sometimes it's on the right so the profile is reversed).
Hope you followed all that (I could try to upload a screenshot if
that would help). Do you calculate the slope? Can interparc be used
to help me?
ImageAnalyst

Subject: interp1 problem node number or distance?

From: John D'Errico

Date: 22 Apr, 2011 02:45:21

Message: 4 of 5

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <ffc6f40c-ccbe-4246-ac9e-4aa2a190a420@z31g2000vbs.googlegroups.com>...
> Very cool John. I have the case where I want the user to trace out a
> curve and then I'm going to take a series of intensity profiles
> perpendicular to the curve, to get the average intensity cross section
> along the curve. imfreehand (or whatever I used) gives a contiguous
> chain of pixel coordinates. To get the cross sectional line I have to
> get the slope (m) and then get (-1/m) and go out a certain distance to
> get the endpoints of the line that will be the cross section. The
> problem with this is that if you try to get the slope over, say, 3
> pixels before and after your point on the curve, then the slope can
> oscillate very wildly. For example
>
> 1 1 1 1 1
> 1
> 1
> 1
> 1 1 1
> As it rounds those corners the line will flip vertical/horizontal.
> For a general purpose curve, it just acts crazy. So I use polyfit to
> get a smoothed version with a bigger number, say 9 points ahead and
> behind. This will smooth out the slope fluctuations but every once in
> a while when the slope flips, say from -0.1 to 0.1, point one and
> point 2 flip to the other side of the curve and now my cross sectional
> profile is reversed (point 1 is not always on the left of the cross
> section - sometimes it's on the right so the profile is reversed).
> Hope you followed all that (I could try to upload a screenshot if
> that would help). Do you calculate the slope? Can interparc be used
> to help me?
> ImageAnalyst

The problem with splines is they have a tendency to
oscillate - ringing behavior. This comes about because
of what I call metaphorical baggage. Splines are models
of thin, flexible beams, so they carry along behavior that
is consistent with how a thin flexible beam will behave
under that same circumstance.

Usually one uses a pchip interpolant for these things.
pchip was designed to handle interpolation while
minimizing ringing behavior. While interparc does offer
pchip as the underlying interpolant, it does not return
a derivative. Ok, this might be an idea for enhancement,
but there is an issue. At best, I can only return the
derivatives of the parametric functions (x'(t),y'(t),z'(t),...)
since interparc is designed to work in n dimensions, and
since the curve itself may have singularities.

Given a parametric interpolant as interparc uses, it
does computes the derivatives dx/dt and dy/dt. In fact,
this is what you need to compute that perpendicular
vector to the curve.

So, as I was writing this response, I modified interparc
to return those derivatives when a second output
argument is provided. I've just posted the modified
code, to appear tomorrow morning.

John

Subject: interp1 problem node number or distance?

From: ImageAnalyst

Date: 22 Apr, 2011 02:55:04

Message: 5 of 5

Thanks - I'll see if I can try it out this weekend.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us