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