]> Creatis software - CreaPhase.git/blob - octave_packages/communications-1.1.1/golombdeco.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / communications-1.1.1 / golombdeco.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} {} golombdeco (@var{code}, @var{m})
18 ##
19 ## Returns the Golomb decoded signal vector using @var{code} and @var{m}. 
20 ## Compulsory m is need to be specified. A restrictions is that a
21 ## signal set must strictly be non-negative. The value of code
22 ## is a cell array of row-vectors which have the  encoded golomb value
23 ## for a single sample. The Golomb algorithm is,
24 ##  used to encode the 'code' and only that can be meaningfully 
25 ## decoded. @var{code} is assumed to have been of format generated
26 ## by the function @code{golombenco}. Also the parameter @var{m} need to
27 ## be a non-zero number, unless which it makes divide-by-zero errors.
28 ## This function works backward the Golomb algorithm see
29 ## @code{golombenco} for more detials on that.
30 ## Reference: Solomon Golomb, Run length Encodings, 1966 IEEE Trans Info' Theory
31 ##
32 ## An exmaple of the use of @code{golombdeco} is
33 ## @example
34 ## @group
35 ##   golombdeco(golombenco(1:4,2),2)
36 ## @end group
37 ## @end example
38 ## @end deftypefn
39 ## @seealso{golombenco}
40
41 ##! /usr/bin/octave -q
42 #A stress test routine
43 #for i=1:100
44 #  sig=abs(randint(1,10,[0,255]));
45 #  k=mod(i,10)+1;
46 #  code=golombenco(sig,k);
47 #  assert(golombdeco(code,k),sig)
48 #end
49 #
50 #for k=1:10;
51 # assert(golombdeco(golombenco(4:10,k),k),[4:10]);
52 #end
53 #
54 function sig_op=golombdeco(code,m)
55   if ( nargin < 2 ) || (strcmp(class(code),"cell")~=1 || m<=0)
56     error('usage: golombdeco(code,m)');
57   end
58   
59   L=length(code);  
60   C=ceil(log2(m));
61   partition_limit=2**C-m;
62  
63
64   power_seq=[2.^(ceil(log2(m))-1:-1:0)];
65   power_seq_mod=power_seq(2:end);
66   
67   for j=1:L
68     word=code{j};
69     WL=length(word);
70     idx=find(word==0)(1);
71     q=sum(word(1:idx));
72     
73     idx2=(WL-idx);
74     word_tail=word(idx+1:end);
75   
76     if(length(word_tail) == C)
77       r=sum(word_tail.*power_seq);
78       r=r-(partition_limit);
79     else
80       r=sum(word_tail.*power_seq_mod);
81     end
82     
83     quot(j)=q;
84     rem(j)=r;
85   end
86   sig_op=quot.*m + rem;
87
88   return
89 end
90 %! 
91 %! assert(golombdeco(golombenco(1:4,2),2),[1:4])
92 %!