I can extract the phase of a complex number in Matlab using the atan()
or atan2()
functions.
atan()
returns in interval limited to [-pi/2,pi/2]
, and atan2()
returns in interval limited to [-pi,pi]
.
I want to see if I can unwrap the extracted phase in each case using the unwrap()
function, but unwrap
is only effective on phase extracted via atan2()
.
R = 1; % Magnitude
theta = linspace(0,6*pi,100); % (radians) Angle array
theta_atan = zeros(1,length(theta)); % Prellocate for calculation
theta_atan2 = zeros(1,length(theta)); % Prellocate for calculation
X = zeros(1,length(theta)); %Prelloc.
Y = zeros(1,length(theta)); %Prelloc.
for i = 1:length(theta)
X(i) = R*cos(theta(i)); % Real part
Y(i) = R*sin(theta(i)); % Imaginary part
theta_atan(i) = atan(Y(i)/X(i));
theta_atan2(i) = atan2(Y(i),X(i));
end
I plot the unwrapped extracted phase using each method:
figure(666)
plot(theta,unwrap(theta_atan));
hold on
plot(theta,unwrap(theta_atan2));
legend('theta atan','theta atan2')
xlabel('input phase')
ylabel('extracted phase')
However, as you can see, unwrapping is only effective on the atan2()
case.
Even if I use unwrap(theta_atan, pi/2)
(in this case unwrapping is based on increments of pi/2 instead of the default, pi), I fail to properly unwrap the atan()
phase.
The second argument to unwrap
is not the period of the input data, but the tolerance. The function always unwraps data assuming a 2π interval. That is, it wants to see that x(i)-x(i+1)
is larger than the tolerance before unwrapping, and smaller after unwrapping. In the case of a tolerance of pi/2
, if, for example, x(i)=0
and x(i+1)=3
, the jump is larger than the tolerance, but adding or subtracting 2*pi
to x(i+1)
does not improve things.
One work-around would be to multiply the input by 2, and divide by 2 after unwrapping:
unwrap(theta_atan * 2) / 2
However, it is always best to use atan2
to obtain an angle.