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} {} shannonfanodeco (@var{hcode},@var{dict})
19 ## Returns the original signal that was Shannonfano encoded. The signal
20 ## was encoded using @code{shannonfanoenco}. This function uses
21 ## a dict built from the @code{shannonfanodict} and uses it to decode a signal
22 ## list into a shannonfano list. Restrictions include hcode is expected to be a binary code;
23 ## returned signal set that strictly belongs in the @code{range [1,N]},
24 ## with @code{N=length(dict)}. Also dict can only be from the
25 ## @code{shannonfanodict(...)} routine. Whenever decoding fails,
26 ## those signal values are indicated by -1, and we successively
27 ## try to restart decoding from the next bit that hasnt failed in
28 ## decoding, ad-infinitum.
30 ## An example use of @code{shannonfanodeco} is
33 ## hd=shannonfanodict(1:4,[0.5 0.25 0.15 0.10])
34 ## hcode=shannonfanoenco(1:4,hd) # [ 1 0 1 0 0 0 0 0 1 ]
35 ## shannonfanodeco(hcode,hd) # [1 2 3 4]
40 ## @seealso{shannonfanoenco, shannonfanodict}
42 function sig=shannonfanodeco(hcode,dict)
43 if ( nargin < 2 || strcmp(class(dict),"cell")~=1 )
44 error('usage: shannonfanoenco(sig,dict)');
46 if (max(hcode) > 1 || min(hcode) < 0)
47 error("hcode has elements that are outside alphabet set ...
52 # O(log(N)) algorithms exist, but we need some effort to implement those
53 # Maybe sometime later, it would be a nice 1-day project
54 # Ref: A memory efficient Shannonfano Decoding Algorithm, AINA 2005, IEEE
57 # TODO: Somebody can implement a 'faster' algorithm than O(N^3) at present
58 # Decoding Algorithm O(N+k*log(N)) which is approximately O(N+Nlog(N))
60 # 1. Separate the dictionary by the lengths
61 # and knows symbol correspondence.
63 # 2. Find the symbol in the dict of lengths l,h where
64 # l = smallest cw length ignoring 0 length CW, and
65 # h = largest cw length , with k=h-l+1;
67 # 3. Match the k codewords to the dict using binary
68 # search, and then you can declare decision.
70 # 4. In case of non-decodable words skip the start-bit
71 # used in the previous case, and then restart the same
72 # procedure from the next bit.
79 # Know the ranges of operation
92 # Now do a O(N^4) algorithm
94 itr=0; #offset into the hcode
100 %decode one element (or just try to).
102 %look for word in dictionary.
103 %with flag to indicate found
104 %or not found. Detect end of buffer.
106 if ( (itr+itr_y) > CL )
110 word=hcode(itr+1:itr+itr_y);
117 if(isequal(dict{itr_z},word))
123 end %for_ loop breaker
126 break; %also need to break_ above then.
141 %! assert(shannonfanodeco(shannonfanoenco(1:4, shannonfanodict(1:4,[0.5 0.25 0.15 0.10])), shannonfanodict(1:4,[0.5 0.25 0.15 0.10])), [1:4],0)