1 ## Copyright (C) 2006 Muthiah Annamalai <muthiah.annamalai@uta.edu>
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
17 ## @deftypefn {Function File} {} golombenco (@var{sig}, @var{m})
19 ## Returns the Golomb coded signal as cell array.
20 ## Also total length of output code in bits can be obtained.
21 ## This function uses a @var{m} need to be supplied for encoding signal vector
22 ## into a golomb coded vector. A restrictions is that
23 ## a signal set must strictly be non-negative. Also the parameter @var{m} need to
24 ## be a non-zero number, unless which it makes divide-by-zero errors.
25 ## The Golomb algorithm [1], is used to encode the data into unary coded
26 ## quotient part which is represented as a set of 1's separated from
27 ## the K-part (binary) using a zero. This scheme doesnt need any
28 ## kind of dictionaries, it is a parameterized prefix codes.
29 ## Implementation is close to O(N^2), but this implementation
30 ## *may be* sluggish, though correct. Details of the scheme are, to
31 ## encode the remainder(r of number N) using the floor(log2(m)) bits
32 ## when rem is in range 0:(2^ceil(log2(m)) - N), and encode it as
33 ## r+(2^ceil(log2(m)) - N), using total of 2^ceil(log2(m)) bits
34 ## in other instance it doesnt belong to case 1. Quotient is coded
35 ## simply just using the unary code. Also accroding to [2] Golomb codes
36 ## are optimal for sequences using the bernoulli probability model:
37 ## P(n)=p^n-1.q & p+q=1, and when M=[1/log2(p)], or P=2^(1/M).
39 ## Reference: 1. Solomon Golomb, Run length Encodings, 1966 IEEE Trans
40 ## Info' Theory. 2. Khalid Sayood, Data Compression, 3rd Edition
42 ## An exmaple of the use of @code{golombenco} is
45 ## golombenco(1:4,2) #
46 ## golombenco(1:10,2) #
50 ## @seealso{golombdeco}
52 function [gcode,Ltot]=golombenco(sig,m)
53 if ( nargin < 2 || m<=0)
54 error('usage: golombenco(sig,m); see help');
58 error("signal has elements that are outside alphabet set ...
59 . Accepts only non-negative numbers. Cannot encode.");
68 partition_limit=2**C-m;
71 if( rem(j) < partition_limit )
74 rem(j)=rem(j)+partition_limit;
78 golomb_part=zeros(1,BITS);
81 % How can we eliminate this loop?
82 % I essentially need to get the binary
83 % representation of rem(j) in the golomb_part(i);
84 % -maybe when JWE or someone imports dec2binvec.
85 % This does MSB -> LSB
87 golomb_part(i)=mod(rem(j),2);
88 rem(j)=floor(rem(j)/2);
92 %actual golomb code: sandwich the unary coded quotient,
95 gcode{j}=[ones(1,quot(j)) 0 golomb_part];
102 %! assert(golombenco(3:5,5),{[0 1 1 0],[0 1 1 1],[1 0 0 0 ]})
103 %! assert(golombenco(3:5,3),{[1 0 0] , [1 0 1 0],[1 0 1 1]})