0% found this document useful (0 votes)
43 views2 pages

Hamming Code Implementation in Python

This Python program demonstrates the hamming code algorithm for error detection and correction in data transmission. It calculates the number of redundant bits needed based on the length of the data, inserts the redundant bits in specific positions, calculates the parity bits, and detects and locates any errors by comparing the recalculated parity bits to the received bits. The program encodes sample data, simulates an error, detects the error, and correctly identifies the position of the error.

Uploaded by

PAURAVI BADIWALE
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)
43 views2 pages

Hamming Code Implementation in Python

This Python program demonstrates the hamming code algorithm for error detection and correction in data transmission. It calculates the number of redundant bits needed based on the length of the data, inserts the redundant bits in specific positions, calculates the parity bits, and detects and locates any errors by comparing the recalculated parity bits to the received bits. The program encodes sample data, simulates an error, detects the error, and correctly identifies the position of the error.

Uploaded by

PAURAVI BADIWALE
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

# Python program to demonstrate

# hamming code
def calcRedundantBits(m):

# Use the formula 2 ^ r >= m + r + 1


# to calculate the no of redundant bits.
# Iterate over 0 .. m and return the value
# that satisfies the equation

for i in range(m):
if(2**i >= m + i + 1):
return i

def posRedundantBits(data, r):

# Redundancy bits are placed at the positions


# which correspond to the power of 2.
j=0
k=1
m = len(data)
res = ''

# If position is power of 2 then insert '0'


# Else append the data
for i in range(1, m + r+1):
if(i == 2**j):
res = res + '0'
j += 1
else:
res = res + data[-1 * k]
k += 1

# The result is reversed since positions are


# counted backwards. (m + r+1 ... 1)
return res[::-1]

def calcParityBits(arr, r):


n = len(arr)

# For finding rth parity bit, iterate over


# 0 to r - 1
for i in range(r):
val = 0
for j in range(1, n + 1):
# If position has 1 in ith significant
# position then Bitwise OR the array value
# to find parity bit value.
if(j & (2**i) == (2**i)):
val = val ^ int(arr[-1 * j])
# -1 * j is given since array is reversed

# String Concatenation
# (0 to n - 2^r) + parity bit + (n - 2^r + 1 to n)
arr = arr[:n-(2**i)] + str(val) + arr[n-(2**i)+1:]
return arr
def detectError(arr, nr):
n = len(arr)
res = 0

# Calculate parity bits again


for i in range(nr):
val = 0
for j in range(1, n + 1):
if(j & (2**i) == (2**i)):
val = val ^ int(arr[-1 * j])
# Create a binary no by appending
# parity bits together.
res = res + val*(10**i)
# Convert binary to decimal
return int(str(res), 2)

# Enter the data to be transmitted


data = '1011001'

# Calculate the no of Redundant Bits Required


m = len(data)
r = calcRedundantBits(m)

# Determine the positions of Redundant Bits


arr = posRedundantBits(data, r)

# Determine the parity bits


arr = calcParityBits(arr, r)

# Data to be transferred
print("Data transferred is " + arr)

# Stimulate error in transmission by changing


# a bit value.
# 10101001110 -> 11101001110, error in 10th position.

arr = '11101001110'
print("Error Data is " + arr)
correction = detectError(arr, r)
if(correction==0):
print("There is no error in the received message.")
else:
print("The position of error is ",len(arr)-correction+1,"from the left")

print()

OUTPUT:

Common questions

Powered by AI

The reversal of the data and redundant bits in the Hamming Code implementation is necessary to facilitate correct alignment during the calculation of parity bits. Since positions are counted backwards from the end, reversing ensures that the bits are logically aligned with the expected computation sequence, allowing for consistent parity bit calculations based on position significance .

Converting binary error positions to decimal is significant in Hamming Code error correction because it provides a human-readable form of the position where an error occurred. This conversion aids in clarity and ease of debugging during transmission checks. By translating the binary error location to a base-10 number, corrections can be accurately communicated and applied .

Bitwise operations contribute significantly to the effectiveness of Hamming Code by providing an efficient mechanism to compute and compare parity bits. The bitwise AND (&) and XOR (^) operations enable the checking of relevant positions for each parity bit by ensuring only those bits of interest are considered during the parity computation. This leads to quick identification of discrepancies, thus pinpointing the exact error location .

The Hamming Code algorithm simulates an error in the transmission by deliberately altering a bit in the data sequence, as shown when the data transfers from '10101001110' to '11101001110'. This serves the purpose of validating the effectiveness of the error detection and correction logic within the Hamming Code, allowing developers to ensure that the algorithm can correctly identify and locate bit errors for a real-time error correction .

The error position is calculated by recomputing the parity bits using the received message and comparing it to the expected parity positions. Each parity bit is checked using bitwise operations over the sequence, and any discrepancies form a binary number. This binary number directly indicates the position of the error in the transmitted message after converting it from binary to decimal, allowing the precise correction of the single-bit error .

Challenges during the insertion of redundant bits include accurately determining their positions and ensuring no data bit is overwritten. During parity computation, aligning and correctly interpreting bit significance can be difficult, especially in larger sequences. These can be addressed by strictly adhering to bitwise operation logic and maintaining detailed logs of bit positions, improving accuracy, and methodically verifying each computation step .

The Python code optimizes error detection by recalculating the parity bits after transmission and comparing them with expected parity positions. By using bitwise operations to optimize check-sums for each position where a parity bit should correspond, the algorithm identifies discrepancies efficiently. This methodical check allows the identification of bit errors and calculates their position in binary, which is then converted to decimal to find where the error occurred .

In the given Python Hamming Code implementation, redundant bits are inserted into the data sequence by first identifying positions that are powers of two. These positions are reserved for redundant bits and initialized with '0'. The data bits are then intermixed with these redundant bits. This setup allows the algorithm to use these positions for parity checks, enabling error detection .

In the Hamming Code, parity bits play a crucial role in error detection by ensuring that the total number of bits with value '1' is even or odd, depending on the parity setting. They are computed by iterating through each bit's position and applying a bitwise OR operation across positions with a specific significance. The parity bit for each power-of-two position is determined through this process, providing a way to check for errors during data verification .

The calculation of redundant bits in Hamming Code ensures data integrity by positioning the redundancy bits at every power of two within the data stream. This arrangement allows the use of parity checks, which can identify an error if there is a mismatch in parity calculations during verification. These bits make it possible to calculate the parity for different combinations of data positions, enabling error detection and correction through the Hamming algorithm .

You might also like