0% found this document useful (0 votes)
23 views29 pages

Itc Practicle

Uploaded by

brainknight714
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views29 pages

Itc Practicle

Uploaded by

brainknight714
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 29

L D Engg College , Ahmedabad Electronics & Communication Engg Dept.

PRACTICAL1

A : Find entropy for the given set of probability using MATLAB.

n=input('How many messages are there? : ');


p=[ ];
for x= 1:n
sprintf('Enter the value of the message %d ''s
probability',x)
p(x)=input('p = ');
end
entr=0;
if sum(p)==1
for x=1:n
entr=entr+p(x)*log(1/p(x));
end
else
disp('Total probability should be equal to 1, Please
enter the correct value....')
end
entr=entr/log(2);
sprintf('entropy = %f bits/message',entr)

Output

How many messages are there? : 4

Enter the value of the message 1 's probability

p = .3

Enter the value of the message 2 's probability

p = .1

Enter the value of the message 3 's probability

p = .2

ans =

Enter the value of the message 4 's probability

p = .4

ans =

entropy = 1.846439 bits/message

ITC (ME CSE SEM-I) 1 Enrollment : 090280705021


L D Engg College , Ahmedabad Electronics & Communication Engg Dept.

Practical 1 B

AIM: - Write a MATLAB to plot the capacity of band limited AWGN channel with
input constraint S and bandwidth B given by
C=B log (1+S/N)

THEORY:- The channel capacity C is, by definition ,the maximum rate of


information transmission over a channel
C=B log (1+S/N)
The equation indicates that the channel capacity goes to infinite as channel’s
bandwidth goes to infinite. This is however not true. For white noise, the noise
power N=B. Hence, as B increases also increases .it can be shown that in
limit as B->,C approaches a limit ,which is as follow
Lim C=1.44 S/N bits/sec

PROCEDURE :-

 We have simulated this practical in MATLAB.


 To execute the program press F5,it will request to input the signal
power & noise PSD.
 See the capacity v/s bandwidth plot for the different value of signal
power and noise PSD.

ITC (ME CSE SEM-I) 2 Enrollment : 090280705021


L D Engg College , Ahmedabad Electronics & Communication Engg Dept.

PROGRAM:-

clc;
s=input('enter signal power');
w=input('enter the value of white power per unit
bandwidth');
b=0:0.1:100;
d=w.*b;
e=s./d;
c=b.*log2(1+e);
plot(b,c);
xlabel('Bandwidth --->');
ylabel('Capicaty--->');
grid;

OUTPUT:

Enter signal power 45


Enter value of white power per unit bandwidth 5

14

12

10
Capicaty--->

0
0 10 20 30 40 50 60 70 80 90 100
Bandwidth --->

ITC (ME CSE SEM-I) 3 Enrollment : 090280705021


L D Engg College , Ahmedabad Electronics & Communication Engg Dept.

PRACTICAL 2-A

AIM : Determine the error correcting capability of given (n,k) code using
hamming bound.

clc;
clear all;
close all;
n=input('enter the code bits : n :');
k=input('enter the data bits : k :');
m=n-k; disp(m);
z=2^m; disp (z);
i=0;
for j=0:1:n
sum=0;
for i=0:1:j
c=factorial(n)/(factorial(n-i)*factorial(i));
sum=sum+c;
end

if sum >=z
j=j-1;
sum=sum-c;
break;
end
end

ans=sprintf('(%d,%d) can correct all combinations of %d


errors and it can also correct %d combinations of %d
errors' , n,k,j,z-sum,j+1);
disp(ans);

OUTPUT

enter the code bits : n :7


enter the data bits : k :4

(7,4) can correct all combinations of 0 errors and it can also correct 7
combinations of 1 errors

ITC (ME CSE SEM-I) 4 Enrollment : 090280705021


L D Engg College , Ahmedabad Electronics & Communication Engg Dept.

PRACTICAL 2-B

AIM : Determine all the codewords and minimum weights of (n,k) linear
block code.

clc;
clear all;
close all;
n=input('enter the code digits');
k=input('enter the data digits');
p=input('enter the parity matrix it must bei k rows and m
coloums'); disp(p)
z=input('enter 1 for systmetic and 0 for nonsystmetic');
msg=[];
for i=0:2^k-1
msg=[msg;de2bi(i,k,'left-msb')];
end
if z==1
%p=[1 0 1;0 1 1;1 1 0];
i=eye([k k])
g=[i p]
else
g=input('enter the generator matrix in k*n ');
end
code =rem(msg*g,2)

disp('difference matrix');
for i=1:2^k,
for j=1:n,
if c(i,j)==c(i+1,j)
x(i)=x(i);
else
x(i)=(x(i)+1);
end;
%x(i)=7-x(i);
end;
end;
disp(x);
a=x(1);
for i=2:16,
if(x(i)>0)
if(a>=x(i))
a=x(i);
end
end
end
disp('Dmin');
disp(a);
disp('no of error that can be corrected');
t=(a-1)/2;
disp(t);

ITC (ME CSE SEM-I) 5 Enrollment : 090280705021


L D Engg College , Ahmedabad Electronics & Communication Engg Dept.

OUTPUT

enter the code digits 7


enter the data digits 4
enter the parity matrix it must bei k rows and m coloums:
[ 1 1 1; 1 0 1; 1 1 0; 0 1 1]

enter 1 for systmetic and 0 for nonsystmetic : 1

Data matrix
0 0 0 0
0 0 0 1
0 0 1 0
0 0 1 1
0 1 0 0
0 1 0 1
0 1 1 0
0 1 1 1
1 0 0 0
1 0 0 1
1 0 1 0
1 0 1 1
1 1 0 0
1 1 0 1
1 1 1 0
1 1 1 1

genmarix =
1 0 0 0 1 1 1
0 1 0 0 1 0 1
0 0 1 0 1 1 0
0 0 0 1 0 1 1

codewords =
0 0 0 0 0 0 0
0 0 0 1 0 1 1
0 0 1 0 1 1 0
0 0 1 1 1 0 1
0 1 0 0 1 0 1
0 1 0 1 1 1 0
0 1 1 0 0 1 1
0 1 1 1 0 0 0
1 0 0 0 1 1 1
1 0 0 1 1 0 0
1 0 1 0 0 0 1
1 0 1 1 0 1 0
1 1 0 0 0 1 0
1 1 0 1 0 0 1
1 1 1 0 1 0 0
1 1 1 1 1 1 1

ITC (ME CSE SEM-I) 6 Enrollment : 090280705021


L D Engg College , Ahmedabad Electronics & Communication Engg Dept.

PRACTICAL 2-C

AIM : Find syndrome for each possible error vector & prepare a suitable
decoding table for (n,k) systematic linear block code.

clc;
clear all;
close all;
n=input('enter the code digits');
k=input('enter the data digits');
i=eye([k k])
d = de2bi(0:2^k-1,3,'left-msb');
a = input('to generate systematic code press 1 or any
other to generate non systematic code: ');

if(a ==1 )

sprintf('enter parity matrix of size %dX%d',k,n-k)


p =input('parity matrix: ');

g=[i p];
h=[p i];
c=rem(d*g,2)
else
sprintf('enter generator matrix of size %dX%d',k,n)
ns = input('generator matrix: ');
c1=rem(d*ns,2)
h=ns;
end

trt = syndtable(h); % Produce decoding table.


sprintf('enter received code of size 1X%d',n)
%recd = [1 1 1 0 0 1 ] % Suppose this is the received
vector.
recd = input('received code: ');
syndrome = rem(recd * h',2);
syndrome_de = bi2de(syndrome,'left-msb'); % Convert to
decimal.
disp(['Syndrome = ',num2str(syndrome_de),...
' (decimal), ',num2str(syndrome),' (binary)'])
corrvect = trt(1+syndrome_de,:) % Correction vector
% Now compute the corrected codeword.
correctedcode = rem(corrvect+recd,2)
%r=input('enter the recive code ');

ITC (ME CSE SEM-I) 7 Enrollment : 090280705021


L D Engg College , Ahmedabad Electronics & Communication Engg Dept.

OUTPUT

enter the code digits 7


enter the data digits 4

to generate systematic code press 1 or any other to generate non systematic


code: 1

enter parity matrix of size 4X3

parity matrix: [1 1 1; 1 0 1; 1 1 0; 0 1 1]

c=

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

Single-error patterns loaded in decoding table. 8 rows remaining.


2-error patterns loaded. 1 rows remaining.
3-error patterns loaded. 0 rows remaining.

enter received code of size 1X7

received code: [1 1 1 1 0 0 1]

Syndrome = 1 (decimal), 0 0 0 1 (binary)

corrvect =

0 0 0 0 0 0 1

correctedcode =

1 1 1 1 0 0 0

ITC (ME CSE SEM-I) 8 Enrollment : 090280705021


L D Engg College , Ahmedabad Electronics & Communication Engg Dept.

Syndrome table

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

ITC (ME CSE SEM-I) 9 Enrollment : 090280705021


L D Engg College , Ahmedabad Electronics & Communication Engg Dept.

PRACTICAL 2-D

AIM : Write a MATLAB code to decode all the received words for (n,k)
linear block code.

clc;
clear all;
close all;
n=input('enter the code digits');
k=input('enter the data digits');
p=input('enter the parity matrix it must bei k rows and m
coloums'); disp(p)
z=input('enter 1 for systmetic and 0 for nonsystmetic');
msg=[];
for i=0:2^k-1
msg=[msg;de2bi(i,k,'left-msb')];
end
if z==1
%p=[1 0 1;0 1 1;1 1 0];
i=eye([k k])
g=[i p]
else
g=input('enter the generator matrix in k*n ');
end
code =rem(msg*g,2)

decmsg=decode(code ,n,k,'linear',g)

OUTPUT

enter the code digits : 7


enter the data digits : 4
enter the parity matrix it must bei k rows and m coloums :
[ 1 1 1; 1 0 1; 1 1 0; 0 1 1]

enter 1 for systmetic and 0 for nonsystmetic1

generator matrix =

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

ITC (ME CSE SEM-I) 10 Enrollment : 090280705021


L D Engg College , Ahmedabad Electronics & Communication Engg Dept.

Code words =

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

decodedmsg =

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

ITC (ME CSE SEM-I) 11 Enrollment : 090280705021


L D Engg College , Ahmedabad Electronics & Communication Engg Dept.

PRACTICAL : 3

AIM : Simulate a Cyclic Code using MATLAB .

clc;
clear all;
close all;

n=7;
k=4;
gx=[1 1 0 1]
d=de2bi(0:2^k-1,'left-msb')
[i,px]= cyclgen(7,gx,'system')
g= circshift(px, [0, 4])
c=rem(d*g,2)

OUTPUT:

Generator Poly:
gx =

1 1 0 1

Data Matrix:
d=

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

ITC (ME CSE SEM-I) 12 Enrollment : 090280705021


L D Engg College , Ahmedabad Electronics & Communication Engg Dept.

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

CodeWord :
c=

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

ITC (ME CSE SEM-I) 13 Enrollment : 090280705021


L D Engg College , Ahmedabad Electronics & Communication Engg Dept.

PRACTICAL : 4

AIM : Simulate a Convolution encoder using MATLAB

Function for convolutional code : convenc

Convolutionally encode binary data

Syntax

code = convenc(msg,trellis)
code = convenc(msg,trellis,puncpat)
code = convenc(msg,trellis,...,init_state)
[code,final_state] = convenc(...)

Description

code = convenc(msg,trellis) encodes the binary vector msg using the


convolutional encoder whose MATLAB trellis structure is trellis. For details
about MATLAB trellis structures. Each symbol in msg consists of
log2(trellis.numInputSymbols) bits. The vector msg contains one or more
symbols. The output vector code contains one or more symbols, each of
which consists of log2(trellis.numOutputSymbols) bits.

code = convenc(msg,trellis,puncpat) is the same as the syntax above, except


that it specifies a puncture pattern, puncpat, to allow higher rate encoding.
puncpat must be a vector of 1s and 0s, where the 0s indicate the punctured
bits. puncpat must have a length of at least log2(trellis.numOutputSymbols)
bits.

code = convenc(msg,trellis,...,init_state) allows the encoder registers to start


at a state specified by init_state. init_state is an integer between 0 and
trellis.numStates-1 and must be the last input parameter. To use the default
value for init_state, specify it as 0 or [ ].

[code,final_state] = convenc(...) encodes the input message and also returns


the encoder's state in final_state. final_state has the same format as
init_state.

ITC (ME CSE SEM-I) 14 Enrollment : 090280705021


L D Engg College , Ahmedabad Electronics & Communication Engg Dept.

PRACTICAL : 4 – A

AIM : Encode a bit stream using Convolution Coding

clc;
clear all;
close all;
a=input('enter the input stream :');
a=[a 0 0]
s2=0;
s3=0;
v=[];
for i=1:length(a)
s1=a(i);
v1=rem((s1+s2+s3),2);
v2=rem((s1+s3),2);
v=[v v1 v2];
s3=s2;
s2=s1;
end
disp('code vector')
v

OUTPUT

Here v1= s1 + s2 + s3
And v2=s1+s3

enter the input stream :[1 0 1 0 0 ]

a=

1 0 1 0 0 0 0

code vector

v=

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

ITC (ME CSE SEM-I) 15 Enrollment : 090280705021


L D Engg College , Ahmedabad Electronics & Communication Engg Dept.

PRACTICAL : 4 – B

AIM : Decode the convolution code using viterbi decoding algorithm and
the check the bit error rate.

trel = poly2trellis(3,[6 7]); % Define trellis.


msg = randint(100,1,2,123); % Random data
code = convenc(msg,trel); % Encode.
ncode = rem(code + randerr(200,1,[0 1;.95 .05]),2); % Add
noise.
tblen = 3; % Traceback length
decoded1 = vitdec(ncode,trel,tblen,'cont','hard'); %Hard
decision
% Use unquantized decisions.
ucode = 1-2*ncode; % +1 & -1 represent zero & one,
respectively.
decoded2 = vitdec(ucode,trel,tblen,'cont','unquant');
% To prepare for soft-decision decoding, map to decision
values.
[x,qcode] = quantiz(1-2*ncode,[-.75 -.5 -.25
0 .25 .5 .75],...
[7 6 5 4 3 2 1 0]); % Values in qcode are between 0 and
2^3-1.
decoded3 = vitdec(qcode',trel,tblen,'cont','soft',3);
% Compute bit error rates, using the fact that the
decoder
% output is delayed by tblen symbols.
[n1,r1] = biterr(decoded1(tblen+1:end),msg(1:end-tblen));
[n2,r2] = biterr(decoded2(tblen+1:end),msg(1:end-tblen));
[n3,r3] = biterr(decoded3(tblen+1:end),msg(1:end-tblen));
disp(['The bit error rates are: ',num2str([r1 r2 r3])])

OUTPUT

The bit error rates are: 0.020619 0.020619 0.020619


The bit error rates are: 0.020619 0.020619 0.020619

ITC (ME CSE SEM-I) 16 Enrollment : 090280705021


L D Engg College , Ahmedabad Electronics & Communication Engg Dept.

PRACTICAL : 5

AIM : Implement a Reed-Solomon codes in MATLAB.

Function for Reed Solomon coding:

 rsenc

Reed-Solomon encoder

Syntax

code = rsenc(msg,n,k)
code = rsenc(msg,n,k,genpoly)
code = rsenc(...,paritypos)

Description

code = rsenc(msg,n,k) encodes the message in msg using an [n,k] Reed-


Solomon code with the narrow-sense generator polynomial. msg is a Galois
array of symbols having m bits each. Each k-element row of msg represents a
message word, where the leftmost symbol is the most significant symbol. n is
at most 2m-1. If n is not exactly 2m-1, rsenc uses a shortened Reed-Solomon
code. Parity symbols are at the end of each word in the output Galois array
code.

code = rsenc(msg,n,k,genpoly) is the same as the syntax above, except


that a nonempty value of genpoly specifies the generator polynomial for the
code. In this case, genpoly is a Galois row vector that lists the coefficients, in
order of descending powers, of the generator polynomial. The generator
polynomial must have degree n-k. To use the default narrow-sense generator
polynomial, set genpoly to [].

code = rsenc(...,paritypos) specifies whether rsenc appends or prepends the


parity symbols to the input message to form code. The string paritypos can be
either 'end' or 'beginning'. The default is 'end'.

 rsgenpoly

Generator Polynomial

The rsgenpoly function produces generator polynomials for Reed-Solomon


codes. This is useful if you want to use rsenc and rsdec with a generator
polynomial other than the default, or if you want to examine or manipulate a
generator polynomial. rsgenpoly represents a generator polynomial using a
Galois row vector that lists the polynomial's coefficients in order of descending
powers of the variable. If each symbol has m bits, the Galois row vector is in

ITC (ME CSE SEM-I) 17 Enrollment : 090280705021


L D Engg College , Ahmedabad Electronics & Communication Engg Dept.

the field GF(2m). For example, the command

r = rsgenpoly(15,13)

r = GF(2^4) array. Primitive polynomial = D^4+D+1 (19 decimal)

Array elements =

1 6 8

finds that one generator polynomial for a [15,13] Reed-Solomon code is X2 +


(A2 + A)X+ (A3), where A is a root of the default primitive polynomial for
GF(16).

 rsdec

Reed-Solomon decoder

Syntax

decoded = rsdec(code,n,k)
decoded = rsdec(code,n,k,genpoly)
decoded = rsdec(...,paritypos)
[decoded,cnumerr] = rsdec(...)
[decoded,cnumerr,ccode] = rsdec(...)

Description

decoded = rsdec(code,n,k) attempts to decode the received signal in code


using an [n,k] Reed-Solomon decoding process with the narrow-sense
generator polynomial. code is a Galois array of symbols having m bits each.
Each n-element row of code represents a corrupted systematic codeword,
where the parity symbols are at the end and the leftmost symbol is the most
significant symbol. n is at most 2m-1. If n is not exactly 2m-1, rsdec assumes
that code is a corrupted version of a shortened code.

In the Galois array decoded, each row represents the attempt at decoding the
corresponding row in code. A decoding failure occurs if rsdec detects more
than (n-k)/2 errors in a row of code. In this case, rsdec forms the
corresponding row of decoded by merely removing n-k symbols from the end
of the row of code.

ITC (ME CSE SEM-I) 18 Enrollment : 090280705021


L D Engg College , Ahmedabad Electronics & Communication Engg Dept.

Example: Reed-Solomon Coding Syntaxes

The example below illustrates multiple ways to encode and decode data using
a [15,13] Reed-Solomon code. The example shows that you can

 Vary the generator polynomial for the code, using rsgenpoly to produce a
different generator polynomial.
 Vary the primitive polynomial for the Galois field that contains the symbols,
using an input argument in gf.
 Vary the position of the parity symbols within the codewords, choosing
either the end (default) or beginning.

This example also shows that corresponding syntaxes of rsenc and rsdec use
the same input arguments, except for the first input argument.

m = 4; % Number of bits in each symbol


n = 2^m-1; k = 13; % Codeword length and message length
data = randint(4,k,2^m); % Four random integer messages
msg = gf(data,m); % Represent data using a Galois array.

% Simplest syntax for encoding


c1 = rsenc(msg,n,k);
d1 = rsdec(c1,n,k);

% Vary the generator polynomial for the code.


c2 = rsenc(msg,n,k,rsgenpoly(n,k,19,2));
d2 = rsdec(c2,n,k,rsgenpoly(n,k,19,2));

% Vary the primitive polynomial for GF(16).


msg2 = gf(data,m,25);
c3 = rsenc(msg2,n,k);
d3 = rsdec(c3,n,k);

% Prepend the parity symbols instead of appending them.

c4 = rsenc(msg,n,k,'beginning');
d4 = rsdec(c4,n,k,'beginning');

% Check that the decoding worked correctly.


chk = isequal(d1,msg) & isequal(d2,msg) &
isequal(d3,msg2) &...
isequal(d4,msg)

ITC (ME CSE SEM-I) 19 Enrollment : 090280705021


L D Engg College , Ahmedabad Electronics & Communication Engg Dept.

OUTPUT :

data =

0 2 4 7 13 13 13 4 4 6 7 13 5
5 3 3 14 8 0 8 3 8 13 14 10 8
13 3 0 7 3 10 11 3 2 13 13 5 11
0 9 11 6 10 6 6 10 11 9 10 4 4

msg = GF(2^4) array. Primitive polynomial = D^4+D+1 (19 decimal)

Array elements =

Columns 1 through 8

0 2 4 7 13 13 13 4
5 3 3 14 8 0 8 3
13 3 0 7 3 10 11 3
0 9 11 6 10 6 6 10

Columns 9 through 13

4 6 7 13 5
8 13 14 10 8
2 13 13 5 11
11 9 10 4 4

c1 = GF(2^4) array. Primitive polynomial = D^4+D+1 (19 decimal)

Array elements =

Columns 1 through 8

0 2 4 7 13 13 13 4
5 3 3 14 8 0 8 3
13 3 0 7 3 10 11 3
0 9 11 6 10 6 6 10

Columns 9 through 15

4 6 7 13 5 14 5
8 13 14 10 8 3 7
2 13 13 5 11 3 12
11 9 10 4 4 1 15

ITC (ME CSE SEM-I) 20 Enrollment : 090280705021


L D Engg College , Ahmedabad Electronics & Communication Engg Dept.

PRACTICAL : 6

AIM : Implement a BCH codes in MATLAB.

Function for BCH coding:

 bchenc

BCH encoder

Syntax

code = bchenc(msg,n,k)
code = bchenc(...,paritypos)

Description

code = bchenc(msg,n,k) encodes the message in msg using an [n,k] BCH


encoder with the narrow-sense generator polynomial. msg is a Galois array of
symbols over GF(2). Each k-element row of msg represents a message word,
where the leftmost symbol is the most significant symbol. Parity symbols are
at the end of each word in the output Galois array code.

code = bchenc(...,paritypos) specifies whether bchenc appends or prepends


the parity symbols to the input message to form code. The string paritypos
can be either 'end' or 'beginning'. The default is 'end'.

 bchdec

BCH decoder

Syntax

decoded = bchdec(code,n,k)
decoded = bchdec(...,paritypos)
[decoded,cnumerr] = bchdec(...)
[decoded,cnumerr,ccode] = bchdec(...)

Description

decoded = bchdec(code,n,k) attempts to decode the received signal in code


using an [n,k] BCH decoder with the narrow-sense generator polynomial.
code is a Galois array of symbols over GF(2). Each n-element row of code
represents a corrupted systematic codeword, where the parity symbols are at
the end and the leftmost symbol is the most significant symbol.

In the Galois array decoded, each row represents the attempt at decoding the
corresponding row in code. A decoding failure occurs if bchdec detects more
than t errors in a row of code, where t is the number of correctable errors as
reported by bchgenpoly. In the case of a decoding failure, bchdec forms the

ITC (ME CSE SEM-I) 21 Enrollment : 090280705021


L D Engg College , Ahmedabad Electronics & Communication Engg Dept.

corresponding row of decoded by merely removing n-k symbols from the end
of the row of code.

decoded = bchdec(...,paritypos) specifies whether the parity symbols in code


were appended or prepended to the message in the coding operation. The
string paritypos can be either 'end' or 'beginning'. The default is 'end'. If
paritypos is 'beginning', then a decoding failure causes bchdec to remove n-k
symbols from the beginning rather than the end of the row.

Representing Words for BCH Codes

A message for an [n,k] BCH code must be a k-column binary Galois array.
The code that corresponds to that message is an n-column binary Galois
array. Each row of these Galois arrays represents one word.

The example below illustrates how to represent words for a [15, 11] BCH
code.

n = 15; k = 5; % Codeword length and message length


msg = gf([1 0 0 1 0; 1 0 1 1 1]); % Two messages in a
Galois array
cbch = bchenc(msg,n,k) % Two codewords in a Galois array.

The output is
cbch = GF(2) array.

Array elements =

Columns 1 through 5

1 0 0 1 0
1 0 1 1 1

Columns 6 through 10

0 0 1 1 1
0 0 0 0 1

Columns 11 through 15

1 0 1 0 1
0 1 0 0 1

ITC (ME CSE SEM-I) 22 Enrollment : 090280705021


L D Engg College , Ahmedabad Electronics & Communication Engg Dept.

Example: BCH Coding

The example below illustrates how to encode and decode data using a [15, 5]
Reed-Solomon code. The example shows that

 You can vary the position of the parity symbols within the codewords,
choosing either the end (default) or beginning.

 Corresponding syntaxes of bchenc and bchdec use the same input


arguments, except for the first input argument.

n = 15; k = 5; % Codeword length and message length


dat = randint(4,k); % Four random binary messages
msg = gf(dat); % Represent data using a Galois array.

% Simplest syntax for encoding


c1 = bchenc(msg,n,k);
d1 = bchdec(c1,n,k);

% Prepend the parity symbols instead of appending them.


c2 = bchenc(msg,n,k,'beginning');
d2 = bchdec(c2,n,k,'beginning');

% Check that the decoding worked correctly.


chk = isequal(d1,msg) & isequal(d2,msg)

OUTPUT:

dat =

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

Parity bits are added at the end.

c1 = GF(2) array.

Array elements =

Columns 1 through 8

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

ITC (ME CSE SEM-I) 23 Enrollment : 090280705021


L D Engg College , Ahmedabad Electronics & Communication Engg Dept.

Columns 9 through 15

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

c2 = GF(2) array.

Parity bits are added at the begining.

Array elements =
Columns 1 through 8
1 1 1 1 1 1 1 1
0 0 1 0 0 0 1 1
1 1 0 1 1 1 0 0
0 0 1 1 0 1 1 1

Columns 9 through 15

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

ITC (ME CSE SEM-I) 24 Enrollment : 090280705021


L D Engg College , Ahmedabad Electronics & Communication Engg Dept.

PRACTICAL : 7

AIM : Implement a Huffman codes in MATLAB.

Creating a Huffman Code Dictionary

Huffman coding requires statistical information about the source of the


data being encoded. In particular, the p input argument in the huffmandict
function lists the probability with which the source produces each symbol in its
alphabet.

For example, consider a data source that produces 1s with probability


0.1, 2s with probability 0.1, and 3s with probability 0.8. The main
computational step in encoding data from this source using a Huffman code is
to create a dictionary that associates each data symbol with a codeword. The
commands below create such a dictionary and then show the codeword
vector associated with a particular value from the data source.

symbols = [1 2 3]; % Data symbols


p = [0.1 0.1 0.8]; % Probability of each data symbol
dict = huffmandict(symbols,p) % Create the dictionary.
dict{1,:} % Show one row of the dictionary.

The output below shows that the most probable data symbol, 3, is
associated with a one-digit codeword, while less probable data symbols are
associated with two-digit codewords. The output also shows, for example, that
a Huffman encoder receiving the data symbol 1 should substitute the
sequence 11.
dict =

[1] [1x2 double]


[2] [1x2 double]
[3] [ 0]

ITC (ME CSE SEM-I) 25 Enrollment : 090280705021


L D Engg College , Ahmedabad Electronics & Communication Engg Dept.

Example: Creating and Decoding a Huffman Code

symbols=input('enter the possible input message : '); %


Distinct data symbols appearing in sig
p=input('Enter the probability of the all possible
message : ');
totalp=sum(p);
if totalp==1
dict = huffmandict(symbols,p); % Create the
dictionary.
else
disp('Total probability must be equal to 1');
end
dict{1,:} % shows the first row of the dict.

msgseq=input('Enter the sequence of the possible


message : ');

msgcode= huffmanenco(msgseq,dict); % Encode the data.


msgdecode= huffmandeco(msgcode,dict); % Decode the code.

OUTPUT

Enter the possible input message : [1 2 3 4 5]


Enter the probability of the all possible message : [.1 .15 .3 .25 .2]

ans =

ans =

1 0 1

Enter the sequence of the possible message : [1 2 3 4 3 2 1 2 3 3 3 2 2 5 3 2]

>> dict{2,:} % Show one row of the dictionary.

ans =

ans =

ITC (ME CSE SEM-I) 26 Enrollment : 090280705021


L D Engg College , Ahmedabad Electronics & Communication Engg Dept.

1 0 0

>> dict{3,:} % Show third row of the dictionary.

ans =

ans =

0 0

>> dict{4,:} % Show forth row of the dictionary.

ans =

ans =

0 1

>> dict{5,:} % Show fifth row of the dictionary.

ans =

ans =

1 1

ITC (ME CSE SEM-I) 27 Enrollment : 090280705021


L D Engg College , Ahmedabad Electronics & Communication Engg Dept.

PRACTICAL : 8

AIM : Implement a Arithmatic codes in MATLAB.

Arithmetic Coding

Arithmetic coding offers a way to compress data and can be useful for
data sources having a small alphabet. The length of an arithmetic code,
instead of being fixed relative to the number of symbols being encoded,
depends on the statistical frequency with which the source produces each
symbol from its alphabet. For long sequences from sources having skewed
distributions and small alphabets, arithmetic coding compresses better than
Huffman coding.

The arithenco and arithdeco functions support arithmetic coding and


decoding.

Representing Arithmetic Coding Parameters

Arithmetic coding requires statistical information about the source of


the data being encoded. In particular, the counts input argument in the
arithenco and arithdeco functions lists the frequency with which the source
produces each symbol in its alphabet. You can determine the frequencies by
studying a set of test data from the source. The set of test data can have any
size you choose, as long as each symbol in the alphabet has a nonzero
frequency.

For example, before encoding data from a source that produces 10 x's,
10 y's, and 80 z's in a typical 100-symbol set of test data, define

counts = [10 10 80];

Alternatively, if a larger set of test data from the source contains 22 x's, 23 y's,
and 185 z's, then define

counts = [22 23 185];

ITC (ME CSE SEM-I) 28 Enrollment : 090280705021


L D Engg College , Ahmedabad Electronics & Communication Engg Dept.

Example: Creating and Decoding an Arithmetic Code

The example below performs arithmetic encoding and decoding, using a


source whose alphabet has three symbols.

symbols=input('enter the possible input message : '); % Distinct


data symbols appearing in sig
counts=input('Enter the count of the all possible message : ');

seq=input('Enter the sequence of the possible message : ');

code = arithenco(seq,counts);

code;
dseq = arithdeco(code,counts,length(seq));

dseq;

OUTPUT:

Enter the possible input message : [1 2 3]


Enter the count of the all possible message : [5 5 10]
Enter the sequence of the possible message :
[1 2 3 1 2 3 1 1 1 2 3 2 3 2 3 3 3 3 3 3]
code =
Columns 1 through 16

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

Columns 17 through 32

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

Columns 33 through 37

0 0 0 0 0

dseq =

Columns 1 through 16

1 2 3 1 2 3 1 1 1 2 3 2 3 2 3 3

Columns 17 through 20

3 3 3 3

ITC (ME CSE SEM-I) 29 Enrollment : 090280705021

You might also like