'Simulating a square wave with a duty cycle (python)
I was trying to simulate a square wave with a duty cycle but something went wrong and i've been spending the last 3 hours understandig why.
Here are the pictures of what the function i have to use (1), what i should get (2), what I get (3) and the script (4).
δ varies from o to 1 (in my plot it was 0.6)
Here is my code
#libraries
import numpy as np
from matplotlib import pyplot as plt
#function
R=3290
C=1.1693*10**(-6)
f=1000
ft=10
tau=(ft*2*np.pi)**-1
delta=tau*f
D=0.6
x=np.linspace(-2/f,2/f, 4999)
k=np.zeros(5000)
i=0
k[0]=1
while i<5000:
k[i]=2*i+1
i+=1
ck = (2/(np.pi*k))*np.sin(k*np.pi*D)
omegak=(2*np.pi)*k*f
Ak=1/(np.sqrt(1+(k*f/ft)**2))
Dphik=np.arctan(-k*f/ft)
g=np.zeros(4999)
for j in range(0,4999):
g[j]=np.sum(ck*np.cos(omegak*x[j]))
#plot
plt.figure(figsize=(10,5))
plt.plot(x, g)
plt.xlabel('t[T]')
plt.ylabel('g(t)')
plt.show()
As you can see from the pictures i doesn't really work that well and i dont know why.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|