diff --git a/8th-Semester-Spring-2025/clinic-consultant/labs/lab-1/lab1q1.py b/8th-Semester-Spring-2025/clinic-consultant/labs/lab-1/lab1q1.py index 05b99e2..6648f21 100644 --- a/8th-Semester-Spring-2025/clinic-consultant/labs/lab-1/lab1q1.py +++ b/8th-Semester-Spring-2025/clinic-consultant/labs/lab-1/lab1q1.py @@ -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 matplotlib.pyplot as plt + + def main(): f_s = 100 # Sample frequency [Hz] T_s = 1/f_s # Sample period [s] @@ -18,5 +29,7 @@ def main(): plt.show() # Show the plot + + if __name__ == "__main__": main() diff --git a/8th-Semester-Spring-2025/clinic-consultant/labs/lab-1/lab1q2.py b/8th-Semester-Spring-2025/clinic-consultant/labs/lab-1/lab1q2.py new file mode 100644 index 0000000..d1fce86 --- /dev/null +++ b/8th-Semester-Spring-2025/clinic-consultant/labs/lab-1/lab1q2.py @@ -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() diff --git a/8th-Semester-Spring-2025/clinic-consultant/labs/lab-1/lab1q3.py b/8th-Semester-Spring-2025/clinic-consultant/labs/lab-1/lab1q3.py new file mode 100644 index 0000000..d2a9d3f --- /dev/null +++ b/8th-Semester-Spring-2025/clinic-consultant/labs/lab-1/lab1q3.py @@ -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()