]> Creatis software - CreaPhase.git/blob - octave_packages/communications-1.1.1/shannonfanodict.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / communications-1.1.1 / shannonfanodict.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} {} shannonfanodict (@var{symbols},@var{symbol_probabilites})
18 ##        
19 ## Returns the code dictionary for source using shanno fano algorithm.
20 ## Dictionary is built from @var{symbol_probabilities} using the shannon
21 ## fano scheme.  Output is a dictionary cell-array, which 
22 ## are codewords, and correspond to the order of input probability.
23 ##
24 ## @example
25 ## @group
26 ##          CW=shannonfanodict(1:4,[0.5 0.25 0.15 0.1]);
27 ##          assert(redundancy(CW,[0.5 0.25 0.15 0.1]),0.25841,0.001)
28 ##          shannonfanodict(1:5,[0.35 0.17 0.17 0.16 0.15])
29 ##          shannonfanodict(1:8,[8 7 6 5 5 4 3 2]./40)
30 ## @end group
31 ## @end example
32 ## @end deftypefn
33 ## @seealso{shannonfanoenc, shannonfanodec}
34 ##
35 function cw_list=shannonfanodict(symbol,P)
36
37   if nargin < 2
38     error('Usage: shannonfanodict(symbol,P); see help');
39   end
40
41   DMAX=length(P);
42   S=1:DMAX;
43   
44   if(sum(P)-1.000 > 1e-7)
45     error('Sum of probabilities must be 1.000');
46   end
47 #
48 #FIXME: Is there any other way of doing the
49 #topological sort? Insertion sort is bad.
50 #
51 #Sort the probability list in 
52 #descending/non-increasing order.
53 #Using plain vanilla insertion sort.
54 #
55   for i=1:DMAX
56     for j=i:DMAX
57       
58       if(P(i) < P(j))
59         
60                                 #Swap prob
61         t=P(i);
62         P(i)=P(j);
63         P(j)=t;
64         
65                                 #Swap symb
66         t=S(i);
67         S(i)=S(j);
68         S(j)=t;
69       end
70       
71     end
72   end
73   
74   
75   #Now for each symbol you need to
76   #create code as first [-log p(i)] bits of
77   #cumulative function sum(p(1:i))
78   #
79   #printf("Shannon Codes\n");
80   #data_table=zeros(1,DMAX);
81    cw_list={};
82    
83    for itr=1:DMAX
84      if(P(itr)~=0)
85        digits=ceil(-log2(P(itr))); #somany digits needed.
86      else
87        digits=0; #dont assign digits for zero probability symbols.
88      end
89      
90      Psum = sum([0 P](1:itr)); #Cumulative probability
91      s=[];
92      for i=1:digits;
93        Psum=2*Psum;
94        if(Psum >= 1.00)
95          s=[s 1];
96          Psum=Psum-1.00;
97        else
98          s=[s 0];
99        end       
100      end
101      cw_list{itr}=s;
102    end
103    
104    #re-arrange the list accroding to input prob list.
105    cw_list2={};
106    for i=1:length(cw_list)
107      cw_list2{i}=cw_list{S(i)};
108    end
109    cw_list=cw_list2;
110
111    return;
112 end
113
114 %!shared CW,P
115 %!test
116 %!  P  = [0.5 0.25 0.15 0.1];
117 %!  assert(shannonfanodict(1:4,P),{[0],[1 0],[1 1 0],[1 1 1 0]})
118 %!