Signal Coding and Estimation Theory Practical

16
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 EF’s COEM Page 1

Transcript of Signal Coding and Estimation Theory Practical

Page 1: Signal Coding and Estimation Theory Practical

SCET-TE(E&TC) 2012

b) Huffman Coding

Program – % Huffman Encoding & Decoding clear all;clcn=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});enddisp(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==1disp('Message recovered correctly');elsedisp('Error in decoding');end

SGR EF’s COEM Page 1

Page 2: Signal Coding and Estimation Theory Practical

SCET-TE(E&TC) 2012

Output –

Enter no. of inputs:5Enter 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] '1' [2] '0 0 0' [3] '0 0 1 1' [4] '0 1' [5] '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 0

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

Message recovered correctly

SGR EF’s COEM Page 2

Page 3: Signal Coding and Estimation Theory Practical

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 dimensiond=input('Enter message to be coded:'); c1=mtimes(d,g); %Matrix Multiplicationdisp('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 EF’s COEM Page 3

Page 4: Signal Coding and Estimation Theory Practical

SCET-TE(E&TC) 2012

Output –

Enter no of codes: 7Enter no of messages: 4Enter parity matrix: [1 1 0;0 1 1;1 1 1;1 0 1]Linear Block Codes : EncodingGenerator 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 : DecodingEnter 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 0 1 1 1 0 0 1 0 0 1 1 1 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 1

SGR EF’s COEM Page 4

Page 5: Signal Coding and Estimation Theory Practical

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 dimensiong1=cyclpoly(n,k,'max');%Produce generator polynomials for cyclic codegp=poly2sym(g1);%Compute characteristic polynomial of matrixdisp('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 Multiplicationc2=sym2poly(c1);%Symbolic numbers,variables & objectsdisp('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 & Remainderdisp('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 wordif (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 EF’s COEM Page 5

Page 6: Signal Coding and Estimation Theory Practical

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): 7Enter no of messages(k): 4Enter parity matrix(p): [1 1 0;0 1 1;1 1 1;1 0 1]Cyclic Codes : EncodingGenerator 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 : DecodingEnter 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 EF’s COEM Page 6

Page 7: Signal Coding and Estimation Theory Practical

SCET-TE(E&TC) 2012

1 1 1 0 0 1 1 1

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

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 1 0

0 1 0

0 0 0

Error at the position j: 4

Correct codeword is:c= 1 1 0 0 1 0 1

SGR EF’s COEM Page 7

Page 8: Signal Coding and Estimation Theory Practical

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 EF’s COEM Page 8

Page 9: Signal Coding and Estimation Theory Practical

SCET-TE(E&TC) 2012

6) Implementation of algorithms for decoding convolution codes using Viterbi’s 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 EF’s COEM Page 9

Page 10: Signal Coding and Estimation Theory Practical

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 Lengthn = 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=4Codeword Length n= 15

Enter Message length k=5Enter 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 EF’s COEM Page 10

Page 11: Signal Coding and Estimation Theory Practical

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= 15Enter Message length k=5Enter 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 0 0 1 1

e = 5

SGR EF’s COEM Page 11