Phase shifted PWM output in Python
Amit Singh, Ph.D.
Developing technology to deliver RF power into the plasma chamber.
I have become an avid user of Python and nowadays, I always find things ( in my work) that can be analyzed using Python. The task for which previously I had to use MATLAB scripts, I can very well complete using the open-source packages in Python.
Recently, I had to analyze the output of a phase-shifted PWM converter. I built a model which can generate the output based on the duty and number of harmonics information. The best part is you can have an interactive plot where you can vary the number of harmonics and duty and see the output.
Below is the code I wrote in the Jupyter notebook. Basically, I made "inverterV" function that takes the number of harmonics and duty as arguments. The number of harmonics is an integer value. Duty ranges from 0-1.
Use the code and customize it to suit your purpose. Modify the plot and make it more appealing if you want. There are a lot of styles and themes available just a click away.
import numpy as np import matplotlib.pyplot as plt import math import matplotlib as mpl mpl.rcParams["figure.dpi"] = 100 import ipywidgets as wg def inverterV(n, duty): n = n n_harmonics = np.arange(1, n+1) duty = duty phid1 = duty*np.pi phid2 = (duty+1)*np.pi #n_harmonics = 6 an = (1/(np.pi*n_harmonics))*(np.sin(n_harmonics*phid1)- np.sin(n_harmonics*phid2)) bn = (1/(np.pi*n_harmonics))* (np.cos(n_harmonics*phid2)- \ np.cos(n_harmonics*phid1)-np.cos(n_harmonics*np.pi)+1) t = np.arange(0,4*np.pi,.01) v = 0*t for i in range(n): j = i+1 v = v + an[i]*np.cos(j*t)+ bn[i]*np.sin(j*t) #print(an[i], bn[i]) #print(i) plt.plot(t,v,'r', label = 'harmonics ='+ str(n)) plt.xlabel('theta in radians') plt.style.use('classic') plt.legend() plt.grid(True) plt.xlim(0,4*np.pi) plt.show wg.interact(inverterV, n = (1,30,1), duty = (0.1,1,0.1))
iColak
3 年Congratulation!