examples/test_cc/test_cc.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 /*------------------------------------------------------------*/
00021 /** @file test_cc.c Test program for convolutional codes      */
00022 /*------------------------------------------------------------*/
00023 
00024 #include <stdio.h>
00025 #include <it/types.h>
00026 #include <it/io.h>
00027 #include <it/vec.h>
00028 #include <it/distance.h>
00029 #include <it/convcode.h>
00030 #include <it/random.h>
00031 #include <it/channel.h>
00032 #include <it/source.h>
00033 #include <it/source_func.h>
00034 #include <it/mat.h>
00035 #include <it/math.h>
00036 
00037 /* which code to test */
00038 #undef NONRECURSIVE_1_3_CODE
00039 #undef NONRECURSIVE_2_3_CODE
00040 #define RECURSIVESYS_1_2_CODE
00041 
00042 mat bspk_metrics (it_convolutional_code_t * cc, vec r)
00043 {
00044   int  i, j, m;
00045   int  N = cc->n;
00046   int  L = vec_length (r) / N;
00047   mat  metrics;
00048   int  n_labels = cc->n_labels;
00049   double d;
00050 
00051 
00052 #define bit(x, n) (((x) >> (n)) & 1)
00053 
00054   metrics = mat_new (n_labels, L);
00055 
00056   for (i = 0; i < L; i++)
00057     for (j = 0; j < n_labels; j++) {
00058       metrics[j][i] = 0;
00059       for (m = 0; m < N; m++) {
00060   d = (r[i * N + m] - (bit (j, N - 1 - m) ? 1 : -1));
00061   metrics[j][i] -= d * d;
00062       }
00063     }
00064 
00065   return (metrics);
00066 }
00067 
00068 int main (void)
00069 {
00070   bvec input;
00071   bvec output;
00072   vec  modulated;
00073   vec  received;
00074   it_convolutional_code_t *cc;
00075   imat generator;   /* generator polynomials, ordered from LSB to MSB */
00076   int  feedback;
00077   mat  metrics;
00078 
00079   /* initialize the random number generator */
00080   //  it_randomize();
00081 
00082   /* generate a random binary sequence of 10 bits */
00083   input = source_binary (10, 0.5);
00084   it_printf ("input = $b\n", input);
00085 
00086   /* construct a convolutional code */
00087 #ifdef NONRECURSIVE_1_3_CODE
00088   generator = imat_new (1, 3);
00089   generator[0][0] = 0133;
00090   generator[0][1] = 0165;
00091   generator[0][2] = 0171;
00092   feedback = 0;
00093 #endif
00094 
00095 #ifdef NONRECURSIVE_2_3_CODE
00096   generator = imat_new (2, 3);
00097   generator[0][0] = 023;
00098   generator[0][1] = 035;
00099   generator[0][2] = 0;
00100 
00101   generator[1][0] = 0;
00102   generator[1][1] = 05;
00103   generator[1][2] = 013;
00104   feedback = 0;
00105 #endif
00106 
00107 #ifdef RECURSIVESYS_1_2_CODE
00108   generator = imat_new (1, 2);
00109   generator[0][0] = 033;  /* LSB is parity */
00110   generator[0][1] = 037;  /* MSB is systematic */
00111   feedback = 037;
00112 #endif
00113 
00114   cc = it_convolutional_code_new (generator, feedback);
00115 
00116   /* encode the sequence */
00117   output = it_cc_encode (cc, input);
00118   it_printf ("output = $b\n", output);
00119 
00120   /* modulate */
00121   modulated = modulate_bpsk (output);
00122   it_printf ("modulated = $v\n", modulated);
00123 
00124   /* add some noise  (gaussian, std dev = 1.5) */
00125   received = channel_awgn (modulated, 1.5);
00126   it_printf ("received = $v\n", received);
00127 
00128   /* compute metrics */
00129   metrics = bspk_metrics (cc, received);
00130 
00131   /* decode */
00132   it_printf ("decoding\n");
00133   bvec_delete (output);
00134   output = it_viterbi_decode (cc, metrics);
00135   it_printf ("decoded = $b\n", output);
00136   it_printf ("input = $b\n", input);
00137 
00138   mat_delete (metrics);
00139   vec_delete (received);
00140   vec_delete (modulated);
00141   bvec_delete (output);
00142   it_delete (cc);
00143   bvec_delete (input);
00144   imat_delete (generator);
00145 
00146   return (0);
00147 }

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