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:
Articles in this series:
Articles in this series:
Articles in this series:
Articles in this series:
Articles in this series:
Articles in this series:
Items relevant to "Raspberry Pi-based Clock Radio, part two":
Articles in this series:
Items relevant to "Secure Remote Mains Switch, part two":
|
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
|