Silicon ChipCircuit Surgery - July 2020 SILICON CHIP
  1. Outer Front Cover
  2. Contents
  3. Subscriptions: PE Subscription
  4. Subscriptions: PicoLog Cloud
  5. Publisher's Letter: Publisher's Statement
  6. Publisher's Letter
  7. Feature: Techno Talk by Mark Nelson
  8. Feature: Net Work by Alan Winstanley
  9. Back Issues: Net Work by Alan Winstanley
  10. Project: Speech Synthesiser with the Raspberry Pi Zero by Tim Blythman
  11. Project: AD584 Precision Voltage References by Jim Rowe
  12. Project: AM/FM/CW Scanning HF/VHF RF Signal Generator by Andrew Woodfield
  13. Project: High-current Solid-state 12V Battery Isolator by Bruce Boardman
  14. Feature: Circuit Surgery by Ian Bell
  15. Feature: AUDIO OUT by Jake Rothman
  16. Feature: Make it with Micromite by Phil Boyce
  17. Feature: PICn’Mix by Mike Hibbett
  18. Feature: Max’s Cool Beans by Max the Magnificent
  19. Feature: Electronic Building Blocks by Julian Edgar
  20. PCB Order Form
  21. Advertising Index

This is only a preview of the July 2020 issue of Practical Electronics.

You can view 0 of the 72 pages in the full issue.

Articles in this series:
  • Techno Talk (July 2020)
  • Techno Talk (July 2020)
  • Techno Talk (August 2020)
  • Techno Talk (August 2020)
  • Techno Talk (September 2020)
  • Techno Talk (September 2020)
  • Techno Talk (October 2020)
  • Techno Talk (October 2020)
  • (November 2020)
  • (November 2020)
  • Techno Talk (December 2020)
  • Techno Talk (December 2020)
  • Techno Talk (January 2021)
  • Techno Talk (January 2021)
  • Techno Talk (February 2021)
  • Techno Talk (February 2021)
  • Techno Talk (March 2021)
  • Techno Talk (March 2021)
  • Techno Talk (April 2021)
  • Techno Talk (April 2021)
  • Techno Talk (May 2021)
  • Techno Talk (May 2021)
  • Techno Talk (June 2021)
  • Techno Talk (June 2021)
  • Techno Talk (July 2021)
  • Techno Talk (July 2021)
  • Techno Talk (August 2021)
  • Techno Talk (August 2021)
  • Techno Talk (September 2021)
  • Techno Talk (September 2021)
  • Techno Talk (October 2021)
  • Techno Talk (October 2021)
  • Techno Talk (November 2021)
  • Techno Talk (November 2021)
  • Techno Talk (December 2021)
  • Techno Talk (December 2021)
  • Communing with nature (January 2022)
  • Communing with nature (January 2022)
  • Should we be worried? (February 2022)
  • Should we be worried? (February 2022)
  • How resilient is your lifeline? (March 2022)
  • How resilient is your lifeline? (March 2022)
  • Go eco, get ethical! (April 2022)
  • Go eco, get ethical! (April 2022)
  • From nano to bio (May 2022)
  • From nano to bio (May 2022)
  • Positivity follows the gloom (June 2022)
  • Positivity follows the gloom (June 2022)
  • Mixed menu (July 2022)
  • Mixed menu (July 2022)
  • Time for a total rethink? (August 2022)
  • Time for a total rethink? (August 2022)
  • What’s in a name? (September 2022)
  • What’s in a name? (September 2022)
  • Forget leaves on the line! (October 2022)
  • Forget leaves on the line! (October 2022)
  • Giant Boost for Batteries (December 2022)
  • Giant Boost for Batteries (December 2022)
  • Raudive Voices Revisited (January 2023)
  • Raudive Voices Revisited (January 2023)
  • A thousand words (February 2023)
  • A thousand words (February 2023)
  • It’s handover time (March 2023)
  • It’s handover time (March 2023)
  • AI, Robots, Horticulture and Agriculture (April 2023)
  • AI, Robots, Horticulture and Agriculture (April 2023)
  • Prophecy can be perplexing (May 2023)
  • Prophecy can be perplexing (May 2023)
  • Technology comes in different shapes and sizes (June 2023)
  • Technology comes in different shapes and sizes (June 2023)
  • AI and robots – what could possibly go wrong? (July 2023)
  • AI and robots – what could possibly go wrong? (July 2023)
  • How long until we’re all out of work? (August 2023)
  • How long until we’re all out of work? (August 2023)
  • We both have truths, are mine the same as yours? (September 2023)
  • We both have truths, are mine the same as yours? (September 2023)
  • Holy Spheres, Batman! (October 2023)
  • Holy Spheres, Batman! (October 2023)
  • Where’s my pneumatic car? (November 2023)
  • Where’s my pneumatic car? (November 2023)
  • Good grief! (December 2023)
  • Good grief! (December 2023)
  • Cheeky chiplets (January 2024)
  • Cheeky chiplets (January 2024)
  • Cheeky chiplets (February 2024)
  • Cheeky chiplets (February 2024)
  • The Wibbly-Wobbly World of Quantum (March 2024)
  • The Wibbly-Wobbly World of Quantum (March 2024)
  • Techno Talk - Wait! What? Really? (April 2024)
  • Techno Talk - Wait! What? Really? (April 2024)
  • Techno Talk - One step closer to a dystopian abyss? (May 2024)
  • Techno Talk - One step closer to a dystopian abyss? (May 2024)
  • Techno Talk - Program that! (June 2024)
  • Techno Talk - Program that! (June 2024)
  • Techno Talk (July 2024)
  • Techno Talk (July 2024)
  • Techno Talk - That makes so much sense! (August 2024)
  • Techno Talk - That makes so much sense! (August 2024)
  • Techno Talk - I don’t want to be a Norbert... (September 2024)
  • Techno Talk - I don’t want to be a Norbert... (September 2024)
  • Techno Talk - Sticking the landing (October 2024)
  • Techno Talk - Sticking the landing (October 2024)
  • Techno Talk (November 2024)
  • Techno Talk (November 2024)
  • Techno Talk (December 2024)
  • Techno Talk (December 2024)
  • Techno Talk (January 2025)
  • Techno Talk (January 2025)
  • Techno Talk (February 2025)
  • Techno Talk (February 2025)
  • Techno Talk (March 2025)
  • Techno Talk (March 2025)
  • Techno Talk (April 2025)
  • Techno Talk (April 2025)
  • Techno Talk (May 2025)
  • Techno Talk (May 2025)
  • Techno Talk (June 2025)
  • Techno Talk (June 2025)
  • Techno Talk (July 2025)
  • Techno Talk (July 2025)
Circuit Surgery Regular clinic by Ian Bell LTspice sources and waveform import/export I n last month’s article on class D, G and H amplifiers we made use of behavioural sources in the LTspice circuit simulator to draw waveforms to illustrate the article. There was insufficient space to explain much about this aspect of LTspice, so now we are going to take a more in-depth look at LTspice sources in general. Also, I was recently asked by someone about how you can get data in and out of LTspice for use with other applications. This is related to the uses of sources in creating and manipulating complex waveforms, so we will look at that too, including importing and exporting WAV audio files as these provide the opportunity to have some fun listening to the results of your circuit simulations. Sources In SPICE, and electronics in general, a source is something which outputs voltage or current, which, depending on the context of its use, will be regarded as a signal, a reference level or a power supply. The output from a source may Fig.2. Example arbitrary PWL waveform imported from a text file (for file contents see text). be anything from a constant (DC) value (typical for references and power supplies) through basic AC signals, such pulses and sinewaves, to highly complex waveforms such as audio. In LTspice (and SPICE in general) there are a variety of components that can be added to a circuit to act as sources. The most basic of these are simply called Voltage and Current. The names of components in SPICE start with a letter indicating the type of component – for these basic sources it is V and I. The V and I sources can be configured to produce a variety of outputs via a dialog. Once the component is placed on the schematic (see Fig.1 below) right-clicking it allows a DC value to be set, but clicking the Advanced button opens another dialog (see Fig.1 left) which allows a variety of signals to be set up. Specifically, these are DC (none), pulses (PULSE), sinewave (SINE), exponential (EXP), frequency-modulated sinewave (SFFM) and arbitrary piece-wise linear (PWL). Selecting one of these options provides access to dialog inputs to configure the sources as required (for example, the frequency of a sinewave), see Fig.1. The PWL option allows you to define an arbitrary waveform by entering a Fig.1. (right) LTspice symbols for independent voltage and current sources and (above) part of the source configuration dialog showing parameters for a sinewave source. Practical Electronics | July | 2020 set of value pairs (a time and the voltage at that time). The main source dialog provides just four points, but you can open another dialog to add more. For more than just a few data points it is often better to use a file for the data (PWL FILE option). The PWL file must be a text file containing the list of time and value pairs (one per line) in a tab or comma-delimited format. Unlike some similar data files there must not be any header information, such as a first line with the column headings. You can create the file with a text editor, or use a spreadsheet such as Excel or LibreOffice Calc and work with CSV (comma-separated values) files. Fig.2 shows an example of a waveform produced from the following text file using the schematic in Fig.3. 0, 0.001, 0.002, 0.0021, 0.0022, 0.0023, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0 0.5 2 -1 -1.5 2 2 4 8 5 0.5 0 The data points in the waveform are those created by the simulation – not the original input data. Here, they are similar, but simulation has an additional data 43 time 0.000000000000000e+000 1.000781250000000e-003 2.000000000000000e-003 2.100000000000000e-003 2.200000000000000e-003 2.300000000000000e-003 3.002343750000000e-003 V(wave) 0.000000e+000 5.011719e-001 2.000000e+000 -1.000000e+000 -1.500000e+000 2.000000e+000 2.004688e+000 I(R1) 0.000000e+000 5.011719e-004 2.000000e-003 -1.000000e-003 -1.500000e-003 2.000000e-003 2.004687e-003 Data exported from the circuit in Fig.3. Fig.3. LTspice schematic used to check import of the PWL waveform file in Fig.2. point at 6.5ms. In this case the file was in the same folder as the LTspice schematic file, so no path is included with the file; however, the full path can be specified. If you are familiar with using formulae in spreadsheet applications, there is potential to use these to create waveforms that are not available from the basic sources settings. If you are a coder then you can write software to generate PWL waveforms and write the data to a text file. Mathematical applications such as MATLAB can also be used to write data out in CSV format that can be read as PWL waveforms in LTspice. LTspice can also export waveform data to a text file. This is done using the main menu: File > Export data as text with waveform window selected. This opens a dialog which allows you to select the currents and voltages you want into include in the file – for example, see Fig.4 which shows the voltage V(wave) and the current in R1 (I(R1)) from the circuit in Fig.3 selected for export. Part of the resulting file is shown above-right. The files exported by LTspice are tab separated and include a header row with the column (field) names. The first value on each row is the time, which is followed by the other selected values. These files are readily loaded into spreadsheets and mathematical applications like MATLAB. Controlled (dependent) sources The V and I sources we have discussed so far can produce a wide variety of waveforms, but these are entirely controlled Fig.4. Exporting LTspice waveforms to a text file. 44 ‘inside’ the source – there is no direct effect on their output from other signals in the circuit, although loading effects may be modelled if a source’s internal resistance is defined (this can be done in the source setup dialog). A source controlled by another voltage or current elsewhere in the circuit is a very useful way to model common circuit behaviours. For example, a voltage amplifier is effectively a voltage source controlled by another voltage – its input signal. Controlled sources can be used to create simulations of circuits based on their operating principles, characteristics and behaviour, rather than a full componentbased design – these are referred to as ‘macromodels’ or ‘behavioural models’. This is useful for trying out ideas without having to develop a full design at component level. It is also used by manufacturers of components such as op amps to publish accurate simulation models of their ICs without revealing full design details. Furthermore, controlled sources are used within SPICE itself to model active devices such as transistors, and they are used in some published models of common components which are not built into SPICE simulators. The most-basic controlled sources are: E Voltage-controlled voltage source F Current-controlled current source G Voltage-controlled current source H Current-controlled voltage source These sources are also referred to individually as, for example, a ‘voltagedependent voltage source, and collectively as ‘linear dependent sources’. The letters E, F, G and H are the initial letters of the element names of these components. The circuit symbols are shown in Fig.5. Note the additional connections for the voltage-dependent sources Fig.5. Dependent source symbols. – the voltage across these points is used to control the source’s output. The currentdependent sources do not have control inputs – their control can only come from a voltage source in the circuit. Transfer Function To define the behaviour of a controlled source it is necessary to specify the relationship (transfer function) between the controlling voltage or current and the source’s output. The simplest version is a constant gain – the source’s output is the controlling value multiplied by the gain. It is also possible to specify the coefficients of a polynomial (a type of mathematical expression) relating the output to the input, however, use of mathematical expressions is better done with a behavioural source, which we will discuss soon. This option is mainly provided to support legacy models. For voltage-dependent sources only, it is possible to define the input-output relationship using a piecewise-linear lookup table – pairs of numbers specify input voltage and corresponding output with that input. Fig.6 shows an LTspice schematic that illustrates use of controlled sources. V1 outputs a 1V, 1kHz sinewave across resistor R1. The voltage across R1 is used to control the voltage-dependent voltage source E1. E1’s gain is 5, so the output will be a 5V, 1kHz sinewave. The current through V1 and R1 is used to control the current-dependent voltage source H1. The waveforms from the simulation are shown in Fig.7. Ammeter In Fig.6 we see a use for voltages sources in SPICE that is not immediately obvious – as ammeters. A 0V voltage source behaves like a short circuit, so has no effect if inserted into a wire, but can then be used by a current dependent source to control its output. In Fig.6 the voltage source Vmeas is used in this way. The control for H1 is specified as Vmeas 5000 – this means: use the current through voltage source Vmeas and multiply by the gain 5000 to obtain the output voltage. The 1V peak across R1 will result in a 1mA peak current, which when multiplied by 5000 will produce a peak output of 5V. The gain for an H1 source is a transresistance (voltage/current). In this circuit we could have used V1 as the Practical Electronics | July | 2020 Fig.6. Circuit to illustrate use of dependent sources. Fig.7. Waveforms from the circuit in Fig.6. Fig.8. Circuit to illustrate use of behavioural sources. in WAV format, and LTspice can export simulation data to WAV files too. An audio wave file will typically contain data in a commonly used audio format, that is, in terms of its sample rate, number of channels and number of bits – for example, as stereo CD-quality audio with two channels of 16-bit data at a sample rate of 44.1 kHz. However, WAV is not limited to standard audio formats and can have sample rates in the range 1Hz to 4.3GHz and any number of channels up to 65,536. This means uses of WAV files with LTspice can go beyond working with audio circuits and they can be used for inputting and recording data for a wide range of circuits. Unlike the PWL data files discussed above, WAV is not directly compatible with spreadsheets, but it can read and written by mathematical software such as MATLAB. Of course, using WAV files for audio circuits means that you can listen to the inputs and outputs of your simulations using a media player application – although you will need the format to be compatible with an audio standard for this to work. Furthermore, other audio generating and processing applications will be usable with your simulation data. To read a WAV file into LTspice you need to place a voltage or current source on your schematic and change its ‘value’ to the form: wavefile=filename controlling source, but we included Vmeas to illustrate what to do if the current required is not flowing via an existing source. The voltage from H1 is inverted with respect to the control voltage because the current in Vmeas is negative due to the convention used by LTspice for current directions through sources. Behavioural sources The E, F, G and H sources have relatively limited options for controlling their outputs – the only controlling inputs available are other circuit voltages and currents and, more importantly they are difficult to use beyond simply gains. Much greater flexibility is provided by the behavioural sources (BV and BI elements) which allow the use of a large range of mathematical expressions to define their output. These expressions can involve time as well as circuit voltages on any wire (to ground), voltage differences (between two wires) and the current in any element (not just voltage sources). Behavioural sources are configured by setting their value to an equation that specifies their operation. For example, for gains equivalent to those for E1 and H1 in Fig.6 we would use the values: V=5*V(control) V=-5000*I(R1) This is illustrated in Fig.8. The waveforms on b1_out and b2_ out are the same as for e_out and h_out respectively in Fig.7. Simulating with WAV files WAV is a standard audio file format, which was released, by Microsoft and IBM in the early 1990s. WAV files can contain compressed audio data, but they are most commonly used with uncompressed data. Although it is not available via the sources dialog or menus, LTspice V and I sources can also read files Practical Electronics | July | 2020 Where filename is the name of a WAV file in the same folder as the schematic, or the full path to the file if it is elsewhere. If the path contains spaces it needs to be put in double quotes. Right click the source value and enter the text to set this up. WAV files support multiple channels, so the required channel can be specified in the source value using: wavefile=filename chan=channel Where channel is the channel number. If it is not specified, then the default is channel 0 (the first channel). To write a WAV file you need to place an LTspice .wave directive on the schematic. This can be done by clicking the .op toolbar button, entering the appropriate text, clicking OK and then clicking on the schematic. The syntax of the directive is: .wave filename nbits samplerate V(net) … Where filename is the name of the WAV file for the waveforms to be written to – in the same folder, or with a full path, as with file reading. The number of bits used is set by nbits and can range from 1 to 32. The sample rate is set by samplerate and, as indicated above, can range from 1 to 4,294,967,295. The settings are followed by the list of net voltages to include in the file (the number listed determines the number of channels). For example, the following will create a file called output1.wav with CD format stereo audio (16-bit 44.1 kHz) with the signals on nets ou1 and out2 in the two stereo channels: .wave output1.wav 16 44.1K V(out1) V(out2) WAV Example As an example of using WAV files as input and output to a simulation we will simulate a simple transistor amplifier. The 45 file. Finally the .wave directive has been added to the schematic to write the amplifier’s output to another WAV file. The settings are similar to the example above except that both channels are being written with the same signal – this will create a file that will play the same waveform through both stereo channels. Audacity Fig.9. LTspice schematic for simulating a basic amplifier with a signal from a WAV file. Fig.10. LTspice simulation results for ten seconds of audio (speech) from a WAV file used as input to the circuit in Fig.9, and the resulting output. When working with WAV files in LTspice it is useful to be able to see the waveforms as recorded in the WAV file, as well as recording audio to save as WAV, listening to the files and maybe manipulating the sound in various ways. A particularly useful tool for doing this is the application Audacity. Audacity is described by its creators as a free, open-source, easy-touse, multi-track audio editor and recorder for Windows, Mac OS X, GNU/Linux and other operating systems. It is available to download from: www.audacityteam.org Opening output1.wav in Audacity and zooming to the same range as shown in Fig.11 will reveal that things are not as we might have hoped – see Fig.12. The waveform is much more severely clipped than in the simulation. This is because unlike the PWL files, which can cover any voltage range, LTspice’s WAV output is limited to an amplitude of ±1V to conform to the WAV format. This is a problem here because the output is larger than this. To get around the ±1V limit imposed by using WAV files we need to be able to scale the simulated signal being output so that it stays within this range. To do this we need to know the maximum (peak) value of the waveform and then divide the signal by this value to create a new waveform that stays within ±1V (we say this new waveform is the output ‘normalised’ to a peak amplitude of 1V). Similarly, when a WAV file is used as input an amplitude of ±1V may not be appropriate for the circuit we are simulating. For the circuit in Fig.9 this is slightly too large, although the clipping produced may provide an opportunity for a listening test – to hear any distortion created by the circuit – but first we have to overcome the clipping caused by the WAV range limits. Normalising Fig.11. Zoom-in on the waveforms from Fig.10 at around 9s showing an example of clipping of the signal. schematic is shown in Fig.9 – this is the circuit from the August 2019 Circuit Surgery article where we discussed the design of basic common-emitter BJT amplifiers. There are three changes from the original simulation. First, the value of input signal 46 voltage source (V1) has been changed from the original sinewave definition to reading a WAV file (wav1.wav). Second, the simulation has been set to run for 10s instead of the original 30ms to match the duration of the audio in the WAV In some circuits the range of signal we are outputting may be obvious, but in other cases it may be less so. It is therefore helpful if we can measure the actual peak value. You could try to do this manually by looking through the waveform, but this is far from ideal. Fortunately, LTspice has a better means of doing this via the .measure (.meas) directive. The .measure directive provides a large range of possibilities for obtaining measurements from simulation data; examples include finding the maximum, minimum, average Practical Electronics | July | 2020 Fig.12. Viewing the WAV file created by the circuit in Fig.9 in Audacity (compare with Fig.11). or RMS value of a waveform. Mathematical expressions can be applied to circuit signals to which the measurement is applied, increasing the scope of the basic measurements. To find the peak value of a signal we can add the following directive to the schematic (using the .op button): .meas peakvalue max abs(v(out)) Here, peakvalue is just a name give to the measurement result, max is the measurement type we are using (find the maximum) and abs(v(out)) is the value to which the measurement is being applied. This is taking the signal V(out) and applying the mathematical ‘absolute value’ function to it. The abs function removes the sign, so that we find the magnitude of the largest peak in the signal, whether it is negative or positive. To find the peak value run the simulation and select View > Spice Error Log from the menu. This will open the log file, in which you should find a line similar to the following: peak: MAX(abs(v(out)))=4.99165 FROM 0 TO 10 Here we see the peak value is 4.99165 V. The normalisation of signals for WAV output is easily performed using a behavioural source – create a new voltage equal to the signal of interest divided by the peak value calculated using the .measure directive. For the circuit in Fig.9, adding the behavioural source shown in Fig.13 does the job. Here, the dividing value has been set to a value slightly larger than the peak. The .wave directive also has to be edited to use the new signal (out_n rather than out in this example). In our example the WAV file name was also changed, to wave2.wav Listening Using normalisation requires the simulation to be run twice – first to obtain the peak value and then again with the correct normalisation set up to produce the WAV file. The results from Fig.5, modified as described, and loaded into Audacity to display the same range as Fig.8 are shown in Fig.10. We see that the WAV file now matches the LTspice waveform Fig.13. Behavioural source (v(out) in Fig.8). The WAV file can added to the circuit in Fig.9 now be listened to in comparison with to normalise the out signal the input, although the difference for WAV output. in this example was not dramatic as only the largest peaks were clipped. Another behavioural source could be added to boost the input signal to cause the circuit to create more distortion, so the effect could be clearly heard. Another experiment worth trying is to reduce the coupling capacitors to a much Simulation files smaller value (say Most, but not every month, LTSpice 1nF), which will is used to support descriptions and cut the gain at low analysis in Circuit Surgery. frequencies and The examples and files are available make the speech for download from the PE website. sound different. Fig.14. Wave file waveform for the output from the circuit in Fig.9 after normalisation using a behavioural source. Practical Electronics | July | 2020 47