Basic sampling theory
Overview
Introduction/motivation Sampling basics A/D conversion Nyquist frequency Binary numbers and quanti ation !achine representation Bitwise operations in " Summary
#ou may have heard of$$$
%oltage and current &Ohm's law( )%*( )"*+
Operational amplifiers
*inear dynamic circuits
Sinusoidal signals All of these concepts are defined in continuous time
Analog filters
,lectronic systems
In the -old days. people constructed all electronic systems using purely analog devices 0pper vertical
deflection plate
"athode
/lass tube &vacuum+
"ontrol electronics *ower vertical deflection plate
,lectronic systems
,lectronic systems are very often used together with non1 electronic devices2 motors( loudspea3ers( antennas( *,D displays( etc$ etc$
!otor current
D"1motor
5egulator
%oltage proportional to speed
4achometer
!odern electronic systems
5oughly since the 6789's( analog systems have gradually been replaced with digital designs( because2
4hey are cheaper 4hey are easier to design 4hey are (re-)programmable
A general system framewor3
Digital/Discrete
Actuator
;ower supply :eadphones Antenna 5udder( engine( propeller
Analog/Continuous
Computer
System/reality
Speed controller !;< encoding =iltering 5oute planning
!otor :uman ear Atmosphere Supertan3er
Sensor
4achometer !icrophone Antenna /;S receiver
"ontinuous signals
4he signals and physical phenomena we observe around us are normally continuous:
"urrents and voltages in the power grid >ater flow in a district heating system 5otation speed of an engine ,tc$ etc$$$
,lectrocardiogram
,lectromyogram
Signals vs$ components
Signal "omponent/ subsystem Signal
"omponent/ subsystem
"omponent/ subsystem
Signal
"omponent/ subsystem
Signal
Discreti ation of signals
"omputers only -understand. -numbers. :ence( continuous signals must be discretized in order for computers to be able to process them 4his is 3nown as sampling
Amplitude
"ontinuous time
Amplitude Discrete time
Sampling
"ontinuous time2 x Discrete time2 x
x&t+( x R! t R x&t+( x R! t "#! # R+! " Z
After sampling( we obtain a se$uence o% discrete &alues2 x" x&"#+
# ? s @ is called sampling time %s A 6/4 ? : @ is called the sampling %re$uency &sometimes also given in ?rad/s@+
Sampling 1 schematics
D/A converter
Actuator
Algorithm
4imer
A/D converter
Sensor
A/D "onversion B the result
"ontinuous1time signal Sampling process
Sampled signal
Information in sampled signals
Say we want to sample the following sinusoidal signalC how do we choose an appropriate sampling frequencyD
Sampling with E9 :
Sampling with F9 :
Sampling with 699 :
Sampling with 6EG :
Sampling with E99 :
Sampling with <99 :
Sampling with G99 :
Nyquist frequency
4he Nyquist1Shannon sampling theorem2 '% a %unction x(t) contains no %re$uencies higher than ( hertz! it is completely determined by gi&ing its ordinates at a series o% points spaced ) / &*(+ seconds apart+
"onversely( it is impossible to reconstruct a continuous1time sinusoidal signal containing frequencies higher than half the sampling frequencyH 4his frequency is called the ,y$uist %re$uency In practice( it is difficult to detect frequencies of more than about I of the sampling frequency
:ow fast should one sampleD
=ast enough C1+ But fast sampling comes at a price( both in terms of more eJpensive A/D converters and faster processing equipment( e$g$( microcontrollers$ Also( if one samples too fast( the information in the signal may be lost in noise etc$ 5ule of thumb2 G %b K %s K E9 %b where %b is the -bandwidth. of the system
Sinusoidal signal models
Loseph =ourier discovered that any repetiti&e signal may be written as a &potenitially infinite+ sum o% sines/cosines(and.idth refers to the frequencies where the amplitude of the sine/cosine components have non1 negligible amplitude$
Loseph =ourier( 68MN16N<9
Sinusoidal signal models
=requency spectrum
A frequency spectrum is a graph that shows how much 'power' each sinusoidal component contributes with
A 6$7 sin&9$N t+ 9$7 sin&N$< t+ 9$E sin&E$< t+ 9 9$N : E$< : N$< : %
Aliasing
In old movies you could sometimes see wagon wheels rotating -bac3wards. 1 this is an optical illusion caused by a limited sampling frequency in the film cameras used$
Aliasing
O<99
O6G9
Image <
Image E
Direction of motion
Image 6
Aliasing B a -false. frequency appearing in a sampled signal
>hen a frequency in a continuous1time signal eJceeds the Nyquist1frequency( but is still below the actual sampling frequency( we see a -reflection. of that frequency in the sampled signalC this is 3nown as aliasing
Increasing frequency content in an analog signal( fiJed %s
"omputing the frequency of the aliased signal
4he frequency of the aliased signal( %a ( is found via the eJpression %a&,+ A P % B , %s P
%s is the sampling frequency % is the actual signal frequency , is an integer
=or eJample$$$
=ind %a if %s is 6EG : and % is 699 : 4est different values of , in the eJpression %a&,+ A P% B , %s P , A 6 2 f = 125 100 = 25 N = 2 : f = |125 200| = 75 N = 3 : f = |125 300| = 175 We can thus expect to see an aliased signal component at 25 H !
Sampling a 699 : signal with %s A 6EG :
4wo and a half wave in 9$6 s Q -oscillation. at EG :
:ow to avoid aliasingD
;re1filter the signal before sampling
*owpass1filter
A/D "onverter
Analog signal affected by unwanted frequencies
Analog signal containing only relevant &low+ frequencies
Digital signal
Bac3 to A/D "onversion
ObsH (inary representation of sampled signal
"ontinuous1time signal Sampling process
Sampled signal
Binary numbers
Binary numbers with n digits can represent different values ,$g$ for n A <:
Some common resolutions: EN A EGM( E6E A F97M( E6M A MGG<M( EEF A 6M888E6M( $$$
Ruanti ation
Numbers represented by binary digits are countable B this means that not all real numbers can be represented on a computer But we can get arbitrarily close by increasing the number of digits
Ruanti ation error
,ach bit corresponds to a voltage interval :ence( quanti ation leads to a $uantization error q of at most the same si e as this interval &+ Best1case &5ounding off+2 q " #
Summary
A/D conversion is a way of representing continuous signals as discrete values )eywords2 discretization and $uantization Sampled &alues are represented in a computer using binary digits
Amplitude Amplitude
4ime
4ime
Summary
It is not possible to reconstruct sinusoidal signals containing frequencies higher than half the sampling frequency B the ,y$uist %re$uency Normally( a sensible choice of sampling frequency is G to E9 times the highest frequency -of interest. to the application( called the band.idth >hen sampling signals with lots of frequencies( you need to be aware of aliasing On the slides after this( you will find some reference material that you might find of relevance later
A/D "onversion B a comparator
A/D "onversion B comparator basics
A/D "onversion B conversion to logic values
A/D "onversion B several comparators in parallel
A continuous voltage signal is compared with different reference voltages and are converted into either %O or 9 % 4hese values are associated with logic values B either 9 or 6
Bitwise operations in "
AND 6 if both bits are 6( 9 otherwise
SA AND and assignment P OR 6 if either bits are 6( 9 if they both are 9
PA OR and assignment T XOR 6 if ON, of the bits is 6 and the other is 9 TA XOR and assignment U one's complement =lips all bits KK Shift Left Shifts all bits to the left( inserting 9's KKA Shift Left and assignment VV Shift Right Shifts all bits to the right VVA Shift Right and assignment
Bitwise operations in "
int main(void) { unsigned int a = 60; unsigned int b = 13; unsigned int c = 0; c = a & b; c = a | b; c = a ^ b; b &= a; c = b << 3; a = ~c; return c; } /* c = 12 = 0000 1100 */ /* c = 61 = 0011 1101 */ /* c = 49 = 0011 0001 */ /* b = 12 = 0000 1100 */ /* c = 96 = 0110 0000 */ /* a =159 = 1001 1111 */ /* 60 = 0011 1100 */ /* 13 = 0000 1101 */
Introdu3tion til digitale filtre
*et us say that we have a sequence of samples
{x }
, " " =9
that we wish to process in some way 4ypically( the x'es are used as input to some digital filter The general formula for a digital filter is where the a's og b's are filter coefficientes and the y's are the output of the filter at the indicated sample numbers
i=9 ai y " i=i =9 bi x " i
na
nb
Introduction to digital filters
>ritten out and rearranged2
a 9 y " = b 9 x " + b6 x " 6 ++ b / x " n a6 y " 6 a n y " n
b a
na and nb are usually small integersC furthermore( it it is common to scale such that a9 A 6$ b9 is often &but not always+ 9$ =or eJample( a so1called second order filter2
y " = b 6 x " 6 + b E x " E a 6 y " 6 a E y " E
Simple filters without dynamics
0nit gain
y "= x"
Simple gain
y " = 0x "
Signal offset
y " = x " c
4ime delay and difference
4ime delay
y " = x " 6
Simple difference filter B corresponds to di%%erentiation
y " = x " x " 6
Averaging filters
Second order averaging filter
6 6 y " = x " x " 6 E E
4hird order averaging filter
6 6 6 y " = x " x " 6 x " E < < <
*ow pass filtersH
"entral difference filter
6 6 y " = x " x " E E E
5ecursive filters
4he output of recursive filters depend on -old. output values B for eJample2
y " = x " y " 6
written out2
y 9 = x 9 y6 y 6= x 6 y 9= x 6 x 9 y6 y E= x E y 6= x E x6 x 9 y6 y <= x < y E= x < x E x 6 x 9 y6 y F= x F y <= x F x < x E x 6 x 9 y6
Integrator
;assive low pass filter
=ormula for simple passive low pass filter2
#s = # s y " = x " 6 y " 6
Sampling time 4ime constant &6/crossover+
Implementation
Digital filters are suitable for implementation in e$g$ Arduino "2
#define N 1000 /* Global constant: Number of samples */ #define DLY 100 /* Global constant: Delay btw samples */ void setup() { /* Initialization code */ } int k = 0; /* Global sample number */ void loop() { double y[ N ], a = 0.9; ymeas = analogRead(0); if ((k < N) && (k > 1)) { y[k] = a*ymeas + (1 - a)*y[k-1]; } if (k == N) { /* Send the filtered samples to PC, or whatever */ } delay(DLY); /* Wait for next sampling time */ }
Implementation
On ;" /* save to file data.dat */ /* load and plot in e.g. Matlab: load -ascii data.dat plot((2:N),data,'r-') */