LTspice Tips - Plot Manually Entered Functions

How to Manually Enter/Edit Functions in the LTspice WaveForm Viewer

The LTspice WaveForm Viewer is able to utilize a host of built in mathematical functions for plotting. The list is similar to those of the BI and BV arbitrary sources, but with a few differences. The documentation for the WaveForm Viewer built in to LTspice is quite good. Searching the LTspice help for “Viewer Overview” is a good place to start if more information is desired, but below are the basics for how to plot a function from scratch or how to edit an existing trace expression.

Here a simple example circuit will be used with a 1V sinusoidal source at 1 kHz (with a phase shift of 90 degrees) and a 1 kOhm load resistor. The net not connected to GND is labeled as sin for convenience. A transient simulation will is done to 10 ms and the maximum time step allowed is 10 ns to force more data points and smoother traces.

Simple_Circuit

Manually Add Traces

To add a trace or traces manually, after running the simulation, right click somewhere inside the plot pane and select the “Add Traces” option.

Add_Traces

A menu will appear that includes all available data sources for the simulation and a text box to enter a trace expression or expressions in to. In this case, two identical (one will be modified later) plots of V(sin) are entered by either typing in the expression or by clicking on V(sin) in the window twice.

Add_Traces_2

Click the OK button and the following should be shown.

Now the second V(sin) trace will be edited with some additional mathematics.

Manually Edit an Existing Trace

By hovering over the name of a trace that is already plotted (a small finger icon will appear) and right clicking that trace can be edited. A box will appear that will allow the trace expression to be manually modified, the assigned color changed, a cursor or cursors attached, or for it to be deleted completely from the plot pane. Here the expression is modified by taking the absolute value of it, and then dividing this by the square root of two.

Edit_Trace

Which results in the following plot output.

Add Additional Plot Panes

Adding another pane essentially means adding another plot with a separate vertical axis and optionally separate horizontal axis. This can be useful when plots become cluttered or have traces that are at drastically different scales from each other. To add a pane, right click somewhere inside an existing plot pane and select the “Add Plot Pane” option. In the below case, this was done twice.

Also note that traces can be quickly dragged from one plot pane to another if desired (left-click a trace name and hold, drag and drop).

More Complex Examples

Alright, now how about something arbitrarily complicated? Sort of a Rube Goldberg for trace expressions… Let’s say the goal is to plot a single expression that is always the maximum of either the original V(sin) function or the already modified version of it… and then to take that and scale it by a piece-wise function that’s an increasing exponential (of reasonable timescale) for the first half (5 ms) of the simulation and a mirrored decreasing exponential for the second half. Also, let’s normalize that exponential function so that it’s maximum value at 5 ms is one and not some silly arbitrary value. I wouldn’t want to give you an example that was arbitrarily silly in any way (yes, I am being sarcastic).

Can it be done? Sure!

Edit_Trace_2

The center plot above is just showing the individual pieces for convenience. It’s left as an exercise for the reader to figure out how the above math works.

If more practical (really, I promise) examples are desired. Take a look at the previous article where instantaneous power is calculated by multiplying the voltage and current of a capacitor and where some simple unit corrections are done, or take a look at the next article for an example where the efficiency of a switching power supply at various load currents is estimated.

Comments

Any questions or comments please go to our TechForum: TechForum