Analyzing Circuits with SPICE on Linux
The options lines shown here:
.opt nopage .width in=72 .width out=80
specify that no page breaks will be in the generated output text and the line length of the input and output text. The first line below instructs SPICE to perform a DC analysis of the circuit where VIN is run from -0.15V to 0.15V in 0.010V increments. The next line tells SPICE to generate operating point information for the circuit. It is this line that reports the transconductances of each transistor.
.dc vin -0.15 0.15 0.010 .op
The middle half of the input file describes the connection of circuit elements and sources. In SPICE connection points are termed nodes. Every SPICE circuit must contain a ground node numbered zero.
iee 3 vee 1m
iee is a current source, known to SPICE by the leading i, where 1mA of current flows from node 3 toward the vee node.
vin input 0 0 sin(0 0.3 5meg) ac 1
vin is the input voltage source connected at the positive end to the input with the negative end connected to ground. The DC value of the input voltage is 0V. The time varying—“transient” in SPICE jargon—portion of the input voltage is a sine wave centered at 0V with an amplitude of 0.3V oscillating at a frequency of 5MHz. The AC portion of the input will be normalized to 1V. That is, the AC analysis in SPICE doesn't exercise large signal behavior of the circuit. Digital behavior is the extreme of large signal behavior. Large signal performance of a circuit can be simulated using transient analysis in SPICE.
rl1 out1 vcc 5k
rl1 is a resistor connected between the out1 and vcc nodes with a value of 5k.
q1 out1 1 3 bjt
q1 is a bjt model instance with collector, base and emitter connections at nodes out1, 1 and 3 respectively. The model definition below names a model bjt of the type npn and with specific parameters. SPICE knows about npn transistors and a number of other types of circuit elements. This definition makes use of line continuation.
.model bjt npn(bf=80 rb=100 ccs=2pf + tf=0.3ns tr=6ns cje=3pf cjc=2pf + va=50)
The .plot line in Listing 1 tells SPICE to plot the voltage values at nodes out1 and out2 calculated during the DC analysis.
This command will run a SPICE analysis using the input file from Listing 1 named diffpair-1.cir:
spice3 -b diffpair-1.cir
The -b option causes SPICE to run in batch mode. Listing 2 shows the output of the SPICE analysis. The operating point information gives the DC bias voltages for all the nodes in the circuit and the current through every voltage source. A customary trick to measure current is to insert a voltage source whose voltage is zero. This does not hinder simulated performance, but the circuit will simulate slightly slower, since there's more in it. Model parameters are reported for each type of circuit model used in the simulation. Operating characteristics for the two bjt instances show the transconductance of each transistor to be 0.0191 A/V.
The DC transfer characteristic plot shows output voltage, from left to right, ranging over 0V to 5V and shows the input voltage, from top to bottom, ranging from -0.15V to +0.15V. Each time I view a plot like this I get nostalgic, recalling the days when I would flip switches on the front panel of a PDP-8 and load boot programs from paper tape. But that's another story. This plot certainly isn't fancy by today's standards, but it does convey the necessary information.
Finally, SPICE reports that the simulation took just under 1/10th of a second to run.
Listing 3 adds more analysis directives to the SPICE input file from Listing 1. The analyses to be performed are DC, transfer function, AC and transient. This time we'll start SPICE with an option to save the resulting data to a file, like so:
spice3 -b -r diffpair-2.raw diffpair-2.cir
After running SPICE the data file can be perused with nutmeg, which comes with the SPICE package. Start nutmeg simply by running
Listing 4 captures an interactive session with nutmeg. At the first nutmeg prompt I entered setplot to give me the names of analysis data sets within the raw data file. I selected dc1 and then plotted the output voltages, shown in Figure 2. When plotted in this way, you can zoom in on sections of the plot using the right mouse button to define the zoomed plot's borders. You can also find the coordinates of any point on the plot or pair of points by either clicking or clicking and dragging the left mouse button. The coordinates display in the window where nutmeg runs. You can put labels on the plot by typing with the keyboard. By setting the type of hard copy device appropriately you can save the plot in a file for later printing, as shown in nutmeg commands 3 through 5.
Next, in Listing 4, I selected the transfer function data. The display command shows me the variables I can query from the transfer function data. At prompt number 9 I asked to see the transfer function that SPICE reports to be 44.60971. This differs somewhat from the idealized gain equation, which neglects output resistance of the bjt.
Rather than just selecting one variable for display, at prompt number 10 I selected all variables for display. This shows the output impedance to be about.
, and the input impedance to be about
Nutmeg prompts 11 through 16 create the AC analysis plots of voltage phase at node out2 in degrees, voltage phase at node out2 in radians and magnitude of the voltage at node out2. These are shown in Figures 3 through 5, respectively.
Prompts 17 through 19 resulted in Figure 6, which shows the input voltage sine wave and the output voltage sine wave. Notice that the output voltage shows distortion of an over-driven amplifier.
At prompt 20 I ran a Fourier analysis on the time domain voltage to find the harmonic content of the distorted sine wave. I had to specify the fundamental frequency as 5MHz, the same as was given in the input file, and the node voltage the Fourier analysis should inspect. As expected from the compressed shape of the output sine wave, the total harmonic distortion (THD) is quite high.
Free DevOps eBooks, Videos, and more!
Regardless of where you are in your DevOps process, Linux Journal can help!
We offer here the DEFINITIVE DevOps for Dummies, a mobile Application Development Primer, and advice & help from the expert sources like:
- Linux Journal
- Synchronize Your Life with ownCloud
- Days Between Dates?
- A GUI for Your CLI?
- Connecting Apache's Web Server to Multiple Instances of Tomcat
- Tech Tip: Really Simple HTTP Server with Python
- Cooking with Linux - Serious Cool, Sysadmin Style!
- An Introduction to OpenGL Programming
- RSS Feeds
- The Only Mac I Use
- Ubuntu & SUSE & CentOS, Oh My!