Using undocumented features, you can create a simulation in LTSpice to validate the quality of your DSP algorithms.
Did you know that you can use LTspice to do digital signal processing (DSP)? Actually, I should say it is useful for validating the operation of a signal-processing algorithm under development. This article summarizes how to use LTspice to simulate the operation of a mixed continuous and sampled systems.
LTspice behaves as a simulator of continuous time systems. Under the hood, the simulator chooses the simulation timestep based on activity. The more activity, the smaller the time interval. Similarly, it increases the time step when it encounters relatively little activity. Each timestep is tagged with current simulation time so that it can be plotted at the right point. You can see this by right clicking in your LTspice Plot window and selecting View → Mark Data Points. In a nutshell, LTspice behaves like a historical analog computer. It is this characteristic that makes LTspice so useful for simulating analog circuits.
[Download the associated LTspice files (zip file)]
LTspice includes a set of proprietary Special Functions/mixed-mode simulation devices generally used to create simulation models. See LTspice Help Special Functions. The Sample device is one of the undocumented members of this family. In the context of LTspice, the Sample element is not just an analog sample and hold amplifier. If you think about it, the sample component is an analog-to-digital converter (ADC) that also behaves as a clocked register. In LTspice, everything is digital, and so the Sample component simply freezes or stores the digital representation of the “analog” signal at its input, transferring it to its output. This is exactly an ADC. Because the Sample element stores data, it behaves as a register.
For example, say you wanted to re-implement a complex analog function into a DSP. Like the circuit that is shown in Figure 1.
The first step is to determine its transfer function using the Laplace transform. This can be hard or easy depending on how much you remember from class, and the complexity of the circuit. For me it is a big problem since I have not used a Laplace transform in over 35 years. I found a nifty program called SapWin4 that can calculate the resulting Laplace transfer function directly from a schematic.
Figure 2 shows the result from SapWin.
Next, convert from the s-plane (continuous time system) to the z-plane (sampled time system). To do this, I used Scilab, an open-source equivalent to Matlab.
Figure 3 shows the steps I used in Scilab – create the variables ‘s’ and ‘z’; enter the s-plane transfer function; apply the Bilinear Transform to convert to sampled system (note that 1/samplerate = 22.6757 µs for 44.1 kHz). Also included is a typographical error illustrating that every keystroke is important in this process.
For further information on the Bilinear Transform see: Chapter 33 from The Scientist and Engineer’s Guide to Digital Signal Processing.
The equation in Fig. 3 directly contains the five necessary coefficients of a second order Biquad filter.
Now we go to LTspice. The next step is to check the Laplace transfer function by using a voltage-controlled voltage source or a behavioral voltage source configured with the Laplace transform equation that came from SapWin (Figure 4).
You can see the results if you run the LTspice simulation file “testBiquad RevA2.asc” in Fig. 4. You may notice that there is some delay or latency between the output of the analog filter, and the output of our continuous time Voltage source filter. I looked into this and found in LTspice Help that the implementation of the Laplace transform involves a sum of the instantaneous voltage, and a convolution of the historical voltage with the impulse response which is found by taking the FFT of a set of the data. This is a complex method and you can read it yourself in Help under the section Arbitrary Behavioral Voltage and Current Sources.
Now we can construct the equivalent digital filter. You can see in the LTspice schematic in Figure 5 that I created a Z-1 component.
You can look inside this by opening its schematic and symbol (right click over the Z-1 component). The Z-1 is built from two sample components running on two separate clocks. The sample component transfers the data from input to output instantaneously without a simulation time step. This causes any subsequent sample component to incorrectly capture the new data rather than the delayed data. It has a hold time problem. To fix this, I moved the output of the capture register (first Sample) to a transfer register clocked on a delayed or inverted clock. The circuit for the Z-1 component is shown in Fig. 5.
Next, we can set up the needed sample delay registers and create the output using a Behavioral Voltage source. The voltage source weights each of the sample delay registers with the stored coefficients from the Scilab transform above. Finally, we add the sample clocks which operate at the sample rate. Figure 6 shows the filter.
This digital filter is an exact representation of the real filter either built in a DSP/ARM processor or built in hardware. You can see by running the simulation and plotting the output that the actual results of a transient analysis show that the Digital filter output very closely resembles the output of the analog filter in Figure 7.
A few more points:
In summary, the LTspice Special Function device called Sample can be used as an Analog to Digital Converter, or clocked register. It stores the digital representation of the analog voltage at its input. Using the Sample component in your simulation is a simple and effective building block enabling LTspice to check the behavior of sampled systems, or mixed continuous/sampled systems.
Erick Cook is a Field Applications Engineer at Arrow Electronics.
good article.. btw can we do an AC analysis on the z transform?..
If not, I am thinking of using that measure and sweeping the frequency using transient analysis.
Leave a comment