# Angle of complex numbers: how to get continuous plot?

**URL:** <https://discourse.julialang.org/t/angle-of-complex-numbers-how-to-get-continuous-plot/108624>\
**Category:** Modelling & Simulations\
**Tags:** dsp\
**Created:** [January 10, 2024, 2:44pm UTC](https://discourse.julialang.org/t/angle-of-complex-numbers-how-to-get-continuous-plot/108624 "2024-01-10T14:44:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [January 10, 2024, 2:44pm UTC](https://discourse.julialang.org/t/angle-of-complex-numbers-how-to-get-continuous-plot/108624/1 "2024-01-10T14:44:20Z")

</div>

I’m trying to plot the phase of a complex transfer function (as in control engineering) using the `angle` function.

So essentially, if I have a transfer function g(s), I compute the `angle` (with `rad2deg` conversion) of g(j\omega) for a number of different frequencies \omega (j=\sqrt{-1}). Here is what I get:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/c/a/ca13b6a68cfff26f38baa7fb55b1e4eaca3eb84f.png)

This looks ugly, and I want to avoid the jumps in the phase (angle) when the phase passes through -180^\circ. I.e., instead of jumping to +180^\circ, I want it to continue to a negative value past -180^\circ.

**Question** : Is there a simple/efficient/smart way to do this?

**Note** : This is a somewhat complex transfer function from a system of PDEs, and does not fit into the ControlSystems framework. [Unless `ControlSystems` allows me to pass on frequency and magnitude and phase instead of the _system_, and then does it form me.]

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [January 10, 2024, 3:13pm UTC](https://discourse.julialang.org/t/angle-of-complex-numbers-how-to-get-continuous-plot/108624/2 "2024-01-10T15:13:55Z")

</div>

> [@BLI](#):
>
> This looks ugly, and I want to avoid the jumps in the phase (angle) when the phase passes through -180^\circ−180∘-180^\circ. I.e., instead of jumping to +180^\circ+180∘+180^\circ, I want it to continue to a negative value past -180^\circ−180∘-180^\circ.

I am afraid you need to write the code that does this yourself… I did that in the past, should only be 5 or 10 lines of code, but I don’t have the code available right now…

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [January 10, 2024, 3:20pm UTC](https://discourse.julialang.org/t/angle-of-complex-numbers-how-to-get-continuous-plot/108624/3 "2024-01-10T15:20:17Z")

</div>

Would the [`unwrap()` function](https://docs.juliadsp.org/stable/util/#DSP.Unwrap.unwrap) from DSP.jl help?

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [January 10, 2024, 3:34pm UTC](https://discourse.julialang.org/t/angle-of-complex-numbers-how-to-get-continuous-plot/108624/4 "2024-01-10T15:34:33Z")

</div>

Yes, `unwrap` seems to do the trick:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/9/b/9b325eb7d4b0c858fa892bee6481a266ddd840e8.png)

The only “problem” (unavoidable?) is that I had to compute the phase in 10^5 datapoints… If I use fewer, say, 10^4, I get the following…

 ![image](https://global.discourse-cdn.com/julialang/original/3X/6/0/60607e4f986fc4174fccdccbfde9855ed2308c1e.png)

…which kind of makes sense: it is almost like an “aliasing” effect where the phase falls more than 360^\circ for each datapoint, I guess.

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [January 10, 2024, 3:56pm UTC](https://discourse.julialang.org/t/angle-of-complex-numbers-how-to-get-continuous-plot/108624/5 "2024-01-10T15:56:10Z")

</div>

Of course, if I “cheat” and limit the `ylims` range, I can get away with 10^4 data points…

 ![image](https://global.discourse-cdn.com/julialang/original/3X/6/b/6b5c06b8e04d1c2c77d5d0c00df7442a958e524f.png)

Nice plots for transfer functions from tube-side, shell-side inlets to tube-side, shell-side outlets in a co-current heat exchanger (with fictitious numbers).

In reality, there may not be these “comb filter” effects if I include heat diffusion and heat capacity of heat exchanger walls…

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [January 10, 2024, 5:08pm UTC](https://discourse.julialang.org/t/angle-of-complex-numbers-how-to-get-continuous-plot/108624/6 "2024-01-10T17:08:14Z")

</div>

Maybe a polar plot for the angle, with radius as 1/omega will look nice

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [January 10, 2024, 8:13pm UTC](https://discourse.julialang.org/t/angle-of-complex-numbers-how-to-get-continuous-plot/108624/7 "2024-01-10T20:13:18Z")

</div>

Not quite clear to me what you mean. I compute some complex numbers as a function of omega. Then I compute the magnitude and phase angle of the complex numbers, and plot these as a function of omega (Bode plot).

Another common plot is the Nichols plot, where one plots magnitude as a function of phase angle with omega as parameter.

A third common plot is a Nyquist plot, where the imaginary value is plotted as a function of the real part, again with omega as parameter.

Do you prefer to a Nichols plot, a Nyquist plot, or something else?

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [January 10, 2024, 8:18pm UTC](https://discourse.julialang.org/t/angle-of-complex-numbers-how-to-get-continuous-plot/108624/8 "2024-01-10T20:18:03Z")

</div>

What I meant was a polar plot with:

```julia
r = 1/ω or 1/log(ω)
θ(r) = angle(ω)

```

No magnitude involved (which is probably what most are interested in, but appears in the other plot).  
Don’t think this plot has a name yet (probably for good reason)
