Monday, October 16, 2006

Transient Analysis and Using .wav Files for I/O

OK, this is cool. If you want to see what something would sound like through your circuit, you can create a voltage source that will use a .wav file to determine its value as a function of time, hook it up to the input, and then write a .wav file as output. This post should take you all the way through. I also recently switched from a Linux/Windows dual boot to a Windowless system, so I may touch from time to time on running LTspice in Linux under WINE.

Setting up a wav-controlled voltage source is as simple as right-clicking on the value of the source ('V' by default), and changing it to
wavefile="path\to\inputfile.wav" chan=N
where path\to\inputfile.wav is the path to the file and N is the channel that you want to use, starting from 0. So N would be 0 if you want to use the left channel of a stereo file. In Windows, you can just put the filename or a relative path and LTspice will look in the directory of the top-level schematic. In WINE this doesn't seem to work so well, and you need to put an absolute path.

To create an output .wav file, you'll need the .WAVE SPICE directive. To create a 16-bit file sampled at 44100 samples per second, you would place this:
.wave "path\to\outputfile.wav" 16 44100 NODE
Again, path\to\outputfile.wav is where you want to write the file, overwriting any other file with the same name. NODE is the name of the node that you want to use as the output.

For this simulation you'll want to use the Transient simulation type. This is the mode that simulates and plots your circuit over time. To use it, open the "Simulate" menu and choose "Edit Simulation Command." Select the leftmost tab, called "Transient," and fill in the start and end times. As far as I know, you should be able to leave the step time blank, and LTspice will figure it out.

After running the simulation, you should have an output .wav file that you can listen to in your favorite media player. Enjoy.

Tuesday, October 10, 2006

Fun with Sources

LTspice has several built-in dependant sources and behavioural models. The basic ones are named e, g, h, f, bv, and bi. We'll also go over some more interesting ways to use the basic voltage and current sources.
h - Current Dependant Voltage Source
f - Current Dependant Current Source
e - Voltage Dependant Voltage Source
g - Voltage Dependant Current Source
bv - Behavioral Voltage Source
bi - Behavioral Current Source

Here I'll be adressing the last four.

The voltage-dependant sources are very simple to use. Just right click on the component and change "Value" to whatever you want your gain to be. By using a negative number you can invert the input voltage.

The behavioral sources are a little more complicated, but still very simple once you know the syntax. The first thing you'll notice is that they only have the 2 terminals of their simple-source counterparts. This is because all of the behavioral modeling is done by combining arbitrary voltages and currents in a mathematical expression that you place in the "Value" field (after right-clicking on the part). This is another place where labeling nodes can be very helpful(Check the tutorial on subcircuits for more details). Lets say you have 2 nodes called V1 and V2. The expression
V=2*V(V1)+V(V2)
will cause the voltage sourced by the behavioral source to be the sum of V2 and twice V1. You can also use a whole host of mathematical functions, as well as creating a function of time. For a full list consult the help file under LTspice-->Circuit Elements-->B. Arbitrary behavioral voltage or current sources.

Now back to the basic sources we used before. It's very easy to modify those same DC sources to be used as AC. Just right-click on the source, then click the "Advanced" button. You can then set all kinds of parameters for the source.

Adding Downloaded Models

LTspice comes with lots of nifty circuit components, but there will be many times that you want to simulate a part that isn't in the default libraries. LTspice makes this very easy for the vast majority of models that you will get from vendors.

We'll use the National LF411 opamp as an example. Go here and download LF411.MOD to your schematic directory to make the part available only to that schematic, or place it in C:\Program Files\LTC\SwCADIII\lib\sub to make it available to any schematic in the future. Open it up in a text file and look for a line that says
.SUBCKT LF411/NS 1 2 99 50 28
This line tells SPICE that the name of this object is LF411/NS, and also describes the what each pin means. We'll need this name to reference the part later on. Back in LTspice, press 's' to insert a SPICE directive. In the textbox type
.include LF411.MOD
This tells SPICE to treat that file as a library that contains model definitions. Then insert the part called opamp2. We'll use this as the symbol for the model we just imported. Right click on the opamp and change the Value to LF411/NS (Remember I told you that name would be important?). There could just as well be several different models within a single library file, each with a unique model name.

And that's all there is to using models!

Subcircuits

There seem to be two different ways to accomplish subcircuits in LTspice. One of them is meant for creating models of components that might be used in many of your circuits. The other is for modularizing a particular project into smaller blocks, and wiring the blocks together. I'll be covering the latter.

Subcircuit blocks are made up of two pieces: The symbol used to represent the subcircuit, and the schematic of the subcircuit itself. Each piece is a separate LTspice file, and they must have the same filename, differing only in the 3 letter exension (.asc for the schematic and .asy for the symbol).

Create the subcircuit in the same way you would create any other schematic, except that anywhere you want to create a place to connect to your subcircuit from the outside world, leave a wire hanging on one end. Like this:



Then right-click on the schematic and choose "Label Net." This allows you to name a node whatever you want, so that instead of N001 it might be called Vout. If you want a nice little directional box around your label you can choose input or output from the "Port Type" menu. Place labels on any unconnected wires that you want to make into ports. Now save the schematic somewhere where you'll find it.

To create the symbol for the subcircuit, click "File," then "New Symbol." The important functional parts of a symbol are the Pins or Ports. Press 'p' to create a new pin/port. Give the port a name that matches one of the nodes that you named on your subcircuit schematic. The pin label positions will vary depending on how you want to lay out your circuit. You can use the graphics drawing functions under the "Draw" menu to make some sort of representative graphic for your subcircuit.

Now just save the symbol, making sure to use the same name that you used for the schematic, and in the same directory.

That's it. You now have a subcircuit that you can use just like any other component. To insert your subcircuit into another schematic, simply press 'F2' to bring up the "Select Component Symbol" dialog, and then in the "Top Directory" drop-down menu select the folder that contains the subcircuit. The folder that it lets you choose is whatever directory the current circuit is saved in, so make sure that the top-level circuit, the subcircuit schematic, and the subcircuit symbol are all be in the same directory.

Later I'll cover more advanced subcircuit techniques like passing parameters down to subcircuits.

Parametric Sweeps and Graphs

So getting voltages and currents is all well and good, but you can do that by hand for most of these basic circuits. With the magic of computers we can calculate those same numbers but do it for a bunch of different values of some parameter, like a particular resistance.

To do this is pretty straightforward, but requires the use of a spice directive. I think you can handle it. I'll use a resistor as an example, but the idea should generalize. Right-click on the resistor, and for its resistance put "{R}" (Without quotes) and click OK. Press 's' to insert a SPICE directive. In the text box type ".step param R 1k 10k .1k". This tells SPICE to step the parameter R from 1k to 10k in steps of .1k. When you click OK, you will be able to place the SPICE directive anywhere on your schematic. The placement of the directive doesn't have any effect on how the simulation runs, so just put it somewhere where you can see it.

When you run the simulation the screen will split and you will see a blank graph on top and your circuit on the bottom. If you click on a node or component you will see its graph show up on the screen, autoscaled. Sometimes the autoscaling gets a little wonky, but never fear and just click on the graph and press CTRL+y. Clicking more nodes or components will plot them on the same graph, and clicking twice will plot that node by itself. To delete a single plot, right click on its name on the top of the graph and click the "Delete this Trace" button.

You can also graph mathematical functions of the voltages and currents in your circuit. For instance, to graph the power through a resistor R2 that connects nodes N001 and N002, click on the graph and press CTRL+a. In the "Expression" textbox, type (V(n001)-V(n002))*I(R2) and press OK. LTspice will even realize to plot the function in Watts!

Lastly, here's a little trick for plotting superpositions of a circuit: build the circuit, then copy and paste it so that you have as many copies as you do sources. Then delete all the sources but one from each circuit so that each copy has a different source. Leave the holes from current sources (open circuit) and connect the holes for voltage sources (short circuit). Then when you run your simulation, you can see the contribution of each source individually. It's a bit of a hack, but it makes it easy to graph them all at the same time.

OK, That should pretty much get you through Lab 1. Coming up next are subcircuits and dependant sources (they're easy!)

Basic Circuit Capture and Simulation

Adding elements in LTspice is fairly straightforward. Pressing r, l, c, d, or g will change your cursor into a resistor, inductor, capacitor, diode, or ground icon, respectively. You can rotate the cursor by pressing CTRL+r, and place the components with your trusty left mouse button. Right clicking or pressing esc will get you back to the default mode.

F2 brings up a menu of less common components, and I'll bring them up as they become useful. The first one you'll probably use is a voltage source. Bring up the component menu, and type "voltage" into the text box to select the component. Pressing ENTER will let you place as many voltage sources as you like.

Once the components are placed, right-click on the component icon to change its parameters. You can use SI prefixes like k, m, u, etc. as usual, except that you use MEG for mega.

To delete a component, first press the 'delete' key and your cursor will change into a pair of scissors. Clicking on a component then removes it. There are two different ways to move a component, called "drag" and "move." The difference is that "drag" pulls any wires connected to the component along with it, while "move" breaks the connections. Press 'F8' to drag components, and 'F7' to move them.

F3 lets you start running wires between components, which will come in handy. Holding down 'ctrl' lets you put diagonal wires while still snapping the endpoints to the grid. As in Pspice, you'll need a ground connection to be able to run a simulation.

Speaking of simulation, click on the little running man icon on top to bring up the simulation menu. For DC circuits you'll want to click on the "DC op pnt" tab and click OK. This will find out the steady state of your circuit. The first thing you'll see is a text list of the voltages at all the nodes and the currents through each component. It's not immediately obvious which node is which, so if you close the text window you can just let your mouse hover over a node or component and see the pertinent info on the status bar at the bottom.

A note about currents: Watch your signs! When you hover your mouse over a component, the current is taken to be positive if it is running down or right, and negative if it is running up or left.

Next time I'll go into parametric sweeps. Fun.

Former Pspice user, meet LTspice

I'm an electrical engineering student at Columbia University, and we're supposed to use Pspice for our intro Circuits labs. I tried to give it a chance, but just couldn't take the constant crashes and bugs. It seems that not everyone has had as much trouble as I have, so maybe it just doesn't like me, but in any case, I found LTspice and it's been smooth sailing since.

I've had to do some reading and research on my own to figure out how to do the labs using LTSpice, so I figured I'd share what I found to make it easier for others who want to follow. Maybe if we get enough people using LTspice they'll start using it in the labs.

Download LTspice from here (click on LTSPICE/SWITCHERCADIII) and install in the normal windows way. If you're a Linux user, LTspice works fine under WINE.

Let me know if you have problems and I'll try to help, but I'm just learning this software too, so you might also want to sign up at this yahoo message group to ask more questions.