DSPRelated.com
Forums

Simple Problem

Started by I. R. Khan December 25, 2003
Hi all,

The following problem is not exactly a DSP problem but I am sure some one
here can give a solution.

I have two vectors both starting from the origin, making a certain angle
with x-axis. I want to calculate their average angle.

In C++ atan2(y,x) gives angle in range of -Pi to Pi. Suppose two vectors
have angles Pi+0.1 and Pi-0.1. These will be represented by -Pi+0.1 and
Pi-0.1 in C++, and average will become zero instead of true value of Pi. How
can I handle this problem?

If I adjust range of angle from 0 to Pi, then same problem happens for
vectors at angles 0.1 and 2Pi-0.1.

Ishtiaq.

"I. R. Khan" <ir_khan@hotmail.com> wrote in message news:<bsg9ue$cac9d$2@ID-198607.news.uni-berlin.de>...
> Hi all, > > The following problem is not exactly a DSP problem but I am sure some one > here can give a solution. > > I have two vectors both starting from the origin, making a certain angle > with x-axis. I want to calculate their average angle. > > In C++ atan2(y,x) gives angle in range of -Pi to Pi. Suppose two vectors > have angles Pi+0.1 and Pi-0.1. These will be represented by -Pi+0.1 and > Pi-0.1 in C++, and average will become zero instead of true value of Pi. How > can I handle this problem? > > If I adjust range of angle from 0 to Pi, then same problem happens for > vectors at angles 0.1 and 2Pi-0.1.
Part of the problem is that your program does what it is told: It takes that angle > pi and maps it into the permissable interval [-pi,pi]. The angle Phi is ambiguous, Phi = phi + n2pi for some integer n. You could, of course, deal with total phase Phi (as oppsed to the principal value phi above), but that opens a complete new set of problems. Rune
Rune, thanks for your comments. You are right, the program is doing what it
is asked to do. But how to tell it what I wnat?

I have 2 (or more) vectors starting from the same origin. I want to find the
average of their angles with the x-axis. If angles are "Pi+a" and "Pi-a",
average should be "Pi". If angles are "a" and "2Pi-a", the average should be
0.

Last paragraph of my initial post should be read as:
If I adjust range of angle from 0 to 2Pi, then same problem happens for
vectors at angles 0.1 and 2Pi-0.1.

Ishtiaq.




"Rune Allnor" <allnor@tele.ntnu.no> wrote in message
news:f56893ae.0312252206.1fa6d350@posting.google.com...
> "I. R. Khan" <ir_khan@hotmail.com> wrote in message
news:<bsg9ue$cac9d$2@ID-198607.news.uni-berlin.de>...
> > Hi all, > > > > The following problem is not exactly a DSP problem but I am sure some
one
> > here can give a solution. > > > > I have two vectors both starting from the origin, making a certain angle > > with x-axis. I want to calculate their average angle. > > > > In C++ atan2(y,x) gives angle in range of -Pi to Pi. Suppose two vectors > > have angles Pi+0.1 and Pi-0.1. These will be represented by -Pi+0.1 and > > Pi-0.1 in C++, and average will become zero instead of true value of Pi.
How
> > can I handle this problem? > > > > If I adjust range of angle from 0 to Pi, then same problem happens for > > vectors at angles 0.1 and 2Pi-0.1. > > Part of the problem is that your program does what it is told: It takes > that angle > pi and maps it into the permissable interval [-pi,pi]. > The angle Phi is ambiguous, Phi = phi + n2pi for some integer n. > > You could, of course, deal with total phase Phi (as oppsed to the > principal value phi above), but that opens a complete new set of > problems. > > Rune
"I. R. Khan" <ir_khan@hotmail.com> wrote in message
news:bsg9ue$cac9d$2@ID-198607.news.uni-berlin.de...
> Hi all, > > The following problem is not exactly a DSP problem but I am sure some one > here can give a solution. > > I have two vectors both starting from the origin, making a certain angle > with x-axis. I want to calculate their average angle. > > In C++ atan2(y,x) gives angle in range of -Pi to Pi. Suppose two vectors > have angles Pi+0.1 and Pi-0.1. These will be represented by -Pi+0.1 and > Pi-0.1 in C++, and average will become zero instead of true value of Pi.
How
> can I handle this problem? > > If I adjust range of angle from 0 to Pi, then same problem happens for > vectors at angles 0.1 and 2Pi-0.1. > > Ishtiaq.
This sounds like the phase unwrapping problem. It probably depends on your application and what you need. Why is the "true value" Pi instead of zero? Because of the real component? There's a clue. Fred
"I. R. Khan" <ir_khan@hotmail.com> wrote in message news:<bsgkmt$cserp$1@ID-198607.news.uni-berlin.de>...
> Rune, thanks for your comments. You are right, the program is doing what it > is asked to do. But how to tell it what I wnat? > > I have 2 (or more) vectors starting from the same origin. I want to find the > average of their angles with the x-axis. If angles are "Pi+a" and "Pi-a", > average should be "Pi". If angles are "a" and "2Pi-a", the average should be > 0.
As Fred Marshall pointed out, it's not obvious from your posts why the phase average should be like you want it to. If you can explain why the average of pi-a and pi+a should be pi and not 0, you may be able to see how your program should be specified. I can't see why the answer 0 is wrong. Rune
Thanks again Rune. I should have explained it a bit more.
If a < Pi/2, then true answer is Pi otherwise 0.
You might have understood now what I want. 

Oh! explaining the problem this way, I have just found how to solve
it. Thanks to clues from you and Fred Marshall.

Ishtiaq.
 
> As Fred Marshall pointed out, it's not obvious from your posts why > the phase average should be like you want it to. If you can explain > why the average of pi-a and pi+a should be pi and not 0, you may be > able to see how your program should be specified. > > I can't see why the answer 0 is wrong. > > Rune
Hi,
Am i missing something?, why not just add pi to the result of the atan and
then you have your angle in 0 - 2pi as you want? then do with them as you
will.

or alternatively, as you are giving the atan routine the x and y values, why
not use the +/- ve nature of them to determine which quadrant you are in?

Greg



"I. R. Khan" <ir_khan@hotmail.com> wrote in message
news:bsg9ue$cac9d$2@ID-198607.news.uni-berlin.de...
> Hi all, > > The following problem is not exactly a DSP problem but I am sure some one > here can give a solution. > > I have two vectors both starting from the origin, making a certain angle > with x-axis. I want to calculate their average angle. > > In C++ atan2(y,x) gives angle in range of -Pi to Pi. Suppose two vectors > have angles Pi+0.1 and Pi-0.1. These will be represented by -Pi+0.1 and > Pi-0.1 in C++, and average will become zero instead of true value of Pi.
How
> can I handle this problem? > > If I adjust range of angle from 0 to Pi, then same problem happens for > vectors at angles 0.1 and 2Pi-0.1. > > Ishtiaq. >

----- Original Message -----
From: "I. R. Khan" <ir_khan@hotmail.com>
Newsgroups: comp.dsp
Sent: Thursday, December 25, 2003 22:29
Subject: Simple Problem


> Hi all, > > The following problem is not exactly a DSP problem but I am sure some one > here can give a solution. > > I have two vectors both starting from the origin, making a certain angle > with x-axis. I want to calculate their average angle. > > In C++ atan2(y,x) gives angle in range of -Pi to Pi. Suppose two vectors > have angles Pi+0.1 and Pi-0.1. These will be represented by -Pi+0.1 and > Pi-0.1 in C++, and average will become zero instead of true value of Pi.
How
> can I handle this problem? > > If I adjust range of angle from 0 to Pi, then same problem happens for > vectors at angles 0.1 and 2Pi-0.1. > > Ishtiaq. >
I thought I had replied to the group, but apparently I just sent it to the OP. ===== It's fairly simple. Convert your vectors to rectangular form and average them there. If you are just trying to calculate average angles, assume that the vectors are of length 1. Here's a quickexample done in Octave:
>> x1 = cos(2)
x1 = -0.41615
>> y1 = sin(2)
y1 = 0.90930
>> x2=cos(-2)
x2 = -0.41615
>> y2 = sin(-2)
y2 = -0.90930
>> x = (x1+x2)/2
x = -0.41615
>> y = (y1+y2)/2
y = 0
>> atan2(y,x)
ans = 3.1416 Note that you don't really need the divide by 2 when calculating x and y. Best wishes, --Phil Martel =========== After a bit more thought, you could avoid the trig like this: Let the angles you want to average be a1 and a2, each in the range [-pi,pi]. Let A = (a1+a2)/2 Let S = a1- a2 if abs(S) < pi Average = A else if A >= 0 Average = -pi + A else Average = pi = A Best wishes, --Phil Martel
> Hi all, > > The following problem is not exactly a DSP problem but I am sure some one > here can give a solution. > > I have two vectors both starting from the origin, making a certain angle > with x-axis. I want to calculate their average angle. > > In C++ atan2(y,x) gives angle in range of -Pi to Pi. Suppose two vectors > have angles Pi+0.1 and Pi-0.1. These will be represented by -Pi+0.1 and > Pi-0.1 in C++, and average will become zero instead of true value of Pi.
How
> can I handle this problem? > > If I adjust range of angle from 0 to Pi, then same problem happens for > vectors at angles 0.1 and 2Pi-0.1. > > Ishtiaq. >
Thanks Phil Martel for the clean solution.
Ishtiaq.

"Phil Martel" <p.martel@comcast.net> wrote in message
news:UvnHb.217163$_M.952220@attbi_s54...
> > > ----- Original Message ----- > From: "I. R. Khan" <ir_khan@hotmail.com> > Newsgroups: comp.dsp > Sent: Thursday, December 25, 2003 22:29 > Subject: Simple Problem > > > > Hi all, > > > > The following problem is not exactly a DSP problem but I am sure some
one
> > here can give a solution. > > > > I have two vectors both starting from the origin, making a certain angle > > with x-axis. I want to calculate their average angle. > > > > In C++ atan2(y,x) gives angle in range of -Pi to Pi. Suppose two vectors > > have angles Pi+0.1 and Pi-0.1. These will be represented by -Pi+0.1 and > > Pi-0.1 in C++, and average will become zero instead of true value of Pi. > How > > can I handle this problem? > > > > If I adjust range of angle from 0 to Pi, then same problem happens for > > vectors at angles 0.1 and 2Pi-0.1. > > > > Ishtiaq. > > > > I thought I had replied to the group, but apparently I just sent it to the > OP. > ===== > It's fairly simple. Convert your vectors to rectangular form and average > them there. If you are just trying to calculate average angles, assume
that
> the vectors are of length 1. Here's a quickexample done in Octave: > >> x1 = cos(2) > x1 = -0.41615 > >> y1 = sin(2) > y1 = 0.90930 > >> x2=cos(-2) > x2 = -0.41615 > >> y2 = sin(-2) > y2 = -0.90930 > >> x = (x1+x2)/2 > x = -0.41615 > >> y = (y1+y2)/2 > y = 0 > >> atan2(y,x) > ans = 3.1416 > > Note that you don't really need the divide by 2 when calculating x and y. > > Best wishes, > --Phil Martel > =========== > > After a bit more thought, you could avoid the trig like this: > Let the angles you want to average be a1 and a2, each in the range
[-pi,pi].
> Let A = (a1+a2)/2 > Let S = a1- a2 > > if abs(S) < pi > Average = A > else if A >= 0 > Average = -pi + A > else > Average = pi = A > > Best wishes, > --Phil Martel > > > Hi all, > > > > The following problem is not exactly a DSP problem but I am sure some
one
> > here can give a solution. > > > > I have two vectors both starting from the origin, making a certain angle > > with x-axis. I want to calculate their average angle. > > > > In C++ atan2(y,x) gives angle in range of -Pi to Pi. Suppose two vectors > > have angles Pi+0.1 and Pi-0.1. These will be represented by -Pi+0.1 and > > Pi-0.1 in C++, and average will become zero instead of true value of Pi. > How > > can I handle this problem? > > > > If I adjust range of angle from 0 to Pi, then same problem happens for > > vectors at angles 0.1 and 2Pi-0.1. > > > > Ishtiaq. > > > >
Thanks Greg for replying. Yes you missed something, surely due to my poor
English. For 0 - 2Pi range, same problem occurs around 0 angle. Try taking
average of 0.1 and 2Pi-0.1. Anyways Phil Martel already gave a solution, too
simple.

"gcd" <gcdmelbSPAM@austarmetro.com.au> wrote in message
news:3fed7742@news.comindico.com.au...
> Hi, > Am i missing something?, why not just add pi to the result of the atan and > then you have your angle in 0 - 2pi as you want? then do with them as you > will. > > or alternatively, as you are giving the atan routine the x and y values,
why
> not use the +/- ve nature of them to determine which quadrant you are in? > > Greg > > > > "I. R. Khan" <ir_khan@hotmail.com> wrote in message > news:bsg9ue$cac9d$2@ID-198607.news.uni-berlin.de... > > Hi all, > > > > The following problem is not exactly a DSP problem but I am sure some
one
> > here can give a solution. > > > > I have two vectors both starting from the origin, making a certain angle > > with x-axis. I want to calculate their average angle. > > > > In C++ atan2(y,x) gives angle in range of -Pi to Pi. Suppose two vectors > > have angles Pi+0.1 and Pi-0.1. These will be represented by -Pi+0.1 and > > Pi-0.1 in C++, and average will become zero instead of true value of Pi. > How > > can I handle this problem? > > > > If I adjust range of angle from 0 to Pi, then same problem happens for > > vectors at angles 0.1 and 2Pi-0.1. > > > > Ishtiaq. > > > >