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

Hamming Code

The document is a big assignment on Hamming Code for a Linear Algebra course at Ho Chi Minh City University of Technology. It covers the principles, encoding and decoding processes, error detection and correction capabilities, and includes Matlab code implementation. The assignment emphasizes the importance of Hamming codes in ensuring data accuracy during transmission and storage.
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)
3 views

Hamming Code

The document is a big assignment on Hamming Code for a Linear Algebra course at Ho Chi Minh City University of Technology. It covers the principles, encoding and decoding processes, error detection and correction capabilities, and includes Matlab code implementation. The assignment emphasizes the importance of Hamming codes in ensuring data accuracy during transmission and storage.
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
You are on page 1/ 20

HO CHI MINH CITY

UNIVERSITY OF TECHNOLOGY

LINEAR ALGEBRA (MT1007)

BIG ASSIGNMENT TOPIC:

HAMMING CODE

Advisor: Nguyễn Tiến Dũng


No. Fullname Student ID
Civil Engineering
1 Lý Quang Nhật 2452892

Civil Engineering
2 Nguyễn Hà Phong 2452959

Civil Engineering
3 Nguyễn Minh Quân 2453073

Transportation
4 Mai Tường Bách 2452115 Engineering

Trương Nguyễn Mechanical


5 2052571 Engineering
Đình Lâm
Ho Chi Minh City, November 6

Big Assignment for Linear Algebra Page 1/20


HO CHI MINH UNIVERSITY OF TECHNOLOGY (HCMUT)

Contents:
I Acknowledgement.................................................................................................4
II Abstract................................................................................................................................... 4
III Introduction................................................................................................................5
IV Theory........................................................................................................................6
1 Principles of Hamming code..................................................................................6
1.1 Parity bits...................................................................................................... 6
1.2 Even or odd parity and parity check for error detection............................... 6
1.3 Position of parity bits....................................................................................6
1.4 Error correction.............................................................................................7
2 Encoding process................................................................................................... 7
3 Decoding and error correction............................................................................... 9
3.1 Reception...................................................................................................... 9
3.2 Syndrome Calculation...................................................................................9
3.3 Syndrome Bits...............................................................................................9
3.4 Error Location...............................................................................................9
3.5 Error Correction............................................................................................9
4 Error Detection and Correction Capabilities....................................................... 10
4.1 Error Detection........................................................................................... 10
4.2 Error Correction..........................................................................................10
4.3 Single-Bit Error Correction.........................................................................11
4.4 Limitations.................................................................................................. 11
5 Hamming distance and properties........................................................................11
5.1 Definition 1................................................................................................. 11
5.2 Definition 2................................................................................................. 11
5.3 Definition 3................................................................................................. 12
5.4 Properties.................................................................................................... 12
V Applications.............................................................................................................. 13
VI Discuss & Extensions.............................................................................................. 14
1 Discuss................................................................................................................. 14
2 Extensions............................................................................................................14
2.1 Extended Hamming Code (SEC/DED).......................................................14
2.2 Triple-Error-Correcting Hamming Code (TECH)...................................... 14
2.3 Quadruple-Error-Correcting Hamming Code (QECH).............................. 14
2.4 Hamming Codes for Non-Binary Alphabets.............................................. 15
VII Matlab Code Implementation & Explanation........................................................ 16
1 Code implementation...........................................................................................16
2 Code Explanation.................................................................................................17
3 Result................................................................................................................... 19
VIII Conclusion............................................................................................................ 19

Big Assignment for Linear Algebra Page 2/20


I. Acknowledgement
First of all, we would like to extend our sincere gratitude to Ho Chi Minh City
University of Technology – VNU-HCM for incorporating Linear Algebra into the
curriculum. In particular, we would like to express our heartfelt thanks to the lecturers
of the department, who have delicately taught and imparted valuable knowledge in
recent days. Throughout our time in the Linear Algebra class, we have observed
growth in critical thinking and have become more diligent and effective in our studies.
This knowledge will undoubtedly be an essential foundation for us in the future.
Linear Algebra is an immensely beneficial subject, highly practical and well-aligned
with students' needs for real-world application. However, due to our limited
knowledge and initial unfamiliarity, despite our best efforts, it is inevitable that this
project may contain certain shortcomings and inaccuracies. We respectfully ask for
your review and feedback to help us improve and complete our project.

Big Assignment for Linear Algebra Page 3/20


HO CHI MINH UNIVERSITY OF TECHNOLOGY (HCMUT)

II. Abstract
With the increased data transmission and exchange between multiple network devices
for information purposes, the probability of an error or damage occurring to the original
data bit is highly possible.

The Hamming code is a method of error detection and correction used in digital
communication and data storage systems. It adds extra bits to a block of data to create a
code word that facilitates the identification and correction of errors that may occur during
transmission or storage. Developed by Richard Hamming in the 1950s, these codes are
designed to detect and correct single-bit errors within a code word.

This abstract delves into the core principles of Hamming codes, emphasizing their role
in enhancing data accuracy, detecting errors, and correcting transmission flaws. By adding
redundancy through carefully positioned parity bits, Hamming codes ensure reliability in
various digital applications, from computer memory systems to data transmission,
fortifying the foundation of error-free communication in modern technological landscapes.

Big Assignment for Linear Algebra Page 4/20


HO CHI MINH UNIVERSITY OF TECHNOLOGY (HCMUT)

III. Introduction
Hamming code is an error correction system that can detect and correct errors
when data is stored or transmitted. Richard Hamming developed them in the early
1950s and are particularly useful in systems where data accuracy is crucial, such as in
computer memory systems or data transmission.

The main purpose of Hamming codes is to add extra bits to a block of data to create
a code word that can be easily checked for errors. Hamming code uses the connection
between data and redundant bits and may be applied to data units of any length.

When the data is transmitted, these extra parity bits allow the receiver to identify if
any errors have occurred and, in some cases, correct them. If an error is detected, the
receiver can use the information from the parity bits to determine which bit is incorrect
and fix it.

Big Assignment for Linear Algebra Page 5/20


HO CHI MINH UNIVERSITY OF TECHNOLOGY (HCMUT)

IV. Theory
1 Principles of Hamming code
The basic idea behind Hamming code involves adding redundant bits to the
original data to create a codeword. These redundant bits are carefully chosen to allow
for the detection and correction of errors. The key principle of Hamming code is based
on the use of parity bits and their specific placement within the codeword. Using
multiple parity checks to binary search for the error.

1.1 Parity bits


Is a bit added to a string of binary code, a simple form of error detection. Parity
bits are additional bits added to the original data to create a codeword. The parity bits
are used to check for errors in the transmitted data. A block of data where most of its
bits carry the message while some of them are stored as redundancy. The only purpose
of the parity bit is to make sure the total number of bits is either even or odd.
1.2 Even or odd parity and parity check for error detection
Hamming code can use either even parity or odd parity. Even parity means that the
total number of 1s in the codeword, including the parity bits, is even. Odd parity
means that the total number of 1s is odd. If there is an error in the transmitted data, the
parity checks will reveal the presence of the error. By checking the number of one’s to
see if it’s even or odd and contrast to the piece of information from the sender.
1’s = 8 (even) 1’s = 9 (odd)
0 1 0 1 0 1 0 1
1 1 0 0 1 1 0 1
0 0 1 1 0 0 1 1
0 1 0 1 0 1 0 1
Sender Receiver

⇒ There is an error in the piece of information.

1.3 Position of parity bits


The positions of the parity bits within the codeword are determined by powers of 2.
The first parity bit checks every bit whose binary representation includes a 1 in the
least significant bit (20).

Big Assignment for Linear Algebra Page 6/20


HO CHI MINH UNIVERSITY OF TECHNOLOGY (HCMUT)

The second parity bit checks every bit whose binary representation includes a 1 in
the second least significant bit (21), and so on.

The reason why the parity bits sit in positions that are power of 2 is because each
of those parity bits sits inside one and only one of the parity groups. No matter how
large the information may be, each bit conveniently touches one of the group only.
Therefore, it should be able to correct data of one chunk of bits, keeping it from
interfering with the other chunks.
By examining the block of bits, it is possible to identify the position of the
erroneous bit.
For example:

1 0 1 0
0 1 2 3

0 1 0 1
4 5 6 7

1 0 0 1
8 9 10 11

0 0 1 0
12 13 14 15

1.4 Error correction


Hamming code can correct single-bit errors. By using multiple parity checks to
binary search for the error, Hamming code can pinpoint exactly the location of the
error-bit.
Once the position of the error is identified, the receiver can flip the value of the
parity bit to fix the number of 0’s and 1’s. Hence restore the chunk of information.

2 Encoding process
Encoding process includes 5 steps:

Step 1: Count the number of information bits


For example: 1010
⇒ The number of information bits(D) is 4
⇒ 5D: D1 D2 D3 D4

Step 2: Calculate the number of parity bits


Using the formula: 2p ≥ (p + m)+1 p:

Big Assignment for Linear Algebra Page 7/20


HO CHI MINH UNIVERSITY OF TECHNOLOGY (HCMUT)

number of parity bits m: number of


information bits
"1" stands for the situation when the transmission is correct
For example: m = 4 then p must equal 3 so that the equation is satisfied
Hence for 4 information bits, 3 parity bits are required
⇒ 4c: c1 c2 c3 ( with c is the parity bit)

Step 3: Form of code


3.1 Find the position for each C and D From the number of parity bits:
We have c1c2c3
0 0 0 → transmission is correct
0 0 1→ 1
0 1 0→ 2
0 1 1→ 3 For c3(1,3,5,7)
1 0 0→ 4 c2(2,3,6,7)
1 0 1→ 5 c1(4,5,6,7)
1 1 0→ 6
1 1 1→ 7

3.2 Make a form


Based on the first number of c:
c3 c2 D1 c1 D2 D3 D4
_ _ 1 _ 1 0 1
1 2 3 4 5 6 7
Step 4: Using Even parity (rules for 1)
For c3(1,3,5,7), we consider the position 3,5 and 7, which is 1 1 1, this is an
odd number of 1 ⇒ c3 = 1
For c2(2,3,6,7), we consider the position 3,6 and 7, which is 1 0 1, this is an
even number of 1 ⇒ c2 = 0
For c1(4,5,6,7), we consider the position 5,6 and 7, which is 1 0 1, this is an
even number of 1 ⇒ c1 = 1

Step 5: Finalize the code


Place the calculated parity bits into their respective positions: 1011010

Big Assignment for Linear Algebra Page 8/20


HO CHI MINH UNIVERSITY OF TECHNOLOGY (HCMUT)

3 Decoding and error correction


3.1 Reception
After transmission, the received code word may contain errors due to noise or other
interference.

3.2 Syndrome Calculation


Calculate the syndrome, which is a vector indicating the presence and location of
errors.
The syndrome is computed by checking the parity of specific groups of bits,
including the parity bits in the received code word.

3.3 Syndrome Bits


Each syndrome bit corresponds to a specific set of bits in the received code word.
If the syndrome bit is 0, the corresponding group of bits is error-free. If the syndrome
bit is 1, there is an error in the corresponding group of bits.
3.4 Error Location
The non-zero syndrome bits indicate the positions of errors in the received code
word. The position of the non-zero syndrome bits determines the error location.
3.5 Error Correction
The error is corrected by flipping the bit at the identified error position. The
corrected code word is then used as the decoded output.
For example:
Let’s consider a 7,4 Hamming code where the original data is 4 bits (D1D2D3D4)
and 3 parity bits are added. The parity bits (P1, P2, P3) cover specific combinations of
data bits.

• P1 checks: P1, D1, D2, D4

• P2 checks: P2, D1, D3, D4

• P3 checks: P3, D2, D3, D4

If the received codeword is 1101011, we can calculate the syndromes:

• Syndrome(P1) = (1+1+1+1)%2 = 0 (no error)

Big Assignment for Linear Algebra Page 9/20


HO CHI MINH UNIVERSITY OF TECHNOLOGY (HCMUT)

• Syndrome(P2) = (1+1+0+1)%2 = 1 (error in position 2)

• Syndrome(P3) = (1+0+0+1)%2 = 0 (no error)

The non-zero syndrome in position 2 indicates an error. Flipping bit 2 corrects the
error, resulting in the original data: 1111011.
It is important to note that Hamming code can correct only one error at a time. If
more than one error occurs, Hamming code can still detect the errors but cannot
correct them.

4 Error Detection and Correction Capabilities


4.1 Error Detection
The parity bits in Hamming codes are used for error detection. Each parity bit
covers a specific set of positions in the code word.
When the received code word is checked, the parity bits are recalculated based on
the received data.
If the calculated parity bits do not match the received parity bits, an error is
detected. The position of the erroneous bit can be determined by identifying which
parity bits are incorrect.
For example: If the received P1 disagrees with the recalculated P1, there is an error
in one or more of the positions 1, 3, 5, 7. The same principle applies to P2 and P4 for
their respective positions.

4.2 Error Correction


Once an error is detected, the position of the erroneous bit can be identified by
analyzing the parity bits that cover that position.
The bit at the identified position is flipped (changed from 0 to 1 or vice versa) to
correct the error.
The receiver can then use the corrected data for further processing.

For example: If P1, P2, and P4 indicate errors, the position of the error can be
determined by finding the intersection of the sets of positions covered by these parity
bits. If, for instance, it is determined that the error is in position 5, then the bit at
position 5 is flipped to correct the error.

Big Assignment for Linear Algebra Page 10/20


HO CHI MINH UNIVERSITY OF TECHNOLOGY (HCMUT)

4.3 Single-Bit Error Correction


Hamming codes are specifically designed to correct single-bit errors. If there is
only one error in the received code word, the parity bits will accurately identify the
position of the error, allowing for correction.

4.4 Limitations
While Hamming codes are effective for correcting single-bit errors, they have
limitations. They can detect and correct only one error per code word. If there are
multiple errors, or if errors occur in both the data and parity bits, Hamming codes may
not be able to correct the data accurately.
In summary, Hamming codes use carefully positioned parity bits to both detect and
correct errors in transmitted data. They are particularly useful in scenarios where the
emphasis is on correcting occasional single-bit errors, as is common in digital
communication and data storage system.
5 Hamming distance and properties
5.1 Definition 1
The Hamming distance between two strings is the number of positions at which the
corresponding bits are different at equal length. In simpler terms, it’s the number of
substitutions needed to change one string into the other. Typically, hamming distance
is denoted by the function d(x, y) where x and y are codewords.

For example: There are 2 strings x and y:


x = 101101 y
= 111011
In this case, the different positions are the second, fourth, and fifth.

Therefore, the Hamming distance between A and B is d(x,y) = 3.

5.2 Definition 2
The minimum Hamming distance is the smallest Hamming distance between all
possible pairs of strings in a set of strings of equal lengths.

For example: There are 3 strings :A = 101,B = 110,C = 010


d(A,B) = 2 d(A,C) = 2 d(B,C) = 1
→ The minimum Hamming distance is dmin = 1

Big Assignment for Linear Algebra Page 11/20


HO CHI MINH UNIVERSITY OF TECHNOLOGY (HCMUT)

5.3 Definition 3
The Hamming weight of a string is the number of symbols that are different from
the zero symbols of the alphabet used. It is thus equivalent to the Hamming distance
from the all-zero string of the same length.

For example: Find the weight of this u = 10110101 So


w(u) = 5

5.4 Properties
Non-negativity: The hamming distance between two strings is always greater
than or equal to zero, and it is zero if and only if the strings are identical.

Symmetry: The hamming distance between two strings is the same regardless
of the order in which they are compared.
Triangle inequality: The hamming distance between two strings is never greater
than the sum of the hamming distances between them and a third string.

Metric: The hamming distance satisfies the four conditions of a metric, which
means it can be used to measure the distance or similarity between strings in a
mathematical way.

Code distance: The hamming distance can be used to define the distance of a code,
which is the minimum hamming distance between any two distinct codewords. This
determines the error detection and correction capabilities of the code.

Big Assignment for Linear Algebra Page 12/20


HO CHI MINH UNIVERSITY OF TECHNOLOGY (HCMUT)

V. Applications
Common applications of using Hamming code are Satellites, Modems, or Wireless
communication.
Basically, Hamming code applies to circumstances where a piece of information is
transferred between sender, receiver and during the transmission there are potential
noises that cause the error in the chunks of bits.
For example:
Suppose you are sending data over a wireless channel, such as Wi-Fi, and you want
to ensure that the received data is not corrupted during transmission. One method of
ensuring data integrity is to use Hamming Code to detect and correct bit errors during
transmission.

Another application of Hamming Code is in computer memory, where data is


stored as bits. When the computer reads data from memory, bit errors can occur due to
electromagnetic interference, also known as soft errors. Hamming Code helps detect
and correct these bit errors, ensuring that the data can be read correctly.Transferring
data from memory to CPU: Hamming Code can be used to protect the data being
transferred from memory to CPU. If there is a slight error during the transfer,
Hamming Code will detect and correct the error before processing the data.

Big Assignment for Linear Algebra Page 13/20


HO CHI MINH UNIVERSITY OF TECHNOLOGY (HCMUT)

VI. Discuss & Extensions


1 Discuss
Hamming code is a method used to check and correct errors that exist during
communication. Thanks to this code, data reliability in wireless applications is
achieved.
Operating principle: As stated above, the code simply adds check bits, and thereby
creates a code string that can do two jobs: checking and correcting errors during
transmission.
Thanks to the hamming code, the integrity and accuracy level have been
significantly improved. And thanks to that, it is widely used, such as communications,
transceivers and even storage.
But because many strings must be created to test, memory or bandwidth problems
must also be improved. In addition, the code cannot check for all errors, so it is
ineffective where there are many errors, and the code can only check for simple errors.

2 Extensions
2.1 Extended Hamming Code (SEC/DED)
Purpose: This extension is designed to detect and correct single errors and
detect double errors (SEC/DED).

Mechanism: It adds extra parity bits to the original Hamming code, enabling the
detection and correction of additional errors.

2.2 Triple-Error-Correcting Hamming Code (TECH)


Purpose: This further extension aims to correct three-bit errors.

Mechanism: It increases the number of parity bits to provide the ability to correct
up to three errors.

2.3 Quadruple-Error-Correcting Hamming Code (QECH)


Purpose: This extension is designed to correct up to four-bit errors.

Big Assignment for Linear Algebra Page 14/20


HO CHI MINH UNIVERSITY OF TECHNOLOGY (HCMUT)

Mechanism: It continues the trend of adding more parity bits to enhance error
correction capabilities.
2.4 Hamming Codes for Non-Binary Alphabets
Purpose: Hamming codes can be extended to work with non-binary alphabets
(symbols with more than two values).

Mechanism: The basic principles of Hamming codes can be adapted to


accommodate symbols with more than two values.

Big Assignment for Linear Algebra Page 15/20


HO CHI MINH UNIVERSITY OF TECHNOLOGY (HCMUT)

VII. Matlab Code Implementation & Explanation

1. Code implementation

data_size = input('Enter number of data bits (data size): ');


data_bits = input('Enter vector data (data bits): ');

parity_bits = 0;
while 2^parity_bits < data_size + parity_bits + 1
parity_bits = parity_bits + 1;
end

total_bits = data_size + parity_bits;

% Create test matrix H


H = flipud(de2bi(1:total_bits, parity_bits, 'left-msb')');
disp('Test Matrix H:');
disp(H);

% Create matrix P
PT = H(:, [3, 5, 6, 7]);

% Select data columns


P = PT';
disp('Parity matrix P:');
disp(PT);

% Create unit matrix


I_k = eye(data_size);

% Initialize matrix G
G = zeros(total_bits, data_size);

% Assign rows of G
G([1, 2, 4], :) = P'; %Rows 1, 2, 4 of G are rows 1, 2, 3 of P
G([3, 5, 6, 7], :) = I_k; % Rows 3, 5, 6, 7 of G are the identity matrix.

Big Assignment for Linear Algebra Page 16/20


HO CHI MINH UNIVERSITY OF TECHNOLOGY (HCMUT)

% Display G matrix
disp('Generator matrix G:');
disp(G);

% Hamming code calculation


hamming_code = mod(G * data_bits', 2);
hamming_code = hamming_code';
disp('Hamming code:');
disp(hamming_code);

error_position = randi([1, length(hamming_code)]);


received_code = hamming_code;
received_code(error_position) = mod(received_code(error_position) + 1, 2);

disp('Hamming code after adding error:');


disp(received_code);
fprintf('Error generated at location: %d\n', error_position);

syndrome = H * received_code';

syndrome_binary = mod(syndrome, 2);

syndrome_decimal = 0;
n = length(syndrome_binary);
for i = 1:n
syndrome_decimal = syndrome_decimal + syndrome_binary(i) * 2^(n-i);
end

% Syndrome vector (binary)


disp('Syndrome vector (binary):');
disp(syndrome_binary');

% Invert the binary vector to correct weights (if needed)


syndrome_binary_flipped = flip(syndrome_binary);

% Convert from binary to decimal


syndrome_decimal = bi2de(syndrome_binary_flipped', 'left-msb');

disp('Syndrome vector (decimal):');


Big Assignment for Linear Algebra Page 17/20
HO CHI MINH UNIVERSITY OF TECHNOLOGY (HCMUT)

2 disp(syndrome_decimal);
2. Code Explanation

3. Result

Figure 1: Result of implementation

VIII. Conclusion
Hamming code has emerged as a cornerstone of reliable communication in the
digital age. Its ability to detect and correct single-bit errors has made it a crucial
component in various applications, from computer memory and satellites to modems
and embedded systems.Hamming code offers a powerful solution for data integrity in
the face of errors. While it has limitations, such as increasing data size, can not detect
and correct multiple errors,its simplicity, efficiency, and reliability make it a valuable
tool for ensuring reliable communication in numerous applications.
IX. References
Big Assignment for Linear Algebra Page 18/20
HO CHI MINH UNIVERSITY OF TECHNOLOGY (HCMUT)

[1] Wright, G. (2022, July). What is Hamming code and how does it work?
WhatIs.com.

[2] Kapoor, A. (2023, October 30). What Is Hamming Code? The Network Technique
to Detect Errors and Correct Data|Simplilearn. Simplilearn.com.

[3] Raut, N. (2023, November 1). What is Hamming Distance?


Www.tutorialspoint.com.

[4] 3Blue1Brown(2020, September 4)

[5] Williams (2023, November 18)

[6] A. L. Garcia and C. Penland, MATLAB Projects for Scientists and


Engineers, Prentice Hall, Upper Saddle River, NJ, 1996.

Big Assignment for Linear Algebra Page 19/20

You might also like