Anda di halaman 1dari 11

SCET-TE(E&TC) 2012

b) Huffman Coding Program


% Huffman Encoding & Decoding clear all; clc n=input('Enter no. of inputs:'); p=input('Enter probabilities of messages:'); symbols=[1:n]; [dict,avglen]=huffmandict(symbols,p); disp('Huffman Dicitionary :');disp(dict); disp('Huffman bit length :');disp(avglen); temp=dict; for i=1:length(temp) temp{i,2}=num2str(temp{i,2}); end disp(temp); sig=input('Enter string of message'); disp('Huffman codes for given messages:'); sample_code=huffmanenco(sig,dict); disp(sample_code); message_rece=input('Enter received bit string:'); message_orig=huffmandeco(message_rece,dict); disp(message_orig); err=isequal(sig,message_orig); if err==1 disp('Message recovered correctly'); else disp('Error in decoding'); end

SGR EFs COEM

Page 1

SCET-TE(E&TC) 2012
Output Enter no. of inputs:5 Enter probabilities of messages:[0.4 0.2 0.1 0.2 0.1] Huffman Dicitionary : [1] [ 1] [2] [1x3 double] [3] [1x4 double] [4] [1x2 double] [5] [1x4 double] Huffman bit length : 2.2000 [1] [2] [3] [4] [5] '1' '0 0 0' '0 0 1 1' '0 1' '0 0 1 0'

Enter string of message [1 3 4 2] Huffman codes for given messages: 1 0 0 1 1 0 1 0 0

Enter received bit string:[1 0 0 1 1 0 1 0 0 0] 1 3 4 2 Message recovered correctly

SGR EFs COEM

Page 2

SCET-TE(E&TC) 2012
3) Implementation of algorithms for generating and decoding linear block codes. Program % Linear Block coding clear all; clc; n=input('Enter no of codes: '); k=input('Enter no of messages: '); lp=n-k; p=input('Enter parity matrix: '); ik=eye(k); %Identity Matrix % Encoding disp('Linear Block Codes : Encoding'); disp('Generator Matrix:'); g=cat(2,ik,p);disp(g); %Concatenate arrays along specified dimension d=input('Enter message to be coded:'); c1=mtimes(d,g); %Matrix Multiplication disp('The codeword is :'); c=mod(c1,2);disp(c);%Modulus after division % Decoding disp('Linear Block Codes : Decoding'); r=input('Enter Received bit:'); disp('Transpose of parity matrix:'); pt=p'; disp(pt); ilp=eye(lp); disp('Parity check matrix:'); h=cat(2,pt,ilp);disp(h); disp('Transpose parity check matrix:'); ht=h'; disp(ht); s1=mtimes(r,ht); disp('Syndrome matrix:'); s=mod(s1,2); disp(s); if (s == 0) disp('The received code is correct.'); else for j=1:1:n m=xor(s,ht(j,:)); if (m==0) row = j; break; end end r(1,row) = ~r(1,row); disp('Correct codeword is:'); end disp('c='); disp(r); SGR EFs COEM Page 3

SCET-TE(E&TC) 2012
Output Enter no of codes: 7 Enter no of messages: 4 Enter parity matrix: [1 1 0;0 1 1;1 1 1;1 0 1] Linear Block Codes : Encoding Generator Matrix: 1 0 0 0 1 1 0 0 1 0 0 0 1 1 0 0 1 0 1 1 1 0 0 0 1 1 0 1 Enter message to be coded:[1 0 1 0] The codeword is : 1 0 1 0 0 0 1 Linear Block Codes : Decoding Enter Received bit:[0 1 0 1 0 1 1] Transpose of parity matrix: 1 0 1 1 1 1 1 0 0 1 1 1 Parity check matrix: 1 0 1 1 1 0 1 1 1 0 0 1 0 1 1 1 0 0 0 0 1

Transpose parity check matrix: 1 1 0 0 1 1 1 1 1 1 0 1 1 0 0 0 1 0 0 0 1 Syndrome matrix: 1 0 1 Correct codeword is: c= 0 1 0 0 0 1

SGR EFs COEM

Page 4

SCET-TE(E&TC) 2012
4) Implementation of algorithms for (7,4) generating and decoding of cyclic code Program % Encoding & Decoding for (7,4) Cyclic code clear all; clc; n=input('Enter no of codes(n): '); k=input('Enter no of messages(k): '); lp=n-k; p=input('Enter parity matrix(p): '); ik=eye(k); %Identity Matrix % Encoding disp('Cyclic Codes : Encoding'); disp('Generator Matrix G:'); g=cat(2,ik,p);disp(g); %Concatenate arrays along specified dimension g1=cyclpoly(n,k,'max');%Produce generator polynomials for cyclic code gp=poly2sym(g1);%Compute characteristic polynomial of matrix disp('Generator Polynomial Gp:');disp(gp); d=input('Enter message to be coded(d):'); dp=poly2sym(d);disp('Message polynomial dp: ');disp(dp); c1=mtimes(dp,gp);disp('dp * gp');disp(c1); %Matrix Multiplication c2=sym2poly(c1);%Symbolic numbers,variables & objects disp('The codeword is (c) :'); c=mod(c2,2);disp(c);%Modulus after division %Decoding disp('Cyclic Codes : Decoding'); r=input('Enter Received bit(r):'); rp=poly2sym(r);disp('Received polynomial rp: ');disp(rp); [qp,remp]=quorem(rp,gp);%Quotient & Remainder disp('Syndrome polynomial:');disp(remp); rem=sym2poly(remp); s=mod(rem,2);disp('Syndrome :');disp(s); disp('Transpose of parity matrix(pt):'); pt=p'; disp(pt); ilp=eye(lp); disp('Parity check matrix(h):'); h=cat(2,pt,ilp);disp(h); disp('Transpose parity check matrix(ht):'); ht=h'; disp(ht); %Corrected code word if (s == 0) disp('The received code is correct.'); else disp('The received code is incorrect.'); row = 0; for j=1:1:n m=xor(s,ht(j,:));disp(m); if (m==0) row = j; disp('Error at the position j: ');disp(row); SGR EFs COEM Page 5

SCET-TE(E&TC) 2012
break; end end r(1,row) = ~r(1,row); disp('Correct codeword is:'); end disp('c='); disp(r);

Output Enter no of codes(n): 7 Enter no of messages(k): 4 Enter parity matrix(p): [1 1 0;0 1 1;1 1 1;1 0 1] Cyclic Codes : Encoding Generator Matrix G: 1 0 0 0 1 1 0 0 1 0 0 0 1 1 0 0 1 0 1 1 1 0 0 0 1 1 0 1 Generator Polynomial Gp: x^3 + x^2 + 1 Enter message to be coded(d):[1 0 1 0] Message polynomial dp: x^3 + x dp * gp (x^3 + x)*(x^3 + x^2 + 1) The codeword is (c) : 1 1 1 0 0 1 0

Cyclic Codes : Decoding Enter Received bit(r):[1 1 0 1 1 0 1] Received polynomial rp: x^6 + x^5 + x^3 + x^2 + 1 Syndrome polynomial: x^2 + 1 Syndrome : 1 0 1 Transpose of parity matrix(pt): 1 0 1 1 SGR EFs COEM Page 6

SCET-TE(E&TC) 2012
1 0 1 1 1 1 0 1 0 0 1

Parity check matrix(h): 1 0 1 1 1 0 1 1 1 0 0 1 0 1 1 1 0 0

Transpose parity check matrix(ht): 1 1 0 0 1 1 1 1 1 1 0 1 1 0 0 0 1 0 0 0 1 The received code is incorrect. 0 1 1 1 0 0 1 1 0 0 0 0

Error at the position j: 4 Correct codeword is: c= 1 1 0 0 1 0

SGR EFs COEM

Page 7

SCET-TE(E&TC) 2012
5) Implementation of algorithms for generating convolution codes using a) Code Tree b) Code Trellis c) Convolution codes Program clear all; clc; m=input('enter the message vector m: '); v1=input('enter the output vector v1: '); v2=input('enter the output vector v2: '); a=conv(m,v1); b=conv(m,v2); a1=rem(a,2); b1=rem(b,2); a3=cat(1,a1,b1); a4=a3(:); code=a4'; disp(code);

Output enter the message vector m: [1 0 1 1] enter the output vector v1: [1 1 1] enter the output vector v2: [1 0 1] Columns 1 through 12 1 1 1 0 0 0 0 1 0 1 1 1

SGR EFs COEM

Page 8

SCET-TE(E&TC) 2012
6) Implementation of algorithms for decoding convolution codes using Viterbis algorithm. Program % Viterbi Decoding clear all; clc; bits=input('Enter the message bits to be encoded: '); trellis=poly2trellis([3],[7 5]); %poly2trellis(ConstraintLength,CodeGenerator); code=convenc(bits,trellis); disp('The encoded message using convolutional code is: '); disp(code); tb=3; %Brust errror lenght; decode=vitdec(code,trellis,tb,'trunc','hard'); disp('The decoded message using viterbi decoding is: '); disp(decode);

Output Enter the message bits to be encoded: [1 0 1 1] The encoded message using convolutional code is: 1 1 1 0 0 0 0 1 The decoded message using viterbi decoding is: 1 0 1 1

SGR EFs COEM

Page 9

SCET-TE(E&TC) 2012
7) Implementation of algorithms for decoding of BCH algorithm. Program %BCH Encoding clear all; clc; m=input('Enter degree of polynomial m='); %find out Codeword Length n = 2^m-1; disp('Codeword Length n= ');disp(n); k=input('Enter Message length k='); ms=input('Enter message ms='); msg = gf(ms); disp(msg); % Find t, the error-correction capability. [genpoly,t] = bchgenpoly(n,k); disp('Error correcting capability :'); disp(t); %t2=input('Enter no. of errors to be added to produce noisy code :'); % Encode the message. code = bchenc(msg,n,k); % Corrupt up to t2 bits in each codeword. tn=1*6; noisycode = code + randerr(1,n,1:tn); % Decode the noisy code. [newmsg,err,ccode] = bchdec(noisycode,n,k); disp(newmsg); if msg==newmsg disp('The message was recovered perfectly.') else disp('Error in recovery of message.'); end Output Enter degree of polynomial m=4 Codeword Length n= 15 Enter Message length k=5 Enter message ms=[1 0 0 1 1] gf object: 1-by-5 Error correcting capability : 3 gf object: 1-by-5 The message was recovered perfectly. SGR EFs COEM Page 10

SCET-TE(E&TC) 2012
8) Implementation of algorithms for RS Coding & Decoding Program %RS CODING & DECODING clear all; clc; n=input('Enter Codeword Length n= '); k=input('Enter Message length k='); m=input('Enter message m='); msg=gf(m,k); c=rsenc(msg,n,k) % Code will be a Galois array. disp(c); r=c; r(1)=2; r(3)=1; r(10)=29; r(11)=12; r(12)=18; [d,e]=rsdec(r,n,k) Output Enter Codeword Length n= 15 Enter Message length k=5 Enter message m=[1 0 0 1 1] c = GF(2^5) array. Primitive polynomial = D^5+D^2+1 (37 decimal) Array elements = Columns 1 through 5 1 0 0 1 1

Columns 6 through 10 26 23 8 26 3

Columns 11 through 15 16 5 24 0 14

gf object: 1-by-15 d = GF(2^5) array. Primitive polynomial = D^5+D^2+1 (37 decimal) Array elements = 1 e=5 SGR EFs COEM Page 11 0 0 1 1

Anda mungkin juga menyukai