Silicon ChipCircuit Surgery - January 2025 SILICON CHIP
  1. Contents
  2. Publisher's Letter: Two new series for the magazine
  3. Feature: The Fox Report by Barry Fox
  4. Feature: Net Work by Alan Winstanley
  5. Feature: Max’s Cool Beans by Max the Magnificent
  6. Project: High-quality Microphone Preamplifier by Phil Prosser
  7. Feature: The History of Electronics, part one by Dr David Maddison
  8. Feature: Circuit Surgery by Ian Bell
  9. Feature: Techno Talk by Max the Magnificent
  10. Feature: The Quason VL6180X laser rangefinder module by Jim Rowe
  11. Project: USB to PS/2 Keyboard & Mouse Adaptors by Tim Blythman
  12. Project: Raspberry Pi-based Clock Radio, part two by Stefan Keller-Tuberg
  13. Subscriptions
  14. Feature: Precision Electronics, part one by Andrew Levido
  15. Project: Secure Remote Mains Switch, part two by John Clarke
  16. PartShop
  17. Market Centre
  18. Advertising Index
  19. Back Issues

This is only a preview of the January 2025 issue of Practical Electronics.

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

Articles in this series:
  • The Fox Report (July 2024)
  • The Fox Report (September 2024)
  • The Fox Report (October 2024)
  • The Fox Report (November 2024)
  • The Fox Report (December 2024)
  • The Fox Report (January 2025)
  • The Fox Report (February 2025)
  • The Fox Report (March 2025)
  • The Fox Report (April 2025)
  • The Fox Report (May 2025)
Articles in this series:
  • Win a Microchip Explorer 8 Development Kit (April 2024)
  • Net Work (May 2024)
  • Net Work (June 2024)
  • Net Work (July 2024)
  • Net Work (August 2024)
  • Net Work (September 2024)
  • Net Work (October 2024)
  • Net Work (November 2024)
  • Net Work (December 2024)
  • Net Work (January 2025)
  • Net Work (February 2025)
  • Net Work (March 2025)
  • Net Work (April 2025)
Articles in this series:
  • Max’s Cool Beans (January 2025)
  • Max’s Cool Beans (February 2025)
  • Max’s Cool Beans (March 2025)
  • Max’s Cool Beans (April 2025)
  • Max’s Cool Beans (May 2025)
  • Max’s Cool Beans (June 2025)
Articles in this series:
  • The History of Electronics, Pt1 (October 2023)
  • The History of Electronics, Pt2 (November 2023)
  • The History of Electronics, Pt3 (December 2023)
  • The History of Electronics, part one (January 2025)
  • The History of Electronics, part two (February 2025)
  • The History of Electronics, part three (March 2025)
  • The History of Electronics, part four (April 2025)
  • The History of Electronics, part five (May 2025)
  • The History of Electronics, part six (June 2025)
Articles in this series:
  • Circuit Surgery (April 2024)
  • STEWART OF READING (April 2024)
  • Circuit Surgery (May 2024)
  • Circuit Surgery (June 2024)
  • Circuit Surgery (July 2024)
  • Circuit Surgery (August 2024)
  • Circuit Surgery (September 2024)
  • Circuit Surgery (October 2024)
  • Circuit Surgery (November 2024)
  • Circuit Surgery (December 2024)
  • Circuit Surgery (January 2025)
  • Circuit Surgery (February 2025)
  • Circuit Surgery (March 2025)
  • Circuit Surgery (April 2025)
  • Circuit Surgery (May 2025)
  • Circuit Surgery (June 2025)
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)
Articles in this series:
  • El Cheapo Modules From Asia - Part 1 (October 2016)
  • El Cheapo Modules From Asia - Part 2 (December 2016)
  • El Cheapo Modules From Asia - Part 3 (January 2017)
  • El Cheapo Modules from Asia - Part 4 (February 2017)
  • El Cheapo Modules, Part 5: LCD module with I²C (March 2017)
  • El Cheapo Modules, Part 6: Direct Digital Synthesiser (April 2017)
  • El Cheapo Modules, Part 7: LED Matrix displays (June 2017)
  • El Cheapo Modules: Li-ion & LiPo Chargers (August 2017)
  • El Cheapo modules Part 9: AD9850 DDS module (September 2017)
  • El Cheapo Modules Part 10: GPS receivers (October 2017)
  • El Cheapo Modules 11: Pressure/Temperature Sensors (December 2017)
  • El Cheapo Modules 12: 2.4GHz Wireless Data Modules (January 2018)
  • El Cheapo Modules 13: sensing motion and moisture (February 2018)
  • El Cheapo Modules 14: Logarithmic RF Detector (March 2018)
  • El Cheapo Modules 16: 35-4400MHz frequency generator (May 2018)
  • El Cheapo Modules 17: 4GHz digital attenuator (June 2018)
  • El Cheapo: 500MHz frequency counter and preamp (July 2018)
  • El Cheapo modules Part 19 – Arduino NFC Shield (September 2018)
  • El cheapo modules, part 20: two tiny compass modules (November 2018)
  • El cheapo modules, part 21: stamp-sized audio player (December 2018)
  • El Cheapo Modules 22: Stepper Motor Drivers (February 2019)
  • El Cheapo Modules 23: Galvanic Skin Response (March 2019)
  • El Cheapo Modules: Class D amplifier modules (May 2019)
  • El Cheapo Modules: Long Range (LoRa) Transceivers (June 2019)
  • El Cheapo Modules: AD584 Precision Voltage References (July 2019)
  • Three I-O Expanders to give you more control! (November 2019)
  • El Cheapo modules: “Intelligent” 8x8 RGB LED Matrix (January 2020)
  • El Cheapo modules: 8-channel USB Logic Analyser (February 2020)
  • New w-i-d-e-b-a-n-d RTL-SDR modules (May 2020)
  • New w-i-d-e-b-a-n-d RTL-SDR modules, Part 2 (June 2020)
  • El Cheapo Modules: Mini Digital Volt/Amp Panel Meters (December 2020)
  • El Cheapo Modules: Mini Digital AC Panel Meters (January 2021)
  • El Cheapo Modules: LCR-T4 Digital Multi-Tester (February 2021)
  • El Cheapo Modules: USB-PD chargers (July 2021)
  • El Cheapo Modules: USB-PD Triggers (August 2021)
  • El Cheapo Modules: 3.8GHz Digital Attenuator (October 2021)
  • El Cheapo Modules: 6GHz Digital Attenuator (November 2021)
  • El Cheapo Modules: 35MHz-4.4GHz Signal Generator (December 2021)
  • El Cheapo Modules: LTDZ Spectrum Analyser (January 2022)
  • Low-noise HF-UHF Amplifiers (February 2022)
  • A Gesture Recognition Module (March 2022)
  • Air Quality Sensors (May 2022)
  • MOS Air Quality Sensors (June 2022)
  • PAS CO2 Air Quality Sensor (July 2022)
  • Particulate Matter (PM) Sensors (November 2022)
  • Heart Rate Sensor Module (February 2023)
  • UVM-30A UV Light Sensor (May 2023)
  • VL6180X Rangefinding Module (July 2023)
  • pH Meter Module (September 2023)
  • 1.3in Monochrome OLED Display (October 2023)
  • 16-bit precision 4-input ADC (November 2023)
  • 1-24V USB Power Supply (October 2024)
  • 14-segment, 4-digit LED Display Modules (November 2024)
  • 0.91-inch OLED Screen (November 2024)
  • The Quason VL6180X laser rangefinder module (January 2025)
  • TCS230 Colour Sensor (January 2025)
  • Using Electronic Modules: 1-24V Adjustable USB Power Supply (February 2025)
Items relevant to "Raspberry Pi-based Clock Radio, part two":
  • Raspberry Pi Clock Radio main PCB [19101241] (AUD $12.50)
  • Raspberry Pi Clock Radio display PCB [19101242] (AUD $7.50)
  • Software for the Raspberry Pi based Clock Radio (Free)
  • Raspberry Pi Clock Radio PCB patterns (PDF download) [19101241-2] (Free)
Articles in this series:
  • Raspberry Pi Clock Radio, Pt1 (January 2024)
  • Raspberry Pi Clock Radio, Pt2 (February 2024)
  • Raspberry Pi-based Clock Radio, part two (January 2025)
Articles in this series:
  • Precision Electronics, Part 1 (November 2024)
  • Precision Electronics, Part 2 (December 2024)
  • Precision Electronics, part one (January 2025)
  • Precision Electronics, Part 3 (January 2025)
  • Precision Electronics, part two (February 2025)
  • Precision Electronics, Part 4 (February 2025)
  • Precision Electronics, Part 5 (March 2025)
  • Precision Electronics, part three (March 2025)
  • Precision Electronics, part four (April 2025)
  • Precision Electronics, Part 6 (April 2025)
  • Precision Electronics, Part 7: ADCs (May 2025)
  • Precision Electronics, part five (May 2025)
  • Precision Electronics, part six (June 2025)
Items relevant to "Secure Remote Mains Switch, part two":
  • Secure Remote Mains Switch receiver PCB [10109211] (AUD $7.50)
  • Secure Remote Mains Switch transmitter PCB [10109212] (AUD $2.50)
  • PIC16F1459-I/P programmed for the Secure Remote Mains Switch receiver (1010921R.HEX) (Programmed Microcontroller, AUD $10.00)
  • PIC16LF15323-I/SL programmed for the Secure Remote Mains Switch transmitter (1010921A.HEX) (Programmed Microcontroller, AUD $10.00)
  • Firmware and ASM source code for the Secure Remote Mains Switch [1010921A/R] (Software, Free)
  • Secure Remote Mains Switch PCB patterns (PDF download) [10109211/2] (Free)
  • Front panel label and drilling diagrams for the Secure Remote Mains Switch (Panel Artwork, Free)
Articles in this series:
  • Secure Remote Mains Switch, Part 1 (July 2022)
  • Secure Remote Mains Switch, Part 2 (August 2022)
  • Secure Remote Switch, Part 1 (December 2024)
  • Secure Remote Mains Switch, part two (January 2025)
Circuit Surgery Regular Regular clinic clinic by by Ian Ian Bell Bell Topics in digital signal processing – Signal filtering in the digital domain W e are looking at various topics related to digital signal processing (DSP). DSP covers a wide range of electronics applications where signals are manipulated, analysed, generated, stored or displayed as digital data but originate from and/or are converted to real-world signals for interaction with humans or other parts of the physical world. Fig.1 shows the key elements of a generic DSP system with a signal path from an analog input via digital processing to an analog output. This does not necessarily represent every DSP system (not all have all the parts shown), but it serves as a reference for the various subsystems we will consider. Last month we discussed some basic concepts of digital signal processing and used a moving average function as an example. This month, we will continue to Analog In Antialiasing filter Sample and hold explore the moving average filter simulation and will also discuss the general structure of digital filters. Frequency responses in LTspice Last month, we obtained the frequency response of a five-point moving average digital filter using behavioural simulation in LTspice. LTspice may not be the most obvious tool to use for digital filters. However, with the help of its transient frequency response analysis (FRA), we obtained the response of a full system comprising sampling, anti-alias and reconstruction filtering and the moving average calculation. The behaviour of the digital filter was implemented with the help of the delay function available from LTspice’s behavioural voltage (and current) source. A delay applied to a (sampled) input signal is equivalent to holding sample data in Digital ADC Digital processing Analog DAC Reconstruction filter Fig.1: a generic digital signal processing (DSP) system structure. Out memory in a digital circuit or computer. Typically, digital filters perform operations on multiple samples, which is equivalent to a set of delays equal to integer multiples of the sampling period. This means we can use the signals obtained by a chain of delays in LTspice as the equivalent to the set of samples held in a DSP system’s memory. We can then use another behavioural source to implement the maths performed by the filter (in this case, taking the average of the five most recent input samples). The FRA in LTspice is a relatively new addition; it is useful because it allows us to obtain a frequency response in cases where AC analysis does not work, such as switched circuits. As we saw last month, AC analysis failed for the full moving average filter due to the sampling operation. FRA is relatively difficult to set up as it requires components to be added to the schematic and configured. For the example last month, it required a fairly cumbersome manual addition of analysis frequencies to obtain an accurate response graph. The FRA was designed for stability analysis of switched mode power supplies, so it is not optimised for what we were doing. AC analysis revisited Fig.2: a behavioural model in LTspice of a moving-average filter without sampling. Practical Electronics | January | 2025 If we only want the frequency response of the filtering operations, not the full sampling system, we can use an AC analysis by removing the sampling part of the system. We can include the antialiasing and reconstruction filtering, or not, as required. AC analysis of just the digital processing model provides the response of the filter itself, without the impact of the sampling and other filters. The LTspice schematic in Fig.2 is a version of the five-point moving average filter from last month without the sampling system. This allows us to run a standard AC analysis. As with the FRA, we cover the range of 100Hz to 5kHz, which is up to the Nyquist limit (half the sampling rate). 33 Fig.3: the simulation results for the Fig.2 schematic. The results are shown in Fig.3. The green trace shows the response of the system, including the antialiasing and reconstruction filters. The red trace shows the response of just the moving average filter by plotting V(y)/V(x4), that is, the gain from the input to the delay chain (on net x4) to the output of the source calculating the average (on net y). Comparing the two traces shows the impact of the antialiasing and reconstruction filters on the overall response – this is a reduction in gain as frequencies get closer to Nyquist rate and the filters have more impact. The response from FRA analysis (shown last month) is different from the two traces in Fig.3 because it also includes the effect of processing a sampled signal. The theoretical response of the moving average filter (as discussed last month) is shown in Fig.4. It closely matches the trace for the response of the moving average filter on its own from Fig.3 (red trace). This is as expected because this part of the LTspice schematic is directly performing the mathematics of the moving average function without any non-ideal circuit effects or additional influences. The simulations discussed last month and above show that LTspice can be used to obtain the frequency response of a basic digital filter with or without the effects of other parts of the system. We can also run transient analysis simulations to look at circuit waveforms. Last month, we pointed out that the moving average filter is not particularly good as a low-pass filter but is useful for removing x(n) y(n) Memory x(n–1) Processing y(n–1) x(n–2) Using N input samples and M output samples y(n–2) x(n–3) x(n–N+1) y(n–3) Memory y(n–M) Fig.5: the general structure of a digital signal processing system. 34 Fig.4: the calculated response of a 5-point moving-average filter. noise from pulse-based signals. Therefore, we will use this as the basis of an example transient simulation. The noise filtering example will work better with a filter averaging over more samples, which raises the issue of defining the function of the filter more effectively in LTspice – editing large expressions on the schematic is not particularly easy. This means we should define the general structure of digital filters in more detail, so we will discuss that before returning to the example. Digital filter structure in this form, it is clear that in general we do not have to multiply all the samples by the same value. If we use different values, we no longer have a moving average filter; by choosing suitable values, we can create filters with different characteristics. We will return to discuss this aspect of digital filters next month. Assuming we multiply each sample by a different value, we can write a general equation for a filter using five input samples as: y(n)=a0x(n)+a1x(n−1)+ a2x(n−2)+a3x(n−3)+a4x(n−4) The general structure of a digital signal The values a0, a1, … a4 are referred to processing system was discussed last month and illustrated in Fig.5. This as the coefficients of the filter. The colis labelled a little differently from last lection of coefficients is also referred to month to show N and M as the number as the filter kernel. of input and output samples used by the We do not have to use exactly five processing, respectively. This is for comsamples, of course, so we can further patibility with the discussion to follow. generalise to using N samples (as deTo recap, the two memories shown in picted in Fig.5). In this case, each term Fig.5 hold past input (x) and output (y) in the summation is of the form aix(n-i), ) + x ( n−2 ) + x ( n−3 ) + x0( n−4 n−1 x ( n )i+isx (an sample data, which can be used in the y ( nwhere integer ranging from to ) )= 5 calculations that deliver the required N-1. We can then write: function. Not all systems have output N −1 y ( n )= ∑ ai x ( n−i ) memory and therefore obtain the output i=0 as a function of just the current and past input samples. This includes the moving With N=5, this is equivalent to the average filter we have been discussing. previous equation. The symbol Σ is the Last month, we wrote the five-point summation operator, which we met in moving average filter function as: the August 2024 Circuit Surgery article. It provides a compact way of writing the x ( n ) + x ( n−1 ) + x ( n−2 ) + x ( n−3 ) + x ( n−4 ) summation of the aix(n-i) terms. y ( n )= 5 In the August issue, we discussed the N −1 Recall thaty (x(n) current input, mathematical operation called convo( ) n )= ∑isathe x n−i i i=0input and so on. The x(n-1) the previous lution. If you recall that or are already average is formed in the standard way: familiar with the mathematics, you will add up all the values and divide by the see that the above equation is a convolunumber of values (five in this case). We tion of the coefficients with the samples. can also write the same function as: The above equation allows us to draw a more detailed version of Fig.5 that depicts y(n)=0.2x(n)+0.2x(n−1)+ the actual processing being performed in 0.2x(n−2)+0.2x(n−3)+0.2x(n−4) a filter (Fig.6). The memory is shown as a chain of delays, each equal to the samThe filter output sample is calculated pling period. These are labelled Δt, with by multiplying each sample by 0.2 and the Greek letter delta (Δ) for difference and adding up the resulting values. Written t for time (a delay is a time difference). Practical Electronics | January | 2025 a0 x(n) × a0 a0x(n) y(n) Σ x(n) × a1 Δt x(n–1) × a1 a1x(n–1) Σ Δt x(n–1) Δt × a2x(n–2) Σ Δt x(n–2) x(n–N) × Memory a1x(n–1) × a2x(n–2) Σ y(n) aN aN Δt × a2 a2 x(n–2) a0x(n) aNx(n–N) Δt x(n–N) Memory Processing Fig.6: the structure of a finite impulse response (FIR) filter. × aNx(n–N) Processing Fig.7: a finite impulse response filter with alternative depiction of the addition part. but if you are new to DSP it useful to know it exists and may merit further investigation. For commonly used filters, you do not have to develop theory from scratch using these transforms to produce designs, and there are tools available to assist with calculations. In Fig.6, the circular blocks labelled × are multipliers that multiply the samples by the relevant coefficients. This could be performed by hardware multipliers or in software. The circular blocks labelled Σ are adders that perform the summation of all the aix(n-i) terms. Again, these could be hardware adders or software operations. The structure in Fig.6 is of a chain of adders, which corresponds with a sequence of operations in software or circuit structured that way. An adder circuit could be structured differently (eg, as a tree of adders rather than a chain). The diagram in Fig.7, which shows all the terms being added but with no specific adder arrangement, is also commonly used to show the structure of this type of filter. The general system structure shown in Fig.5 includes the processing of output samples. Digital filters can also use output samples similarly to the use of input samples already discussed. That is, output samples can be multiplied by a coefficient (bn) and included in the summation that produces the output. The structure of such a filter is shown y(n) in Fig.8, in which M b1 previous output samples are used in the b1y(n–1) y(n–1) × Δt calculation of the filb2 ter’s current output. The structure of the memory section is very similar to the delay chain (B1 to B4) in the LTspice model in Fig.2. The Δt delay blocks can be implemented as registers in a (multi-bit wide) shift register in a digital circuit clocked at the sample rate, or as locations in a buffer memory used to store the samples in a software implementation. If you look at similar diagrams to Fig.6 on websites or in textbooks, the delay blocks will often be labelled with z -1 rather than Δt. This relates to the use of a mathematical technique called the Z-transform. This is similar to the (probably better known) Laplace transform. In both cases, time domain representations of signals and circuit responses are converted to an alternative form (called the Laplace domain and z-domain), facilitating analysis and design. In both cases, the alternative domain is based on a complex-number representation of frequency (not simple frequencies in Hz). The Laplace domain is used for continuous time signals and the z-domain for sampled signals. In the z-domain, multiplying by z-n creates a delay of n sampling periods, hence the labelling of a single sample delay block with z-1. We will not investigate the Z-transform in detail due to the strong maths involved, a0 x(n) × a0x(n) Σ Σ a1 Δt x(n–1) × a1x(n–1) Σ Σ Σ Σ a2 Δt x(n–2) × a2x(n–2) b2y(n–2) aN Δt Memory x(n–N) × × y(n–2) Δt bM aNx(n–N) bMy(n–M) Processing × y(n–M) Δt Memory Fig.8: the structure of an infinite impulse response (IIR) filter. Practical Electronics | January | 2025 Impulse response We have discussed impulses in previous articles. For continuous time systems, an impulse is an infinitely short pulse of unit area, but for sampled signal processing, it is a single sample of value 1. It is useful to consider what happens if we apply an impulse to a filter – the result is called the impulse response, and signal processing theory shows that this defines the behaviour of the filter. Consider an impulse input to the filter in Figs.6 & 7. Initially, all the samples in memory and the input x(n) will be zero. For one sampling period, x(n) will change to 1 and then return to 0 for all time. When x(n)=1, the output y(n) will be a0x(n) = a0, as all other samples in the memory at the time will be zero and will not contribute anything to the summation total. On the next sampling period, x(n)=0 and the 1 will move to x(n-1) in the memory structure. It will remain the only non-zero sample value contribution to the summation. So, the output will then be a1x(n) = a1. On the next sampling period, the 1 will move to the x(n-2) memory location and, following the same pattern, the output will be equal to a2. Thus, with an impulse input, the filter will deliver the values of its coefficients in sequence, so the impulse response clearly defines the filter (the coefficients are key to the specific response). After all the coefficients have been fed out, the output will then be zero for all time. Thus, the period over which the filter produces a non-zero output from an impulse input is finite. This type of filter is therefore called a Finite Impulse Response (FIR) filter. The filter structure shown in Fig.8 has a more complex behaviour. Past output samples are fed back into the filter, so once the output has taken a non-zero value, it will circulate around the output sample processing loop for all time. Therefore, filters with this structure will produce an infinitely long non-zero output from an impulse input and are referred to as Infinite Impulse Response (IIR) filters. Real IIR filter implementations may return to zero output after an impulse input due to numerical precision limitations. LTspice functions Having discussed digital filter structures in general terms, we will return to our LTspice simulations. The previous example had five coefficients, but real filters may have many more, and all the coefficients all may be different. Also, for accurate operation, the coefficients need to be defined to a good number of decimal places. This requires a lot of text to define the filter function (eg, the expression for V in the B5 source in Fig.2), which is difficult to edit in the source value dialog. 35 Fig.9: an LTspice simulation of a moving-average filter applied to a noisy pulse waveform Behavioural sources can be defined in terms of numerous built-in functions, but it is also possible to write your own functions using the .func directive. We can use this feature with the built-in table function to define a look-up table of coefficients. For example, we can define a function called a that defines five coefficients of values 0.2, 0.4, 0.5, 0.3 and 0.1 as follows: .func a(i){table(i,0,0.2,1, + 0.4,2,0.5,3,0.3,4,0.3)} As with any SPICE directive, this can be placed on the schematic. The table function uses the first parameter (i in this case, which is passed to the function) to look up values defined by the number pairs given in the remaining parameters. So, for example, using a(2) in a behavioural source expression would be equivalent to 0.5, and a(0) would be 0.2. The table function can actually interpolate between the defined values, but this is not needed for coefficient look-up. Writing the function with one coefficient on each line makes reading and editing it easier, so a better format is: .func co(i) {table(i, + 0,0.2, + 1,0.2, + 2,0.2, + 3,0.2, + 4,0.2)} 36 This is the same code with extra line breaks. We must put a + character at the start of each new line of the function; this is called a line continuation. SPICE directives must normally be on a single line, but line continuation provides a means of formatting a single line across multiple lines of text as viewed by the human reader. The compiler treats lines starting with + as part of the previous line. With the coefficient function defined, we can write another function to implement the filter, for example, with five coefficients and five samples (x0 to x4) passed to the function: .func filter(x0,x1,x2,x3,x4) { + a(0)*x4+a(1)*x3+a(2)*x2+ + a(3)*x1+a(4)*x0 } Again, this can be put in the schematic, but for larger numbers of coefficients, it becomes easier to use a separate text file instead. To do this, put the code for both functions in a single text file and use the SPICE .inc directive to use it as part of the simulation. For example, if the file name is “filter_def.txt”, put the SPICE directive .inc filter_def.txt in the schematic (as a directive, not just text). The behavioural source implementing the filter output is then defined using the filter function, for example: V=filter(V(x0),V(x1),V(x2), + V(x3),V(x4)) Here, V(x0) to V(x4) are the input sample voltages after the various delays, as in Fig.2. Transient filter simulation The schematic shown in Fig.9 is similar to Fig.8 from last month. It is a moving-average filter using 11 rather than five samples. The coefficients and filter function are defined in the file “MA11.txt”, which follows the pattern of the code discussed above. The 11 coefficients are all equal to 0.0909091 (an approximation of 1/11). The simulation includes the sampling process (unlike Fig.2, although we did include it in last month’s circuit). The sampling period is 10μs (equivalent to a 100kHz sampling frequency). The reconstruction and antialiasing filter cutoffs are set to fs/4, which is 25kHz, well below the Nyquist frequency limit (50kHz) to allow for the filter’s roll-off. The input signal is a pulse waveform with noise added to it. A clean pulse waveform, a 1V square wave with 4ms cycle time (2ms pulses) is created by source V1, and behavioural source B12 adds random noise to this using the white function. The white function produces random number between -0.5 and 0.5, smoothly transitioning between values. Here, it is configured to change the voltage every 1/50k seconds (20us). This is achieved Practical Electronics | January | 2025 Fig.10: the results of the LTspice simulation of the configuration shown in Fig.9. by passing 50k*time to the white() function. The maximum noise amplitude is larger than the pulse amplitude, which produces a signal that would likely be difficult to use without filtering. Specifically, the largest peak-to-peak output possible from the noise source is 1.15 V (due to 1.15*white() in the expression for V), but most of the time the amplitude is lower. The harmonics of the square wave are very small beyond the Nyquist limit, being 1/200th of the signal amplitude or lower. The noise voltage changes only every 20μs, which means that the noise spectral amplitude is also reducing above the Nyquist frequency. These factors, combined with the filter, prevent too much aliasing. Components A1 and A2 are logic buffer gates with logic thresholds of 0.5V and Practical Electronics | January | 2025 1.0V (the default for LTspice logic gates). These are connected to the input (A1) and filtered output (A2) respectively of the moving average filter. The setup in this example is not designed to meet any specific use-case or design requirement. It is just configured to produce some example waveforms and illustrate how the sampled signal processing function can be included in an LTspice transient simulation. The results are shown in Fig.10. The upper trace (green) is the clean input square wave. The second trace (red) is the sampled square wave plus noise at the input to the moving average filter (signal x10). The fact that this is sampled cannot easily be seen at this zoom level. The third trace (cyan) shows the output of the logic buffer with x10 as the input. Ideally, this should be equal to the source signal, but the noise causes unwanted switching (glitches). The fourth trace (orange) is the reconstructed output of the moving average filter, illustrating the reduction in noise with respect to the input. The bottom trace (magenta) is the output of the logic buffer connected to the filter output. This is very close to the source signal (no glitches), with some delay due to the processing time PE of the filter. Simulation files Most months, LTSpice is used to support descriptions and analysis in Circuit Surgery. The examples and files are available for download from the PE website: https://bit.ly/pe-downloads 37