# Plotting library for the Python programming language.
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
def plotSignal(t,x,ptitle='Signal'):
fig = plt.figure(figsize=(10,6))
ax1 = fig.add_axes([0, 0, 1, 0.8]) # [left, bottom, width, height]
ax1.set_xlabel('t')
ax1.set_ylabel('x')
ax1.set_title(ptitle)
ax1.fill_between(t,0,x,color='green', alpha=0.4)
ax1.plot(t, x, color='green', lw=6);
def step(t, t_0):
return 1 * ((t - t_0) >= 0)
t = np.arange(-10.0, 10.0, 0.01)
t_0 = 0
u = step(t, t_0)
plotSignal(t,u,'Heaviside or Unit Step');
t_0 = 5
u_s = step(t, t_0)
plotSignal(t,u_s,'Shifted Heaviside or Unit Step');
pulse = u - u_s
plotSignal(t,pulse,'Unit Pulse from the difference of Unit Step functions');
with
def delta_approx(t,eps):
d = np.zeros(len(t))
for m,n in enumerate(t):
if abs(n) <= eps/2.:
d[m] = 1/float(eps)
return d
eps = 0.1
delta = delta_approx(t,eps)
plotSignal(t,delta)
eps = 0.1
for k in xrange(5):
eps = eps + eps*k
delta = delta_approx(t,eps)
plotSignal(t,delta)
from IPython.display import IFrame
IFrame('http://en.wikipedia.org/wiki/Dirac_delta_function#/media/File:Dirac_function_approximation.gif', width='100%', height=400)
def dImpulse(n, n_0):
return 1 * ((n - n_0) == 0)
n = np.arange(-10, 11, 1)
n_0 = 1
dDelta = dImpulse(n,n_0)
fig = plt.figure(figsize=(12,8))
ax1 = fig.add_axes([0, 0, 0.8, 0.8]) # [left, bottom, width, height]
ax1.set_xlabel('t')
ax1.set_ylabel('x')
ax1.set_ylim([0,1.5])
ax1.set_title('Discrete Impulse')
markerline, stemlines, baseline = stem(n, dDelta, '-.');
setp(markerline, 'markerfacecolor', 'g');
setp(baseline, 'color','r', 'linewidth', 2);
t = np.arange(0, 10.0, 0.1)
a = 1.12
for k in xrange(5):
a = a + a * k
expSig = np.power(a, t)
plotSignal(t,expSig, 'Exponential Signal $ x(t) = a^t$, a = %0.2f' %a);
t = np.arange(-10.0, 10.0, 0.1)
alpha = -0.2 + 0.3j
x = exp(alpha*t)
fig = plt.figure(figsize=(16,12))
plt.subplot(2, 2, 1)
plt.plot(t,real(x),'g*');
plt.title('Real Part')
plt.xlabel('t')
plt.subplot(2, 2, 2)
plt.plot(t,imag(x),'r*');
plt.title('Imaginary Part')
plt.xlabel('t')
plt.subplot(2, 2, 3)
plt.plot(t,abs(x),'bs');
plt.title('Magnitude Part')
plt.xlabel('t')
plt.subplot(2, 2, 4)
plt.plot(t,(180/pi)*angle(x),'go');
plt.title('Phase Part')
plt.xlabel('t');
t = np.arange(0.0, 1.0, 0.01)
s = np.sin(2*np.pi*t)
fig = plt.figure(figsize=(12,8))
ax1 = fig.add_axes([0, 0, 1, 0.8]) # [left, bottom, width, height]
ax1.set_ylabel('x(t)')
ax1.set_title('a sine wave')
line, = ax1.plot(t, s, color='green', lw=3)
t = np.arange(0.0, 1.0, 0.01)
s2 = np.cos(2*np.pi*t)
fig = plt.figure(figsize=(12,8))
ax1 = fig.add_axes([0, 0, 1, 0.8]) # [left, bottom, width, height]
ax1.set_ylabel('x(t)')
ax1.set_title('a cosine wave')
line, = ax1.plot(t, s2, color='green', lw=3)
t = np.arange(-10.0, 10.0, 0.01)
alpha = -0.5 + np.pi*4j
x = exp(alpha*t)
plt.figure(figsize=(12,8))
plt.subplot(2, 1, 1)
plt.plot(t,real(x),'g-');
plt.title('Real Part')
plt.xlabel('t')
plt.subplot(2, 1, 2)
plt.plot(t,imag(x),'r-');
plt.title('Imaginary Part')
plt.xlabel('t');
def rect(t,tau):
x = np.zeros(len(t))
for k,tk in enumerate(t):
if np.abs(tk) <= tau/2.:
x[k] = 1
else:
x[k] = 0
return x
t = np.arange(-10.0, 10.0, 0.1)
pulse = rect(t,2)
plotSignal(t,pulse,'Rectangular Pulse Signal');
def triangular(t,tau):
x = np.zeros(len(t))
for k,tk in enumerate(t):
if np.abs(tk) <= tau/1.:
x[k] = 1 - np.abs(tk)/tau
else:
x[k] = 0
return x
t = np.arange(-10.0, 10.0, 0.1)
pulse = triangular(t,2)
plotSignal(t,pulse,'Triangular Pulse Signal');
def signum(t):
x = np.zeros(len(t))
for k,tk in enumerate(t):
if tk > 0:
x[k] = 1
elif tk < 0:
x[k] = -1
else:
x[k] = 0
return x
t = np.arange(-10.0, 10.0, 0.1)
sign = signum(t)
plotSignal(t,sign,'Signum');
def sinc(x):
try:
x = np.pi * x
return sin(x) / x
except ZeroDivisionError:
return 1.0
def sincSignal(t):
x = np.zeros(len(t))
for k,tk in enumerate(t):
x[k] = sinc(tk)
return x
t = np.arange(-10.0, 10.0, 0.1)
s = sincSignal(t)
plotSignal(t,s,'Sinc');