Assignment 6
Assignment 6
−20, … ,100.
iii. Is the system specified by h(n) stable?
CODE:
import numpy as np
import matplotlib.pyplot as plt
from scipy import signal
plt.tight_layout()
plt.show()
# Checking stability:
poles = np.roots(a)
print(f"Poles of the system: {poles}")
# print(np.absolute(poles[0]))
for pole in poles:
if np.absolute(pole)>1:
print("unstable system.")
break
else:
print("system is stable.")
break
OUTPUT:
PLOT:
SUMMARY:
This Python code computes and compares the responses of a system using three
methods: analytical, manual convolution, and the `lfilter` function. It first
defines input sequences and performs manual convolution. The analytical and
filter-based responses are calculated for a difference equation, and the results
are plotted. The stability of the system is also checked by finding the poles of
the transfer function, printing "Unstable system" if any pole lies outside the unit
circle.
Python program for let 𝑥(𝑛) = (0.8) 𝑛𝑢(𝑛), ℎ(𝑛) = (−0.9) 𝑛𝑢(𝑛), and
𝑦(𝑛) = ℎ(𝑛) ∗ 𝑥(𝑛). Use 3 columns and 1 row of subplot for the
2.)
following parts.
i. Determine y(n) analytically. Plot first 51 samples of y(n) using
the stem function.
ii. Truncate x(n) and h(n) to 26 samples. Use convolution function
to compute y(n). Plot y(n) using the stem function. Compare
your result with those of part 1.
iii. Using the filter function, determine the first 51 samples of
x(n)*h(n). Plot y(n) using the stem function. Compare your
results with those of parts 1 and 2.
CODE:
import numpy as np
import matplotlib.pyplot as plt
import scipy.signal as signal
for n in range(y_length):
for k in range(len_x):
if 0 <= n - k < len_h:
y[n] += x[k] * h[n - k]
return y
n1 = np.arange(0, 51)
n2 = np.arange(0, 26)
alpha = 0.471
beta = 0.529
y_analytical = alpha * (0.8**n1) + beta * ((-0.9)**n1)
x = 0.8**n2
h = (-0.9)**n2
# Manual convolution
y_manual = manual_convolution(x, h)
# Using lfilter
b1 = [1]
a1 = [1, 0.1, -0.72]
impulse = np.zeros(51)
impulse[0] = 1
y_filter = signal.lfilter(b1, a1, impulse)
plt.subplot(1, 3, 2)
plt.stem(np.arange(0, len(y_manual)), y_manual[:51])
plt.title('Manual Convolution Response')
plt.xlabel('n')
plt.ylabel('y[n]')
plt.subplot(1, 3, 3)
plt.stem(n1, y_filter)
plt.title('Using lfilter')
plt.xlabel('n')
plt.ylabel('y[n]')
plt.show()
OUTPUT:
PLOTS:
SUMMARY: