QUESTION:
Can you utilize stereo data and encrypt voice messages with LTspice audio WAV files?
Answer:
If music be the food of love, simulate on.
This RAQ explains how to use LTspice® audio WAV files for the syntax of stereo (and higher channel count).
LTspice can be used to generate WAV files as an output of a circuit simulation as well as import WAV files to excite a circuit simulation. It is well documented that mono WAV files can be used as an input in LTspice, and LTspice can be used to produce a WAV output. This article details how to use LTspice audio WAV files for the less well-known syntax of stereo (and higher channel count).
LTspice has many superpowers, but its handling of audio files is one of its more impressive talents. While it is fascinating to see a circuit come to life on the computer screen, creating a sound file that can be played outside of LTspice enables engineers to use another sense to evaluate simulations. Using mono LTspice audio WAV files is well documented. This article expands the discussion to stereo (or more channels), and shows how to export stereo data from, and import it to, LTspice via WAV files. It also illustrates a few tips and tricks with WAV files that will enable the reader to further utilize WAV files.
Generating a Stereo WAV File
Let’s start by producing a stereo wave file from a mono signal. Figure 1 shows a circuit that generates a 1 V, 1 kHz sine wave and splits it into two channels, alternating the signal between them—the 1 kHz tone is switched in 2-second intervals between CH1 and CH2.
The command .wave “C:\export.wav” 16 44.1k V(CH1) V(CH2) digitizes each channel with 16-bit resolution, sampled at 44.1 kSPS, and stores the resultant audio in C:\export.wav. In the command above, each signal listed after the sample rate becomes its own channel in the WAV file. LTspice can store as many as 65,535 channels in a single LTspice audio WAV file—just append signals to the above command as desired.
By default, LTspice’s .wave command saves the first listed channel as the left audio channel and the second as the right audio channel. In this case, when export.wav is played back through a media player, CH1 will be read as the left channel and CH2 will read as the right, regardless of the circuit node naming convention. Note that, by default, CH1 and CH2 are stored as chan 0 and chan 1, respectively, in the .wav file, which is important for reading the file discussed below.
This exported stereo audio file can be used to stimulate another circuit, shown in Figure 2, which uses the two channels from export.wav as signal inputs.
The voltage sources, V1 and V2, are placed as usual, then the voltage signals from export.wav are assigned by holding down the CTRL key and right clicking over each voltage source, revealing the Component Attribute Editor as shown in Figure 3.
As mentioned above, when the LTspice audio WAV file is first generated, as many as 65,535 channels can be digitized into one WAV file—simply append as many channels as desired onto the end of the .wave command. Remember, by default, LTspice names the first channel Channel 0, the next one is named Channel 1, and so on. In this case, export.wav (generated by the simulation in Figure 1), stores the voltage V(CH1) as Channel 0 and V(CH2) as Channel 1. To play back these channels using a voltage source, specify the .wav file and channel in the value line of the voltage source. In this case:
- To instruct V1 to replay Figure 1’s V(CH1): wavefile=“C:\export.wav” chan=0
- To instruct V2 to replay Figure 1’s V(CH2): wavefile=“C:\export.wav” chan=1
Audio Separation
Theoretically, playing export.wav through a media player should switch between playing the 1 kHz tone entirely through the left speaker (or headphone) for two seconds then through the right speaker for two seconds. Nevertheless, complete stereo separation is not guaranteed and this depends on the quality of the media player used during playback.
Playing export.wav through a laptop showed about 30% of the left channel appearing on the right channel when measured on an oscilloscope as shown in Figure 4.
Playing the same file on a mobile phone handset (circa 2000) gave a more separated result, showing no perceivable crosstalk, but a slight amount of distortion at full volume as shown in Figure 5.
Repeating the experiment on a later generation 2018 handset showed no perceivable crosstalk, a full 1 V peak signal, and very little distortion as shown in Figure 6. Note the oscilloscope plots were taken with a 500 mV/div sensitivity.
The same file was used across all three platforms, showing that LTspice is producing the WAV file with full separation, but resulting playback depends greatly on the quality of the player’s audio stage.
Voice Encryption
The circuit in Figure 7 shows a basic method of voice encryption whereby an audio signal is encrypted using a random number sequence, then decrypted.
The file voice.wav contains the original audio. An Excel spreadsheet is used to generate a random number sequence with a 100 µs change period. The results are copied into a text file called random.txt. An extract of random.txt is shown in Figure 8.
This file is used to generate a randomly changing voltage, V(RAND), using the piecewise linear (PWL) voltage source in LTspice.
V(RAND) is added to the voice signal using the behavioral voltage source B1. The output is then multiplied by V(RAND) and the result is sent to the encrypt.wav file. Listening to encrypt.wav showed that the original audio was barely perceivable.
Figure 9 shows the original voice, the encrypted voice, and the decrypted voice signals in the LTspice plot window.
A second behavioral voltage source was then used to decrypt the original audio signal and the result was sent to the file decrypt.wav.
Producing a WAV File from a Differential Voltage Source
The syntax of the .wave command does not allow for the digitization of differential voltages. However, this can easily be overcome by using a behavioral voltage source as shown in Figure 10.
The behavioral voltage source outputs a voltage equal to V(OUT1) – V(OUT2) and this can be used in the .wave command in the usual way as shown.
Indeed, the variables inside the behavioral voltage source’s function can include any voltage or current in the circuit and these can be manipulated using any of the mathematic functions of LTspice. The net result can then be exported to an LTspice audio WAV file in the normal way.
LTspice is a powerful simulator, but its results do not have to be contained inside LTspice. Using the .wave command, LTspice can import, manipulate, and export audio files for playing on a media player.