0% found this document useful (0 votes)
16 views

Function Hamming (1)

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Function Hamming (1)

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

function hamming74()

%--- Encoding ---

% Generator Matrix for hamming (7 ,4)

G={1000110;

0100101;

0010011;

0001111];

% Prompt user for 4-bit data word

Data = input (‘Enter the 4-bit data vector ( e.g. , [1 0 1 1] ) : ‘ ) ;

if length (data) ~= 4

Error (‘data must be 4 bits long. ‘ ) ;

end

% Encoding; Multiply the data by the generator matrix

Encoded_data = mod(data * G, 2) ;

disp( ‘ Encloded 7-bit Hamming code is: ‘);

disp(encoded_data) ;

% --- Introduce an error (Optional) ---

Introduce_error = input( ‘ Do you want to introduce an error? (yes=1 /

no=0) : ‘ ) ;

if introduce_error == 1

error_pos >= 1 && error_pos <= 7

encoded_data(error_pos) = mod( encoded_data(error_pos) + 1, 2) ; %

flip the bit to introduce erroe

disp(‘Corrupted code with error: ‘ ) ;

disp( encoded_data) ;

else

error(‘ position must be between 1 and 7 . ‘ ) ;

end

% ---- Decoding ---

% parity – check matrix for hamming (7,4)

H = [1101000;

1010100;

0110010;

1 1 1 0 0 0 1] ;

% syndrome calculation

Syndrome = mod(H * encoded_data’ , 2) ;

Disp(‘syndrome: ‘ ) ;

Disp(syndrome ‘ )

% Detect and correct the error if the syndrome is non-zero

Error_position = bi2de ( syndrome’ , ‘left-msd’ ) ;

if error-position == 0

Disp(‘No error detected. ‘ ) ;

else

Disp( [ ‘Error detected at position: ‘ , num2str (error_position] ) ;

Encoded_data(error_position) = mod(encoded_data(error_position) +1, 2);

% correct the error

Disp(‘ corrected code: ‘ ) ;

Disp (encoded_data ) ;

end

% Extract the original 4-bit data

Decoded_data = encoded_data(1:4) ;

Disp (‘decoded 4-bit data: ‘ ) ;

Disp ( decoded_data)
end

You might also like