Sunteți pe pagina 1din 11

BANGLADESH UNIVERSITY OF ENGINEERING AND

TECHNOLOGY



EEE 312
DIGITAL SIGNAL PROCESSING 1 LABORATORY
Experiment 1: Study of Sampling, Quantization and Encoding
Date of Performance: 16
th
July, 2014
Date of Submission: 13
th
August, 2014

Submitted to:

Dr. Mohammed Imamul Hassan Bhuiyan
Professor
Department of Electrical and Electronic Engineering
&
Dr. Mohammad Ariful Haque
Associate Professor
Department of Electrical and Electronic Engineering

Submitted by:
Mohammad Tariqul Islam
Student ID: 1006071
Level 3, Term 2
Partners Student No: 1006092

Problem Statement
Consider an analog signal:
() = 1.5 +1.2 cos (2 75 +30)
(i) Obtain the sampled signal with Fs=250Hz.
(ii) Obtain the quantized signal with levels = 8.
(iii) Obtain PCM encoded bit stream with 5.
(iv) Obtain the reconstructed signal from PCM signal.
Solution
The Matlab code first obtains the maximum and minimum values of the signal from a closely
packed samples. Then look up table (l) for the levels is created. After that sampled signal (S) is
obtained. From the sampled signal the discrete points are quantized (Q) according to the levels in
the look up table. The quantized signal is also digitized (D) and a PCM encoded bit stream (B) is
produced. After that the bit stream is processed to reconstruct the quantized signal (R) and from
that an estimation of the actual signal (X) using linear interpolation.
The code is given below.
Code
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Homework 1
%
% Experiment 1
% Study of Sampling, Quantization and Encoding
%
% Mohammad Tariqul Islam
% 1006071
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear;
close all;
clc;

f = @(t) (1.5+1.2*cos(2*pi*75.*t+pi/6)); %given function
bits=3; %no of bits
levels = 2^bits; %no of levels
Fs = 250; %sample frequency
tmin=0; %starting time
tmax=1; %end time

%looking for maximum and minimum value of function
t1=tmin:0.0001:tmax;
x=f(t1);
maxx = max(x);
minx = min(x);

%calculating delta
delta = abs((maxx-minx)/(levels-1));

l = minx:delta:maxx; %look up table for levels
Ts = 1/Fs; %sample time step

t=tmin:Ts:tmax; %t array
S = f(t); %sampled array


lens = length(S); %no of samples
lenl = length(l); %no of levels; should be equal to levels; prone to errors
hdelta = delta/2; % half of delta
Tsby3=Ts/bits; %one third of sample time step


Q = ones(size(S)); %contains quantized data
D = Q; %contains digital data
B = ones(1,lens*bits); %contains binary stream as voltage level (1,0)=(5,-5);
TB = B; %contains time steps for each bits.
TB(1)=0; %initializing starting time;


%getting quantized, digital and bit stream data;
for i=1:lens
%for each discrete value comparing them to each levels from low to high
for j=1:lenl-1
%if a vaule in S is between tow adjacent levels then...
if (S(i)>=l(j) && S(i)<=l(j+1))
%if the value is far from the lower level od the two levels
if (S(i)-l(j))>=hdelta
%higher level is assigned
Q(i)=l(j+1);
D(i)=j;
else
%else lower level is assigned
Q(i)=l(j);
D(i)=j-1;
end

%convert digital data to binary char array with 'bits' no of
%bits
BIN = dec2bin(D(i),bits);

%for each character in array
for k=1:bits
%calculate the value of it's place in Bit Stream
z=(i-1)*bits+k;

%if bit is 1 assign 5V else -5V
if(BIN(k)=='1')
B(z)=5;
else
B(z)=-5;
end

%TB(z-1)=TB(0) for z=1; hence it is avoided
if(z>1)
TB(z)=TB(z-1)+Tsby3;
end
end %end for k

%break when this is done
break;
end %end if value in between
end %end for j
end %end for i

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Reconstructing the signal%
%
% Known Facts [Object (variable_name)]:
%
% PCM Encoded stream (B), Sample Frequency (Fs)/Time step (Ts)
% Bits in sample (bits)/levels (levels), Levels as lookup table (l)
%
% Have to calculate [same as above]:
%
% Reconstructed Quantized Signal (R),
% Actual signal from R using linear interpolation (X)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

lenb=length(B); %length of bit stream
R=ones(1,lenb/bits); %reconstructed signal
lenr=length(R); %length of reconstructed signal

%for each element in R
for i=1:lenr
%initialize a variable to 1
val=1;
%for each bit
for k=1:bits
%calculating the place from where Bit has to be plucked
z=(i-1)*bits+k;

%if voltage level if 5 bit is 1, else 0;
if B(z)==5
z=1;
else
z=0;
end
%updating the value of val
val=val+z*2^(bits-k);
end %end for k
%obtaining quantized data from lookup table
R(i)=l(val);
end %end for i

%reconstructing the signal
T = t1;
X = interp1(t,R,T);

%calculating error signal
error = abs(X-x);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Plotting different Graphs
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%

figure,plot(t1,x); %plotting actual signal;
title('Actual Signal Vs. Time');
xlabel('Time (s)');
ylabel('Actual Signal');
xlim([0 0.1]);
ylim([0 3]);

figure,stem(t,S); %plotting sampled signal;
title('Sampled Signal Vs. Time');
xlabel('Time (s)');
ylabel('Sampled Signal');
xlim([0 0.1]);
ylim([0 3]);

figure,stem(t,Q); %plotting quantized signal;
title('Quantized Signal Vs. Time');
xlabel('Time (s)');
ylabel('Quantized Signal');
xlim([0 0.1]);
ylim([0 3]);

figure,stairs(t,D);%plotting digital data;
title('Digital Levels Vs. Time');
xlabel('Time (s)');
ylabel('Digital Signal (levels)');
xlim([0 0.1]);
ylim([0 8]);

figure,stairs(TB,B);%plotting binary stream;
title('Bit Stream');
xlabel('Time (s)');
ylabel('Bit Stream (V)');
xlim([0 0.1]);
ylim([-6 6]);

figure,stem(t,R); %plotting reconstructed quantized signal
title('Reconstructed Quantized Signal Vs. Time');
xlabel('Time (s)');
ylabel('Reconstructed Quantized Signal');
xlim([0 0.1]);
ylim([0 3]);

figure,plot(T,X); %plotting original signal
title('Estimation of Original Signal');
xlabel('Time');
ylabel('Actual Signal');
xlim([0 0.1]);
ylim([0 3]);

figure,plot(t1,x,'b',T,X,'r'); %Comparing two signals
title('Comparing Two Signal');
xlabel('Time');
ylabel('Actual Signal');
xlim([0 0.1]);
ylim([0 3]);

figure,plot(t1,error); %plotting absolute error
title('Error');
xlabel('Time');
ylabel('Error');
xlim([0 0.1]);
ylim([0 1]);



Graphs
Different graphs are plotted. They are given below:
1. Actual Signal:



2. Sampled Signal

3. Quantized Signal

4. Digital Signal

5. PCM Encoded Bit Stream

6. Reconstructed Quantized Signal

7. Estimation of Original signal from Reconstructed Quantized Signal

8. Comparing Two Signals

9. Absolute Error

Discussion:
Digital signal processing is more preferable due to its flexibility and configurability compared to
analog signal processing. In contrast to analog signal digital signal can be stored. In practical
purposes digital signal processing is cost effective and can be controlled more efficiently than
analog signal. Analog signals are converted to digital signal via sampling, quantization, and
encoding.
The digital data is encoded to store it asa digital form. The preferred method of encoding in this
experiment is PCM Encoding. The PCM encoding scheme used in this experiment is uniform;
known as uniform quantizer. For application specific purposes non-uniform quantizer can be
used.

S-ar putea să vă placă și