PythonとSciPyを使ってみた
MATLABの代替になることを期待してSciPyにチャレンジ。
というかPythonに初チャレンジ。
まずPython,NumPy,SciPy,matplotlibのインストーラをダウンロード、インストール。
ここを参考にEasy_installをインストール。
(ez_setup.pyをダウンロードして実行)
PythonのScriptsディレクトリ(C:\Python27\Scriptsとか)を環境変数のpathに追加。
コマンドプロンプトを開いて、必要なものをインストール。
> easy_install pip > pip install python-dateutil > easy_install pyparsing
これで準備完了。
試しに下のコードを実行してみた。
# -*- coding: utf-8 -*- from pylab import * import scipy.signal w1=100*2*pi w2=1000*2*pi a1=w1+w2 a0=w1*w2 b1=w2 b0=0 Ts=0.001 t=arange(0, 100, Ts) Ns=size(t) u=sin(2*pi*10*t) num=[b1,b0] den=[1,a1,a0] rt,y,rx=scipy.signal.lsim((num,den),u,t) figure() plot(t,u,'b-',label='Input') plot(t,y,'r-',label='Output') xlim(0,1) ylim(-2,2) xlabel('Time[sec]') ylabel('Voltage[V]') legend() grid(True) show()
同じ内容をMATLABで書いて比較。
w1=100*2*pi; w2=1000*2*pi; a1=w1+w2; a0=w1*w2; b1=w2; b0=0; Ts=0.001; t=0:Ts:100; Ns=length(t); u=sin(2*pi*10*t); num=[b1 b0]; den=[1 a1 a0]; y=lsim(tf(num,den),u,t); figure hold on plot(t,u,'b-') plot(t,y,'r-') xlim([0 1]) ylim([-2 2]) xlabel('Time[sec]') ylabel('Voltage[V]') legend('Input','Output') grid on
MATLABの方が実行速いんだけど…