Rowan-Classes/6th-Semester-Spring-2024/DSP/PracticeProblems/quiz1.py
2024-02-22 14:22:36 -05:00

65 lines
1.5 KiB
Python

import numpy as np
import matplotlib.pyplot as plt
PI = np.pi
INF = 10
print("# Quiz 1 Preparation - Aidan Sharpe")
print("""## Question 1
Find the energy and average power of $x(n) = e^{j 2\pi n / 3}$
""")
def x_1(n):
return np.exp(2j*PI*n/3)
x_n = x_1(np.arange(-INF, INF))
E_x = np.sum( np.abs(x_n)**2 )
print("Energy of x[n]:", E_x if E_x < INF else "Infinity")
N = 3
x_n = x_1(np.arange(0, N))
P_x = (1/N) * np.sum( np.abs(x_n)**2 )
print("Power of x[n]:", P_x if P_x < INF else "Infinity")
print("""## Question 2
Is $x(n - \pi)$ a valid discrete signal?
No""")
print("""## Question 3
Consider the system $y[n] = S[x[n]] = e^{n+3}\cos(n-5)x[n-4]$. Is the system linear, time-invariant, causal, and BIBO stable?
""")
a_3 = 5
def S_3(x,n,a=1):
return np.exp(n+3) * np.cos(n-5) * x(n-4) * a
def x_3_1(n):
return n*(n-1)*np.heaviside(n,0)
def x_3_2(n):
return 5*n*np.heaviside(n,0)
def x_3_1_2(n):
return x_3_1(n) + x_3_2(n)
S_ax_3 = S_3(x_3_1, np.arange(-INF, INF), a=a_3)
y_3 = S_3(x_3_1, np.arange(-INF, INF))
print("Homogeneous:", abs(sum(S_ax_3 - a_3*y_3)) < 0.01, '\n')
S_x1_x2_3 = S_3(x_3_1_2, np.arange(-INF, INF))
y1_3 = S_3(x_3_1, np.arange(-INF,INF))
y2_3 = S_3(x_3_2, np.arange(-INF, INF))
print("Superposition:", abs(sum(S_x1_x2_3 - (y1_3 + y2_3))) < 0.01, '\n')
print("""## Quesiton 4
Consider the LTI sstem with impulse response $h[n] = u[n]$. If the input x[n] = n (0.3)^n u[n]$ and the output is $y[n]$, calculate:
### a)
$y(-1)$""")