]> Creatis software - CreaPhase.git/blob - octave_packages/communications-1.1.1/golombenco.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / communications-1.1.1 / golombenco.m
1 ## Copyright (C) 2006 Muthiah Annamalai <muthiah.annamalai@uta.edu>
2 ##
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
6 ## version.
7 ##
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
11 ## details.
12 ##
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/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Function File} {} golombenco (@var{sig}, @var{m})
18 ##
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).
38 ##
39 ## Reference: 1. Solomon Golomb, Run length Encodings, 1966 IEEE Trans
40 ## Info' Theory. 2. Khalid Sayood, Data Compression, 3rd Edition
41 ##
42 ## An exmaple of the use of @code{golombenco} is
43 ## @example
44 ## @group
45 ##   golombenco(1:4,2) #  
46 ##   golombenco(1:10,2) # 
47 ## @end group
48 ## @end example
49 ## @end deftypefn
50 ## @seealso{golombdeco}
51
52 function [gcode,Ltot]=golombenco(sig,m)
53   if ( nargin < 2 || m<=0)
54     error('usage: golombenco(sig,m); see help');
55   end
56
57   if (min(sig) < 0)
58     error("signal has elements that are outside alphabet set ...
59         . Accepts only non-negative numbers. Cannot encode.");
60   end
61
62   L=length(sig);
63   quot=floor(sig./m);
64   rem=sig-quot.*m;
65
66
67   C=ceil(log2(m));
68   partition_limit=2**C-m;
69   Ltot=0;
70   for j=1:L
71     if( rem(j) <  partition_limit )
72       BITS=C-1;
73     else
74       rem(j)=rem(j)+partition_limit;
75       BITS=C;
76     end
77     Ltot=Ltot+BITS+1;
78     golomb_part=zeros(1,BITS);
79
80     %
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
86     for i=BITS:-1:1
87       golomb_part(i)=mod(rem(j),2);
88       rem(j)=floor(rem(j)/2);
89     end
90
91     %
92     %actual golomb code: sandwich the unary coded quotient,
93     %and the remainder.
94     %
95     gcode{j}=[ones(1,quot(j)) 0  golomb_part];
96   end
97   Ltot=sum(quot)+Ltot;
98  
99   return
100 end
101 %! 
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]})
104 %!