# How to plot sliding average in ltspice?

I would like to plot a sliding average of some voltages and currents. Ideally I would just add a new trace with some expression, but all expressions that I found so far only operate on single values.

Are there any expressions (like absdelay for B sources) that would do the trick?

• In microcap, the transient analysis machine allows you to display (say) "v(V1)" and "avg(v(V1))" - look for something like this. – Andy aka Nov 28 '14 at 19:34
• @Andyaka: I don't think that I want to spend that much for a hobby. – PlasmaHH Nov 28 '14 at 20:40
• Dude I said - "look for something LIKE this" – Andy aka Nov 28 '14 at 22:10

Summary
To plot the the moving (sliding) average use .MEAS,.PARAM and .STEP LTSpice directives (see the detailed explanation below). As a quick partial solution, use zoom in and Ctrl+Click on a plot title to show the average value (only a single value, not plot) for the selected abscissa range.

Solution. Plotting moving average for a signal

Suppose there is a following set-up and one needs to know the moving average of V(out):

Step 1: create the directive

Create the following SPICE directive (Edit -> Spice Directive):

.param t=0
.step param t 100n 900n 100n
.param S=100n
.meas tran Moving_Average avg(v(out)) TRIG time VAL=n-S/2 TARG time VAL=n+S/2


Comment for the directive:

1st line: define a time variable t.
2nd line: Step t from 100ns to 900 ns with the step 100ns.
3rd line: Set the moving average span: 100 ns.
4th line: Syntax: Moving_Average - the name of the newly created variable to be calculated (put here whatever you like).
TRIG time VAL=t-S/2 - start of averaging.
TARG time VAL=t+S/2 - end of averaging.
E.g. if t=300 ns, averaging spans from 250 ns till 350 ns (300 +/- 100/2).

Step 2: run simulation, open the log file and plot the moving average

Run simulation

Open the Spice Error Log (View -> Spice Error Log), right click on any place and select Plot Stepped Measured Data

See the moving average plotted

Quick partial solution (see average value for a specified time range)

Suppose one has a chart like this:

And wishes to calculate an average value during [0.7us,0.8us].

Step 1: Specify the time range.

Double click on the abscissa axis and specify the needed range. Alternatively use Zoom to Rectangle tool (magnifier button in the top bar).

Step 2: Calculate the average

Ctrl + left mouse click on a chart title (bold green title V(out) in the picture) to see the average value for the specified range.

Most variants of spice will allow you to print a trace to a file (choose text mode to make it more portable) which you can manipulate in a spreadsheet or your own code. That is how I do it in HSPICE, TSPICE etc.. The way that SPICE operates is that it computes ahead, determines if the results are within bounds and if not, it stops, re-evaluates the bias points changes time step and recomputes, so it jumps back and forth in a jittery fashion and then streams to the output.

There is another method, besides Sergei Gorbikov's methods. .MEAS, .STEP, or Ctrl+Click, though very viable, imply running the simulation and then seeing the results. If you need a quasi-real-time plot, you can use definite integration, which is nothing but a moving-average in an analog way: How to integrate a signal in LTSpice? (link only, rather than repeating the answer). The results would be plotted as the simulation goes, with a period delay. A better solution would be integrating first and then delaying, in which case the integrators can be G+C, which are a much better choice than idt().

If you know your signal to be periodic with no even harmonics, you could modify the circuit to be only half a period delay, by adding a home-brew quadrature to the input (simple pi/2 delay, derived from the original input). If your frequency has a variable period, you could use the behavioural source version of the definite integration, where delay() could have passed an external, variable delay. This delay could be an lowpass filtered version of the pulsed output, properly scaled.

If absolute real-time is needed, I'm afraid that's simply not possible, unless your modulation (error) voltage and the carrier are known, but then you could simply plot V(err) as a function of the carrier's amplitude.