Through examples, we define several operations on signals and show how they transform them. Then we define what is a filter and the notion of impulse response.
We begin by defining a test signal.
# rectangular pulse
N=20; L=5; M=10
r=np.zeros(N)
r[L:M]=1
#
plt.stem(r)
_=plt.ylim([0, 1.2])
def op1(signal):
transformed_signal=np.zeros(np.size(signal))
for t in np.arange(np.size(signal)):
transformed_signal[t]=signal[t]-signal[t-1]
return transformed_signal
def op2(signal):
transformed_signal=np.zeros(np.size(signal))
for t in np.arange(np.size(signal)):
transformed_signal[t]=0.5*signal[t]+0.5*signal[t-1]
return transformed_signal
plt.figure()
plt.stem(op1(r))
_=plt.ylim([-1.2, 1.2])
plt.title("Filtering of rectangular signal with op1")
plt.figure()
plt.stem(op2(r),'r')
_=plt.ylim([-0.2, 1.2])
plt.title("Filtering of rectangular signal with op2")
We define a sine wave and check that the operation implemented by "op1" seems to be a derivative...
t=np.linspace(0,100,500)
sig=np.sin(2*pi*0.05*t)
plt.plot(t,sig, label="Initial signal")
plt.plot(t,5/(2*pi*0.05)*op1(sig), label="Filtered signal")
plt.legend()
Composition of operations:
plt.stem(op1(op2(r)),'r')
_=plt.ylim([-1.2, 1.2])
def op3(signal):
transformed_signal=np.zeros(np.size(signal))
for t in np.arange(np.size(signal)):
transformed_signal[t]= 0.7*transformed_signal[t-1]+signal[t]
return transformed_signal
plt.stem(op3(r),'r')
plt.title("Filtering of rectangular signal with op3")
_=plt.ylim([-0.2, 3.2])
def op4(signal):
transformed_signal=np.zeros(np.size(signal))
for t in np.arange(np.size(signal)):
transformed_signal[t]= 1*transformed_signal[t-1]+signal[t]
return transformed_signal
plt.stem(op4(r),'r')
plt.title("Filtering of rectangular signal with op4")
_=plt.ylim([-0.2, 5.6])
# And then..
plt.figure()
plt.stem(op1(op4(r)),'r')
plt.title("Filtering of rectangular signal with op1(op4)")
_=plt.ylim([-0.2, 1.2])
Definition A filter is a time-invariant linear system.
def dirac(n):
# dirac function
return 1 if n==0 else 0
def dirac_vector(N):
out = np.zeros(N)
out[0]=1
return out
d=dirac_vector(20)
fig,ax=plt.subplots(2,2,sharex=True)
ax[0][0].stem(op1(d), label="Filter 1")
ax[0][0].legend()
ax[0][1].stem(op2(d), label="Filter 2")
ax[0][1].legend()
ax[1][0].stem(op3(d), label="Filter 3")
ax[1][0].legend()
ax[1][1].stem(op4(d), label="Filter 4")
ax[1][1].legend()
plt.suptitle("Impulse responses")
The impulse response of op4(op1) is given by
h=op4(op1(dirac_vector(20)))
plt.stem(h, label="Filter 4(1)")
_=plt.axis([-5, 20, 0, 1.2])
This is nothing but a Dirac impulse! We already observed that op4(op1(signal))=signal; that is the filter is an identity transformation. In other words, op4 acts as the "inverse" of op1. Finally, we note that the impulse response of the indentity filter is a Dirac impulse.