Swapnil. S. Desai Student ID: 1001418334 Email: Assignment 2: Golomb Coding
Swapnil. S. Desai Student ID: 1001418334 Email: Assignment 2: Golomb Coding
Desai
Student ID: 1001418334
Email: swapnilsanjay.desai@mavs.uta.edu
Assignment 2: Golomb Coding
Question: Write a program to encode the following sequence of 10 values using the
Golomb code with parameter m=5
32, 33, 35, 38, 39,50,58,60, 67, 80,
For prediction use the previous value in the sequence
y i yi 1 and assume a
prediction of zero for the first element of the sequence. Print out the coded sequence.
Solution: Please refer the below code for solution and results:
MATLAB Code:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%
% Name: Swapnil Desai
% Student ID: 1001418334
% Email: swapnilsanjay.desai@mavs.uta.edu
% Golomb code
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%
% Clear command window, workspace and close all figure files
bdclose all;
clear all;
close all;
clc;
% Inputs
m= input('Enter the value of m:');
n= input('Enter the number of values in sequence');
%n= [32, 33, 35, 38, 39,50,58,60, 67, 80];
length= length(n);
%
log(m) to the base 2
Upper= ceil(log2(m));
Lower= floor(log2(m));
range= (2^Upper - m);
% Golomb code for input sequence
for i=1:length
quotient= floor(n(i)/m);
remain= n(i)-quotient*m;
if (remain< Lower)
remain= dec2bin(remain, Lower);
else
remain= dec2bin(remain+3,Upper);
end
if quotient==0
q_code='';
else
q_code='1';
for j=1: quotient-1
ones='1';
zeros=0;
q_code=strcat(ones,q_code);
end
end
q_code=strcat(q_code,'0');
codeword= strcat(q_code,remain);
Input= [num2str(n(i)) '' '----> Codeword ---->'];
disp([Input,codeword]);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Initialising prediction sequence
preseq=zeros(1,length);
for i = 1: length
q(i) = floor(n(i)/m);% for quotient
r(i) = mod(n(i),m); % for remainder
if ( i == 1)
preseq(i) = n(i) - 0;
else
preseq(i) = n(i) - n(i-1);
end
if(q(i)==0)
qseq(1)=0;
qseq(2)=0;
else
for j = 1: q(i)
qseq(j) = 1;
end
qseq(j + 1) = 0;
end
end
for i = 1:length
q(i) = floor(preseq(i) / m);
r(i) = mod(preseq(i), m);
if (q(i) == 0)
qseq(1) = 0;
qseq(2) = 0;
else
for j = 1: q(i)
qseq(j) = 1;
end
qseq(j + 1) = 0;
end
if (mod(m, 2) ~= 0)
if ((r(i) >= 0) && (r(i)<range))
t = Lower;
Results:
Input values:
Output sequence:
Enter the value of m:5
Enter the number of values in sequence[32 33 35 38 39 50 58 60 67 80]
32----> Codeword ---->1111110101
33----> Codeword ---->1111110110
35----> Codeword ---->1111111000
38----> Codeword ---->11111110110
39----> Codeword ---->11111110111
50----> Codeword ---->1111111111000
58----> Codeword ---->111111111110110
60----> Codeword ---->111111111111000
67----> Codeword ---->11111111111110101
80----> Codeword ---->1111111111111111000
32
[1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 0]'00'
1
[0 0 1 1 1 1 0 1 1 1 1 1 1 1 1 1 0]'00'
2
[0 0 1 1 1 1 0 1 1 1 1 1 1 1 1 1 0]'00'
3
[0 0 1 1 1 1 0 1 1 1 1 1 1 1 1 1 0]'00'
1
[0 0 1 1 1 1 0 1 1 1 1 1 1 1 1 1 0]'00'
11
[1 1 0 1 1 1 0 1 1 1 1 1 1 1 1 1 0]'00'
8
[1 0 0 1 1 1 0 1 1 1 1 1 1 1 1 1 0]'00'
2
[0 0 0 1 1 1 0 1 1 1 1 1 1 1 1 1 0]'00'
7
[1 0 0 1 1 1 0 1 1 1 1 1 1 1 1 1 0]'00'
13
[1 1 0 1 1 1 0 1 1 1 1 1 1 1 1 1 0]'00'
>>
MATLAB snap: