You are on page 1of 13

Lab Sheet 1

Analog to Digital Conversion (ADC) and Digital to


Analog Conversion (DAC) Fundamentals
 Prerequisites
The recommended prerequisites for this lab are as follows…
1. A fundamental knowledge on MATLAB
2. A theoretical knowledge on sampling,reconstruction and quantization.

 Outcomes
After completing this lab, the students will
1. Understand signal sampling and reconstruction and their implementation using
MATLAB.
2. Understand quantization and its implementation using MATLAB.

 Outlines of This Lab


1. Lab Session 1.1: Sampling of Continuous Time (CT) signal
2. Lab Session 1.2: Reconstruction of Discrete Time (DT) signal to generate CT signal
3. Lab Session 1.3: Observation of effect of aliasing
4. Lab Session 1.4: Quantization
5. Lab Session 1.5: Simple ADC and DAC Systems
6. Home work

1.1 Sampling of CT signal


1.1.1 Introduction:
Signals available in our environment are mostly analog. As a result we cannot use them for
digital signal processing since digital signal processing (DSP) deals with discrete time signal.
This requires the sampling of an analog signal and then quantizing the amplitude of the sampled
signal.

1.1.2 Sampling:
Sampling is a process of converting a continuous time signal into a discrete time signal. To
convert a continuous signal into a discrete signal, samples of the signal are taken periodically, at
regular intervals.

1|Page
If y (t ) is a continuous time signal, the corresponding discrete signal will be y(nTs) where
n  1, 2,3, 4,.... and Ts is the sampling interval. We can also write the discrete signal as y (n)
implying the sampling period of Ts.
1.1.2 Sampling Frequency:
Sampling frequency is the number of samples taken per second by the sample block of ADC.
1
The sampling frequency and Ts are related by f S  .
Ts
1.1.3 The Nyquist–Shannon Sampling Theorem:
The Nyquist–Shannon sampling theorem is a fundamental result in the field of information
theory, in particular telecommunications and signal processing. The theorem is commonly called
Shannon's sampling theorem.
The theorem states that exact reconstruction of a continuous-time baseband signal from its
samples is possible if the signal is bandlimited and the sampling frequency is greater than twice
the signal bandwidth. The theorem also leads to an effective reconstruction formula.

1.1.4 MATLAB Example


Example 1.1:

Consider an analog signal x a (t )  cos(20t ) , 0  t  1 . It is sampled at Ts = 0.01, 0.05 and


0.1sec intervals to obtain x(n). For each Ts, plot x(n). Comment on your results.
Note: To represent a CT signal in MATLAB, x(t) is generated using time interval of 0.001s and
plotting it without any line style.

Solution:

T=1;
t=0:0.001:T;
xa=cos(20*pi*t);
Ts=0.01;
N1=round(T/Ts);
n1=0:N1-1;
x1=cos(20*pi*n1*Ts);
subplot(3,1,1); plot(t,xa,n1*Ts,x1,'o');axis([0,1,-1.1,1.1]);
ylabel('x1(n)'); title('Sampling of x_{a}(t)using Ts=0.01');

Ts=0.05;
N2=round(T/Ts);
n2=0:N2;
x2=cos(20*pi*n2*Ts);
subplot(3,1,2); plot(t,xa,n2*Ts,x2,'o'); axis([0,1,-1.1,1.1]);
ylabel('x2(n)'); title('Sampling of x_{a}(t)using Ts=0.05');

Ts=0.1;
N3=round(T/Ts);
n3=0:N3;
x3=cos(20*pi*n3*Ts);subplot(3,1,3);
plot(t,xa,n3*Ts,x3,'o'); axis([0,1,-1.1,1.1]);
ylabel('x3(n)');title('Sampling of x_{a}(t)using Ts=0.1');

2|Page
Output:

Sampling of x a(t)using Ts=0.01


1
x1(n)

-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Sampling of x a(t)using Ts=0.05
1
x2(n)

-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Sampling of x a(t)using Ts=0.1
1
x3(n)

-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1

1.2 Reconstruction of CT signal


From the sampling theorem and the preceding example, it is clear that if we sample band-limited
xa(t) above its Nyquist rate, then we can reconstruct xa(t) from its samples x(n). This
reconstruction can be thought of as a 2-step process:
(i) First the samples are converted into a weighted impulse train.

(ii) Then the impulse train is filtered by an ideal analog lowpass filter band-limited to
the [−Fs/2, Fs/2] band.

This two-step procedure can be described mathematically using an interpolating formula

3|Page
Where

is an interpolating function. We observe that this ideal interpolation is not practically feasible
because the entire system is non causal and hence not realizable.

1.2.1 MATLAB Example


Example 1.2:
Reconstruct the analog signal ya(t) from the samples x(n) (from example 1.1) by means of the
sinc interpolation. (Use t=0.001). Estimate the frequency in ya(t) from your plot. Comment on
your results.
Solution:
The following block of codes implement the sinc interpolation as shown in the equation for
reconstruction for Ts=0.01 sec. This code needs to be appended to the end of block of codes in
Example 3.1 so that the sampled signals x1, x2 and x3 can be used to reconstruct the
corresponding analog signals. You need to change the variables Ts, n1, x1 and xa1 to their
corresponding variable names for Ts= 0.05 and 0.1 sec (i.e., change n1 to n2, x1 to x2 etc. for
Ts=0.05).

%% For Ts= 0.01


Ts=0.01;
xa1 = zeros(1,length(t));
for i=1:length(x1)
g=sinc((t - (i-1)*Ts)/Ts);
xa1 = xa1+x1(i)*g;
end

%% For Ts= 0.05


Ts=0.05;
% fill in codes
%% For Ts= 0. 1
Ts=0.1;
% fill in codes
figure
subplot(3,1,1);
plot(t,xa1)
axis([0,1,-1.1,1.1]);
ylabel('x_{a}(t)');
title('Reconstruction of x_{a}(t) when Ts=0.01 ');
subplot(3,1,2);
plot(t,xa2)
title('Reconstruction of x_{a}(t) when Ts=0.05 ');

4|Page
Output:

Reconstruction of x a(t) when Ts=0.01


1
x (t)

0
a

-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Reconstruction of x a(t) when Ts=0.05
1
x (t)

0
a

-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Reconstruction of x a(t) when Ts=0.1
1
x (t)

0
a

-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1

Comments: From the plots, it is clear that reconstruction from samples at Ts=0.01 and 0.05
depicts the original frequency, but the reconstruction for Ts = 0.1 shows the original frequency
aliased to zero.

1.3 Observation of effect of aliasing:


Example 3.3: Plot three functions on same plot as follows:
(i) x1  cos 6t
(ii) x 2  cos 14t
(iii) x3  cos 26t Use t = nT, T=0.001sec and n =500
Now sample (i) at Ts = 0.1sec. Plot this on the same plot also. What do the results indicate?
Explain.

Solution :
T=0.001;
n=0:500;
x1=cos(2*pi*3*n*T); x2=cos(2*pi*7*n*T); x3=cos(2*pi*13*n*T);
plot(n*T,x1); hold on; plot(n*T,x2,'r'); plot(n*T,x3,'g');
Ts=0.1;
N=round(1/Ts);
n1=0:N/2;
x1=cos(2*pi*3*n1*Ts);
plot(n1*Ts,x1,'o');

Repeat for x2 and x3 and plot with different line markers.

5|Page
Output:

0.8

0.6

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1
0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5

1.4 Quantization
1.4.1 Theoretical background:
In digital signal processing, quantization is the process of approximating a continuous range of
values (or a very large set of possible discrete values) by a relatively small set of discrete
symbols or integer values.
Quantization is the representation of the sampled values of amplitude by a finite set of levels. It
converts a continuous-amplitude sample to a discrete-amplitude sample.

Here, b = number of bits assigned to quantize a signal at L = 2b and Δ = .

6|Page
For any sampled amplitude of x, its quantized value can be determined by finding its location in
terms of Δ first as
h = round ( )

Then the quantized value can be given by Xq = hΔ + Xmin.

1.4.2 Custom function for quantization:


function y=uquant(x,N)
%x= original sequence
%N=Number of steps
maxval=max(max(x));
minval=min(min(x));
stepsize=(maxval-minval)/(N-1);
h=round((x-minval)/stepsize);
y=h*stepsize+minval;

Example 1.4:
Use the function uquant( ) to quantize a vector:

x= {1 2 -3 -4 5 10 12 13 16 11 0 -5 7 9 10 16} at a level
(a) 2 (b) 4 (c) 8 (d) 16

Solution:

x=[1 2 -3 -4 5 10 12 13 16 11 0 -5 7 9 10 16 ];
y1=uquant(x,2)
y2=uquant(x,4)
y3=uquant(x,8)
y4=uquant(x,16)

Output
The data output is :
x=
1 2 -3 -4 5 10 12 13 16 11 0 -5 7 9 10 16
y1 =
-5 -5 -5 -5 -5 16 16 16 16 16 -5 -5 16 16 16 16
y2 =
2 2 -5 -5 2 9 9 16 16 9 2 -5 9 9 9 16
y3 =
1 1 -2 -5 4 10 13 13 16 10 1 -5 7 10 10 16
y4 =
Columns 1 through 9
0.6000 2.0000 -3.6000 -3.6000 4.8000 10.4000 11.8000 13.2000 16.0000
Columns 10 through 16
10.4000 0.6000 -5.0000 7.6000 9.0000 10.4000 16.0000
You can also find quantization error or noise (e.g., for y1) by
computing quant_noise1= x-y1;
stem(quant_noise1);
After finding the quantization noise or error for all quantized outputs (y1 to y4), stem them in the
same figure in different sub-plots. You will notice that the quantization noise or errors reduce as
b is increased. Explain why this happens.

7|Page
1.5 Implementation of Simple ADC and DAC
1.5.1 Introduction
In this session we‟ll put together our codes for sampling, quantization and reconstruction to
implement a simple ADC for generating quantized samples followed by a DAC to regenerate the
analog samples for various sampling rates and quantization levels. This will give us a system
level understanding of the functions of ADC and DAC and the fidelity of signal waveform as it is
subjected to the processes of ADC and DAC.
An ADC comprises sampling followed by quantization. The DAC consists of sinc interpolation
of the output of ADC. We‟ll use the code from Example 3.1 to implement our ADC and DAC.
The following example illustrates the implementation. You can change b and Ts to see the effect
of ADC and DAC on the signal quality.

1.5.2 MATLAB Example


Example 1.5:
clear all
close all
%% Parameter file:
Ts=0.01; %Sampling interval
b= 2; % Number of bits in Quantizer
T=1; %Time duration for analog signal
F= 10; %Frequency of analog signal
%% ADC:
% Generation of analog signal:
t=0:0.001:T;
xa=cos(20*pi*t);
% Sampling
N1=round(T/Ts);
n1=0:N1-1;
x1=cos(20*pi*n1*Ts);
% Quantization:
L= 2^b; %number of levels
y1= uquant(x1,L);

%% DAC:
xa1 = zeros(1,length(t));
for i=1:length(y1)
g=sinc((t - (i-1)*Ts)/Ts);
xa1 = xa1+y1(i)*g;
end

%% Plots:
figure
plot(t,xa)
hold on
plot(t,xa1,'r')
ylabel('Signal level');
legend('Original Signal', 'Reconstructed signal')
title('ADC and DAC operation');

8|Page
Home Work:
HW1.01: Given a sinusoid waveform with a frequency of 100 Hz,
x(t)  4.5sin  2 100t 
sampled at 8000Hz. Write a MATLAB program to quantize x(t) using 4 bits to obtain and plot
the quantized signal, assuming the dynamic range is between -5 to 5 volts.

HW1.02: An analog signal xa(t) =sin(1000t) is sampled using the following sampling intervals.
In each case, plot the DT signals and their corresponding reconstructed signals in the same
figure.
(a) Ts = 0.1ms
(b) Ts = 1ms
(c) Ts = 0.01ms

Authorship: This lab sheet was prepared by B.K.M. Mizanur Rahman, Assistant Professor of
EEE Department of UIU. It was reviewed and revised by Dr. Raqibul Mostafa, Professor of EEE
Department in Fall 2017.

9|Page
Optional work materials

Lab Session 1.4:Effect of sampling on sound


1.4.1: USING sound( ) TO DEMONSTRATE THE ALlASING EFFECT

MATLAB has a function sound() that can convert a discrete signal sequence into continuous-
time sounds. We can use it to demonstrate the effect of aliasing very easily.
Example 1.4:
Introduce the “chirp” phenomena where the instantaneous frequency of some special signal will
increase over time. Consider the signal

 1 
x(t )  sin  o t   t 2 
 2 
Set o = 2(3000) rad/sec and  = 2000 rad/sec2.

1. Store in the vector x the samples of the chirp signal on the interval 0t1, and let T is the
same value as above.
2. Use sound(x,fs) to play the chirp signal contained in x. Can you explain what you
just heard?
3. Can you predict the time at which the played signal has zero (or very low) frequency? Use
a longer x sequence to verify your prediction.

The MATLAB code :


f=3000; %frequency of the sine wave
fs=8192; %sampling frequency
Ts=1/fs; %sampling time
Ns=512; %512 points
t= [0:Ts:Ts*20*(fs-1)]; %50/8000~6.25ms
x=sin(2*pi*f*t+0.5*2000*t.^2);%sine function for 50 samples
sound(x,fs) %play the wave

Task:
(i) Change fs=1000, 2000, 3000, 5000,6000, 8000 respectively and repeat the above
code. Can you explain what you just heard in each case? Why the sound is distorted
below a certain frequency? Try to detect the sampling frequency below which the
sound you have just heard is distorted.

10 | P a g e
Example 1.5: Playing practical sound:
Steps:
(i) Load an audio file:
[y fs ]=audioread('audioclip1.wav'); % Place the file 'audioclip1.wav' in the current folder
[y fs ]=auread(„'audioclip1.wav‟) returns the sample rate (fs) in Hertz and the
number of bits per sample (bits) used to encode the data in the file.

(ii) plot the function : >> plot(y)


The following curve will be shown :

0.6

0.4

0.2

-0.2

-0.4

-0.6

-0.8
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5
5
x 10

(iii) Find the sample rate :


>> fs
Output: fs =16000
(iv) Play the sound :
>> sound(y,fs)
(vi) Find the size of the sound file :
>> size(y)
Output: ans = 410530 1
(v) Play sound at different sampling frequency:
>> sound(y,8000)
% hear the sound
(vi) Play sound at different sampling frequency:
>> sound(y,32000)
% hear the sound
Comment on results of (v) and (vi)

Lab Session 1.6: Effect of quantization on


image :
Example 1.5:
Steps
(i) Load an image :
x=imread('fountainbw.tif');
% Make sure that the image is available

11 | P a g e
x=double(x);
image(x)
The following image will appear :

50

100

150

200

250

300

350

400

50 100 150 200 250 300 350 400 450 500 550

(ii) Make the image gray :


>> colormap(gray(256)) The figure will be gray colored.
(iii) Make image size square
>> axis('image') The image will be like the following:

50

100

150

200

250

300

350

400
50 100 150 200 250 300 350 400 450 500 550

(iv) Quantize the image at different level and see the effect :
MATLAB code :
y=imread('fountainbw.tif');
subplot(2,4,1);
image(y)
colormap(gray(256));
axis('image');
y=double(y);
for b=1:7
N=2^b;
J=uquant(y,N);
subplot(2,4,b+1)
image(J);
colormap(gray(256));
axis('image');
end

12 | P a g e
output :

13 | P a g e

You might also like