]> Creatis software - CreaPhase.git/blob - octave_packages/communications-1.1.1/riceenco.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / communications-1.1.1 / riceenco.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} {} riceenco (@var{sig}, @var{K})
18 ##
19 ## Returns the Rice encoded signal using @var{K} or optimal K . 
20 ## Default optimal K is chosen between 0-7. Currently no other way
21 ## to increase the range except to specify explicitly. Also returns
22 ## @var{K} parameter used (in case it were to be chosen optimally) 
23 ## and @var{Ltot} the total length of output code in bits.
24 ## This function uses a @var{K} if supplied or by default chooses
25 ## the optimal K for encoding signal vector into a rice coded vector.
26 ## A restrictions is that a signal set must strictly be non-negative.
27 ## The Rice algorithm is used to encode the data into unary coded
28 ## quotient part which is represented as a set of 1's separated from
29 ## the K-part (binary) using a zero. This scheme doesnt need any 
30 ## kind of dictionaries and its close to O(N), but this implementation
31 ## *may be* sluggish, though correct.
32 ##
33 ## Reference: Solomon Golomb, Run length Encodings, 1966 IEEE Trans
34 ## Info' Theory
35 ##
36 ## An exmaple of the use of @code{riceenco} is
37 ## @example
38 ## @group
39 ##   riceenco(1:4) #  
40 ##   riceenco(1:10,2) # 
41 ## @end group
42 ## @end example
43 ## @end deftypefn
44 ## @seealso{ricedeco}
45
46 function [rcode,K,Ltot]=riceenco(sig,K)
47   if ( nargin < 1 )
48     error('usage: riceenco(sig,{K})');
49   elseif (nargin < 2)
50     use_optimal_k=1;
51   else
52     use_optimal_k=0;
53   end
54
55   if (min(sig) < 0)
56     error("signal has elements that are outside alphabet set ...
57         . Accepts only non-negative numbers. Cannot encode.");
58   end
59
60     
61   L=length(sig);
62
63   ##compute the optimal rice parameter.
64   if(use_optimal_k)
65     k_opt=0;
66     len_past=sum(sig)+L+k_opt*L;
67     quot=sig;
68     
69     for k=1:7
70       k_pow_2=2**k;
71       quot_k=floor(sig./k_pow_2);
72       len=sum(quot_k)+L+k*L;
73       if(len < len_past)
74         len_past=len;
75         k_opt=k;
76         rem=mod(sig,k_pow_2);
77         quot=quot_k;
78       end
79     end
80     Ltot=len_past;
81     K=k_opt;
82     K_pow_2=2**K;
83   else
84     K_pow_2=2**K;
85     quot=floor(sig./K_pow_2);
86     rem=mod(sig,K_pow_2);
87   end
88
89   for j=1:L
90     rice_part=zeros(1,K);
91     %
92     % How can we eliminate this loop?
93     % I essentially need to get the binary
94     % representation of rem(j) in the rice_part(i)
95     %
96     for i=K:-1:1
97       rice_part(i)=mod(rem(j),2);
98       rem(j)=floor(rem(j)/2);
99     end
100     rcode{j}=[ones(1,quot(j)) 0 rice_part];
101   end
102   Ltot=sum(quot)+L+K*L;
103   
104   return
105 end
106 %! 
107 %! assert(riceenco(1:4,2),{[0 0 1],[0 1 0], [0 1 1], [ 1 0 0 0]})
108 %!