Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
170 views

Matlab Code Functions Used I.) CRC Modulo 2 Division: Function

This MATLAB code implements cyclic redundancy check (CRC) encoding and decoding. It contains functions for CRC division, determining the size of a number in a given radix, cyclic code encoding, and cyclic code decoding. The cyclic encoding section generates a code word from input data and generator polynomials. The cyclic decoding section checks for errors by dividing the received code word by the generator polynomial and indicating any non-zero remainders, which indicate bit errors. The code is tested by encoding a sample data word, and decoding both the correct and error-containing received code words.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
170 views

Matlab Code Functions Used I.) CRC Modulo 2 Division: Function

This MATLAB code implements cyclic redundancy check (CRC) encoding and decoding. It contains functions for CRC division, determining the size of a number in a given radix, cyclic code encoding, and cyclic code decoding. The cyclic encoding section generates a code word from input data and generator polynomials. The cyclic decoding section checks for errors by dividing the received code word by the generator polynomial and indicating any non-zero remainders, which indicate bit errors. The code is tested by encoding a sample data word, and decoding both the correct and error-containing received code words.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

MATLAB CODE

Functions used
i.)CRC modulo 2 division
function rem=crcdiv(data_d,gen)
msgs=size_t(data_d,2);
ds=size_t(gen,2);
temp=0;
rem=0;
for i=1:msgs
temp = ( bitsrl(uint16(data_d),msgs-i) );
temp=mod(temp,2);
rem=bitsll(uint16(rem),1);
rem=rem+double(temp);
if(size_t(rem,2) == ds)
rem=bitxor(rem,gen);
end
end

ii.)size_t() size of number,given a radix


function sz=size_t(no,rad)
sz=floor( log10(double(no))/log10(rad) +1);

Cyclic code encoder


syms x;
n=input(Enter n);
k=input('Enter k ');
data=input('Enter the data word ');
gen=( primpoly(n-k) );%Primitive polynomial of degree n-k
%disp(data);
disp('Generator:');
disp( dec2bin( gen ) );
w=1;
data_d=0;
for i=length(data):-1:1
data_d=data_d+data(i)*w;
w=w*2;
end
data_d=data_d*2^(n-k);
rem=crcdiv(data_d,gen);
code=data_d+rem;
disp('Code word generated:');
disp(dec2bin(code));

Cyclic code decoder


n=input('Enter n ');
k=input('Enter k ');
gen=input('Enter the generator
w=1;
gen_d=0;
for i=length(gen):-1:1

');

gen_d=gen_d+gen(i)*w;
w=w*2;
end
code=input('Enter the code word received ');
code_d=0;
w=1;
for i=length(code):-1:1
code_d=code_d+code(i)*w;
w=w*2;
end
for i=1:n
dtab(i)=crcdiv(2^(i-1),gen_d);
%disp(dtab(i))
end
syn=crcdiv(code_d,gen_d);
if(syn==0)
disp('No errors in received codeword')
end
for i=1:n
if(dtab(i)==syn)
fprintf('Bit error at position %d\n',n-i+1);
end
end

OUTPUT
Cyclic encoder
i.)Data word [1 0 0 1]
Enter n 7
Enter k 4
Enter the data word [1 0 0 1]
Generator:
1011
Code word generated:
1001110

OUTPUT:
Cyclic decoder
i.) Received Codeword : 1001110
Original Codeword : 1001110
Enter n 7
Enter k 4
Enter the generator [1 0 1 1]
Enter the code word received [1 0 0 1 1 1 0]
No errors in received codeword
ii.) Received Codeword : 1001010
Original Codeword : 1001110
Enter n 7
Enter k 4
Enter the generator [1 0 1 1]
Enter the code word received [1 0 0 1 0 1 0]
Bit error at position 5

You might also like