[46726a]: inst / unaryenc.m Maximize Restore History

Download this file

unaryenc.m    54 lines (51 with data), 1.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
## Copyright (C) 2006 Muthiah Annamalai <muthiah.annamalai@uta.edu>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; If not, see <http://www.gnu.org/licenses/>.
## -*- texinfo -*-
## @deftypefn {Function File} {} unaryenc (@var{value})
##
## This function encodes the decimal value.
## Useful if you are trying to perform golomb-rice coding
## value needs to be a number or row-vector. @var{value}
## is a non-negative number.
##
## Unary encoding of a +ve number N is done as follows,
## use N-ones followed by one zero. For instance, the
## unary coded value of 5 will be then (111110) in base2
## which is 31x2 = 62. From this definition, decoding follows.
##
## @example
## @group
## message = [5 4 4 1 1 1]
## coded = unaryenc(message)
## @result{} [62 30 30 2 2 2]
## unarydec(coded)
## @result{} [5 4 4 1 1 1]
## @end group
## @end example
## @end deftypefn
## @seealso{unarydec}
##
## Optimal for exponential sequences
## pow(2,-n) kind of sequences this is optimal.
##
function rval=unaryenc(val)
if val==0
rval=val;
return
end
rval=2.^(val)-1; % add somany 1's
rval=rval*2; % append 0
end
%!assert(unaryenc([5 4 4 1 1 1]), [62 30 30 2 2 2],1)