0% found this document useful (0 votes)
15 views6 pages

Assignment 6

The document outlines an assignment involving the analysis of a difference equation using Python. It includes tasks to compute and plot the impulse and unit step responses, check system stability, and compare different methods of calculating system responses. The stability of the system is determined by analyzing the poles of the transfer function.

Uploaded by

aditya arya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views6 pages

Assignment 6

The document outlines an assignment involving the analysis of a difference equation using Python. It includes tasks to compute and plot the impulse and unit step responses, check system stability, and compare different methods of calculating system responses. The stability of the system is determined by analyzing the poles of the transfer function.

Uploaded by

aditya arya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

ASSIGNMENT 6

equation 𝑦(𝑛) − 𝑦(𝑛 − 1) + 0.9𝑦(𝑛 − 2) = 𝑥(𝑛); ∀𝑛


1.) Python program for the given following difference

i. Calculate and plot the impulse response ℎ(𝑛) at 𝑛 =

ii. Calculate and plot the unit step response ℎ(𝑛) at 𝑛 =


−20, … ,100.

−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

# Define the coefficients of the difference equation


# y(n) - y(n-1) + 0.9*y(n-2) = x(n)
a = [1, -1, 0.9] # Coefficients of y(n) terms
b = [1] # Coefficients of x(n) terms (since x(n) = delta(n) for
impulse response)

# Define time range from n = -20 to 100


n = np.arange(-20, 101)

# Impulse response: find the response to delta(n) (impulse input)


delta = np.zeros(len(n))
delta[np.where(n == 0)] = 1 # delta(n) = 1 at n = 0
h_impulse = signal.lfilter(b, a, delta)

# Unit step response: find the response to u(n) (step input)


u = np.zeros(len(n))
u[np.where(n >= 0)] = 1 # u(n) = 1 for n >= 0
h_step = signal.lfilter(b, a, u)

# Plotting the impulse response


plt.figure(figsize=(10, 6))
plt.subplot(2, 1, 1)
plt.stem(n, h_impulse, basefmt=" ")
plt.title('Impulse Response h(n)')
plt.xlabel('n')
plt.ylabel('h(n)')
plt.grid()

# Plotting the step response


plt.subplot(2, 1, 2)
plt.stem(n, h_step, basefmt=" ")
plt.title('Unit Step Response h(n)')
plt.xlabel('n')
plt.ylabel('h(n)')
plt.grid()

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

def manual_convolution(x, h):


len_x = len(x)
len_h = len(h)
y_length = len_x + len_h - 1
y = np.zeros(y_length)

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)

# Plotting the results


plt.subplot(1, 3, 1)
plt.stem(n1, y_analytical)
plt.title('Analytical Response')
plt.xlabel('n')
plt.ylabel('y[n]')

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()

# Checking system stability


poles1 = np.roots(a1)
print(f"Poles of the system: {poles1}")
for pole in poles1:
if np.absolute(pole) > 1:
print("Unstable system.")
break

OUTPUT:
PLOTS:

SUMMARY:

This Python code calculates and compares system responses using


three methods: analytical, manual convolution, and the `lfilter`
function. It defines input sequences, computes the manual
convolution, and applies the `lfilter` function for a given difference
equation. The results from all methods are plotted for visual
comparison. Additionally, the system's stability is assessed by
calculating its poles; if any pole lies outside the unit circle, the system
is declared unstable.

You might also like