6
\$\begingroup\$

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?

\$\endgroup\$
4
  • \$\begingroup\$ In microcap, the transient analysis machine allows you to display (say) "v(V1)" and "avg(v(V1))" - look for something like this. \$\endgroup\$
    – Andy aka
    Nov 28, 2014 at 19:34
  • \$\begingroup\$ @Andyaka: I don't think that I want to spend that much for a hobby. \$\endgroup\$
    – PlasmaHH
    Nov 28, 2014 at 20:40
  • \$\begingroup\$ Dude I said - "look for something LIKE this" \$\endgroup\$
    – Andy aka
    Nov 28, 2014 at 22:10
  • \$\begingroup\$ @PlasmaHH curious how you implement a sliding average in a B source with absdelay ? \$\endgroup\$
    – cyril42e
    Apr 25, 2021 at 22:50

3 Answers 3

10
\$\begingroup\$

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):

enter image description here

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

enter image description here

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

enter image description here

See the moving average plotted

enter image description here

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

Suppose one has a chart like this:

enter image description here

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).

enter image description here

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.

enter image description here

\$\endgroup\$
4
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.