lab1q3 discrete version
This commit is contained in:
parent
1522fce3cd
commit
6d2257bbf8
@ -1,6 +1,17 @@
|
|||||||
|
"""
|
||||||
|
ECE 09351 - Digital Signal Processing
|
||||||
|
Lab 1 Question 1
|
||||||
|
|
||||||
|
Translated from MATLAB by Aidan Sharpe
|
||||||
|
Last Modified: January 16, 2025
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
f_s = 100 # Sample frequency [Hz]
|
f_s = 100 # Sample frequency [Hz]
|
||||||
T_s = 1/f_s # Sample period [s]
|
T_s = 1/f_s # Sample period [s]
|
||||||
@ -18,5 +29,7 @@ def main():
|
|||||||
|
|
||||||
plt.show() # Show the plot
|
plt.show() # Show the plot
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
@ -0,0 +1,36 @@
|
|||||||
|
"""
|
||||||
|
ECE 09351 - Digital Signal Processing
|
||||||
|
Lab 1 Question 2
|
||||||
|
|
||||||
|
Translated from MATLAB by Aidan Sharpe
|
||||||
|
Last Modified: January 16, 2025
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
f_sweep = np.arange(1,25)*np.pi # Angular frequency sweep from pi to 24*pi
|
||||||
|
f_max = np.max(f_sweep) # Maximum frequency in the frequency sweep
|
||||||
|
f_s = 2*f_max # Sample frequency at twice maximum
|
||||||
|
T_s = 1/f_s # Sampling period
|
||||||
|
|
||||||
|
E = np.empty_like(f_sweep) # Array to store energy calculations
|
||||||
|
for i, f in enumerate(f_sweep): # Loop over all frequencies in sweep
|
||||||
|
W = np.arange(0, f, T_s) # Define the variable of integration
|
||||||
|
x = np.sinc(0.5*W/np.pi) # np.sinc(0.5*W/np.pi) = sinc(0.5*W)
|
||||||
|
E[i] = np.sum(x**2 * T_s)/np.pi # Integrate x^2, with dt = T_s
|
||||||
|
|
||||||
|
plt.stem(f_sweep, E) # Plot energy at each frequency in sweep
|
||||||
|
plt.hlines(0.99, f_sweep[0], f_sweep[-1], colors="red") # Red horizontal line at 99% total energy
|
||||||
|
plt.xlabel("Angular frequency")
|
||||||
|
plt.ylabel("Signal energy")
|
||||||
|
plt.show()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
@ -0,0 +1,48 @@
|
|||||||
|
"""
|
||||||
|
ECE 09351 - Digital Signal Processing
|
||||||
|
Lab 1 Question 3
|
||||||
|
|
||||||
|
Translated from MATLAB by Aidan Sharpe
|
||||||
|
Last Modified: January 17, 2025
|
||||||
|
"""
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
import scipy as sp
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
|
|
||||||
|
def cft(g, f):
|
||||||
|
result = np.zeros(len(f), dtype=complex)
|
||||||
|
|
||||||
|
for i, ff in enumerate(f):
|
||||||
|
result[i] = complex_quad(lambda t: g(t)*np.exp(-2j*np.pi*ff*t), -5, 5)
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
def complex_quad(g, a, b):
|
||||||
|
t = np.linspace(a, b, 2501)
|
||||||
|
x = g(t)
|
||||||
|
return sp.integrate.simpson(y=x, x=t)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
g = lambda t: np.sinc(0.5*t/np.pi)
|
||||||
|
|
||||||
|
f_s = 1
|
||||||
|
T_s = 1/f_s
|
||||||
|
t_0 = 1000
|
||||||
|
t = np.arange(-t_0, t_0, T_s)
|
||||||
|
f = np.linspace(-f_s/2, f_s/2, len(t), endpoint=False)
|
||||||
|
omega = 2*np.pi*f
|
||||||
|
|
||||||
|
G = np.fft.fftshift(np.fft.fft(g(t)) * np.exp(-2j*np.pi*f*t_0) /f_s)
|
||||||
|
G_analytic = 2*np.pi*(np.heaviside(omega+0.5, 1) - np.heaviside(omega-0.5, 1))
|
||||||
|
|
||||||
|
plt.plot(f, G_analytic, color="red")
|
||||||
|
plt.scatter(f, G)
|
||||||
|
plt.show()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
Loading…
Reference in New Issue
Block a user