単相インバータ波形(ハーフブリッジ)のpython script

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker 
import scipy.signal as signal

class Inverter:
    def __init__(self,N=2**10):
        self.N=N #サンプリング数
        
    #sin波
    def sin_wave(self,Es,f1):
        x_sin=np.linspace(0,2*np.pi,self.N)
        y_sin=Es*np.sin(x_sin*f1)
        
        return(x_sin,y_sin)
    
    #三角波
    def tri_wave(self,Et,mf,f1):
        x_tri=np.linspace(0,2*np.pi,self.N)
        y_tri=Et*signal.sawtooth(x_tri*mf*f1-np.pi/2,0.5)
        
        return(x_tri,y_tri)
    
    #方形波
    def rectangle_wave(self,x_sin,y_sin,x_tri,y_tri,V):
        x_rec = x_sin
        y_rec = np.where(y_sin > y_tri, V/2, -V/2)
 
        return (x_rec, y_rec)


sin=Inverter()
tri=Inverter()
rec=Inverter()


"""
振幅変調率=sin波の振幅/三角波の振幅=0.8になるようにしている。
周波数変調率=三角波の周波数/sin波の周波数=15になるようにしている。
三角波の振幅はEt=5 Vで変調波の振幅はEs=4 Vである。
電源電圧V=30 Vである。
正弦波の基本周波数f=1 Hzである。
"""
ma=0.8
mf=15
Et=5
Es=Et*ma
V=30
f1=1
N=2**10
f=1/N

#sin波の生成
x_sin,y_sin=sin.sin_wave(Es,f1)
x_sin1,y_sin1=sin.sin_wave(V*ma/2,f1)

#三角波の生成
x_tri,y_tri=tri.tri_wave(Et,mf,f1)

#矩形波の生成
x_rec,y_rec=rec.rectangle_wave(x_sin,y_sin,x_tri,y_tri,V)



fft_data = (abs(np.fft.rfft(y_rec)))/(N/2*V/2)  #矩形波をフーリエ変換し基本波の振幅との比をとっている。
freqList = np.fft.rfftfreq((len(y_rec)),f) 

position=[0,np.pi,2*np.pi] #軸の主要値
labels=['0','π','2π']      #軸の主要値の名前

#ax1に三角波とsin波を図示する。
fig1=plt.figure()
ax1=fig1.add_subplot()
ax1.plot(x_sin,y_sin)
ax1.plot(x_tri,y_tri)
ax1.set_title("sin wave,tri_wave") #図のタイトル
ax1.set_xlabel("phase")   #x軸の名前
ax1.set_ylabel("voltage") #y軸の名前
ax1.xaxis.set_major_locator(ticker.FixedLocator(position))   #軸の主要値の場所を指定
ax1.xaxis.set_major_formatter(ticker.FixedFormatter(labels)) #軸の主要値の名前を指定

#ax2に基本波と矩形波を図示する。
fig2=plt.figure()
ax2=fig2.add_subplot()
ax2.plot(x_sin1,y_sin1,linestyle="dashed")
ax2.plot(x_rec,y_rec)
ax2.set_title("sin wave,rec wave") #図のタイトル
ax2.set_xlabel("phase")   #x軸の名前
ax2.set_ylabel("voltage") #y軸の名前
ax2.xaxis.set_major_locator(ticker.FixedLocator(position))   #軸の主要値の場所を指定
ax2.xaxis.set_major_formatter(ticker.FixedFormatter(labels)) #軸の主要値の名前を指定

#ax3に矩形波の高調波成分を図示する。
fig3=plt.figure()
ax3=fig3.add_subplot()
ax3.plot(freqList,fft_data)
ax3.set_title("harmonic of rec wave") #図のタイトル
ax3.set_xlabel("frecency")
ax3.set_ylabel("power") #y軸の名前
ax3.set_xlim(0.0,100)

以上です。