Silicon ChipCircuit Surgery - July 2022 SILICON CHIP
  1. Outer Front Cover
  2. Contents
  3. Subscriptions: PE Subscription
  4. Subscriptions
  5. Publisher's Letter: The importance of repair
  6. Feature: Mixed menu by Mark Nelson
  7. Feature: The Fox Report by Barry Fox
  8. Project: Net Work by Alan Winstanley
  9. Project: Single-Chip Silicon Labs FM/AM/SW Digital Radio Receiver by Charles Kosina
  10. Project: Model Railway Level Crossing by LES KERR
  11. Project: Advanced GPS Computer by Tim Blythman
  12. Feature: Make it with Micromite by Phil Boyce
  13. Feature: Max’s Cool Beans by Max the Magnificent
  14. Feature: Flowcode Graphical Programming by Martin Whitlock
  15. Back Issues: Flowcode Graphical Programming by Martin Whitlock
  16. Feature: Circuit Surgery by Ian Bell
  17. Feature: AUDIO OUT by Jake Rothman
  18. Feature: Electronic Building Blocks by Julian Edgar
  19. PCB Order Form
  20. Advertising Index

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

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

Articles in this series:
  • (November 2020)
  • Techno Talk (December 2020)
  • Techno Talk (January 2021)
  • Techno Talk (February 2021)
  • Techno Talk (March 2021)
  • Techno Talk (April 2021)
  • Techno Talk (May 2021)
  • Techno Talk (June 2021)
  • Techno Talk (July 2021)
  • Techno Talk (August 2021)
  • Techno Talk (September 2021)
  • Techno Talk (October 2021)
  • Techno Talk (November 2021)
  • Techno Talk (December 2021)
  • Communing with nature (January 2022)
  • Should we be worried? (February 2022)
  • How resilient is your lifeline? (March 2022)
  • Go eco, get ethical! (April 2022)
  • From nano to bio (May 2022)
  • Positivity follows the gloom (June 2022)
  • Mixed menu (July 2022)
  • Time for a total rethink? (August 2022)
  • What’s in a name? (September 2022)
  • Forget leaves on the line! (October 2022)
  • Giant Boost for Batteries (December 2022)
  • Raudive Voices Revisited (January 2023)
  • A thousand words (February 2023)
  • It’s handover time (March 2023)
  • AI, Robots, Horticulture and Agriculture (April 2023)
  • Prophecy can be perplexing (May 2023)
  • Technology comes in different shapes and sizes (June 2023)
  • AI and robots – what could possibly go wrong? (July 2023)
  • How long until we’re all out of work? (August 2023)
  • We both have truths, are mine the same as yours? (September 2023)
  • Holy Spheres, Batman! (October 2023)
  • Where’s my pneumatic car? (November 2023)
  • Good grief! (December 2023)
  • Cheeky chiplets (January 2024)
  • Cheeky chiplets (February 2024)
  • The Wibbly-Wobbly World of Quantum (March 2024)
  • Techno Talk - Wait! What? Really? (April 2024)
  • Techno Talk - One step closer to a dystopian abyss? (May 2024)
  • Techno Talk - Program that! (June 2024)
  • Techno Talk (July 2024)
  • Techno Talk - That makes so much sense! (August 2024)
  • Techno Talk - I don’t want to be a Norbert... (September 2024)
  • Techno Talk - Sticking the landing (October 2024)
  • Techno Talk (November 2024)
  • Techno Talk (December 2024)
  • Techno Talk (January 2025)
  • Techno Talk (February 2025)
  • Techno Talk (March 2025)
  • Techno Talk (April 2025)
  • Techno Talk (May 2025)
  • Techno Talk (June 2025)
Circuit Surgery Regular clinic by Ian Bell Distortion and distortion circuits – Part 2 L ast month, we started looking at distortion, which is the changing of the shape of waveforms due to nonlinearities in circuits such as amplifiers. This was inspired by a couple of PE projects by John Clarke on sound processing effects for musicians, including the recent Digital FX system and the Nutube Guitar Overdrive and Distortion Pedal from March 2021. Although deliberate distortion is used creatively by musicians, distortion is often an unwanted characteristic and circuit designers make significant efforts to minimise it. The amount of unwanted distortion is often measured using Total Harmonic Distortion (THD), particularly in audio applications. Distortion and Spectra In Part 1 last month we covered the basic principles of distortion, including the importance of the spectrum of a signal in understanding distortion and the mathematics behind THD. To recap briefly, a spectrum is a plot of signal strength (amplitude or power) against frequency, which therefore shows all the frequencies present in a signal (within the range plotted). Any periodic waveform can be formed by adding together a set of sinewaves of various frequencies and different amplitudes. This ‘sum of sinewaves’ is known as a Fourier series. The spectrum plot of a periodic signal therefore comprises a set of peaks at various frequencies, and the sinewave itself is the only signal that has just one peak in its spectrum. When a sinewave is input to an ideal amplifier the output spectrum has the same form as the input – a single peak – but with the amplitude changed by the amplifier’s gain. For a non-ideal (non-linear) amplifier the output will contain frequencies which were not present in the input. The presence of the additional frequencies changes the shape of the waveform. Change of wave shape is an intuitive idea of what distortion does, but is not sufficient to define distortion. A change of waveform shape will also occur for non-sinusoidal inputs to perfect linear filters (eg, ‘rounding off’ of a square wave). This is because the relative amplitude of the spectral peaks is changed – but the output does not contain additional frequencies. The theory we discussed last month shows that for a sinewave input the additional output frequencies due to distortion will be at multiples of the input frequencies, that is, harmonics of the input. If we find the ratio between the power of the output signal at the input frequency and the power of the sum of all the additional frequencies due to distortion, then we get a useful indication of how much distortion a circuit produces. Because we are adding up all the contributions of the harmonics, this measure is called ‘Total Harmonic Distortion’. If the input signal contains multiple non-harmonically related frequencies the effects of distortion are Fig.1. A very simple circuit for investigating spectra in LTspice. more complex, containing sum, difference and other combination frequencies – this is called ‘intermodulation distortion’. In the context of a single music note, the input will in most cases already contain harmonics – these define the timbre of the note (musical quality other than pitch loudness), thus distortion will change the relative harmonic content and hence the timbre, typically producing a ‘fuzzier’, ‘grittier’ or ‘harsher’ sound. Using LTspice Last month, we illustrated parts of the article with LTspice simulations, including plots of signal spectra. However, we did not explain how these were obtained, so this month we will look at how to plot signal spectra and calculate THD with LTspice. THD can be calculated manually from a spectral plot, but LTspice has a Simulation files Most, but not every month, LTSpice is used to support descriptions and analysis in Circuit Surgery. The examples and files are available for download from the PE website. 54 Fig.2. The waveform of V(x) from the transient analysis of Fig.1. Practical Electronics | July | 2022 The circuit in Fig.1 is the minimum needed to g e t LTs p i c e t o d i s p l a y a spectrum. To obtain a waveform to analyse with the FFT we need to run a transient simulation; the figure shows that a 5ms transient analysis has been set up. The resulting waveform is shown in Fig.2. ‘FFT’ refers to the ‘Fast Fourier Transform’, which is the mathematical algorithm used to calculate a spectrum from a waveform. The fundamental mathematics is the Discrete Fourier Transform (DFT) – ‘discrete,’ because the waveform data is a set of sample points, not a continuous mathematical function. The FFT is an efficient computer implementation of the DFT. FFT plot Fig.3. The FFT dialog in LTspice. command for calculating it automatically. This does not mean that the spectral plots are not useful – they provide a visual insight into the harmonic content of a distorted waveform. For example, musicians often discuss the relative merits of distortion dominated by odd or even harmonics and relative levels of higher and lower order harmonics in terms of quality of sound. The spectrum plot allows a quick assessment of such properties of a signal. Next month, we will look at analogue circuits used to create distortion for musical effects. To see the spectrum for the waveform, right click on the waveform and select ‘View’ from the pop-up and then ‘FFT’ from the submenu. The dialog shown in Fig.3 will appear. Make sure the signal of interest, in this case V(x), is selected and click OK. This will display a spectrum similar to the one in Fig.4 – note that the frequency range plotted in Fig.4 has been changed from the default to focus on the lower frequencies closer to the input frequency. To change the axis range right-click on axis numbers and enter the new range values in the dialog. The expected spectrum for a pure 1kHz sinewave is simply a single very sharp peak at 1kHz. The spectrum in Fig.4 has a very wide peak and plenty of content at other frequencies. Fortunately, there are several things we can do to obtain a more convincing spectrum. The spectrum in Fig.4 is plotted in decibels. The largest peaks, other than the expected one at 1kHz, are about 65dB below the 1kHz peak, this is about 2000-times smaller (10-65/20 = 5.6x10 -4). This may seem small, but remember the spectrum should be zero at all frequencies except 1kHz. LTspice spectral plots will always have some content where ideally the value is zero due to the limitations in the accuracy of the calculations, but we need to make sure the errors are acceptable. In this case, we would expect much smaller errors given that we have an ideal sinewave and perfect (linear) resistor. Compression One problem with this initial simulation is due to the waveform data compression that LTspice employs. This is often useful because a compressed waveform file can be 50-times smaller than an un-compressed one. However, the compression is lossy, which means that information is lost during compression. Although it usually does not detract from the look of a waveform plot, this loss may degrade the quality of a spectrum calculated from the waveform data, so when you intend to analyse spectral data, compression should be turned off. You can turn off compression using Control Panel from the Tools menu: go to the Compression tab and set the Window Size (No. of Points): value to zero. However, this setting is not remembered between program invocations, so it is better to put the command on the schematic. To do this, click on the schematic window and then on the .op (SPICE Directive) button on the menu bar. Enter the following text into the dialog .option plotwinsize=0 Next, click on the schematic to add the command. Close the spectrum window and then run the simulation again (this is necessary to generate the uncompressed waveform data). Then display the spectrum again – see Fig.5. Noise floor Fig.4. The initial spectrum does not look very convincing. Practical Electronics | July | 2022 For real circuits, there is always random noise present, and in measurements of signal spectra this will produce results similar to the relatively flat part of the spectrum away from the peak in Fig.5. Pure random noise over an infinite time has a perfectly flat spectrum (no peaks) at all frequencies, but with real measurements there will be variations, 55 Accuracy Fig.5. Avoiding data compression greatly reduces noise in the spectrum Fig.6. Using a longer simulation (more cycles) gives a narrower peak, but this attempt has more noise due to lack of simulation accuracy. as in Fig.5. The general level of the flattish part of the spectrum between the signal peaks is called the noise floor. In simulations, like the example here, there is no random noise in the signal, but numerical errors in the simulation produce a noise floor in the spectrum. The decibel scale appears to exaggerate the noise level if you are not used to reading decibel-scaled graphs. In Fig.5 the noise floor is around 160dB below the peak, this corresponds to a voltage 300 million times smaller than the peak. This is much smaller than the noise floor in typical real circuits. We now have a clean spectrum, but the peak is still much wider than expected. This is due to not having enough good waveform data for the FFT to use. One thing we can do is to run the simulation for longer, so that more waveforms cycles are included. To do this use Edit Simulation Cmd from the Simulate menu and change the Stop Time to 50m. Run the simulation again and you will see about 50 cycles. 56 The spectrum should now look like Fig.6 – the frequency range has been changed to match the previous plots. The peak at 1kHz is now much narrower, but the noise floor is worse again. This is again due to lack of accuracy in the simulation. This time it is not data compression, but the number of data points at which the simulator has calculated the waveform that is the problem. The data points used by the FFT are not at the same points in time as the points on the waveform calculated by the simulator. This is simply because they work in different ways. The FFT uses regularly spaced samples, whereas the simulator calculates the waveform at the ‘next time step’, and this length of time can vary depending on how the simulator’s calculations perform. If there is not a simulator waveform data point at the time required by the FFT, the FFT has to interpolate the required value using the nearest data points, which may lead to errors. However, we can force the simulator to use a certain minimum time step, so that more waveform data points are calculated during the simulation, providing better data for the FFT. To reduce the time step, again use Edit Simulation Cmd, this time entering 10n in the Minimum Timestep box. Leave the Stop Time at 50m. This will force the simulator to calculate waveform data points at least every 10ns (nanoseconds). Run the simulation again – you will find it takes longer. View the spectrum again; it should look like Fig.7. We now have a sharp peak and a noise floor well below the peak. The spectrum is much closer to that of an ideal sinewave than our initial attempt. Another way to improve accuracy is to set another option: .options numdgt=7 Fig.7. Improving simulation accuracy (at the expense of run time) gives a spectrum with a much better noise floor. Practical Electronics | July | 2022 Fig.8. Further improvements in accuracy using double-precision maths. This is added to the schematic in the same way the plotwinsize directive used earlier. The LTspice help informs us that historically (in SPICE) this was used to set the number of significant figures used for output data, but in LTspice setting numdgt greater than 6 causes double-precision maths to be used in calculations. Plotting Once a good quality spectrum has been obtained it is not essential to include the noise floor in the plot. For results such as those in Fig.8, the noise is so small that it is not relevant, and we can limit the decibel range to values close to the peak. This makes it easier to see the relative levels of different peaks. For example, Fig.9 shows a circuit to create a signal comprising two sinewaves, from sources V1 and V2 at 1kHz and 5kHz respectively. The 1kHz signal has twice the amplitude of the 5kHz one. This figure also illustrates use of the .plotwinsize and .numdgt directives discussed above. Plotting the results from Fig.9 using the full decibel range (as in Fig.8) makes it difficult to see the relative amplitude of the two frequencies. The plot in Fig.10 is the FFT results from the circuit in Fig.9 with the decibel range set to more convenient values. From this we can clearly see that the 5kHz signal (V2) is 6dB below the 1kHz signal (V1). This is a signal amplitude ratio of 20log(V2/V1) = –6dB or V1/V2 = 10–6/20 = 0.5, that is the 5kHz signal is at half the amplitude of the 1kHz Fig.9. Generating two sinewaves at 1kHz and 5kHz. signal, as we woud expect from the source configurations. This brings us to an issue which sometimes confuses people: what exactly the FFT result is plotting. The V1 source in Fig.9 has an amplitude of 1.4142V (√2). This value was chosen because it represents an RMS (root mean square) AC voltage of 1V. The RMS value of an AC voltage is the value of the DC voltage which would dissipate the same power in a given resistor and represents an Fig.10. Results from simulation of the circuit in Fig.9. average of the power delivered over a waveform cycle. For a sinewave, the RMS value is the peak value divided by 2. The default LTspice FFT plot is in dBV – that is, decibels relative to 1Vrms, specifically 20log(Vrms/1Vrms), so 1.4142 (1Vrms) is plotted as 0dB (20log(1)) and 0.7071V as –6dB (20log(0.5)), as shown in Fig.10. The FFT plot can also be set up to display RMS values instead of decibels by right-clicking the amplitude axis and selecting Linear in the Representation section. This version of the plot in Fig.10 is shown in Fig.11. FFT settings Fig.11. Linear (voltage) plot of the results from simulation of circuit in Fig.9. Practical Electronics | July | 2022 The FFT dialog (Fig.3) provides a variety of settings which we can use to control 57 Fig.12. Using the wrong time range for a periodic signal produces an inaccurate spectrum. the way in which the FFT is calculated. So far, we have only used the default settings. The number of samples used by the FFT can be changed. The default is 262144. Increasing this value will slow the FFT calculation but increase the maximum frequency for which the spectrum is valid (for a given time range of data used by the FFT). The transient simulation maximum time step should be sufficiently small when extending the FFT frequency range. As we are using a low frequency here the number of points can be reduced and still provide a reasonable spectrum. However, the accuracy will also reduce, so the peak may get wider and the noise floor higher. The time range over which the FFT is calculated (the window) can have a significant impact on the results, particularly for periodic waveforms like our sinewave. For such waveforms the range should be an exact number of waveform cycles. The FFT is effectively calculated on a waveform which is made up of infinitely repeated copies of the window range. Discontinuities at the ends of the range introduce errors into the spectrum – this is known as ‘spectral leakage’. By default, the entire simulation is used for the FFT. This may not be the best approach to use; for example, if a circuit takes several cycles to stabilise, it would be best to start any FFT analysis after this. The FFT time range can be set in LTspice by zooming in on the waveform, or by typing in specific time values. To try this, open the FFT dialog again and select ‘Specify a time range’ in the ‘Time range to include’ section. Change the ‘End time’ from 50ms to 49.5ms and click OK to obtain the spectrum. It will look like Fig.12. The peak is wide and slopes off slowly. If the signal is not periodic, windowing functions can be used to ‘fade out’ the edges of the time range to prevent any discontinuities causing spectral leakage. Normally, these would not be needed for periodic waveforms such as sines – if we correctly used an exact number of cycles. THD Analysis LTspice is able to perform THD analysis. Fig.13 shows the circuit from last month which we used to illustrate the output spectra from a couple of nonlinear functions. It is a good idea to check that the spectrum looks reasonable (using the FFT plot) before looking at THD results to make sure that the results will be valid. The .four SPICE directive on the schematic shown in Fig.13 instructs LTspice to perform a Fourier component and THD analysis on a periodic signal. The syntax of the .four directive is .four <frequency> [Nharmonics] [Nperiods] <data trace1> [<data trace2> ...] Fig.13. LTspice schematic for example THD analysis. The frequency parameter specifies the fundamental frequency for which the analysis is performed. This will usually be the fundamental frequency of the input signal. Nharmonics specifies the number of harmonics for which the Fourier components will be calculated (the default is 9). Nperiods specifies how many waveform periods back from the end of the simulation are used to perform the analysis. The default setting is 1, but changing to –1 causes the entire simulation to be used. The example in Fig.13 is: .four 1kHz 10 V(y1) V(y2) V(y3) Fig.14. Fourier component and THD analysis results for signal y2 in Fig.13. 58 This calculates ten Fourier components for a fundamental of 1kHz for signals y1, y2 and y3 using one waveform period. The results are accessed using View -> Spice Error Log. A table listing part of the results from the circuit in Fig.13 is shown in Fig.14. The THD for signal y2 is 10.4%. Practical Electronics | July | 2022