Question

I've run into a little phase wrapping problem in matlab.

I apply atan(x) to determine the inverse tangent of another vector, element-wise. I am expecting results in the range between 0 and pi. atan(x) gives me results between -pi/2 and pi/2, as is standard.

Is there a smart way of shifting the output range without having to go through all elements either in the input or the output to move them to the desired range by if statements?

Regards,

RTT

Was it helpful?

Solution

You need to transform the interval (-pi/2, 0) into (pi/2, pi). For that purpose you can use mod:

y = mod(atan(x), pi);

Another possibility: use atan2. This lets you specify the sine and the cosine of the desired angle as separate inputs, and thus you can control the output interval. To obtain an output in (0, pi), the sine (first input of atan2) should be positive. Therefore you can use abs(x) as first input and move the sign of x to the second input:

y = atan2(abs(x), sign(x));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top