1 ## Copyright (C) 2006 Muthiah Annamalai <muthiah.annamalai@uta.edu>
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
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
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/>.
17 ## @deftypefn {Function File} {} riceenco (@var{sig}, @var{K})
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.
33 ## Reference: Solomon Golomb, Run length Encodings, 1966 IEEE Trans
36 ## An exmaple of the use of @code{riceenco} is
46 function [rcode,K,Ltot]=riceenco(sig,K)
48 error('usage: riceenco(sig,{K})');
56 error("signal has elements that are outside alphabet set ...
57 . Accepts only non-negative numbers. Cannot encode.");
63 ##compute the optimal rice parameter.
66 len_past=sum(sig)+L+k_opt*L;
71 quot_k=floor(sig./k_pow_2);
72 len=sum(quot_k)+L+k*L;
85 quot=floor(sig./K_pow_2);
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)
97 rice_part(i)=mod(rem(j),2);
98 rem(j)=floor(rem(j)/2);
100 rcode{j}=[ones(1,quot(j)) 0 rice_part];
102 Ltot=sum(quot)+L+K*L;
107 %! assert(riceenco(1:4,2),{[0 0 1],[0 1 0], [0 1 1], [ 1 0 0 0]})