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:
Fit a linear function of two angles

Subject: Fit a linear function of two angles

From: Benjamin Kraus

Date: 30 Apr, 2012 02:34:08

Message: 1 of 6

I've got two lists of angles (in radians, ranging from -pi to pi), that have a fixed offset from each other (plus a fair bit of noise).

angle1 = angle2+offset

I'm trying to calculate the offset. The problem is that both angles run from -pi to pi, so it isn't as simple as calling 'polyfit(angle1,angle2,1)', because I need to take into account that the angles wrap around. I know this is a simple problem, with a relatively simple solution, but it is eluding me at the moment.

Does anybody have any suggestions?

- Ben

Subject: Fit a linear function of two angles

From: Roger Stafford

Date: 30 Apr, 2012 04:38:08

Message: 2 of 6

"Benjamin Kraus" <bkraus@bu.edu> wrote in message <jnktmv$663$1@newscl01ah.mathworks.com>...
> I've got two lists of angles (in radians, ranging from -pi to pi), that have a fixed offset from each other (plus a fair bit of noise).
>
> angle1 = angle2+offset
>
> I'm trying to calculate the offset. The problem is that both angles run from -pi to pi, so it isn't as simple as calling 'polyfit(angle1,angle2,1)', because I need to take into account that the angles wrap around. I know this is a simple problem, with a relatively simple solution, but it is eluding me at the moment.
>
> Does anybody have any suggestions?
>
> - Ben
- - - - - - - -
  Try using 'unwrap' on each of the lists.

Roger Stafford

Subject: Fit a linear function of two angles

From: Matt J

Date: 30 Apr, 2012 15:15:07

Message: 3 of 6

"Benjamin Kraus" <bkraus@bu.edu> wrote in message <jnktmv$663$1@newscl01ah.mathworks.com>...
> I've got two lists of angles (in radians, ranging from -pi to pi), that have a fixed offset from each other (plus a fair bit of noise).
>
> angle1 = angle2+offset
>
=========

Another idea. Transform this to

 A=sin(offset)=sin(angle1-angle2)
 B=cos(offset)=cos(angle1-angle2)

where A and B are unknowns constrained so that norm([A,B])=1 and
sin(angle1-angle2) and cos(angle1-angle2) are transformations of the data.
You can least squares solve this problem using

http://www.mathworks.com/matlabcentral/fileexchange/27596-least-square-with-2-norm-constraint

and then extract the desired offset using
offset=atan2(B,A).

Subject: Fit a linear function of two angles

From: Matt J

Date: 30 Apr, 2012 15:29:07

Message: 4 of 6

"Matt J" wrote in message <jnma9r$812$1@newscl01ah.mathworks.com>...
> "Benjamin Kraus" <bkraus@bu.edu> wrote in message <jnktmv$663$1@newscl01ah.mathworks.com>...
> > I've got two lists of angles (in radians, ranging from -pi to pi), that have a fixed offset from each other (plus a fair bit of noise).
> >
> > angle1 = angle2+offset
> >
> =========
>
> Another idea. Transform this to
>
> A=sin(offset)=sin(angle1-angle2)
> B=cos(offset)=cos(angle1-angle2)
>
> where A and B are unknowns constrained so that norm([A,B])=1 and
> sin(angle1-angle2) and cos(angle1-angle2) are transformations of the data.
> You can least squares solve this problem using
>
> http://www.mathworks.com/matlabcentral/fileexchange/27596-least-square-with-2-norm-constraint
>
> and then extract the desired offset using
> offset=atan2(B,A).

Or maybe atan2(A,B)

Subject: Fit a linear function of two angles

From: Matt J

Date: 30 Apr, 2012 16:01:07

Message: 5 of 6

"Roger Stafford" wrote in message <jnl4vg$430$1@newscl01ah.mathworks.com>...
>
> Try using 'unwrap' on each of the lists.
=====

Or maybe unwrap their difference?

offset=mean(unwrap(angle1-angle2));

Subject: Fit a linear function of two angles

From: Star Strider

Date: 3 May, 2012 14:46:07

Message: 6 of 6

"Benjamin Kraus" <bkraus@bu.edu> wrote in message <jnktmv$663$1@newscl01ah.mathworks.com>...
> I've got two lists of angles (in radians, ranging from -pi to pi), that have a fixed offset from each other (plus a fair bit of noise).
>
> angle1 = angle2+offset
>
> I'm trying to calculate the offset. The problem is that both angles run from -pi to pi, so it isn't as simple as calling 'polyfit(angle1,angle2,1)', because I need to take into account that the angles wrap around. I know this is a simple problem, with a relatively simple solution, but it is eluding me at the moment.
>
> Does anybody have any suggestions?
>
> - Ben


I'm not certain what you mean by the angles 'wrapping around', since if you have the angles themselves rather than trigonometric functions of the angles as your original data, you shouldn't get any 'aliasing'. Maybe I'm missing something. If you derived them from (x,y) data, did you use 'atan2'?

With respect to calculating the offset, another option is to do a linear regression with the usual model

y = m*x + b

however constraining m = 1 by definition, changing the model to

y = x + b

then using 'nlinfit'. Here's my simulation:

        % Generate Data
        AnglA = 2*pi * rand(10,1) - pi; % Angle defined over [-pi, pi]
        AnglB = AnglA + pi*0.1 + (0.5-rand(10,1))*1; % 'Offset' here = pi*0.1

        % Sort if desired, 'unwrap' if necessary; 'AngIx' generates references to AnglB
        [Ang, AngIx] = sort(AnglA, 'ascend');

        % Define model and fit to data
        y = inline('x + b', 'b', 'x'); % Implicit function would also work
        Ofst = nlinfit(AnglA, AnglB, y, 0.1) % Estimate offset
        % 'Ofst' is y-intercept, x-intercept is -Ofst in this model

If you need statistics on the parameters and the fit, 'nlinfit' also gives you the option of calculating them. If you want to constrain the parameter estimates over a narrow range rather than defining them specifically in the model, I suggest 'lsqcurvefit'.

This approach also works for other constrained simple models such as forcing an intercept at the origin (y = m*x), a parabola symmetric about the y-axis (y = m*x^2 + b) and others.

Tags for 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