In this simple exercise, we recall the effect of delays and scaling on signals. It is important for students to experiments with that to ensure that they master these simple transformations.
Study the code below and experiment with the parameters
# Define a simple function
def f(t):
return np.exp(-0.25*t) if t>0 else 0
T= np.linspace(-10,20,200)
L=len(T)
x=np.zeros(L) # reserve some space for x
t0=0; a=1 # initial values
# Compute x as f(a*t+t0)
k=0
for t in T:
x[k]=f(a*t+t0)
k=k+1
# Plotting the signal
plt.plot(T,x)
plt.xlabel("Time")
plt.ylabel("Amplitude")
plt.grid(b=True)
# Experiment with several values of a and t0:
# a=1 t0=0
# a=1 t0=+5 (advance)
# a=1 t0=-5 (delay)
# a=-1 t0=0 (time reverse)
# a=-1 t0=5 (time reverse + advance)
# a=-1 t0=-5 (...)
This to show that you do automatically several tests and plot the results all together.
def compute_x(a,t0):
k=0
for t in T:
x[k]=f(a*t+t0)
k=k+1
return x
list_tests=[(1,0),(1,5), (1,-5)]#,(-1,0),(-1,3), (-1,-3) ]
for (a,t0) in list_tests:
x=compute_x(a,t0)
plt.plot(T,x,label="a={},t0={}".format(a,t0))
plt.xlabel("Time")
plt.ylabel("Amplitude")
plt.grid(b=True)
_=plt.legend()
And finally an interactive version
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = (8,4)
def f(t):
out=np.zeros(len(t))
tpos=np.where(t>0)
out[tpos]=np.exp(-0.25*t[tpos])
return out
t= np.linspace(-10,20,200)
L=len(t)
x=np.zeros(L)
def compute_xx(t0,a):
x=f(a*t+t0)
plt.plot(t,x)
plt.axis([-10, 20, 0, 1])
s_t0=widgets.FloatSlider(min=-20,max=20,step=1)
s_a=widgets.FloatSlider(min=0.1,max=5,step=0.1,value=2)
_=interact(compute_xx,t0=s_t0,a=s_a)