]> Creatis software - CreaPhase.git/blob - octave_packages/communications-1.1.1/shannonfanoenco.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / communications-1.1.1 / shannonfanoenco.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} {} shannonfanoenco (@var{hcode},@var{dict})
18 ## 
19 ## Returns the Shannon Fano encoded signal using @var{dict}.
20 ## This function uses a @var{dict} built from the @code{shannonfanodict}
21 ## and uses it to encode a signal list into a shannon fano code.
22 ## Restrictions include a signal set that strictly belongs  in the
23 ## @code{range [1,N]} with @code{N=length(dict)}. Also dict can only be
24 ## from the @code{shannonfanodict()} routine.
25 ## An example use of @code{shannonfanoenco} is
26 ##
27 ## @example
28 ## @group
29 ##          hd=shannonfanodict(1:4,[0.5 0.25 0.15 0.10])
30 ##          shannonfanoenco(1:4,hd) #  [   0   1   0   1   1   0   1   1   1   0]
31 ## @end group
32 ## @end example
33 ## @end deftypefn
34 ## @seealso{shannonfanodeco, shannonfanodict}
35 ##
36
37 function sf_code=shannonfanoenco(sig,dict)
38   if nargin < 2
39     error('usage: huffmanenco(sig,dict)');
40   end
41   if (max(sig) > length(dict)) || ( min(sig) < 1)
42     error("signal has elements that are outside alphabet set ...
43         Cannot encode.");
44   end
45   sf_code=[dict{sig}];
46   return
47 end
48 %! 
49 %! assert(shannonfanoenco(1:4, shannonfanodict(1:4,[0.5 0.25 0.15 0.10])),[   0   1   0   1   1   0   1   1   1   0],0)
50 %!