-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgolomb.m
More file actions
executable file
·64 lines (61 loc) · 2.8 KB
/
golomb.m
File metadata and controls
executable file
·64 lines (61 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
function [golombCode, length]=golomb(m,n)
%% Variables
unarycode = string;
fixed_lengthCode = string;
golombCode =string;
%% Quotient and Remainder Calculation
quotient = floor(n/m);
remainder = n - quotient*m;
bits_counter = 0;
%% GroupID
while quotient>0
unarycode = unarycode + '1';
quotient = quotient-1;
bits_counter=bits_counter+1;
end
unarycode = unarycode+'0';
bits_counter=bits_counter+1;
%% Fixed-length Code calculation
% Created a string matrix for all the possible remainders
% Column 1: Remainder
% Column 2: Remainder in the respective bits
% Column 3: Length
possible_remainders = strings(m,3);
lower_remainder_bits = floor(log2(m));
higher_remainder_bits = ceil(log2(m));
for i=1:1:m
possible_remainders(i,1)=num2str(i-1); % Fill column 1 with the remainder
% Column 2 is the representation of the remainder
if i<=((2^higher_remainder_bits)-m) %The first x remainders are represented with lower_remainder_bits
possible_remainders(i,2)=possible_remainders(i,2) + dec2bin(str2num(possible_remainders(i,1)),lower_remainder_bits);
possible_remainders(i,3)=lower_remainder_bits;
elseif i>((2^higher_remainder_bits)-m) %The Remaining m-x remainders are represented with higher_remainder_bits
value_to_encode = (i-1) + (2^higher_remainder_bits)-m;
possible_remainders(i,2)=possible_remainders(i,2) + dec2bin(value_to_encode,higher_remainder_bits);
possible_remainders(i,3)=higher_remainder_bits;
end
end
%% Need to figure out a way to check if m is a power of 2: m = 2^K, K = 0,1,2... integer - this step was taking too long for execution with convergence, so hardcoded it.
if m==2
% remainder is represented with log2(2) bits; 1 bit
fixed_lengthCode = fixed_lengthCode + dec2bin(remainder,log2(m));
bits_counter = bits_counter + log2(m);
elseif m==3
% Find r from possible_remainders(:,1) and the corresponding
% possible_remainders(:,2) is the remainder value
fixed_lengthCode = fixed_lengthCode + possible_remainders(remainder+1,2);
bits_counter = bits_counter +str2num(possible_remainders(remainder+1,3));
elseif m==4
% remainder is represented with log2(4) bits; 2 bits
fixed_lengthCode = fixed_lengthCode + dec2bin(remainder,log2(m));
bits_counter = bits_counter + log2(m);
elseif m==5
% Find r from possible_remainders(:,1) and the corresponding
% possible_remainders(:,2) is the remainder value
fixed_lengthCode = fixed_lengthCode + possible_remainders(remainder+1,2);
bits_counter = bits_counter + str2num(possible_remainders(remainder+1,3));
end
%% Combine the UnaryCode and Fixed-length code for the given positive integer and m
golombCode = unarycode + fixed_lengthCode;
length= bits_counter;
end