Rowan-Classes/8th-Semester-Spring-2025/clinic-consultant/labs/lab-1/lab1q2.py
2025-01-18 18:20:20 -05:00

37 lines
1.4 KiB
Python

"""
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()