0% found this document useful (0 votes)
9 views3 pages

Hamming (7,4) Code Encoding & Decoding

Uploaded by

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

Hamming (7,4) Code Encoding & Decoding

Uploaded by

sharathnaik4444
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

//Encoding and decoding of(7,4) Hamming code

data = [1, 0, 1, 1]; // Input 4-bit data

disp("Original 4-bit Data:");

disp(data);

encoded = hamming74_encode(data);

disp("Encoded 7-bit Code:");

disp(encoded);

// Simulate 1-bit error at bit 5

received = encoded;

received(5) = 1 - received(5);

disp("Received with error at bit 5:");

disp(received);

// Decode and correct

[decoded, err_pos] = hamming74_decode(received);

disp("Decoded & Corrected 4-bit Data:");

disp(decoded);

if err_pos == 0 then

disp("No error detected.");

else

mprintf("Error detected and corrected at bit position: %d\n", err_pos);

end

// Function to encode 4-bit data using Hamming (7,4)

function encoded=hamming74_encode(data)

if length(data) <> 4 then

error("Input must be a 4-bit binary vector.");

end

encoded = zeros(1, 7); // 1x7 vector


// Assign data bits: d1→3, d2→5, d3→6, d4→7

encoded(3) = data(1);

encoded(5) = data(2);

encoded(6) = data(3);

encoded(7) = data(4);

// Calculate parity bits using bitxor

encoded(1) = bitxor(bitxor(encoded(3), encoded(5)), encoded(7)); // p1

encoded(2) = bitxor(bitxor(encoded(3), encoded(6)), encoded(7)); // p2

encoded(4) = bitxor(bitxor(encoded(5), encoded(6)), encoded(7)); // p3

endfunction

// Function to decode and correct 7-bit Hamming code

function [corrected_data, error_pos]=hamming74_decode(received)

if length(received) <> 7 then

error("Input must be a 7-bit binary vector.");

end

// Calculate syndrome bits

s1 = bitxor(bitxor(received(1), received(3)), bitxor(received(5), received(7)));

s2 = bitxor(bitxor(received(2), received(3)), bitxor(received(6), received(7)));

s3 = bitxor(bitxor(received(4), received(5)), bitxor(received(6), received(7)));

// Convert binary syndrome to decimal error position

error_pos = s1 + 2*s2 + 4*s3;

corrected = received;

// Correct error if needed

if error_pos <> 0 then

corrected(error_pos) = 1 - corrected(error_pos);

end

// Extract original data: d1, d2, d3, d4


corrected_data = [corrected(3), corrected(5), corrected(6), corrected(7)];

endfunction

You might also like