0% found this document useful (0 votes)
15 views

DCOM Codes

The document discusses experiments on Shannon Fano coding, Huffman coding, cyclic codes, and data conditioning formats. It includes MATLAB code to generate Shannon Fano and Huffman codes from symbol probabilities, encode messages using cyclic codes, calculate syndromes, and plot data signals in polar and unipolar RZ formats.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

DCOM Codes

The document discusses experiments on Shannon Fano coding, Huffman coding, cyclic codes, and data conditioning formats. It includes MATLAB code to generate Shannon Fano and Huffman codes from symbol probabilities, encode messages using cyclic codes, calculate syndromes, and plot data signals in polar and unipolar RZ formats.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

EXPERIMENT NO.

Aim:

To obtain standard Shannon Fano code and code efficiency using MATLAB, a DMS generates
symbols a,b,c,----with probability.

Code :

%SHANNON FANO

clc;
clear all;
close all;

m=input('Enter the no. of messages ensembles : ');


z=[];
h=0;l=0;
display('Enter the probabilities in descending order');
for i=1:m
fprintf('Ensemble %d\n',i);
p(i)=input('');
end

%Finding each alpha values

a(1)=0;
for j=2:m
a(j)=a(j-1)+p(j-1);
end
fprintf('\n Alpha Matrix');
display (a);

%Finding each code length

for i=1:m
n(i)= ceil(-1*(log2(p(i))));
end
fprintf('\n Code length matrix');
display(n);

%Computing each code

for i=1:m
int=a(i);
for j=1:n(i)
frac=int*2;
c=floor(frac);
frac=frac-c;
z=[z c];
int=frac;
end
fprintf('Codeword %d',i);
display(z);
z=[];
end

%Computing Avg. Code Length & Entropy

fprintf('Avg. Code Length');


for i=1:m
x=p(i)*n(i);
l=l+x;
x=p(i)*log2(1/p(i));
h=h+x;
end
display(l);
fprintf('Entropy');
display(h);

%Computing Efficiency

fprintf('Efficiency');
display(100*h/l);
fprintf('Redundancy');
display(100-(100*h/l));

EXPERIMENT NO. 2

Aim:

To obtain standard Huffman code and code efficiency using MATLAB , a DMS generates symbols
a,b,c with probability.

Code :

clc;
clear all;
close all;

symbols = (1:7);
prob = [0.25,0.25,0.125,0.125,0.125,0.0625,0.0625];
[dict,avglen] = huffmandict(symbols,prob,2,'min');
dict(:,2) = cellfun(@num2str,dict(:,2),'UniformOutput',false);

display(dict);
display(avglen);
entropy =0;
for i = 1:7;
entropy = entropy + ((prob(i))*(log2(1/prob(i))));

end
display('entropy:');
display(entropy);

display('avglen or hcap');
display(avglen);

display('efficiency');
display(100*(entropy/avglen))

Experiment no. :- 06
Aim: The generator polynomial for a (7,4) cyclic code is g(x)=x3+x2+1. Find the codeword for
message sequences i) 1010 and ii) 1000 using Polynomial division method. Draw the block
diagram of cyclic encoder using LFSR and mudulo-2 adders and check the codeword for above
message sequences.

BY SHIFT REGISTER METHOD


Code :

clc;
clear all;
n=input ('Enter n:' );
k=input('Enter k :');
disp('The generator polynomial is x^3 + x + 1');
g1 = 1;
g2 = 0;
fprintf('Hence g1 = %d and g2 = %d\n',g1,g2);
e= [0,1,0,1];
disp ('Message bits are:');
fprintf('%d %d %d %d\n',e(1,1),e(1,2),e(1,3),e(1,4));
ff0= 0;
ff1= 0;
ff2=0;
for i=1:k
df = xor(e(1,i), ff2);
ff2 = ff1;
ff1 = xor(df, ff0);
ff0 = df;
end
disp('Parity bits obtained are:');
fprintf('%d %d %d\n', ff2, ff1, ff0);
disp('Encoder output');
fprintf('%d %d %d %d %d %d %d', e(1,1),e(1,2),e(1,3),e(1,4), ff2, ff1, ff0);

BY GENERATOR POLYNOMIAL METHOD


Code:-
clc;
clear all;

k=input('enter the length of message word');


n=input('enter the length of codeword');
m=input('enter the message word');
G=cyclpoly(n,k,'min')
gx=poly2sym(G)
Experiment no. :07
Aim : The generator polynomial for a (7,4) cyclic code is g(n)=1+x+x2. Draw the
block diagram of decoder(Syndrome calculator) for this code. Find syndrome for
the received code 1101100.
Code :
%......CYCLIC DECODAR......%
clc;
clear all;
close all;
s0=0;
s1=0;
s2=0;
for i=1:7
z(i)=input('enter the received bits ');
end
d=[0 z(1) z(2) z(3) z(4) z(5) z(6) z(7)];
display(d);

disp('syndrome output');
for i=1:8
df = s2;
s2=s1;
s1=xor(s0,df);
s0=xor(df,d(1,i));
end
fprintf('%d %d %d',s2 ,s1,s0);
EXPERIMENT NO. : 08
Aim:
To study different data conditioning/reconditioning formats and plot them for Unipolar and Polar RZ,
Code :

clc;

clear all;

close all;

data = [1 0 1 0 0 0 1 1 0];

bitrate = 1;

% polar

T=length(data)/bitrate;

n=200;

N=n*length(data);

dt=T/N;

t = 0:dt:T;

x = zeros(1,length(t)); %output signal

for i = 0:length(data)-1

if data (i+1) == 1

x(i*n+1:(i+0.5)*n) = 1;

x((i+0.5)*n+1:(i+1)*n) = 0;

else

x(i*n+1:(i+1)*n) = -1;

x((i+0.5)*n+1: (i+1)*n) = 0;

end

end

figure;

subplot(2,1,1);

plot (t,x, 'linewidth',3);


axis([0 t(end) -1.1 1.1])

grid on;

title(['polar RZ: [' num2str(data)']' );

%unipolar

T = length(data)/bitrate;

n = 200;

N = n*length (data);

dt = T/N;

t = 0:dt:T;

x = zeros(1,length(t)); %output signal

for i = 0:length(data)-1

if data (i+1) == 1

x(i*n+1:(i+0.5)*n) = 1;

x((i+0.5)*n+1:(i+1)*n) = 0;

else

x(i*n+1:(i+1)*n) = 0;

x((i+0.5)*n+1: (i+1)*n) = 0;

end

end

figure;

subplot(2,1,2);

plot (t,x, 'linewidth',3);

axis([0 t(end) -0.1 1.1])

grid on;

title(['unipolar RZ: [' num1str(data)']' );

You might also like