Skip to content

Commit 199d173

Browse files
authored
Create rectangular_pulse_wave.py
1 parent 07ffafd commit 199d173

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

rectangular_pulse_wave.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# plotting rectangular pulse waves for PWM visualization
2+
#
3+
import numpy as np
4+
import matplotlib.pyplot as plt
5+
6+
# plotting routine
7+
fig,ax = plt.subplots(figsize=(16,10))
8+
ax.grid(True,color='#FCFCFC',linewidth=1.5)
9+
ax.set_facecolor('#e6e6e6')
10+
ax.set_ylim([-0.8,5.99])
11+
ax.set_xlabel('Time [s]',fontsize=26,labelpad=15)
12+
ax.set_ylabel('Amplitude [V]',fontsize=26,labelpad=15)
13+
ax.tick_params('both',labelsize=16)
14+
15+
D = 0.5 # duty cycle [%]
16+
T = 0.02 # period in seconds
17+
tau = D*T # pulse duration
18+
n_size = 10000 # size of the Fourier sum
19+
n = np.linspace(1.0,n_size,n_size) # vector for Fourier sum
20+
21+
periods_to_plot = 3 # how many periods to plot in figure
22+
t = np.linspace(0.0,T*periods_to_plot,1000) # time vector
23+
t_shift = t - (tau/2.0) # shift about zero point to capture full first cycle
24+
25+
f_t = [] # rectangular pulse variable
26+
for t_i in t_shift:
27+
f_t.append(5.0*((tau/T) + np.sum((2.0/(n*np.pi))*(np.sin((np.pi*n*tau)/(T)))*(np.cos((2.0*np.pi*n*t_i)/(T))))))
28+
29+
# annotations to show duty cycle, period, off band
30+
annot1 = ax.annotate("", xy=(T, 5.25), xytext=(T+tau, 5.25),
31+
arrowprops=dict(arrowstyle="|-|",facecolor='k',
32+
linewidth=3))
33+
txt1 = ax.text(T+(tau/2.0),5.6,'{0:2.0f}% Duty Cycle ({1:2.0f}ms)'.format(D*100.0,tau*1000.0),
34+
{'color': 'black', 'fontsize': 12, 'ha': 'center', 'va': 'center',
35+
'bbox': dict(boxstyle="round", fc="#FCFCFC", ec="#FCFCFC", pad=0.6)})
36+
annot2 = ax.annotate("", xy=(T, -0.25), xytext=(2.0*T, -0.25),
37+
arrowprops=dict(arrowstyle="|-|",facecolor='k',
38+
linewidth=3))
39+
txt2 = ax.text(T+(T/2.0),-0.5,'{0:2.0f}ms Period ({1:2.0f}Hz)'.format(T*1000.0,1.0/T),
40+
{'color': 'black', 'fontsize': 12, 'ha': 'center', 'va': 'center',
41+
'bbox': dict(boxstyle="round", fc="#FCFCFC", ec="#FCFCFC", pad=0.6)})
42+
ax.plot(t,f_t,linewidth=4,color=plt.cm.Set1(1))
43+
plt.show()

0 commit comments

Comments
 (0)