-
Notifications
You must be signed in to change notification settings - Fork 0
/
golomb_enco.m
31 lines (23 loc) · 860 Bytes
/
golomb_enco.m
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
function G_Code = GolombEncoder(n,m)
%G(n,m)
q = floor(n/m); %quotient
r=rem(n,m); %temainder
fl=floor(log2(m));
a=floor(log2(m)+1);
q1=ones(1,q);
Unary_q=[q1 0]; %UnaryCode(q)
if m==1 %if m=1, G(n,m)=UnaryCode(q)
G_Code=Unary_q;
else
if 2^(nextpow2(m))-m==0 %=0 only if m is a power of 2
Binary_r=de2bi(r,log2(m),'left-msb'); %log2(m)-bit binary code of r
else
if r < (2^(a) - m)
Binary_r=de2bi(r,fl,'left-msb'); %b-bit binary representation of r
else
r=r+(2^(a) - m);
Binary_r=de2bi(r,fl+1,'left-msb'); %b+1-bit binary representation of r
end
end
G_Code=[Unary_q Binary_r]; %Output the Golomb Code
end