examples/test_vlc/test_vlc.c

Go to the documentation of this file.
00001 /*
00002    libit - Library for basic source and channel coding functions
00003    Copyright (C) 2005-2005 Vivien Chappelier, Herve Jegou
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public
00016    License along with this library; if not, write to the Free
00017    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 */
00019 
00020 /** @file test_vlc.c Test program for Variables Length Codes */
00021 
00022 
00023 #include <stdio.h>
00024 #include <it/io.h>
00025 #include <it/vec.h>
00026 #include <it/vlc.h>
00027 #include <it/source.h>
00028 
00029 int main (int argc, char **argv)
00030 {
00031   vlc_t *vlc;
00032   vec  nodes_pdf;
00033   vec  nodes_expectation;
00034   vec  pdf = vec_new_string ("0.5 0.3 0.1 0.1");
00035   vec  v1N = vec_new_1N (5);
00036   ivec S;
00037   bvec B;
00038   ivec D;
00039   int  N = 20;
00040 
00041   it_printf ("pdf     = $v\n", pdf);
00042   it_printf ("symbols = $v\n", v1N);
00043 
00044   vlc = vlc_hu_tucker (pdf);
00045   printf ("\n[ Hu-Tucker ]\n");
00046   vlc_print (vlc);
00047   printf ("\nmdl( vlc )  = %g\n", vlc_mdl (vlc, pdf));
00048 
00049   nodes_pdf = vlc_nodes_pdf (vlc, pdf);
00050   nodes_expectation = vlc_nodes_expectation (vlc, pdf, v1N);
00051   it_printf ("\nP(nodes)=$v\nE(nodes)=$v\n", nodes_pdf, nodes_expectation);
00052   vlc_delete (vlc);
00053 
00054   vlc = vlc_huffman (pdf);
00055   printf ("\n[ Huffman code ]\n");
00056   vlc_print (vlc);
00057   printf ("\nmdl( vlc )  = %g\n", vlc_mdl (vlc, pdf));
00058 
00059   vlc_quasi_lexicographic (vlc, pdf, v1N);
00060   printf ("\n[ p-lex Huffman code ]\n");
00061   vlc_print_all (vlc, pdf, v1N);
00062   printf ("\nmdl( vlc )  = %g\n", vlc_mdl (vlc, pdf));
00063   printf ("minh( vlc ) = %d\n", vlc_minh (vlc));
00064   printf ("maxh( vlc ) = %d\n", vlc_maxh (vlc));
00065 
00066   S = source_memoryless (N, pdf);
00067   printf ("Length( S ) = %d\n", ivec_length (S));
00068 
00069   B = vlc_encode_concat (vlc, S);
00070 
00071   D = vlc_decode_concat (vlc, B);
00072 
00073   it_printf ("\n[Coding and Decoding]\nS = $d\nB = $b\nD = $d\n", S, B, D);
00074 
00075   /* Custom VLC is read from a string */
00076   vlc_delete (vlc);
00077   vlc = vlc_read ("{0 11 101 100}");
00078   printf ("Custom Code = ");
00079   vlc_print (vlc);
00080   printf ("\n");
00081 
00082 
00083   vlc_delete (vlc);
00084   vec_delete (pdf);
00085   vec_delete (v1N);
00086   ivec_delete (S);
00087   bvec_delete (B);
00088   ivec_delete (D);
00089   return 0;
00090 }

Hosted by
Copyright (C) 2005-2006 Hervé Jégou
Vivien Chappelier
Francois Cayre
libit logo courtesy of Jonathan Delhumeau