]> Creatis software - CreaPhase.git/blob - octave_packages/communications-1.1.1/ricedeco.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / communications-1.1.1 / ricedeco.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} {} ricedeco (@var{code}, @var{K})
18 ##
19 ## Returns the Rice decoded signal vector using @var{code} and @var{K}. 
20 ## Compulsory K is need to be specified.
21 ## A restrictions is that a signal set must strictly be non-negative.
22 ## The value of code is a cell array of row-vectors which have the 
23 ## encoded rice value for a single sample. The Rice 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{riceenco}.
27 ##
28 ## Reference: Solomon Golomb, Run length Encodings, 1966 IEEE Trans Info' Theory
29 ##
30 ## An exmaple of the use of @code{ricedeco} is
31 ## @example
32 ## @group
33 ##   ricedec(riceenco(1:4,2),2) 
34 ##  
35 ## @end group
36 ## @end example
37 ## @end deftypefn
38 ## @seealso{riceenco}
39
40 ##
41 ##
42 ##! /usr/bin/octave -q
43 ##A stress test routine
44 ##for i=1:100
45 ##  sig=abs(randint(1,10,[0,255]));
46 ##  [code,k]=riceenco(sig)
47 ##  sig_d=ricedeco(code,k)
48 ##  if(isequal(sig_d,sig)~=1)
49 ##    error('Some mistake in ricedeco/enco pair');
50 ##  end
51 ##end
52 ##
53 function sig_op=ricedeco(code,K)
54   if ( nargin < 2 ) || (strcmp(class(code),"cell")~=1)
55     error('usage: ricedeco(code,K)');
56   end
57
58   L=length(code);
59   
60   K_pow_2=2**K;
61
62   if(K ~= 0)
63     power_seq=[2.^((K-1):-1:0)];
64     for j=1:L
65       word=code{j};
66       idx=find(word==0)(1);
67       val=sum(word(1:idx));
68       sig_op(j)=val*K_pow_2 + sum(word(idx+1:end).*power_seq);
69     end
70   else
71     for j=1:L
72       sig_op(j)=sum(code{j});
73     end
74   end
75   
76   return
77 end
78 %! 
79 %! assert(ricedeco(riceenco(1:4,2),2),[1:4])
80 %!