Lab-6 (Discrete Equations)
Lab-6 (Discrete Equations)
(Difference Equations)
EC-3705
Code:
import numpy as np
from scipy.signal import dlti, dstep, dimpulse, residue
import matplotlib.pyplot as plt
_, y1 = dimpulse(system, n=120);
_, y2 = dstep(system, n=120);
n = np.arange(-20, 100);
_, p, _ = residue(num, den)
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.
y = np.zeros(len(n));
y1[(n >= n1) & (n < n1 + len(x1))] = x1;
return y,n
y1 = np.zeros(len(n))
y2 = np.zeros(len(n))
n_shifted = n + shift
return x, n_shifted
n_folded = -(n+len(x)-1)
x_folded = x[::-1]
return x_folded, n_folded
n = np.arange(0, 50);
x1 = analytical_solution(n);
y1, _ = conv(truncate_seq(0.8, 26), 0, truncate_seq(-0.9, 26), 0);
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.