EXPERIMENT 3 Date ___________
Impulse Response & Total Response
AIM:(a) Find the convolution of x1(n) = {1,2,2,1}and x2(n)={1,-1,2}.
↑ ↑
( b) Determine the impulse response and total responseofthefollowing
LTI systems described by their constantcoefficientdifferenceequation.
Assume y(-1)=1 and y(-2)=0 for all systems
(i) y(n) – ½ y(n-1) = x(n) with x(n) = 10cos(πn/4)u(n)
(ii) y(n) – 0.9 y(n-1) + 0.81 y(n-2) = x(n) with x(n) = u(n)
(iii) y(n)-4y(n-1)-4y(n-2) = x(n)+3x(n-1) with x(n) = n (1/3)n u(n)
Part (a) Convolution
Program:
import
numpy
as
np
import
matplotlib.pyplot
as
plt
# Define sequences and their indices
x1 = np.array([
1
,
2,
2,
1])
n1 = np.arange(
-2
,
2
+
1
)
# n1 = [-2, -1, 0, 1, 2]
x2 = np.array([
1
,
-1
,
2])
n2 = np.arange(
-2
,
0
+
1
)
# n2 = [-2, -1, 0]
# Perform convolution
y = np.convolve(x1, x2)
# Calculate index of y
m = np.arange(
min
(n1) +
min
(n2),
max
(n1) +
max
(n2))
# Plotting
plt.stem(m, y)
plt.xlabel(
'n'
)
plt.ylabel(
'y(n)'
)
plt.title(
'Output of Convolution'
)
plt.grid(
True
)
25
plt.show()
Plots:
Part (b)(i) Impulse response of y(n) – ½ y(n-1) = x(n)
Program:
import
numpy
as
np
import
matplotlib.pyplot
as
plt
# Define parameters
N =
20
# Initialize input signal x(n) as an impulse signal
x1 = np.array([
1
] + [
0
] * (N -
1))
x = np.zeros(N +
1)
x[
1
:] = x1
# Initialize output signal y(n) with initial conditions
y = np.zeros(N +
1)
# Initialize y(n) with zeros
y[
0
] =
1
# y(-1) = 1
26
# Perform computation based on difference equation
for
i
in
range
(N):
y[i +
1] = (
1
/
2
) * y[i] + x1[i]
# Define index for plotting
m = np.arange(
-1
, N)
# m = [-1, 0, 1, ..., N-1]
# Input Signal x(n)
plt.subplot(
2
,
1,
1)
plt.stem(m, x)
plt.xlabel(
'n'
)
plt.ylabel(
'x(n)'
)
plt.title(
'Input Signal'
)
# Output Signal y(n)
plt.subplot(
2
,
1,
2)
plt.stem(m, y)
plt.xlabel(
'n'
)
plt.ylabel(
'y(n)'
)
plt.title(
'Output Signal'
)
plt.tight_layout()
27
Plots:
Part (b)(i) Total response of y(n) – ½ y(n-1) = x(n) with x(n) =
10cos(πn/4)u(n)
Program:
import
numpy
as
np
import
matplotlib.pyplot
as
plt
# Define parameters
N =
20
n = np.arange(
0
, N)
# n = [0, 1, ..., N-1]
# Define input signal x(n)
x1 =
10
* np.cos(n * np.pi /
4)
x = np.zeros(N +
1)
x[
1
:] = x1
# Initialize output signal y(n) with initial conditions
y = np.zeros(N +
1)
# Initialize y(n) with zeros
y[
0
] =
1
# y(-1) = 1
28
# Perform computation based on difference equation
for
i
in
range
(N):
y[i +
1] = (
1
/
2
) * y[i] + x1[i]
# Define index for plotting
m = np.arange(
-1
, N)
# m = [-1, 0, 1, ..., N-1]
# Input Signal x(n)
plt.subplot(
2
,
1,
1)
plt.stem(m,x)
plt.xlabel(
'n'
)
plt.ylabel(
'x(n)'
)
plt.title(
'Input Signal'
)
# Output Signal y(n)
plt.subplot(
2
,
1,
2)
plt.stem(m, y)
plt.xlabel(
'n'
)
plt.ylabel(
'y(n)'
)
plt.title(
'Output Signal'
)
Plots:
29
Part (b)(ii) Impulse response of y(n) – 0.9 y(n-1) + 0.81 y(n-2) = x(n)
Program:
import
numpy
as
np
import
matplotlib.pyplot
as
plt
# Define parameters
N =
20
n = np.arange(
0
, N)
# n = [0, 1, ..., N-1]
# Initialize input signal x(n) as an impulse signal
x1 = np.array([
1
] + [
0
] * (N -
1))
x = np.zeros(N +
2)
x[
2
:] = x1
# Initialize output signal y(n) with initial conditions
y = np.zeros(N +
2)
# Initialize y(n) with zeros
y[
0
] =
0
# y(-2) = 0
y[
1
] =
1
# y(-1) = 1
# Perform computation based on difference equation
for
i
in
range
(N):
y[i +
2] =
0.9
* y[i +
1] -
0.81
* y[i] + x[i
+ 2]
# Define index for plotting
m = np.arange(
-2
, N)
# m = [-2, -1, 0, 1, ..., N-1]
# Input Signal x(n)
plt.subplot(
2
,
1,
1)
plt.stem(m, x)
plt.xlabel(
'n'
)
plt.ylabel(
'x(n)'
)
plt.title(
'Input Signal'
)
# Output Signal y(n)
plt.subplot(
2
,
1,
2)
plt.stem(m, y)
plt.xlabel(
'n'
)
plt.ylabel(
'y(n)'
)
plt.title(
'Output Signal'
)
plt.tight_layout()
30
Plots:
Part (b)(ii) Total response of y(n) – 0.9 y(n-1) + 0.81 y(n-2) = x(n) with x(n)
= u(n)
Program:
import
numpy
as
np
import
matplotlib.pyplot
as
plt
# Define parameters
N =
20
n = np.arange(
0
, N)
# n = [0, 1, ..., N-1]
# Initialize input signal x(n) as an impulse signal
x1 = np.ones(N)
x = np.zeros(N +
2)
x[
2
:] = x1
# Initialize output signal y(n) with initial conditions
y = np.zeros(N +
2)
# Initialize y(n) with zeros
y[
0
] =
0
# y(-2) = 0
31
y[
1
] =
1
# y(-1) = 1
# Perform computation based on difference equation
for
i
in
range
(N):
y[i +
2] =
0.9
* y[i +
1] -
0.81
* y[i] + x1[i]
# Define index for plotting
m = np.arange(
-2
, N)
# m = [-2, -1, 0, 1, ..., N-1]
# Input Signal x(n)
plt.subplot(
2
,
1,
1)
plt.stem(m, x)
plt.xlabel(
'n'
)
plt.ylabel(
'x(n)'
)
plt.title(
'Input Signal'
)
# Output Signal y(n)
plt.subplot(
2
,
1,
2)
plt.stem(m, y)
plt.xlabel(
'n'
)
plt.ylabel(
'y(n)'
)
plt.title(
'Output Signal'
)
plt.tight_layout()
32
Plot:
Part (b)(iii) Impulse response of y(n)-4y(n-1)-4y(n-2) = x(n)+3x(n-1)
Program:
import
numpy
as
np
import
matplotlib.pyplot
as
plt
# Define parameters
N =
20
n = np.arange(
0
, N)
# n = [0, 1, ..., N-1]
# Initialize input signal x(n) as impulse signal
x1 = np.array([
1
] + [
0
] * (N -
1))
x=np.zeros(N+
2
)
x[
2
:]=x1
# Initialize output signal y(n) with initial conditions
y = np.zeros(N +
2)
# Initialize y(n) with zeros
y[
0
] =
0
# y(-2) = 0
y[
1
] =
1
# y(-1) = 1
33
# Extend input signal to account for initial conditions
x = np.zeros(N +
2)
x[
2
:] = x1
# Perform computation based on difference equation
for
i
in
range
(
1
, N):
y[i +
2] =
4
* y[i +
1] +
4
* y[i] + x[i +
2]
+
3
* x[i +
1
]
# Define index for plotting
m = np.arange(
-2
, N)
# m = [-2, -1, 0, 1, ..., N-1]
# Plotting Input Signal x(n)
plt.subplot(
2
,
1,
1)
plt.stem(m, x)
plt.xlabel(
'n'
)
plt.ylabel(
'x(n)'
)
plt.title(
'Input Signal'
)
# Plotting Output Signal y(n)
plt.subplot(
2
,
1,
2)
plt.stem(m, y)
plt.xlabel(
'n'
)
plt.ylabel(
'y(n)'
)
plt.title(
'Output Signal'
)
plt.tight_layout()
34
Plot:
Part (b)(iii) Total response of y(n)-4y(n-1)-4y(n-2) = x(n)+3x(n-1) with input
x(n) = n (1/3)n u(n)
Program:
import
numpy
as
np
import
matplotlib.pyplot
as
plt
# Define parameters
N =
20
n = np.arange(
0
, N)
# n = [0, 1, ..., N-1]
# Initialize input signal x(n)
x1 = n * (
1
/
3
)**n
# Initialize output signal y(n) with initial conditions
y = np.zeros(N +
2)
# Initialize y(n) with zeros
y[
0
] =
0
# y(-2) = 0
y[
1
] =
1
# y(-1) = 1
35
# Extend input signal to account for initial conditions
x = np.zeros(N +
2)
x[
2
:] = x1
# Perform computation based on the difference equation
for
i
in
range
(
1
, N):
y[i +
2] =
4
* y[i +
1] +
4
* y[i] + x[i +
2]
+
3
* x[i +
1
]
# Define index for plotting
m = np.arange(
-2
, N)
# m = [-2, -1, 0, 1, ..., N-1]
# Plotting Input Signal x(n)
plt.subplot(
2
,
1,
1)
plt.stem(m, x)
plt.xlabel(
'n'
)
plt.ylabel(
'x(n)'
)
plt.title(
'Input Signal'
)
# Plotting Output Signal y(n)
plt.subplot(
2
,
1,
2)
plt.stem(m, y)
plt.xlabel(
'n'
)
plt.ylabel(
'y(n)'
)
plt.title(
'Output Signal'
)
plt.tight_layout()
36
Plot:
CONCLUSION :
Marks/Grade Teacher’s Signature
37