退屈な日常①
These days, I read novels in the train on the way to school. Especially, the novel called "kenki" is one of my favorite novels, because the main charactor called tagane is the strongest soldier.
JupyterLab上でのアニメーションの出力方法
JupyterLab上でmatplotlibのアニメーションを出力する。
以下に参考URLを記載する。
JupyterLab上でmatplotlibのアニメーションを出力する - Qiita
内容を記述する。(消えると困るので)
方法1:IPython.display.HTMLの使用
確認用のコードを以下に示す
import numpy as np import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation from IPython.display import HTML fig, ax = plt.subplots() xdata, ydata = [], [] ln, = plt.plot([], [], 'ro') def init(): ax.set_xlim(0, 2 * np.pi) ax.set_ylim(-1, 1) return ln, def update(frame): xdata.append(frame) ydata.append(np.sin(frame)) ln.set_data(xdata, ydata) return ln, ani = FuncAnimation(fig, update, frames=np.linspace(0, 2 * np.pi, 128), init_func=init, blit=True, interval=50) HTML(ani.to_jshtml()) # HTML(ani.to_html5_video()) # またはこちら
方法2:jupyter-matplotlibの使用
確認用のコードを以下に示す
import numpy as np import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation %matplotlib widget fig, ax = plt.subplots() xdata, ydata = [], [] ln, = plt.plot([], [], 'ro') def init(): ax.set_xlim(0, 2 * np.pi) ax.set_ylim(-1, 1) return ln, def update(frame): xdata.append(frame) ydata.append(np.sin(frame)) ln.set_data(xdata, ydata) return ln, ani = FuncAnimation(fig, update, frames=np.linspace(0, 2 * np.pi, 128), init_func=init, blit=True, interval=50)
単相インバータ波形(ハーフブリッジ)の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)
以上です。
グラフの書き方(sin波,cos波)
import numpy as np import matplotlib.pyplot as plt import matplotlib.ticker as ticker x=np.linspace(0,2*np.pi,1000) #0~2πまで1000個作成 y1=np.sin(x) y2=np.cos(x) fig=plt.figure() #figureを作成 ax1=fig.add_subplot(211) #figure内の2行1列1番目にax1を作成 ax2=fig.add_subplot(212) #figure内の2行1列2番目にax2を作成 ax1.plot(x,y1) #sin波作成 ax2.plot(x,y2) #cos波作成 #図の体裁を整えていく position=[0,np.pi,np.pi*2] #軸の主要値 labels=['0','π','2π'] #軸の主要値の名前 ax1.set_title("sin 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.set_title("cos 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)) #軸の主要値の名前を指定 plt.tight_layout() #グラフのラベルやタイトルが重ならないようにする plt.show
以上だよw
2021/6/13
今日は一日バイトやった。昼からやけどW
そんあとは友達とapeしてランクでは死にまくったけどカジュアルは楽しかった。
友達は通信環境が悪いせいか時々停止しててそん時にばり撃たれてるのは笑えたw