Friday, 12 September 2014

#5 HAMMING CODE GENERATION USING EVEN PARITY


WHAT IS HAMMING CODE ?


Hamming code is a combination of message bits and redundant bits (bits that are added to message data in order to detect as well as correct 1-bit error). Hamming code is used in order to correct single bit error in the data. It can be generated by using both even parity as well as odd parity redundancy. Here, I used even parity in order to generate the hamming code. For detection part, wait for a while :P. 

MATLAB CODE:

clc;

clear all
close all
%HAMMING CODE GENERATION BY PRAJWAL KOTAMRAJU (EVEN PARITY) 
%ENTER THE MESSAGE SIGNAL.. Ex:[0 1 1 0]
m=input('enter the messsage sequence- ');
p=length(m);
disp('length of input sequence is')
disp(p);
%Condition for number of redundancy bits 'r' is given by
% 2^r>=p+r+1
%In order to accomplish this, let us have a loop
for l=1:p
if(2^l >= p+l+1)
    r=l;
    break;
end
end
disp('redundancy bits that are to be added to message bits are ');
disp(r);
%The total hamming code length = redundancy bits+ message length
h=r+p;
%Let us have a 1-D matrix for hamming code with all zeros initially
hc=zeros(1,h);
%Let us also have a matrix for positions of redundant bits
for l=1:r
    rp(l)=2^(l-1);
end
%Now let us 1st place the message bits in respective positions
%What we are exactly doing is, we are placing message bits at positions
%other than redundant bit positions.
t=1;
for l=1:h
    k=0;
    for c=1:r
        if(l==rp(c))
            k=1;
            break;
        end
    end
      if(~k)
      hc(l)=m(t);
      t=t+1;
      end
end
%At this stage we have hamming code with message bits placed at desired
%positions
%Now it's time to find redundancy bits and place them in hamming code
%1st for loop- no. of redundant bits
%2nd for loop- Check bit positions for given 'l' value
%3rd for loop- to group check bits .. like 2 as a group, 4 as a group..
for l=1:r
    q=0;
    for v=rp(l):2*rp(l):h;
         for c=1:rp(l)
             if(v+c-1<=h)
                if(hc(v+c-1)==1)
                   q=q+1;
                end
             end
         end
    end
    if(mod(q,2)~=0)
        hc(rp(l))=1;
    end
end
%Finally the hamming code is given by 
disp('hamming code (even parity) for corresponding message data is ');
disp(hc);




For any further details or for any doubts in case of code, contact me at pj.kotamraju225@gmail.com ,+Prajwal Kotamraju
Good bye for now :D !!

No comments:

Post a Comment

Any queries ?