examples/test_ac/test_ac.c

Go to the documentation of this file.
00001 /** @file test_ac.c Text program for arithmetic codes */
00002 
00003 #include <stdio.h>
00004 #include <stdlib.h>
00005 #include <math.h>
00006 
00007 #include <it/vec.h>
00008 #include <it/io.h>
00009 #include <it/arithmetic_codec.h>
00010 #include <it/random.h>
00011 #include <it/source.h>
00012 #include <it/source_func.h>
00013 
00014 #define LENGTH 1000
00015 #define COUNT 1000
00016 #define PRECISION 9
00017 
00018 int main (void)
00019 {
00020   arithmetic_coder_t *coder;
00021   arithmetic_decoder_t *decoder;
00022   int  i, count;
00023   vec  prob0;
00024   ivec data;
00025   ivec dec;
00026   vec  pdf;
00027   bvec buffer;
00028   int  rate;
00029   float mean_entropy, mean_rate;
00030 
00031   /*  it_randomize(); */
00032 
00033   data = ivec_new (LENGTH);
00034   dec = ivec_new (LENGTH);
00035 
00036   buffer = bvec_new (10 * LENGTH);
00037 
00038   /* create the coder and decoder */
00039   coder = arithmetic_coder_new (PRECISION);
00040   decoder = arithmetic_decoder_new (PRECISION);
00041 
00042   /*** binary arithmetic coding ***/
00043 
00044   mean_entropy = mean_rate = 0;
00045   for (count = 0; count < COUNT; count++) {
00046 
00047     /* some random source distribution */
00048     prob0 = source_uniform (LENGTH, 0, 1);
00049 
00050     for (i = 0; i < LENGTH; i++) {
00051       mean_entropy +=
00052   -(prob0[i] * log (prob0[i]) +
00053     (1.0 - prob0[i]) * log (1.0 - prob0[i])) / log (2);
00054     }
00055 
00056     for (i = 0; i < LENGTH; i++)
00057       data[i] = (it_rand () > prob0[i]);
00058 
00059     /*
00060        printf("data: ");
00061        for(i = 0; i < LENGTH; i++)
00062        printf("%d", data[i]);
00063        printf("\n");
00064      */
00065 
00066     /* encode */
00067     arithmetic_coder_start (coder, buffer);
00068     for (i = 0; i < LENGTH; i++)
00069       arithmetic_coder_encode_bit (coder, prob0[i], data[i]);
00070     rate = arithmetic_coder_stop (coder);
00071 
00072     /* decode */
00073     arithmetic_decoder_start (decoder, buffer);
00074     for (i = 0; i < LENGTH; i++) {
00075       dec[i] = arithmetic_decoder_decode_bit (decoder, prob0[i]);
00076       if (dec[i] != data[i])
00077   printf ("error at bit %d\n", i);
00078     }
00079     if (rate != arithmetic_decoder_stop (decoder))
00080       printf ("decoding error\n");
00081 
00082     mean_rate += rate;
00083 
00084     /*
00085        printf("data: ");
00086        for(i = 0; i < LENGTH; i++)
00087        printf("%d", dec[i]);
00088        printf("\n");
00089      */
00090 
00091     vec_delete (prob0);
00092   }
00093   printf ("entropy: %.4f bits\n", ((float) mean_entropy) / (LENGTH * COUNT));
00094   printf ("rate: %.4f bits\n", ((float) mean_rate) / (LENGTH * COUNT));
00095 
00096   ivec_delete (data);
00097 
00098   /*** symbolic arithmetic coding ***/
00099 
00100   /* Define Maxted and Robinson source */
00101   pdf = vec_new_string ("0.4 0.2 0.2 0.1 0.1");
00102 
00103   /* generate a vector of LENGTH samples */
00104   data = source_memoryless (LENGTH, pdf);
00105 
00106   arithmetic_coder_start (coder, buffer);
00107   for (i = 0; i < LENGTH; i++)
00108     arithmetic_coder_encode_symbol (coder, pdf, data[i]);
00109   rate = arithmetic_coder_stop (coder);
00110 
00111   arithmetic_decoder_start (decoder, buffer);
00112   for (i = 0; i < LENGTH; i++) {
00113     dec[i] = arithmetic_decoder_decode_symbol (decoder, pdf);
00114     if (dec[i] != data[i])
00115       printf ("error at bit %d\n", i);
00116   }
00117   if (rate != arithmetic_decoder_stop (decoder))
00118     printf ("decoding error\n");
00119 
00120   printf ("entropy: %.4f bits\n", entropy (pdf));
00121   printf ("rate: %.4f bits\n", (float) rate / LENGTH);
00122 
00123   /* release resources */
00124   arithmetic_coder_delete (coder);
00125   arithmetic_decoder_delete (decoder);
00126 
00127   ivec_delete (data);
00128   ivec_delete (dec);
00129 
00130   return (0);
00131 }

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