LTSpice Simulation Using WAV Files
Learn how to interface with LTSpice using WAV files.
Learn how to interface with LTSpice using WAV files.
LTSpice has the capability to record data from voltage and current nodes as part of a transient simulation. Data is recorded in a WAV file. The data can be read back by voltage and current sources and used as inputs to other LTSpice circuits, viewed using a simple playback circuit or exchanged with common data analysis applications.
In this article, examples of general purpose recording and playback circuits and details of the data format are explored.
Recording Simulation Data
An intriguing capability of LTSpice is recording Transient simulation data as WAV files. I did not understand what this meant until looking at the detailed LTSpice specification and seeing how these specs aligned with low-level details of the WAV file format. WAV files are not just for audio. One key thing to know is WAV files are not compressed. You put a number in, you get the same number out. Apparently, in the music industry, it is the ultimate for high-quality recording. Also, the data is organized into channels like you would expect in a music recording studio. However, the number of channels can be very large. Way beyond what music recording could require. To me, this is a good format for a general-purpose, multi-channel data recorder.
This table summarizes the specifications from LTSpiceHelp
|LTSpice .WAVE Specifications||Value|
|Number of Channels||1 to 65535|
|Number of Sampling Bits||1 to 32|
|Samples per Simulated Second||1 to 4,294,967,295 (232-1)|
|Full-scale Voltage Range||-1 to +1 V|
|Full-scale Current Range||-1 to +1 A|
Wow! Imagine an actual data recorder with over 60,000 channels, 32-bit analog-to-digital conversion, and over one billion samples per second. I want one.
My tests found the maximum samples per second to be about ½ of the value in the table with a Transient simulation running for 10 nanosec. Also, I could not verify the number of sampling bits beyond around 24 bits.
All this is done by putting a .WAVE directive in the schematic. Here is an example.
.wave Circuit_Data.wav 16 10K V(ab) V(cd) V(ef) V(gh)
I have deliberately not used the example from LTSpiceHelp. That example is appropriate for creating a WAV file that can be played back as audio. We are going in a totally different direction. The command breaks down as follows:
- Circuit_Data.wav - The output file
- 16 - The number of sampling bits. Think analog-to-digital converter resolution.
- 10K - The number of samples per simulated second.
- V(ab), etc. - A list of the nodes in the schematic to record. In this case, there are four. V(ab) is channel 0.
A General Purpose WAV Recorder
Here is a general-purpose recorder that can be added to circuits. The schematic shows a User Circuit connected to the recorder. This example has four channels, but it could have from 1 to 65535 channels. I have only tried up to eight!
Note, the figures can be seen with much greater detail if opened in a separate tab.
The Voltage Controlled Voltage Sources (E100, etc.) serve two functions. First, they isolate the node name in the User Circuit from the node names in the .WAVE directive. The User Circuit node is attached to the recorder input without worrying about node names. Second, the E sources provide scaling for the inputs since the .WAVE function has a fixed full scale range. .PARAM statements in the “User Parameters” list are modified to establish the full scale range of each channel independently. Another “User Parameter” is the number of samples per second. I fixed the filename in the .WAVE directive.
Playing Back WAV Data
Here is the corresponding player which reads in the data to be plotted or connected to other circuits.
The voltage sources read in the data for each channel. The voltages that were scaled in the recorder are then unscaled by Voltage Controlled Voltage Sources. The full scale specified for each channel is set with .PARAM statements that match the recorder .PARAM statements. The .TRAN directive works normally and is used to play back all or a portion of the data.
Sample Plots of Recording and Playback
The plots on the left are the simulation being recorded. The circuit is a resistive voltage divider with a capacitor added to give varying shape to the signal edges. The recording is for two seconds. The plots on the right are outputs of the player. The playback skips the first second of the recording and plots the final second. Just as with any analog-to-digital converter, make sure the sample rate is high enough to prevent aliasing.
To produce these circuits and plots, I had four windows open. Two schematic windows and two plot windows. Just click the Runner on the recorder and then his friend on the player.
Compatibility with Data Analysis Applications
The data format is very general purpose and can be read by common data analysis programs. As a test, the recorder was run with the voltage source changed from a pulse train to a constant +1 VDC for simplicity. The WAV file was read by SciLab into a matrix with the wavread(filename) function. No other processing was required.
Each row of the matrix is a WAV channel, and each set of samples in time is a column. In this display, the top row is the first six samples of Channel 0, second row is Channel 1, etc.
0.1000061 0.1000061 0.1000061 0.1000061 0.1000061 0.1000061 0.0750122 0.0750122 0.0750122 0.0750122 0.0750122 0.0750122 0.0499878 0.0499878 0.0499878 0.0499878 0.0499878 0.0499878 0.0249939 0.0249939 0.0249939 0.0249939 0.0249939 0.0249939
Remember the recording was set for a full-scale range of +/-10 V. This means the data was divided by 10 to stay within the -1 V to +1 V range of the .WAVE function; 1 V at the node is recorded as 0.1 V, 0.75 V is recorded as 0.075 V, etc. The player multiplies this data by 10 to get back the original voltages.
Did the corresponding SciLab wavwrite(y, filename) function produce an LTSpice compatible file? Did the file play back correctly? Yes, it did!
MATLAB has the same functions, but I did not verify them. There is a third-party function available for reading LTSpice “raw” files into MATLAB, LTSPICE2MATLAB(). This will have the ultimate in amplitude and time resolution. There is no mention of going the other way with a MATLAB2LTSPICE() function.
Recording and playback of WAV files by LTSpice seem useful in several ways.
- WAV files can be exchanged between LTSpice and common data analysis programs. This allows both types of simulators to run cooperatively, and we can utilize the strengths of each one.
- Recorded data can be used as input to another LTSpice simulation. For example, the recordings can be testing data that is used repeatedly during development. Also, the output of a simulation performed by one team can be used as input for another team, and the teams do not have to constantly synchronize design files.
- The result of the simulation can be played back without the original circuit. For example, the performance of the circuit can be demonstrated but the circuitry kept confidential. Also, the results can be seen and explored by someone without the skills to run a complex LTSpice simulation. LTSpice can be used to just view plots without much training, or a data-analysis application can be used for viewing. For example, the project manager can say, “Send me the WAV file”.