3
votes

I'm a bit confused about the angle() function in Matlab, in particular when applied to an array of real numbers.

The angle() function should give me the phase of a complex number. Example: y = a + bi, ==> phase = arctan(b/a). Indeed, the following works:

for t=1:1000
    comp(t) = exp(1i*(t/10));
end

phase_good_comp1 = unwrap(angle(comp)); %this gives me the right answer
b = imag(comp);
a = real(comp);
phase_good_comp2 = atan(b./a); %this gives me the right answer too, but 
wrapped (not sure if there is a way to unwrap this, but unwrap() does not 
work)

figure(1)
plot(phase_good_comp1)
hold on
plot(phase_good_comp2,'--r')
legend('good phase1', 'good phase2')
title('complex number')

Here's the plot for the complex numbers --

enter image description here

Note that I can use either the angle() function, or the explicit definition of phase, as I have shown above. Both yield good results (I can't unwrap the latter, but that's not my issue).

Now if I apply the same logic to an array of real numbers, I should get a constant phase everywhere, since no imaginary part exists, so arctan(b/a) = arctan(0) = 0. This works if I use the explicit definition of phase, but I get a weird result if I use angle():

for t=1:1000
    ree(t) = cos((t/10));
end

phase_bad_re = unwrap(angle(ree)); %this gives me an unreasonable (?) answer
b = imag(ree);
a = real(ree);
phase_good_re = atan(b./a); %this gives me the right answer

figure(1)
plot(phase_bad_re)
hold on
plot(phase_good_re,'--r')
legend('bad phase', 'good phase')
title('real number')

Here's the plot for the real numbers --

enter image description here

Why the oscillation when I use angle()???

2
Are you sure you don't need the other conventional atan definition, implemented as atan2?bright-star

2 Answers

4
votes

The Matlab documentation tells you how to compute this:

The angle function can be expressed as angle(z) = imag(log(z)) = atan2(imag(z),real(z)).

https://www.mathworks.com/help/matlab/ref/angle.html

Note that they define it with atan2 instead of atan.

Now your data is in the range of cosine, which includes both positive and negative numbers. The angle on the positive numbers should be 0 and the angle on the negative numbers should be an odd-integer multiple of pi in general. Using the specific definition that they've chosen to get a unique answer, it is pi. That's what you got. (Actually, for the positive numbers, any even-integer multiple of pi will do, but 0 is the "natural" choice and the one that you get from atan2.)

If you're not clear why the negative numbers don't have angle = 0, plot it out in the complex plane and keep in mind that the radial part of the complex number is positive by definition. That is z = r * exp(i*theta) for positive r and theta given by this angle you're computing.

1
votes

Since sign of cosine function is periodically changed, angle() is also oscillated.

Please, try this.

a=angle(1);
b=angle(-1);

Phase of 1+i*0 is 0, while phase of -1+i*0 is 3.14.

But, in case of atan, b/a is always 0, so that the result of atan() is all 0.