This is only a preview of the May 2024 issue of Practical Electronics. You can view 0 of the 72 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:
|
Circuit Surgery
Regular clinic by Ian Bell
Topics in digital signal processing – sampling
T
his month, we will start to
look at various topics related to
digital signal processing (DSP).
DSP covers a wide range of electronics
applications where real-world signals
such as sound, images, temperature, pressure, position and speed are converted
to digital data for manipulation, analysis, storage and display. The resulting
processed digital information, or new
information which has been generated
digitally, can also be converted into realworld signals for human consumption
(eg, audio and video) or manipulation
of the physical world (eg, mechanical
and thermal). These systems can be relatively complex (for example, streaming
movies from the internet or control of
industrial robots) or relatively simple
(for example, a basic microcontroller
measuring temperature).
Fig.1 shows the key elements of
DSP systems, with a signal path from
analogue input via digital processing to
analogue output. The input signal passes
through an analogue antialiasing filter
which removes frequencies that would
otherwise cause errors. A sample-andhold circuit captures the filtered input
signal at the points in time at which it is
to be digitised. The process of sampling,
and the use of antialiasing filters are key
concepts in DSP and will be discussed in
more detail in this article. An analogueto-digital converter (ADC) produces
digital codes that represents the values of
the analogue signal at the input sampling
instants. The digital processing performs
operations on the digitised signal (data);
this can be implemented directing in
digital hardware or by software running
on a processor.
The result of the digital processing
of the input signal (or results of direct
digital generation) are converted to
analogue values at the output sample
times using a digital-to-analogue
converter (DAC). The output of the
DAC is a ‘stepped’ waveform which
is converted to a smooth analogue
waveform by the reconstruction filter.
Fig.1 shows key components in a generic
DSP system, but not all DSP systems
will contain all these components. For
example, digital generation does not
require the input half from Fig.1 and the
system’s output may not require a DAC
if it is just one or more on/off controls,
or a pulse-width-modulated signal.
Sampling and quantisation
We assume that analogue signals can have
an infinite number of possible values
between their upper and lower limits
(peak values). We also assume that their
value may be different (by some arbitrary
small amount) after an infinitely small
change in time. This is not strictly true
when you consider that electric current
depends on discrete charge carries (eg,
electrons) and quantum effects come
in to play when at sufficiently small
scales. However, for the majority of
analogue electronic circuit designs it
is reasonable to assume this property
for analogue signals.
On the other hand, digitised analogue
signals can only have a finite number
of possible values. This is because they
are represented by digital code values,
which have a finite number of bits.
An ADC/DAC with an N-bit output/
input digital word can handle at most
2N different values. When an analogue
signal is digitised, the process of going
from an effectively infinite number of
possible values to a finite one is called
quantisation and is an inherent property
of ADCs. We will look at ADCs (and
DACs) in more detail in a later article.
Digitised (sampled) analogue signals
also only change to a new value after
a finite amount of time – this is the
fundamental nature of sampling, where
the signal value is captured and stored
for processing at specific points in
time. Sampling usually occurs with a
fixed time interval (called the sample
period, T) between sample points. A
sampling period of T corresponds with
a sampling frequency (or rate) of 1/T.
Sampling frequency is a key performance
parameter of DSP systems. In the most
basic approaches, the sampling frequency
is the same throughout the system, but it
is also possible to use different rates in
different parts of a system. For example,
initially sampling the analogue signal
at a relatively high rate, but effectively
resampling (within the digital part) to a
lower rate for performing the processing
operations (this is called ‘downsampling’
or ‘decimation’).
A signal defined at a set of sample
times is referred to as a discrete time
signal, to distinguish it from a continuous
time signal. Digital (DSP) circuits process
discrete time signals, but it is also possible
to build analogue circuits which work with
sampled signals. These use continuousvalue analogue signals that update to new
values at each sample point. Switchedcapacitor circuits are an important example
of this technology, and are widely used
in integrated circuit design.
Sampled signals
Fig.1. Generic DSP system structure.
The top plot in Fig.2 shows a set of
sample points, with sample period T,
on an analogue waveform. The sampled
signal itself, in its most general form, is
just a set of values associated with the
sample times. It does not have a value
between these times – this is illustrated
in the middle waveform in Fig.2. This
representation fits directly with digital
data – a set of discrete values stored in
a digital circuit, computer memory or
other media.
If sampled values are processed in
an analogue circuit, then the signal is
often held constant between sample
points and waveforms typically have
a stepped appearance, like the bottom
Out
plot in Fig.2. This is the waveform
that would be seen at the output
of both the sample-and-hold and
Practical Electronics | May | 2024
63
Analogue
In
Antialiasing
filter
Sample and
hold
Digital
ADC
Digital
processing
Analogue
DAC
Reconstruction
filter
Continuous waveform with sample points
T
Samples
Sample and hold
Fig.2. Sampled waveforms.
DAC blocks of the generic DSP system
shown in Fig.1. The input waveform
is sampled at each sample time and
held at this value until the next sample
time. At the ADC input the flat parts of
the waveform provide the ADC with
a constant input during the time it
takes to perform the conversion. If the
value being converted is not exactly
equal to the signal at the sample time
then errors will be introduced into the
digitised signal.
The stepped waveform (particularly
at the DAC output) is a called a zeroorder hold. It represents the creation, or
reconstruction, of a continuous-in-time
analogue signal from a discrete set of
samples. This is an interpolation process
– we need to fill in the gaps between the
sample points to create a waveform that
is continuous-in-time. This is similar
to finding the ‘line of best fit’ for data
points on a graph.
The term ‘zero order’ refers to the
mathematical principle of approximating
a function (or set of data points) with a
polynomial. A polynomial function of
x is of the form a0+a1x+a2x2+a3x3… and
so on, where the an terms are numerical
values. The order of the polynomial is
the highest power of x used. A zero-order
polynomial just has a constant value – a
flat line that does not change with x (or
t for time in the case of the waveform).
It is possible to create higher-order
s(t)
xC(t)
X
xs(t) = s(t)xC(t)
Fig.3. Simple model of sampling a signal
with a pulse train.
64
‘holds’; for example,
p
Area = p x 1
a first-order hold
1
produces a straightline value change
t
0
between sample
–1T
1T
2T
3T
t i m e s . H o w e v e r,
these are relatively Fig.4. Sampling pulse train s(t).
rare/advanced
techniques in DSP systems.
contains frequencies relatively close to
If we sample an analogue
the sampling frequency.
signal, convert it to digital
Moiré aliasing patterns occur when
t
with an ADC and then convert
a digital camera has trouble imaging
it directly back to an analogue
an intricate pattern, the result is
signal with a DAC, the
visual artefacts in photos or videos.
resulting zero-hold signal is
A well-known example is a finely
not an accurate reconstruction
striped or patterned shirt (eg, on a TV
of the original signal, However,
presenter) causing strange waves or
assuming the sampling
swirl patterns to appear in a digital
t
was performed correctly,
image. This happens when the pixel
the original signal can be
size is similar to the pattern size and
reconstructed by passing the
is less common than it used to be due
DAC output through a lowto the increase in resolution of digial
pass filter – the reconstruction
cameras and video presentation.
filter shown in Fig.1.
Any sampling process can be
affected by potential ambiguities. This
includes manually making real-world
Aliasing
measurements (eg, of temperature).
The first block in Fig.1 is called the
Imagine measuring the temperature in
‘antialiasing filter’, so an obvious
a garden once a day in the afternoon
question is, what is aliasing and why
and plotting the results. The shape of
do we need a filter for it? Aliasing is a
the graph is likely to be an inaccurate
problem which can potentially occur
representation of the temperature
in any signal processing system which
changes. For example, there is likely to
samples the signal. This includes all
be a daily cycle with lower temperatures
digital audio and video signal processing.
at night. An actual near-constant
Digital imaging sensors such as cameras
temperature and strong day-night
and scanners can also suffer from aliasing
cycle with similar temperature each
effects. Aliasing is so called because it
afternoon will produce the same form of
causes ambiguities in the sampled data,
graph with this measurement schedule.
that is, there could be more than one
Measuring every hour would give a
signal which resulted in the same set
much more accurate picture of what
of sample values – thus one signal is an
was happening. The choice of sample
alias of the other. Aliasing effects can
rate requires an understanding of the
be observed in everyday life; one oftensignal being sampled.
quoted example is the ‘wagon wheel
effect’ where the wheels of a vehicle in
a film or video (perhaps a stagecoach
Sampling concepts
in a western, as the name suggests)
and nomenclature
appear to be turning at the wrong speed,
An idealised sampled signal (x s (t)),
backwards or even stationary. Search
which only exists at the sample points,
online for ‘wagon wheel effect’ videos
can be approximated by multiplying the
if you would like to see some examples.
continuous signal (xc(t)) by a train of
A video camera samples the scene in
short-duration, unit-amplitude pulses
front of it at a certain number of frames
(s(t)). This is a conceptual model rather
per second. If the wheel of a vehicle
than practical implementation and is
in the shot rotates an exact number of
illustrated in Fig.3. The pulse train
whole revolutions in the time between
signal is shown in Fig.4. The duration
frames it will appear to be in the same
of each pulse (p) is ideally zero.
position in each frame. When the film
There is a potential problem here in
is viewed the wheel will appear to be
that a unit amplitude pulse of duration
stationary despite the fact that the vehicle
zero simply disappears. As p reduces to
is obviously in motion. However, if the
zero the area under the pulse on the graph
wheel was rotating much more slowly,
(p × 1 – see shading on Fig.4) becomes
or if the video frame rate was much
zero. Fortunately, there is a mathematical
higher, we would get a large number
solution – a function called the unit
of frames per revolution and playback
impulse or Dirac delta (written as d(t))
would look as it should. This leads us
which is defined to be zero everywhere
to the insight that problems will occur
except at time zero, but does ‘exist’ in
if the input (the scene in this case)
the sense that the area under the Dirac
Practical Electronics | May | 2024
Fig.5. LTspice schematic to create a sampled waveform.
which is what we generally refer to as
a waveform. The symbol t is taken to
refer to continuous time, and the original
continuous signal (xc(t)) has defined
values for all values of t. However, for the
sampled signal the value is only defined
at integer (n) multiples of T, that is nT,
so we can write xs(t) as x(nT). With the
‘s’ subscript being less necessary as nT
implies sampling.
Since T represents the time difference
between samples it is also sometime
written as DT, the symbol D (upper-case
delta) meaning a difference between
values of a variable. In its most abstract
form, a set of samples is just a sequence
of values, and we can refer to these using
an index for the position in the sequence,
that is x(n) rather than x(nT). For example,
we can describe calculations performed
on sampled data, where each step occurs
at the sampling interval, just using x(n)
and other relative samples in the sequence
such as x(n – 1) and x(n + 1) to reference
the values used in the calculation. If you
look at websites and books on DSP you
will often see terms such as x(nT), x(nDT),
x(n) and d(t) and variants of these in the
equations used in these sources.
LTspice sampling pulses
Fig.6. Results from simulating the LTspice schematic in Fig.5.
delta pulse is not zero, despite its zero
duration. The area under the Dirac delta
is by definition, a value of one. This is a
useful mathematical abstraction which
occurs frequently in the mathematics
of sampled signal processing and is
widely used in other areas of physics
and engineering. It is worth being aware
of the impulse concept even if you do
not want to get into all the advanced
maths that it gets used in.
The sampling pulses occur at multiples
of sampling period T; that is, there is
a pulse at time t = 0, t = T, t = 2T, t =
3T and so on. There can also be pulses
before the nominal reference time zero
(t = 0), at times t = –T, t = –2T and so
on. In general, mathematical analysis
of signals often assumes they are of
infinite duration.
The term x(t) means the value of
the signal x as a function of time (t),
We can create sampled signals based
on the model illustrated in Fig.3 and
Fig.4 using LTspice. This can help us
illustrate sampling concepts such as
aliasing. The LTspice model in Fig.5
creates a 1kHz sinewave using source
V1. A 9kHz sampling pulse train is
created using source V2. The sinewave
and sampling pulses are multiplied by
behavioural source B1 to produce the
samples signal in the manner shown in
Fig.3. The pulse width of the sampling
pulses is chosen to be small but easily
visible on the plots because our main
aim here is to create illustrative graphs.
The result of the simulation is shown
in Fig.6. The top trace (red) is the 1kHz
sinewave. The second trace (green)
shows the sampling pulse train and the
third (cyan) the sampled waveform. The
bottom trace has the original sinewave
and sampled waveform shown together
to confirm that the sample pulses align
with the original waveform.
Fig.7 shows a couple more sinewave
signals added to the schematic in Fig.5;
Introduction to LTspice
Fig.7. Adding more sinewaves to the schematic in Fig.5.
Practical Electronics | May | 2024
Want to learn the basics of LTspice?
Ian Bell wrote an excellent series of
Circuit Surgery articles to get you up
and running, see PE October 2018
to January 2019, and July/August
2020. All issues are available in
print and PDF from the PE website:
https://bit.ly/pe-backissues
65
Fig.8. Results from simulating the LTspice schematic in Fig.7. All three waveforms will produce the same set of samples and are
therefore indistinguishable after sampling.
Fig.9. Sampling at exactly the Nyquist rate.
these are 8kHz and 17kHz. The simulation
results in Fig.8 show that the sample
pulses from sampling the 1kHz sinewave
at 9kHz exactly line up with the other two
sinewaves. That means that if we sample
a 1kHz, 8kHz or 17kHz sinewave at 9kHz
we get the same set of samples. Thus, once
we only have the sample data (further
through the signal processing system, or
in a digital storage), we cannot tell if the
data was originally due to a 1kHz, 8kHz
or 17kHz sinewave. In fact, there are many
more sinewaves that will also produce the
same sample data with 9kHz sampling.
Another way to look at this is that if we
sampled an 8kHz sinewave at 9kHz then
when the signal was reconstructed (eg,
when playing a digital audio recording)
we may end up with a 1kHz tone instead
of the original 9kHz. The output contains
something that was never there in the
first place – just like the illusionary
stationary or backward-turning wheels
on the ‘wagon’. Real audio signals, such
as voice and music, are generally much
more complex than single sinewaves, but
the same aliasing processes can occur
during sampling. The resulting output
may contain tones which were not in
the input, resulting in a loss of fidelity.
Nyquist rate
Fig.10. Waveforms of sampling at exactly the Nyquist rate, example 1.
66
Mathematical analysis of the process of
sampling a signal shows that signals with
a frequency of less than half the sampling
frequency are reliably represented in
the sampled data. This is known as the
Practical Electronics | May | 2024
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:
https://bit.ly/pe-downloads
rather than 9kHz (that is exactly twice
the 1kHz signal frequency).
Example results are shown in Fig.10
where it can be seen that the samples
exactly align with the peaks in the
sinewave. These samples will produce
a square wave at 1kHz when passed to a
DAC, which will in turn produce a 1kHz
sinewave when passed through a suitable
reconstruction filter – the sampled signal
is successfully recovered. However,
Fig.11. Waveforms of sampling at exactly the Nyquist rate, example 2.
Fig.11 shows the same situation except
Nyquist sampling theorem (also known as the sampling theorem,
that the sample pulses are shifted in time (the Tdelay parameter
the Nyquist-Shannon theorem and the Whittaker-Nyquistfor the pulse source V2 is changed to 0 instead of 250µs).
Shannon sampling theorem, the names honouring people who
The sampling aligns with the zero crossing of the sinewave,
first published significant work on the topic). The theory shows
so all the samples are zero and therefore the sampled signal
that if the input does not contain any frequency components
cannot be recovered.
at or beyond half the sampling frequency then it is possible to
perfectly reproduce the original signal from the sample data.
Antialiasing filters
A sampling frequency of twice the maximum signal frequency
If we remove all frequencies at and above half the sampling
is called the Nyquist frequency or Nyquist rate. Sampling at
frequency from the input before sampling it, then we will
less than this frequency is called ‘undersampling’ (which does
not have any problems with aliasing. This is the job of the
have its uses). Sampling at a rate significantly higher than
antialiasing filter. By sampling at just over the Nyquist rate
the Nyquist rate is called oversampling and facilitates low
it is theoretically possible to perfectly recover the sampled
-noise, low-distortion signal processing. Sampling at exactly
signal, but doing so implies a close-to-ideal antialiasing filter
the Nyquist rate may enable the signal to be recovered but the
– we will discuss this in more detail in another article. In
results may be wrong for specific alignment of the samples and
cases where there is certainty that the sampled signal does not
waveform. This is show in the simulation in Fig.9 to Fig.11. The
contain frequencies at and above half the sampling frequency
schematic (Fig.9) is similar to Fig.5 but with 2kHz sampling
an antialiasing filter is not required.
Antialiasing filters are not only required in audio and video
signal processing; the image sensors in digital cameras are
spatially sampling the image projected by the lens onto the
sensor. If the image contains small repetitive details aliasing
can occur. To overcome this, an optical filter is used to
provide an amount of blurring which will prevent aliasing.
However, in recent years more digital cameras have become
available without anti-aliasing filters. These provide better
image quality in situations where aliasing would not occur.
NEW!
5-year collection
2017-2021
All 60 issues from Jan 2017
to Dec 2021 for just £44.95
PDF files ready for
immediate download
See page 6 for further details and
other great back-issue offers.
Fig.12. The
moiré effect in
photography is an
example of aliasing.
It creates artifical
stripey patterns
when images are
captured with
repeating patterns
that match the
spatial frequency of
the camera’s sensor
pixels.
Purchase and download at:
www.electronpublishing.com
Practical Electronics | May | 2024
67
|