Professional Documents
Culture Documents
Lab Assessment 02: Name Shambel Gonfa (18bce2429)
Lab Assessment 02: Name Shambel Gonfa (18bce2429)
Lab Assessment 02: Name Shambel Gonfa (18bce2429)
SUBMITTED TO:
PROF. SHASHANK MOULI SATAPATHY
2) Write a python program to show the implementation of Golomb Encoding-decoding
technique.
a) Encode x=25, 37, with b=11 and b=16.
import math
N = int(input("Enter x :"))
M = int (input("Enter b :"))
q = N//M
r=N%M
quo ='0'*q+'1'
b = math.floor(math.log2(M))
k = 2**(b + 1)-M
if r < k:
rem = bin(r)[2:]
l = len(rem)
if l<b:
rem = '0'*(b-l)+rem
else:
rem = bin(r + k)[2:]
l = len(rem)
if l<b + 1:
rem = '0'*(b + 1-l)+rem
golomb_code = quo + rem
print("The golomb code encoding for x = {} and b = {} is {}".
format(N, M, golomb_code))
b) Decode the Golomb encoded sequence 1111111110010001101 with b = 10.
import math
def decode(x):
num=0;
for i in range(len(x)):
num+=(int(x[len(x)-1-i])*(math.pow(2,i)));
return num;