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

DCshortCodes - Ipynb - Colab

Codes

Uploaded by

yelogo7571
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)
21 views3 pages

DCshortCodes - Ipynb - Colab

Codes

Uploaded by

yelogo7571
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

#5 entropy

from math import log

while True:
num = int(input('\nEnter the number of probabilities: '))
Probabilities = [float(input(f'Enter Probability {i+1}: ')) for i in range(num)]

total = sum(Probabilities)
if total == 1:
H = sum(p * log(1 / p, 2) for p in Probabilities)
print('\nEntropy = %.3f' % H)
break
else:
print('\nThe sum of the Probabilities is %.3f, which is not equal to 1. Retry!' % total)

Enter the number of probabilities: 3


Enter Probability 1: 0.4
Enter Probability 2: 0.4
Enter Probability 3: 0.4

The sum of the Probabilities is 1.200, which is not equal to 1. Retry!

Enter the number of probabilities: 3


Enter Probability 1: 0.4
Enter Probability 2: 0.4
Enter Probability 3: 0.2

Entropy = 1.522

#6 shanon fano

import math
n = int(input("Enter number of probabilities: "))
# Input probabilities
probabilities = [float(input(f"Enter probability {i+1}: ")) for i in range(n)]
sorted_prob = sorted(probabilities, reverse=True)

# Input code lengths


code_lengths = [float(input(f"Enter code length {i+1}: ")) for i in range(n)]

# Calculate entropy
entropy = 0 # Initialize entropy
for p in sorted_prob:
entropy -= p * math.log2(p)
# Calculate average code length
avg_code_length = sum(sorted_prob[i] * code_lengths[i] for i in range(n))

# Calculate efficiency
efficiency = (entropy / avg_code_length) * 100

# Display results
print(f"\nEntropy: {entropy:.2f}")
print(f"Average Code Length: {avg_code_length:.2f}")
print(f"Efficiency: {efficiency:.2f}%")

Enter number of probabilities: 5


Enter probability 1: 0.5
Enter probability 2: 0.25
Enter probability 3: 0.125
Enter probability 4: 0.075
Enter probability 5: 0.05
Enter code length 1: 1
Enter code length 2: 2
Enter code length 3: 3
Enter code length 4: 4
Enter code length 5: 4

Entropy: 1.87
Average Code Length: 1.88
Efficiency: 99.81%

#7 LBC

# Define a basic Generator Matrix for (7,4) linear block code


G = [
[1, 0, 0, 0, 1, 1, 1],
[0, 1, 0, 0, 1, 0, 0],
[0, 0, 1, 0, 1, 1, 0],
[0, 0, 0, 1, 1, 0, 1]
]
# All possible 4-bit data combinations
data_bits = [
[0, 0, 0, 0],
[0, 0, 0, 1],
[0, 0, 1, 0],
[0, 0, 1, 1],
[0, 1, 0, 0],
[0, 1, 0, 1],
[0, 1, 1, 0],
[0, 1, 1, 1],
[1, 0, 0, 0],
[1, 0, 0, 1],
[1, 0, 1, 0],
[1, 0, 1, 1],
[1, 1, 0, 0],
[1, 1, 0, 1],
[1, 1, 1, 0],
[1, 1, 1, 1]
]

# Function to generate codeword


def generate_codeword(data, G):
# Calculate the codeword by summing up each row of G for data bit = 1
codeword = [0] * 7 # Initialize 7-bit codeword
for i in range(4): # For each bit in data
if data[i] == 1: # Add the corresponding row in G if data bit is 1
for j in range(7):
codeword[j] = (codeword[j] + G[i][j]) % 2
return codeword

# Generate codewords for all data bits


print("Data Bits | Parity Bits | Codeword")
for data in data_bits:
codeword = generate_codeword(data, G)
parity_bits = codeword[4:] # Last 3 bits are parity bits
print(f"{data} | {parity_bits} | {codeword}")

Data Bits | Parity Bits | Codeword


[0, 0, 0, 0] | [0, 0, 0] | [0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 1] | [1, 0, 1] | [0, 0, 0, 1, 1, 0, 1]
[0, 0, 1, 0] | [1, 1, 0] | [0, 0, 1, 0, 1, 1, 0]
[0, 0, 1, 1] | [0, 1, 1] | [0, 0, 1, 1, 0, 1, 1]
[0, 1, 0, 0] | [1, 0, 0] | [0, 1, 0, 0, 1, 0, 0]
[0, 1, 0, 1] | [0, 0, 1] | [0, 1, 0, 1, 0, 0, 1]
[0, 1, 1, 0] | [0, 1, 0] | [0, 1, 1, 0, 0, 1, 0]
[0, 1, 1, 1] | [1, 1, 1] | [0, 1, 1, 1, 1, 1, 1]
[1, 0, 0, 0] | [1, 1, 1] | [1, 0, 0, 0, 1, 1, 1]
[1, 0, 0, 1] | [0, 1, 0] | [1, 0, 0, 1, 0, 1, 0]
[1, 0, 1, 0] | [0, 0, 1] | [1, 0, 1, 0, 0, 0, 1]
[1, 0, 1, 1] | [1, 0, 0] | [1, 0, 1, 1, 1, 0, 0]
[1, 1, 0, 0] | [0, 1, 1] | [1, 1, 0, 0, 0, 1, 1]
[1, 1, 0, 1] | [1, 1, 0] | [1, 1, 0, 1, 1, 1, 0]
[1, 1, 1, 0] | [1, 0, 1] | [1, 1, 1, 0, 1, 0, 1]
[1, 1, 1, 1] | [0, 0, 0] | [1, 1, 1, 1, 0, 0, 0]

#8 Cyclic Non Systematic

import numpy as np
generator= [Link]([int(bit) for bit in input("Enter a generator bit:")])
msg=[Link]([int(bit) for bit in input("Enter a message bit:")])
print("Generator:",generator)
print("Message:",msg)
result= [Link](generator, msg, mode='full')%2
print("Codeword:",result)

Enter a generator bit:1011


Enter a message bit:1001
Generator: [1 0 1 1]
Message: [1 0 0 1]
Codeword: [1 0 1 0 0 1 1]

#9 Convolution

import numpy as np

# Generator vectors
g1 = [Link]([1, 0, 1])
g2 = [Link]([1, 1, 1])
# Message
m = [Link]([1, 0, 1, 0, 1])

# C l ti d d l 2
# Convolution and modulo 2
v1 = [Link](g1, m) % 2
v2 = [Link](g2, m) % 2

# Print outputs
print("OUTPUT of PATH1: {}".format(v1))
print("OUTPUT of PATH2: {}".format(v2))

# Combine outputs into a single array


output = [Link]((v1, v2)).[Link]()

print("CONVOLUTION ENCODER OUTPUT: {}".format(output))

OUTPUT of PATH1: [1 0 0 0 0 0 1]
OUTPUT of PATH2: [1 1 0 1 0 1 1]
CONVOLUTION ENCODER OUTPUT: [1 1 0 1 0 0 0 1 0 0 0 1 1 1]

You might also like