Pcm

You might also like

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

Quantization is done by dividing the range of possible values of the analog samples into

some different levels and assigning the center value of each level to any sample in the
quantization interval. Quantization approximates the analog sample values with the
nearest quantization values.

The encoder encodes the quantized samples. Each quantized sample is encoded into
an 8-bit codeword

Matlab code:

clc;
closeall;
clearall;
% Signal Generation
n=input('Enter n value for n-bit PCM system : ');
n1=input('Enter number of samples in a period : ');
x=0:2*pi/n1:4*pi; %n1 number of samples have to be selected
L=2^n;

% 3 bit PCM so enter n=3


% no of samples as n1=10
% no. of quantization levels n=3, 8 quantization levels

s=8*sin(x); %s is a vector which contains sampled values


subplot(3,1,1);
plot(s);
title('Analog Signal');
ylabel('Amplitude--->');
xlabel('Time--->');
%values of s :
0 4.7023 7.6085 7.6085 4.7023 0 -4.7023
-7.6085 -7.6085 -4.7023 0 4.7023 7.6085 7.6085 4.7023 0 -
4.7023 -7.6085 -7.6085 -4.7023 0

These values will display the sine signal.

% Sampling Operation

subplot(3,1,2);
stem(s);grid on; title('Sampled Sinal'); ylabel('Amplitude--->'); xlabel('Time--
->');

%stem function plots discrete values of s

Quantization Process

[index, quants] = quantiz(sig, partition, codebook) specifies codebook, which


prescribes a value for each partition in the scalar quantization.

codebook is a vector whose length must exceed the length of partition by one.

The function also returns quants, which contains the scalar quantization of sig and
depends on the quantization levels and prescribed values in the codebook.
Index determines on which partition interval, each input value is mapped.
For example:

vmax=8;
vmin = -vmax;
del = (vmax-vmin)/L;% step size = dynamic range/no of levels
part =vmin : del: vmax;% level are between vmin and vmax with difference of del
code1= vmin-(del/2) : del : vmax+(del/2);% Contain Quantized values
[ind,q]=quantiz(s, part, code1); % Quantization process

Vmax = 8
Vmin = -8
del = 16/8 = 2
part = -8: 2 : 8
code1 = -8-1: 2 : 8+1 % -9 : 2 : 9
code1 = -9 -7 -5 -3 -1 1 3 5 7 9

part = -8 -6 -4 -2 0 2 4 6 8

ind = 4 7 8 8 7 5 2 1 1 2 4 7 8
8 7 5 2 1 1 2 4

q = -1 5 7 7 5 1 -5 -7 -7 -5 -1 5 7
7 5 1 -5 -7 -7 -5 -1
-1 5 7 7 5 1 -5 -7 -7 -5 -1 5 7
7 5 1 -5 -7 -7 -5 -1

s = 0 4.7023 7.6085 7.6085 4.7023 0 -4.7023


-7.6085 -7.6085 -4.7023 0 4.7023 7.6085 7.6085 4.7023 0 -
4.7023 -7.6085 -7.6085 -4.7023 0

Partitions <=- -8 and -6 and -4 -2 0 2 4 and 6 and >=8


8 <=-6 <=-4 and and and and <=6 <=8
<=- <=0 <=2 <=4
2
Sample - - - - 0 - - 4.7023 7.6085
values 7.6085 4.7023
Index no 0 1 2 3 4 5 6 7 8 9
Quantized -7 -5 -1 4 7

The first sample value is 0, in code book(code1) nearest value is -1


Second sample value is 4.7023, in codebook nearest value is 5 and so on

% ind contain index number and q contain quantized values

L1=length(ind);
L2=length(q);

L1=21 and L2=21

Since the index values are starting from 1 to 8


Since we need the index as binary decimal so it has to be started from 0 to N
This operation is performed by the first for loop
L1: length of index numbers
Ind now contains
3 6 7 7 6 4 1 0 0 1 3 6 7
7 6 4 1 0 0 1 3

for i=1:L1
if (ind(i)~=0)% To make index as binary decimal so started from 0to N
ind(i)=ind(i)-1;
end
i=i+1;
end

vmin=-8-(2/2)=-9

if q(i) contains -9 value then it should be brought back to with in the levels(-8 to 8 i.e in
the codebook it -7 to 7))

q(i)=-8+1=-7 this is performed with the below for loop


L2: length of quantized values

fori=1:L2
if(q(i)==vmin-(del/2))% To make quantize value inbetween the levels
q(i)=vmin+(del/2);
end
end

To display quantized signal

subplot(3,1,3);
plot(x,q,'.');grid on;
hold on;
stairs(x,q);
% Display the Quantize values
title('Quantized Signal');
ylabel('Amplitude--->');
xlabel('Time--->');
% Encoding Process
figure
code=de2bi(ind,'left-msb');% Convert the decimal to binary
k=1;
fori=1:L1
for j=1:n
coded(k)=code(i,j);% convert code matrix to a coded row vector
j=j+1;
k=k+1;
end
i=i+1;
end
Ind : 3 6 7 7
subplot(2,1,1); grid on; 6 4 1 0 0 1 3 6 7
7 6
stairs(coded); 4 %1 Display0 the 0encoded 1 signal
3
axis([0 100 -2 3]); title('Encoded Signal');
The following output we get if de2bi of ind is performed
ylabel('Amplitude--->');
0 xlabel('Time--->');
1 1
1 1 0
1 1 1
1 1 1
1 1 0
1 0 0
0 0 1
0 0 0
0 0 0
0 0 1
0 1 1
1 1 0
1 1 1
1 1 1
1 1 0
1 0 0
0 0 1
0 0 0
0 0 0
0 0 1
0 1 1

The for loop is used to convert above code vector to coded row vector

for i=1:21
for j=1:3

coded row vector values are(one row)


0 1 1 1 1 0 1 1 1 1 1 1 1
1 0 1 0 0 0 0 1 0 0 0 0
0 0 0 0 1 0 1 1 1 1 0 1
1 1 1 1 1 1 1 0 1 0 0 0
0 1 0 0 0 0 0 0 0 0 1 0
1 1

To plot above values we are using stairs(coded)

You might also like