回路シミュレーターSPICE, SCAMを使う

seihiguchi2006-07-25



ひょんなことから電気回路の(簡単な)解析を行うことになったので、電気工学が専門ではない者としては、回路図から伝達関数を導き出す経過はなるべく避けて通りたい。ということで、SPICEとSCAMを使ってみた。

SPICE

SPICE is a general-purpose circuit simulation program for nonlinear dc, nonlinear transient, and linear ac analyses. Circuits may contain resistors, capacitors, inductors, mutual inductors, independent voltage and current sources, four types of dependent sources, lossless and lossy transmission lines (two separate implementations), switches, uniform distributed RC lines, and the five most common semiconductor devices: diodes, BJTs, JFETs, MESFETs, and MOSFETs. SPICE originates from the EECS Department of the University of California at Berkeley.
(http://infopad.eecs.berkeley.edu/~icdesign/SPICE/)

という風に本家に説明があるのだが、随分と昔から使われている回路シミュレーターである。いろいろと亜種があり、GUIが備わった商用版ばかりが見つかったのだが、Ngspiceというのがちゃんとしたパッケージで配布されていた。

Ngspice is a mixed-level/mixed-signal circuit simulator. Its code is based on three open source software packages: Spice3f5, Cider1b1 and Xspice. Ngspice is part of gEDA project, a full GPL'd suite of Electronic Design Automation tools.
(http://ngspice.sourceforge.net/)

次のような参考書をあさりながら作業を進めていたのだが、

Spice: A Guide to Circuit Simulation and Analysis Using Pspice

Spice: A Guide to Circuit Simulation and Analysis Using Pspice

Spice for Circuits and Electronics Using Spice

Spice for Circuits and Electronics Using Spice

いろいろと不都合が生じたためSCAM (Symbolic Circuit Analysis in MatLab)にチェンジした。


SCAM Symbolic Circuit Analysis in MatLab


MATLABで動く回路シミュレーターなのだが、要はSPICEが数値解析しかできないところを、MATLABのSymbolic Math Toolboxの機能を使って、Symbolicに電圧や電流の式について解くことができる。当然得られた式に数値を代入して評価することができ、SPICEに慣れていない者としてはSCAMの方が格段に使いやすく感じた。


ダウンロードは以下の本家サイトから, "scam.m"をもってくる。


使い方は上記サイトに詳述されているが

  1. SPICEに似た, 回路ファイルを作成する。
  2. scam.m をMATLAB上で実行する。


実行ファイルの一部:

clc;
fname = 'goSCAM.cir';
if ~exist( 'v_1' )
scam
end;

pretty(v_4);
[n4,d4] = numden(v_4);
nc4 = collect(collect(collect(collect(n4,Cs1), Cs2), Cx1), Cx2);
dc4 = collect(collect(collect(collect(d4,Cs1), Cs2), Cx1), Cx2);
V4 = collect(collect(collect(collect(v_4,Cs1), Cs2), Cx1), Cx2);
dV4dCx1 = diff(V4, Cx1);


% numerical evaluation
cp = 1e-12; cg = 20e-12; vd1 = 1; vd2 = vd1; vc = 0;
cx1s = logspace(-13,-11,15); cx2 = 5e-12;
cs1s = logspace(-13,-11,15); cs2 = 10e-12;
for i = 1:length(cx1s)
cx1 = cx1s(i);
dV4dCx1n= subs(dV4dCx1, {Cp,Cg,Vd1,Vd2,Vc,Cx1,Cx2,Cs1,Cs2},{cp,cg,vd1,vd2,vc,cx1,cx2,cs1,cs2});

figure(1);
loglog(cx1s(i), dV4dCx1n, 'or'); grid on; hold on;
ylabel('\partial V_{tm}/\partial C_{x1}');
xlabel('Primary Capacitance, C_{x1} [F]');
end
print -depsc2 sensitivity_plot.eps


% ---------------
% V_4
% ---------------
if 1==1
rname = fname;
i = find( rname == '.' );
if ~isempty(i),
rname = rname(1:i(1)-1);
end;
tname = [rname '_v4.tex' ];

fid = fopen( tname, 'w' );
fprintf( fid, '\\documentclass[12pt]{article}\n' );
fprintf( fid, '\\pagestyle{empty}\n' );
fprintf( fid, '\\begin{document}\n\n' );
fprintf( fid, '${\\bf{\\it V4} = \\frac { \n' );
fprintf( fid, '%s', latex(nc4) );
fprintf( fid, '\n}{\n' );
fprintf( fid, '%s', latex(dc4) );
fprintf( fid, '\n}\n}$\n\n\\end{document}\n' );
fclose( fid );
end

%%% EOF %%%

SCAMのいいところはsymbolicに式を評価してくれるところなのだが、MATLAB上ではかなり読みにくい。特に複雑な数式は。latex.mを使うと、latex形式で(latexソースに貼付けられるように)出力してくれる。。。というコマンドがあるということを今日知った。


また、numden.mを使うと分数から分母と分子を取り出し、tf.mで伝達関数を作り、stepやbodeなどで評価できる。。。。のもいい。なんでこんな便利な関数を知らなかったんだろぅ。
要するにこんな感じ。

R1 = 100; R2 = 200; C1 = 1e-6; C2 = 5e-6;
[num, den] = numden(eval(v_3/Vin));
sys = tf(sym2poly(num), sym2poly(den));

figure(1); clf;
subplot(211); step(sys);
subplot(212); bodemag(sys);

こんな回路ファイルを作ってから動かす (sample.cir)。

Vin 1 0 s
R1 1 2 s
R2 1 3 s
C1 3 0 s
C2 2 0 s