Rowan-Classes/8th-Semester-Spring-2025/clinic-consultant/labs/lab-2/lab2q5.py
Aidan Sharpe 82b435b6a2 Week 3
2025-02-14 22:27:10 -05:00

27 lines
483 B
Python

import numpy as np
import matplotlib.pyplot as plt
def u(n):
return np.heaviside(n, 1)
def main():
n = np.arange(101)
x = u(n-1)/n
# Avoid divide by zero
x[0] = 0
# Calculate the energy as a cumulative sum from n=0 to each sample
energy = np.cumsum(np.abs(x)**2)
plt.stem(n, energy)
plt.hlines(np.pi**2 / 6, n[0], n[-1], color='r')
plt.xlabel("Time index")
plt.ylabel("Energy")
plt.show()
if __name__ == "__main__":
main()