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:
- You may find that a simulation of the sampled system runs a lot slower than the original continuous time system. Either use this as a validation tool or, take steps to speed up the simulation. These steps may include using a Save statement to limit the saved data to just nodes of interest; eliminate any continuous time components from the simulation – even a simple RC will cause many timesteps to occur during each Sample period. A Sinewave clock caused the sampling process to operate more advantageously and it runs much faster than with a Pulse based source. The goal of this effort is to minimize the number of timesteps needed per sample period. Ideally it would be just one, but in reality, it’s roughly 8-10. It is also important to remain cognizant of sample jitter when setting up the sample clock.
- It should be possible to use this method for simulating the operation of all kinds of sampled systems including control systems, PID, or even Neural Networks.
- Certainly, a real and useful application for this is simulating where the Analog inputs from sensors and actuators meet the digitizer and controller.
- LTspice includes the ability to directly use WAV files. This can be very helpful for stimulating the system with an arbitrary waveform or for recording long streams of simulation results.
- The stability of the system can be tested using techniques that are discussed in the LTspice installation directory FRA sub-directory – Frequency Response Analysis.
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.
- Use LT SPICE to analyze input filters
- Simulate digital filters with PSpice
- Validate Spice models before use
- Use Spice to analyze DRL in an ECG front end
- A New Trilogy of LTSpice Circuits With NTC Thermistors, PART III: A Last Burst of LTSpice Simulations for Temperature Control Circuits (LTC1040, LT8391 and more)
- 2021: A Simulation Odyssey for Thermistors
- Filter Design using the “Million Monkeys Method”