LABREPORT3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

United International University

Department of Electrical and Electronics


Engineering
Digital signal processing Laboratory
Summer 212-EEE - 3310 – A
Tanzidul Aziz
ID: 021191049
Lab report – 03
Lab session name: Sampling reconstruction and Quantization of DT Signals.
Objective:
1. Understand how to sampling a signal.
2. Reconstruction of the sampling signal.
3. Learn quantization and implementation of a signal.
4. Observe the effect of different frequencies in a signal.

Lab work:
Example 3.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.
Solution: The sampling theorem specifies the minimum sampling rate at which a
continuous-time signal needs to be uniformly sampled so that the original signal can
be completely recovered or reconstructed by these samples alone. This is usually
referred to as Shannon's sampling theorem in the literature.

MATLAB codes for sampling :


t=0:0.001:1;
xa=cos(20*pi*t);

Ts=0.01; N1=round(1/Ts); n1=0:N1;


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(1/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(1/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');

Output:

Example 3.2:

Reconstruct the analog signal ya(t) from the samples x(n) utilizing the sinc
interpolation (Use t=0.001.) Estimate the frequency in ya(t) from your plot.
Comment on your results.

Solution: A continuous-time signal can be processed by processing its samples


through a discrete-time system. For reconstructing the continuous-time signal from
its discrete-time samples without any error, the signal should be sampled at a
sufficient rate that is determined by the sampling theorem.
MATLAB codes
Ts=0.01; Fs=1/Ts;
N1=round(1/Ts); n1=0:N1;
x1=cos(20*pi*n1*Ts);
xa1=x1*sinc(Fs*(ones(length(n1),1)*t-
(n1*Ts)'*ones(1,length(t))));
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 ');

Ts=0.05; Fs=1/Ts;
xa2=x2*sinc(Fs*(ones(length(n2),1)*t-
(n2*Ts)'*ones(1,length(t))));
subplot(3,1,2); plot(t,xa2); axis([0,1,-1.1,1.1]);
ylabel('x_{a}(t)'); title('Reconstruction of x_{a}(t)
when Ts=0.05 ');

Ts=0.1; Fs=1/Ts;
xa3=x3*sinc(Fs*(ones(length(n3),1)*t-
(n3*Ts)'*ones(1,length(t))));
subplot(3,1,3);plot(t,xa3);axis([0,1,-1.1,1.1]);
ylabel('x_{a}(t)'); title('Reconstruction of x_{a}(t)
when Ts=0.1 ');

Output:
Example 3.3: Plot three functions on the same plot as follows:
(i) x1 = cos6t
(ii) x2 = cos14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: Aliasing is when a continuous-time sinusoid appears as a discrete-time
sinusoid with multiple frequencies. The sampling theorem establishes conditions
that prevent aliasing so that a continuous-time signal can be uniquely reconstructed
from its samples.
MATLAB codes
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');

Output:
Example 3.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  Set o = 23000) rad/sec and = 2000 rad/sec2.
 2 
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.

Solution: The more samples that are taken, the more detail about where the waves
rise and fall is recorded and the higher the quality of the audio. Also, the shape of
the sound wave is captured more accurately.
MATLAB code :

f=3000;
fs=8000;
Ts=1/fs;
t=[0:Ts:Ts*20*(fs-1)];
x=sin(2*pi*f*t+0.5*2000*t.^2);
sound(x,fs)

Example 3.5: Playing practical sound

MATLAB code :

[y fs] = audioread('audioclip0.mp3');
plot(y)
Example 3.6:
Solution: This Simulink model was used to observe the effect of frequency in an
audio signal. The original signal can be smoothly run if the sampling time is set to
low.
Scope output:

Example 3.7:
Solution:
The downsampled signal's frequency spectrum will have its magnitude lowered by
the downsampling factor. Downsampling refers to converting a higher sampling
rate to a lower one. Downsampling is done to decrease the bit rate when
transmitting over a limited bandwidth or to convert to a more limited audio format.
Output:
Example 3.8:
Use the function 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: Quantization is the process of replacing analog samples with approximate
values taken from a finite set of allowed values. The approximate values
corresponding to a sequence of analog samples can then be specified by a digital
signal for transmission, storage, or other digital processing.
MATLAB codes:
x=[1 2 -3 -4 5 10 12 13 16 11 0 -5 7 9 10 16 ];
z=length(x); n=1:z;
y1=uquant(x,16); y2=uquant(x,8);
y3=uquant(x,4); y4=uquant(x,2);
fprintf(' x = %s \n y1 = %s \n y2 = %s \n y3 = %s
\n y4 = %s \n ', ...
num2str(x), num2str(y1), num2str(y2), num2str(y3),
num2str(y4) );

figure;
subplot(2,2,1); stem(n,y1); hold on; plot(n,x,'k');
title('Quantization with 16 levels'); grid
subplot(2,2,2); stem(n,y2); hold on; plot(n,x,'k');
title('Quantization with 8 levels'); grid
subplot(2,2,3); stem(n,y3); hold on; plot(n,x,'k');
title('Quantization with 4 levels'); grid
subplot(2,2,4); stem(n,y4); hold on; plot(n,x,'k');
title('Quantization with 2 levels'); grid
Output:
>> EX8
x = 1 2 -3 -4 5 10 12 13 16 11 0 -5 7 9 10 16
y1 = 0.6 2 -3.6 -3.6 4.8 10.4 11.8 13.2
16 10.4 0.6 -5 7.6 9 10.4 16
y2 = 1 1 -2 -5 4 10 13 13 16 10 1 -5 7 10 10 16
y3 = 2 2 -5 -5 2 9 9 16 16 9 2 -5 9 9 9 16
y4 = -5 -5 -5 -5 -5 16 16 16 16 16 -5 -5 16 16 16 16
Example 3.9:
Solution: We simplify time into discrete numbers. Another example is capturing a digital image
by representing each pixel by a certain number of bits, thereby reducing the continuous color
spectrum of real-life to discrete colors Quantization.

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

50

100

150

200

250

300

350

400

50 100 150 200 250 300 350 400 450 500 550
Quantize the image at different level and see the effect:
y=imread('fountainbw.tif');
subplot(2,4,1); image(y); colormap(gray(256));
for b=1:7
N=2^b; J=uquant(y,N);
subplot(2,4,b+1); image(J); colormap(gray(256));
end

Output:

−1000| t|
Lab Evaluation: Let, x a ( t ) = e . Sample the function at Fs =
5000 sample/sec and 1000 sample/sec and comment on your result.
Solution:
MATLAB codes:
fs1=5000;
t1=0:1/fs1:20*1/fs1;
x1=exp(-1000*abs(t1));

fs2=1000;
t2=0:1/fs2:20*1/fs2;
x2=exp(-1000*abs(t2));
subplot(2,1,1);
plot(t1,x1);
hold on;
stem(t1,x1);

subplot(2,1,2);
plot(t2,x2);
hold on;
stem(t2,x2);
Output:

Home Work:
HW3.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.
Solution:
MATLAB codes:
fs = 8000;
T = 1/fs;
t = 0:T:.02;
x = 4.5*sin(2*pi*100*t);
z=length(x);
n=1:z;
b = input('input number of bits =>');
N=2^b;
y1=uquant(x,N);
subplot(2,2,1);
stem(n,y1,'k');
hold on;
plot(n,y1,'r','LineWidth',3);
hold on;
plot(n,x,'b','LineWidth',2);
subplot(2,2,2);stem(n,y1,'k');
subplot(2,2,3);plot(n,y1,'r','LineWidth',2);
subplot(2,2,4);plot(n,x,'r','LineWidth',2);

Output:
HW3.02:
An analog signal xa (t) =sin(1000t) is sampled using the following sampling
intervals. In each case, plot the spectrum in the resulting discrete-time signal.
(a) Ts = 0.1ms
(b) Ts = 1ms
(c) Ts = 0.01ms
Solution:
MATLAB codes:
%a
ts1=0.1*10^-3;
fs1=1/ts1;
t1=0:1/fs1:20*1/fs1;
x1=sin(1000*pi*t1);
subplot(3,1,1);
plot(t1,x1);
hold on;
stem(t1,x1);
%b
ts2=1*10^-3;
fs2=1/ts2;
t2=0:1/fs2:20*1/fs2;
x2=sin(1000*pi*t2);
subplot(3,1,2);
plot(t2,x2);
hold on;
stem(t2,x2);
%c
ts3=.01*10^-3;
fs3=1/ts3;
t3=0:1/fs3:20*1/fs3;
x3=sin(1000*pi*t3);
subplot(3,1,3);
plot(t3,x3);
hold on;
stem(t3,x3);
Otput:
Conclusion:
Sampling converts a time-varying voltage signal into a discrete-time signal, a
sequence of real numbers. Quantization replaces each real number with an
approximation from a finite set of discrete values. Most commonly, these discrete
values are represented as fixed-point words.

You might also like