When simulating a circuit in LTspice, the goal is likely to gain insight into the analog behaviors and trade-offs of your circuit. It is also likely that the output of that circuit will be digitized and post-processed, and that the resulting digital elements can be more difficult to include in your LTspice simulation.
In this blog post, I’ll introduce some circuit elements to help you model a digital filter in LTspice. Doing so will enable you to explore the behavior and performance of an analog circuit with filtering applied to the digitized signal.
Introducing a Simple Digital Filter: The Moving Average FIR
Let’s start with what is possibly the simplest low-pass filter example, the moving average filter. The Wikipedia article on FIR Filters illustrates a very simple moving average example using three taps.

Figure 1: Block Diagram of a Moving Average FIR Filter
Chapter 15 of The Scientist & Engineer's Guide to Digital Signal Processing is also a useful resource for understanding moving average filters.
In the three-tap example above, the output is the average of three consecutive samples of the input signal:
This can also be simplified to reduce the number of multiply blocks needed:
Easy enough. But how would we implement this in LTspice?
Introducing Filter Model Blocks in LTspice
To implement this filter in LTspice, we need three functions: a unit delay, an add, and a multiply.
The unit delay block is implemented as an arbitrary voltage source (bv in the LTspice components library) with a delay() function. This block requires a sampling frequency to be set. This parameter should be set at the top level of your schematic with a .PARAM directive. The following directive:
.param Fs=1000
sets the sampling frequency to 1ksps. The unit delay block converts the sampling frequency to a sampling period (1ms) and uses that value to delay the input of the block by that amount.

Figure 2: LTspice Schematic for Unit Delay Block
The Add and Multiply blocks are much simpler with no knowledge of sampling period. Both are implemented simply with voltage-controlled voltage sources. The Add block requires two input signals to add together. The Multiply block requires one input signal, and uses a parameter M to set the multiply value.

Figure 3: LTspice Schematic for Adder Block

Figure 4: LTspice Schematic for Multiplier Block
Implementation of a Moving Average FIR in LTspice
We can combine these three blocks to create a simplified moving average FIR in LTspice. To explore this example, open the Moving Average FIR Filter.asc file in the .zip file attached. For LTspice to find all of the necessary files, you will need to unzip the .zip file before opening the schematic file.
The filter blocks in the example above have been implemented as hierarchical blocks, which allows you to double-click on the blocks to open the underlying schematics in those blocks. In the zip package, you will see a .asc and .asy file for each of these blocks. The .asc file is the underlying schematic, and the .asy file is the symbol.

Figure 5: Three-Tap Moving Average FIR LTspice Schematic
Exploring Results in a Transient Simulation
Select Simulate -> Run/Pause from the menu to run the transient simulation specified using the .TRAN directive in the schematic. Figure 6 shows the input, output, and internal delay nodes of the filter circuit. Click on the nodes in the schematic to plot them, then right-click on the waveform and select Add Plot Pane Below to create multiple panes.

Figure 6: Transient Simulation Results Showing Running Average Behavior
Since LTspice is still an analog simulator, care must be taken to ensure the input signal is a representation of a digital signal. In this example, I’ve ensured that the signal doesn’t vary faster than the sample rate. Another approach could be to digitize the signal with a sample-and-hold circuit.
Exploring Results in an AC Simulation
In the top-level schematic, right-click on the .AC directive, and then click OK to make that directive active. As a result, the .TRAN directive will be commented out (in blue).

Figure 7: .AC Directive Enabled, .TRAN Directive Disabled
Plotting the output shows the magnitude and phase response of the filter. Notice that the end frequency in the .AC directive was set to the Nyquist frequency, Fs/2.

Figure 8: AC Analysis of FIR Filter, Magnitude and Phase vs. Frequency
To get a plot that is a closer match to the image in the Wikipedia article example, right-click on the vertical axis to change the scale to Linear, and right-click on the horizontal axis to uncheck Logarithmic.

Figure 9: Changing Magnitude Axis to Linear Scale

Figure 10: Changing Frequency Axis to Linear Scale
This will result in a linear-linear plot that will more closely resemble the magnitude and phase plots in the Wikipedia example.

Figure 11: Linear-Linear Plot of FIR Frequency Response
Observing Aliasing in an AC Simulation
Initially, the end frequency was set to Nyquist (Fs/2) to ensure the frequency response matched what a digital filter response is expected to be; and digital filters don’t have any concept of frequencies beyond Nyquist.
The real value in adding a digital filter to your otherwise analog simulation is that you can observe the actual behavior of your digital filter on a continuous signal; in other words, you can observe the concept of aliasing.
Let’s explore this by changing the .AC directive to extend out several multiples of Nyquist. Figure 12 shows a simulation that extends to 5*Nyquist.

Figure 12: Linear-Linear Plot of Frequency Response, Showing Alias Regions
This analog-friendly model of a digital filter allows you to simulate the effect of filtering and aliasing when added to the output of a signal chain. I’ll explore this in future blog posts, so stay tuned!
Read all the blogs in A Pinch of LTspice series.
*A special thanks to Steve Potratz for introducing me to digital filter models in LTspice.