[?] Subscribe To This Site


leftimage for poohprod.ru

WinSpice and Matlab: automation of electronic simulations

In this article, a Matlab routine to drive the WinSpice circuit simulator is
presented. This method can be easily adapted to simulate and drive any circuit entered in this SPICE version.

The performance of the Matlab driver is illustrated by simulating a typical amplifier circuit using some resistors, capacitors and a transistor. In this example we're using Matlab ver. 7.0.1 and WinSpice3 ver. 1.04.07.

For more information about Matlab, visit mathworks.com.

For more information about WinSpice, visit winspice.com.

SPICE stands for Simulation Program with Integrated Circuit Emphasis

Interchanging information between Matlab and WinSpice allows the implementation of mathematical or optimization algorithms and graphical possibilities not included in the circuit simulator environment itself. The approach developed here is very flexible and utilizes the inherent capability of this SPICE simulator to execute text files and print data to external .csv (comma separated values) files.

First, the circuit in Spice is defined (.cir file). This circuit is simulated and tested within the WinSpice environment before any attempt is made to interact with Matlab.

Second, we prepare the output of the simulator by writing necessary data to an external file, that is, we include 'write' commands in the .cir file, so that the results are saved as external (.csv) text files available to Matlab.

Third, we now generate a new version of the .cir file using Matlab capabilities for manipulating cell-arrays (string matrices), we then insert a Matlab statement to run the Spice simulator from the command line with the .cir filename as a parameter.

After the simulation, we can read the results in the .csv files, process them and act accordingly.

First Step:

We create, test and simulate this circuit in WinSpice:

amplifier being run with Winspice

We use these values:

R1 = 10k ohms, R2 = 5k ohms, R3 = 1k ohm,
R4 = 50 ohms, R5 = 1k ohm, R6 = 3k ohms

C1 = C2 = C3 = 10 uF

Q1 = transistor 2N2222

V1(ac) = 0.1V amplitude 1k Hz
V2(dc) = 30 V

The .cir file (text file) needed is:

--- Simple amplifier with Transistor ----

V1 Vin 0 dc 0 ac 1 sin(0 0.1 1e3)
V2 Vcc 0 dc 30

R1 Vcc b 10e3
R2 b 0 5e3
R3 Vcc c 1ek
R4 e Vr 50
R5 Vr 0 1e3
R6 Vout 0 3e3

C1 c Vout 10e-6
C2 Vr 0 10e-6
C3 b Vin 10e-6

Q1 c b e Q2N2222

+(IS=3.108E-15 XTI=3 EG=1.11 VAF=131.5 BF=217.5
+ NE=1.541 ISE=190.7E-15 IKF=1.296 XTB=1.5 BR=6.18
+ NC=2 ISC=0 IKR=0 RC=1 CJC=14.57E-12 VJC=.75
+ MJC=.3333 FC=.5 CJE=26.08E-12 VJE=.75 MJE=.3333
+ TR=51.35E-9 TF=451E-12 ITF=.1 VTF=10 XTF=2)

AC DEC 10 10 10MEGHZ
plot vdb(Vout)

TRAN 10E-6 5E-3
plot v(Vout)

The AC and Transient analysis deliver these results:

AC results for the amplifier simulated in WinSpice

transient analysis delivered by the amplifier simulated by Winspice

Second Step:

We include appropriate 'write' commands in the .cir file, so that the results are saved as .csv files available to Matlab.

The control block changes into:

AC DEC 10 10 10MEGHZ
write f_ac_out.csv vdb(Vout)

TRAN 10E-6 5E-3
write f_tran_out.csv v(Vout)

Third Step:

We now create a function (to be run by Matlab) that can reproduce the above .cir file (simulation of the amplifier), modify relevant parameters with the command 'num2str' (which changes numbers into strings), and launch the circuit simulator. This is the full code that accomplishes it:

function [Rac Rtran] = spice_amp(x)
% Create the file .cir to be run by WinSpice3.
% Built-in function 'num2str' is used to modify parameters
% wherever it is convenient.

b{1} = ['-------------Simple amplifier with Transistor----------- '];
b{2} = [
' '];
b{3} = [
' '];
b{4} = [
'V1 Vin 0 dc 0 ac 1 sin(0 0.1 1e3) '];
b{5} = [
'V2 Vcc 0 dc 30 '];
b{6} = [
' '];
b{7} = [
'R1 Vcc b 10e3 '];
b{8} = [
'R2 b 0 5e3 '];
b{9} = [
'R3 Vcc c ' num2str(x(1))];
b{10} = [
'R4 e Vr ' num2str(x(2))];
b{11} = [
'R5 Vr 0 1e3 '];
b{12} = [
'R6 Vout 0 3e3 '];
b{13} = [
' '];
b{14} = [
'C1 c Vout 10e-6 '];
b{15} = [
'C2 Vr 0 10e-6 '];
b{16} = [
'C3 b Vin 10e-6 '];
b{17} = [
' '];
b{18} = [
'Q1 c b e Q2N2222 '];
b{19} = [
' '];
b{20} = [
'.MODEL Q2N2222 NPN '];
b{21} = [
'+(IS=3.108E-15 XTI=3 EG=1.11 VAF=131.5 BF=217.5 '];
b{22} = [
'+ NE=1.541 ISE=190.7E-15 IKF=1.296 XTB=1.5 BR=6.18 '];
b{23} = [
'+ NC=2 ISC=0 IKR=0 RC=1 CJC=14.57E-12 VJC=.75 '];
b{24} = [
'+ MJC=.3333 FC=.5 CJE=26.08E-12 VJE=.75 MJE=.3333 '];
b{25} = [
'+ TR=51.35E-9 TF=451E-12 ITF=.1 VTF=10 XTF=2) '];
b{26} = [
' '];
b{27} = [
'.control '];
b{28} = [
'AC DEC 10 10 10MEGHZ '];
b{29} = [
'write amp_ac_out.csv vdb(Vout) '];
b{30} = [
' '];
b{31} = [
'TRAN 10E-6 5E-3 '];
b{32} = [
'write amp_tran_out.csv v(Vout) '];
b{33} = [
'quit '];
b{34} = [
'.endc '];
b{35} = [
'.end '];
b{36} = [
' '];

% Save file .cir line-by-line
[r,c] = size(b);
fp = fopen(
'amplifier.cir', 'w+');
for i = 1 : c
'%s\n', b{i});

% Run WinSpice
% Make sure wspice3.exe and .cir files are available in this directory

! wspice3 amplifier.cir

% Read data saved in .csv files
% (assuming name termination _ac_out.csv and _tran_out.csv)

[Rac Rtran] = getWSpicedata('amp');

This is an important portion of additional code to read the .csv files.

function [Rac Rtran] = getWSpicedata(WSpiceBaseFile)
% Read csv files generated by a 'write' in a 'WSpiceBaseFile' (.cir file)
% Rac{1} = Frequency Points
% Rac{2} = AC response
% Rtran{1} = Time Points
% Rtran{2} = Tran response

ac = fopen([WSpiceBaseFile '_ac_out.csv']);
Rac = textscan(ac,
'%f %*f %f %*f', 'delimiter',',','headerLines', 1);

tran = fopen([WSpiceBaseFile '_tran_out.csv']);
Rtran = textscan(tran,
'%f %f', 'delimiter',',','headerLines', 1);


Now, we're ready to perform some simulations driving WinSpice from Matlab.

clear; clc; close all

% We try some initial resistors
R3 = 1e3;
R4 = 50;
x = [R3 R4];

% Matlab launches the simulator including our parameters
[Ra Rt] = spice_amplifier(x);


% We try another gain modifying one of the resistors
R3 = 500;
x = [R3 R4];
[Ra Rt] = spice_amplifier(x);


For R3 = 1k and R4 = 50, the obtained results are:

winspice simulation driven by matlab

Winspice simulation started by Matlab

For R3 = 500 and R4 = 50, the obtained results are:

another simulation of the amplifier launched by Matlab

transient analysis driven bt Matlab

We can now use WinSpice as if it was another built-in Matlab function!
This means that we can work through circuit optimization using Matlab...

We can try some numbers in our design, we can see the results and use Matlab to modify relevant values in the circuit, in iterations.

Very powerful
, isn't it?


/ 2014.

See a variation of this technique

From 'WSpice' to home

From 'WinSpice' to Matlab Programming




footer for winspice page

Related pages

smith chart problemsbinary to decimal conversion programhow do you convert hexadecimal to binarycalc integralmatlab surface plotiteration method in matlabmatlab beginnerssmith chart tutorial pdfhow to convert grey code to binarymaclaurin series sin x 2matlab gui tutorial for beginners101100 binarymatlab gui image processingmatlab fzerosolving problems with percents calculatorscilab plot colorsum of alternating harmonic seriesmatlab 3d matrixmatlab plot3lagrange polynomial interpolation matlabconverting binary to decimal examplesharmonic series sum to n termsdefine piecewise functionfourier series in matlab coderc cktexamples of decimal to binary conversionbell shaped curve generatorohm calcpythagorean theorem calculator right trianglefeval matlabdisplay function in matlabpermutations calcscilab to matlab conversionmotorcycle depreciation calculatorboxplot in matlabpermutations calcdouble integral calculator onlinesummation of trigonometric seriesconvert from ascii to charexponential decay calculatorsolids of revolution examplesgraphing a piecewise defined functiongcd codematlab determinantletter a in asciisvd in matlabmclaurin series calculatorisnan matlabhow to solve integrals in matlabbinary hex tableradioactive half life calculatorammoritization tablematlab regressmatlab permutationdefine functions in matlabintegral with limits calculatortaylor series online calculatorsvd matlab codecalculating angles between vectorseuclidean algorithm to find gcdmatlab tablesdepreciation rate laptopsimple interest to compound interest converterhow to calculate the salvage valuematlab defining functionsmatlab convert cell array to stringheaviside step functionsrecursion formula calculatorelectricity cost calculator wattssolving a system of equations in matlab