Plotting in Python¶

AWe distinguish real and complex signals. A real valued signal x is a mapping from time $t \in \mathbb{R} $ to a scalar value x(t). We call x a signal and also denote the signal as x(t).

A complex valued signal $x$ is a mapping from $t \in \mathbb{C}$. We can characterize signals by looking at the time domain. Traditionally time is represented as a real value running from $t=-\infty$ to $t=\infty$. Such signals are called continuous time signals.

When using computers we start to represent signal with a discrete and countable stream of numbers: we call these numbers signal samples. At every time instance $t = n\Delta t$ the signal value $x(n\Delta t)$ is stored in memory. All signal valuess between the sample times are simply ignored. These signals are called discrete time signals.

Plotting real values signals¶

Consider a CT signal $x(t)$. A real valued CT signal is plotted using the standard ways to plot a function $x: \mathbb{R} \rightarrow \mathbb{R}$. Consider the sinusoidal signal $x(t)=325 \sin(2\pi 50 t)$ for a short interval of time we can plot this as:

                            t              =              np              .              linspace              (              -              0.02              ,              0.05              ,              1000              )              plt              .              plot              (              t              ,              325              *              np              .              sin              (              2              *              np              .              pi              *              50              *              t              ));              plt              .              xlabel              (              't'              );              plt              .              ylabel              (              'x(t)'              );              plt              .              title              (              r              'Plot of CT signal \$x(t)=325 \sin(2\pi 50 t)\$'              );              plt              .              xlim              ([              -              0.02              ,              0.05              ]);              #@savefig sineplot.png              plt              .              show              ()            

(Source code, png, hires.png, pdf)

_images/plotting-1.png

Evidently the above plot is a lie. You cannot plot a CT signal on a digital device. In Computer Graphics class you have or will learn that a function (signal) is plotted as a chain of straight lines (and even the straight lines are discretized as a set of dots). But it sure looks like a CT signal doesn't it?

Things are different when we consider a DT signal. Given the values of $x[n]$ for say $n=0$ to $n=100$ we could use the same strategy and plot it as we did for the CT signal: just connect the dots $(n, x[n])$. That is done in practice often. However in these notes when dealing with the differences between CT and DT signals we want to stress that a DT signal essentially is just a sequence of numbers. There is no need to think about $x[n+0.2]$ in the discrete domain and certainly no need to visualize it.

Therefore a DT signal is plotted as a sequence of vertical bars. A stem plot:

                            n              =              np              .              arange              (              50              );              dt              =              0.07              /              50              x              =              np              .              sin              (              2              *              np              .              pi              *              50              *              n              *              dt              )              plt              .              xlabel              (              'n'              );              plt              .              ylabel              (              'x[n]'              );              plt              .              title              (              r              'Plot of DT signal \$x[n] = 325 \sin(2\pi 50 n \Delta t)\$'              );              #@savefig dtsineplot.png              plt              .              stem              (              n              ,              x              );            

(Source code, png, hires.png, pdf)

_images/plotting-2.png

Plotting Complex Valued Signals¶

Now consider the complex valued CT signal:

x(t) = e^{j 100 \pi t}

Plotting such a signal as if it were a real valued signal results in a Python warning that complex numbers are casted to real by discarding the imaginary parts. To see everything we have to plot both the real and imaginary part of the signal.

                            t              =              np              .              linspace              (              -              0.02              ,              0.05              ,              1000              )              plt              .              subplot              (              2              ,              1              ,              1              );              plt              .              plot              (              t              ,              np              .              exp              (              2j              *              np              .              pi              *              50              *              t              )              .              real              );              plt              .              xlabel              (              't'              );              plt              .              ylabel              (              'Re x(t)'              );              plt              .              title              (              r              'Real part of  \$x(t)=e^{j 100 \pi t}\$'              );              plt              .              xlim              ([              -              0.02              ,              0.05              ]);              plt              .              subplot              (              2              ,              1              ,              2              );              plt              .              plot              (              t              ,              np              .              exp              (              2j              *              np              .              pi              *              50              *              t              )              .              imag              );              plt              .              xlabel              (              't'              );              plt              .              ylabel              (              'Im x(t)'              );              plt              .              title              (              r              'Imaginary part of  \$x(t)=e^{j 100\pi t}\$'              );              plt              .              xlim              ([              -              0.02              ,              0.05              ]);              #@savefig csineplot.png              plt              .              show              ()            

(Source code, png, hires.png, pdf)

_images/plotting-3.png

Instead of plotting the real and imaginary part of a complex signal, we can also plot the magnitude and angle of the complex values.

                            t              =              np              .              linspace              (              -              0.02              ,              0.05              ,              1000              )              plt              .              subplot              (              2              ,              1              ,              1              );              plt              .              plot              (              t              ,              np              .              abs              (              np              .              exp              (              2j              *              np              .              pi              *              50              *              t              ))              );              plt              .              xlabel              (              r              '\$t\$'              );              plt              .              ylabel              (              r              '\$|x(t)|\$'              );              plt              .              title              (              r              'Absolute value of  \$x(t)=e^{j 100 \pi t}\$'              );              plt              .              xlim              ([              -              0.02              ,              0.05              ]);              plt              .              subplot              (              2              ,              1              ,              2              );              plt              .              plot              (              t              ,              np              .              angle              (              np              .              exp              (              2j              *              np              .              pi              *              50              *              t              ))              *              360              /              (              2              *              np              .              pi              ));              plt              .              xlabel              (              '\$t\$'              );              plt              .              ylabel              (              r              '\$\angle x(t)\$'              );              plt              .              title              (              r              'Phase of \$x(t)=e^{j 100 \pi t}\$'              );              plt              .              xlim              ([              -              0.02              ,              0.05              ]);              #@savefig cabsanglesineplot.png              plt              .              show              ()            

(Source code, png, hires.png, pdf)

_images/plotting-4.png

Note that both plots of the complex signal are equivalent. They both do represent the same signal. Also note the seemingly strange behaviour of the phase (angle) plot. It looks quite discontinuous. But it is really not because the phase jumps from +180 degrees to -180 degrees and that is of course the same angle! This phenomenon will be seen quite a lot when plotting the phase of complex signals and functions. It is called phase wrapping. If we would allow angles outside the range from -180 to +180 we could obtain a perfectly straight line (doing just that is called phase unwrapping).

A few special functions¶

In DSP we use some special functions:

1. Constant signal

Constant Function
CT DT
$x(t) = 1$ $x[n] = 1$

**2. Step function **

A step function is like a light switch which is turned on at t = 0.

Step Function
CT DT
x(t) = \begin{cases} 0 &: t<0\\ 1 &: t\geq 0\end{cases} x[n] = \begin{cases} 0 &: n<0\\ 1 &: n\geq 0\end{cases}

3. Pulse function

The pulse function, also called the impulse function, in DT is easy: everywhere zero except at $n=0$ where the values is $1$. The DT pulse is written as $\delta[n]$.

In CT it is more difficult. The pulse in CT is written as $\delta(t)$. It is mathematically defined with the sifting

property:

x(0) = \int_{-\infty}^{\infty} x(t)\delta(t)dt

A usefull way to think about a pulse signal is to consider a signal in which you want to concentrate a finite amount of energy in a short a time interval as possible. Energy in a signal is measured by integrating a signal over a period of time.

onsider for instance the signal $x_a(t)$ defined as:

x_a(t) = \begin{cases}           \frac{1}{2a} &: -a \leq t \leq a\\           0 &: \text{elsewhere}           \end{cases}

The total energy in this signal is $1$ (the integral of $x(t)$ over the entire domain). This is true irrespective of the value of $a$. In the limit when $arightarrow0$ the value $x_a(0)\rightarrow\infty$ but still the total area under the function remains 1. In a sloppy way we may define:

\delta(t) = \lim_{a\rightarrow0} x_a(t)

The strange thing thus is that the function value is infinite (well more accurate said it is not well defined) at $t=0$ whereas the integral over the entire domain is finite and equal to 1.

We will see many uses of the pulse function. The translated CT pulse function $3 \delta(t-2)$ is plotted as:

                            n              =              np              .              arange              (              10              );              x              =              np              .              zeros_like              (              n              );              x              [              2              ]              =              3              ;              plt              .              vlines              (              n              ,              0              ,              x              ,              'b'              );              plt              .              ylim              (              -              1              ,              4              );              plt              .              plot              (              n              ,              0              *              n              ,              'b'              );              #@savefig pulseplot.png              plt              .              show              ();            

(Source code, png, hires.png, pdf)

_images/plotting-5.png

The straight line indicates it is a pulse and the length of the line indicates the total energy.

Pulse Function
CT DT
x(t) = \delta(t) \equiv  \lim_{a\rightarrow0} x_a(t) x[n] = \delta[n]  \equiv \begin{cases} 1 &:  n=0\\ 0 &: \text{elsewhere}  \end{cases}

Exercises¶

Write Python functions to plot signals/functions like the examples above. Plot the following functions:

  • Step function u(t) and u[n]
  • Calculate a series of samples of a sine function of 50 Hz (for example 300 or more). Plot these samples as dots into

a graph and plot the sine as well. Change the number of samples to a lower number. What can you deduce from the result?