0% found this document useful (0 votes)
17 views

Lab-6 (Discrete Equations)

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

Lab-6 (Discrete Equations)

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

Assignment-6

(Difference Equations)
EC-3705

Q1. Calculate impulse response and unit step response for


y(n) - y(n-1) + 0.9y(n-2) = x(n)

Code:
import numpy as np
from scipy.signal import dlti, dstep, dimpulse, residue
import matplotlib.pyplot as plt

# assuming y(-1) = 0 and y(-2) = 0


# H(z) = 1/(1 - z^-1 + 0.9z^-2) from analysis

num = [1, 0, 0];


den = [1, -1, 0.9];

system = dlti(num, den);

_, y1 = dimpulse(system, n=120);
_, y2 = dstep(system, n=120);

n = np.arange(-20, 100);

_, p, _ = residue(num, den)

print("Poles: ", p);


print("Since poles lie inside unit circle, therefore system is stable");

plt.figure(figsize=(8, 8))

plt.subplot(2, 1, 1)
plt.stem(n, np.squeeze(y1))
plt.title('Impulse Response')
plt.xlabel('n')
plt.ylabel('y(n)')

plt.subplot(2, 1, 2)
plt.stem(n, np.squeeze(y2))
plt.title('Unit Step Response')
plt.xlabel('n')
plt.ylabel('y(n)')

plt.show()

Output:
Poles: [0.5+0.80622577j, 0.5-0.80622577j]
Since poles lie inside unit circle, therefore system is stable
Summary:

First of all, the transfer function for this discrete sequence was calculated. It came out to
be: H(z) = 1/(1 - z^-1 + 0.9z^-2).
Then the numerator and denominator coefficients were fed into dlti function to define the
lti system.
Then using dimpulse and dstep functions and the lti system as input, we were able to find
out and plot the impulse and step response.
The poles were found out to be at [0.5+0.80622577j, 0.5-0.80622577j] using the residue
function. And since they lie inside the unit circle, the system is stable.

Q2. X(n) = (0.8)^n * u(n), y(n) = (-0.9)^n * u(n).


Find y(n) = x*h(n)
Code:
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import lfilter

def zero_padding(x1, n1, left_limit, right_limit):


n_min = min(n1, left_limit);
n_max = max(n1 + len(x1) -1, right_limit);
n = np.arange(n_min, n_max + 1);

y = np.zeros(len(n));
y1[(n >= n1) & (n < n1 + len(x1))] = x1;
return y,n

def align_signals(x1, n1, x2, n2):

n_min = min(n1, n2)


n_max = max(n1 + len(x1) - 1, n2 + len(x2) - 1)
n = np.arange(n_min, n_max + 1)

y1 = np.zeros(len(n))
y2 = np.zeros(len(n))

y1[(n >= n1) & (n < n1 + len(x1))] = x1


y2[(n >= n2) & (n < n2 + len(x2))] = x2

return y1, y2, n

def multiply_signals(x1, n1, x2, n2):

x, h, n = align_signals(x1, n1, x2, n2)


res = np.zeros(len(x))
for i in range(len(x)):
res[i] = x[i]*h[i];
return res, n

def signal_addition(x1, n1, x2, n2):

y1, y2, n = align_signals(x1, n1, x2, n2)


return y1 + y2, n

def signal_shifting(x, n, shift):

n_shifted = n + shift
return x, n_shifted

def signal_folding(x, n):

n_folded = -(n+len(x)-1)
x_folded = x[::-1]
return x_folded, n_folded

def conv(x, n1, h, n2):


n_conv = n1+n2
l = len(x)+len(h)-1
y = np.zeros(l)

h_folded, n2_folded = signal_folding(h, n2)


for i in range(l):
h_shifted, n2_shifted = signal_shifting(h_folded, n2_folded, n_conv + i)
multiplied, n = multiply_signals(x, n1, h_shifted, n2_shifted)
for a in multiplied:
y[i]+=a

return y, np.arange(n_conv, n_conv + l)


def analytical_solution(n):
x = np.zeros(len(n));
for i in range(len(n)):
x[i] = (9/17)*((-0.9)**i)*(1 - ((-8/9)**(i+1)));
return x;1

def truncate_seq(a, n):


x = np.zeros(n);
for i in range(n):
x[i] = (a)**i;
return x;

n = np.arange(0, 50);
x1 = analytical_solution(n);
y1, _ = conv(truncate_seq(0.8, 26), 0, truncate_seq(-0.9, 26), 0);

num = [1, 0, 0];


den = [1, 0.1, -0.72];
impulse = np.zeros(51);
impulse[0] = 1;

y2 = lfilter(num, den, impulse);

plt.figure(figsize=(8, 6))

plt.subplot(1, 3, 1)
plt.stem(n, x1)
plt.title('Convolution by Analysis')
plt.xlabel('n')
plt.ylabel('y(n)')

plt.subplot(1, 3, 2)
plt.stem(np.arange(0, 51), y1)
plt.title('Convolution by Computation')
plt.xlabel('n')
plt.ylabel('y(n)')

plt.subplot(1, 3, 3)
plt.stem(np.arange(0, 51), y2)
plt.title('Convolution by lfilter')
plt.xlabel('n')
plt.ylabel('y(n)')
plt.tight_layout()
plt.show()

Output:

Summary:

In this question, first of all the expression for y(n) was found out using analysis. It came out
to be : y(n) = (9/17) * ( (-0.9)**n )*( 1 - ( (-8/9)**(n+1) ) ). This function was then plotted
manually.

In the second approach, we defined x(n) and h(n). Then using the convolution function that
we defined in week 2, y(n) was plotted.
In the third approach, we found z-transform of both x(n) and h(n). Then we multiplied both
of them, since convolution in time domain is multiplication in z-domain. Then we got :
Y(z) = z^2 /( z^2 + 0.1z - 0.72 ). The coefficients for numerator and denominator were fed
into the lfilter and y(n) was plotted.

You might also like