[?] Subscribe To This Site

XML RSS





leftimage for poohprod.ru

Circuit Simulator (WinSpice and Matlab, improved interface)

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


The concept of the Matlab driver is illustrated by simulating a very simple RC low-pass filter. In this example we're using Matlab ver. 7.0.1 and WinSpice3 ver. 1.04.07.

A first approach to the circuit driver was presented here.

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 optimization algorithms and graphical possibilities not included in the circuit simulator environment itself. The approach developed now is very flexible and utilizes the inherent capability of this SPICE simulator to include other circuit text files with parameters, 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. We separate the components of interest and include them in a different .cir file (using the .include directive in the main file).

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

Third, we now generate a version of the .cir file that includes parameters using Matlab capabilities for manipulating strings. We then run the circuit simulator from the command line.

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 simple RC circuit in WinSpice:

RC low pass filter in our circuit simulator

We initially use these values:

R = 10k ohms, C = 1 nF

The main .cir file (which is also a text file) is:

---- RC - LP filter ----

V1 Vin 0 dc 0 ac 1

.include param.cir
R vin out {RES1}
C out 0 {CAP1}

.control
AC DEC 10 10 1MEGHZ
plot vdb(out)
.endc
.end

The actual parameters are located in a separate file named 'param.cir', which in this case only contains one line:

.param RES1=10e3 CAP1=1e-009

As expected, the AC analysis delivers this result (the cutoff frequency is about 16 KHz):

response of the RC filter


Second Step:

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

The control block changes into:

.control
AC DEC 10 10 1MEGHZ
write RC_LP_filter_ac_out.csv vdb(out)
quit
.endc
.end


Third Step:

We now create a function (to be run by Matlab) that can reproduce the secondary .cir file (including the parameters), 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] = WS_RC_low_pass(x)
% Create and save the file 'param.cir' to be included in
% another file to be run by WinSpice3.
% The built-in function 'num2str' is used to modify parameters
% wherever it is convenient.

p = ['.param RES1=' num2str(x(1)) ' CAP1=' num2str(x(2))];
dlmwrite(
'param.cir', p, 'delimiter','');

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

! wspice3 RC_LP_filter.cir

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

[Rac] = getWSpicedata2('RC_LP_filter');

Compare the simplicity of this code against the previously developed code.
We eliminated the need to re-build the complete '.cir' file with Matlab, and we concentrate only on the relevant parameters.


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

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

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



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

clear; clc; close all

% We try some initial components
R1 = 10e3;
C1 = 1e-9;
x = [R1 C1];
[Ra] = WS_RC_low_pass(x);

figure(1)
semilogx(Ra{1},Ra{2})
grid
on

% We try another cutoff frequency by modifying the resistor
R1 = 1e3;
x = [R1 C1];
[Ra] = WS_RC_low_pass(x);

figure(2)
semilogx(Ra{1},Ra{2})
grid
on


For R1 = 10k and C1 = 1 nF, the obtained result is:

RC low pass filter


For R1 = 1k and C1 = 1 nF, the obtained result is:

RC filter response after modification

As before, we can now use WinSpice as if it was another built-in Matlab function! This means that we can work through circuit optimization using functions such as 'fminbnd' or 'fminsearch'...

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


Reference:
/ 2014.




Electronic Experiments Creative Electronics Electrical Calculations

From 'Circuit Simulator' to home

From 'Circuit Simulator' to Matlab Programming

Top


footer for circuit simulator page































































Related pages


hexadecimal binary tablecombinational logic circuitsfibonacci constantmatlab gui tutorial for beginnersanimation using matlabascii binary tablepolar coordinates conversionc program to find adjoint of a matrixmatlab defining functionsquadratic calculator programmatlab function declarationhexa to binary convertereir calculationsolve function matlabsawtooth fourierdefine rc circuitcramers ruleshow to create an amortization tablesalvage value calculationmatlab solve linear equationsmatlab handleintegration calcnormal distribution plot matlabnodal analysis examplesconversion from binary to octalgraphing piecewise functionsscilab vs matlabgold band resistornominal interest rate exampleseuclidean algorithmscorrecting power factor using capacitorsmatlab axis labelharmonic seriesgauss jordan matricesfit straight line matlabhow to find annual depreciation ratemaclaurin series expansion calculatorsimpson's rule exampleinterest calculator continuouslyalgorithm of bisection methodzy smith chart color pdfmatlab gui animationhow to generate random numbers in matlabmatlab interp3ascii conversion tablebinary to hexadecimal conversion formulataylor series cosxsum of harmonic series formulamatlab finance tutorialpolyfit polyvalevaluate double integral calculatorwriting a function in matlabsquare feet to inches calculatormultiple regression in matlabgeomean formulakcl and kvl solved problemsadmittance chartgray code to binary conversioncapacitor time constant calculatorcombinational logic circuitsalvage value and depreciationplot 3d matlabgold band on resistorhexadecimal to decimal conversion tablefit straight line matlabhow to convert from cartesian to polarnum2str matlabmatlab fplotplot graph on matlabfor and while loops matlabhow do you convert binary to hexadecimalquadratic formula calculator programtrendline matlabdraw a circle in matlabmatlab plot pdf