Phase shifted PWM output in Python

Phase shifted PWM output in Python

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))
Output of the code. Vary the 'n' and 'duty' to generate different outputs.



Congratulation!

回复

要查看或添加评论,请登录

Amit Singh, Ph.D.的更多文章

  • Animating infinity

    Animating infinity

    A few weeks ago, we celebrated my son's second birthday. His name is Anant, which has its roots in Sanskrit and means…

  • SPWM ac-dc conceptual design

    SPWM ac-dc conceptual design

    Let's do a conceptual design of a single-phase bidirectional ac-dc converter. The design criterion for the example…

  • Toroid Inductor simulation

    Toroid Inductor simulation

    FEMM is open-source software that can simulate electromagnetic and electrostatic problems. Recently, I was developing a…

    3 条评论
  • Demystifying capacitor energy loss using LTSpice

    Demystifying capacitor energy loss using LTSpice

    The power of LTSpice stems from the fact that it is a free and widely used software for electrical simulation and…

    2 条评论
  • Capacitor charging puzzle

    Capacitor charging puzzle

    One of the most popular puzzles in the power electronics community is the capacitor charging puzzle. It goes like this.

  • Fundamentals of Single Phase AC-DC PWM Rectifier

    Fundamentals of Single Phase AC-DC PWM Rectifier

    There exist several types of AC-DC rectifiers. The selection of a particular AC-DC rectifier depends on many factors…

    5 条评论
  • Displacement Power Factor measurement block in MATLAB Simulink

    Displacement Power Factor measurement block in MATLAB Simulink

    More often, in power electronic converter simulation, we need to measure the phase difference between sinusoidal input…

社区洞察

其他会员也浏览了