Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-core / src / lib / viterbi / decode.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2008 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 /*
24  * This is a minimal example demonstrating how to call the Viterbi decoder
25  * in continuous streaming mode.  It accepts data on stdin and writes to 
26  * stdout.
27  *
28  */
29
30 extern "C" {
31 #include "viterbi.h"
32 }
33
34 #include <cstdio>
35 #include <cmath>
36
37 #define MAXCHUNKSIZE 4096
38 #define MAXENCSIZE MAXCHUNKSIZE*16
39
40 int main()
41 {
42   unsigned char data[MAXCHUNKSIZE];
43   signed char syms[MAXENCSIZE];
44   int count = 0;
45
46   // Initialize metric table
47   int mettab[2][256];
48   int amp = 100;
49   float RATE=0.5;
50   float ebn0 = 12.0;
51   float esn0 = RATE*pow(10.0, ebn0/10);
52   gen_met(mettab, amp, esn0, 0.0, 4);
53
54   // Initialize decoder state
55   struct viterbi_state state0[64];
56   struct viterbi_state state1[64];
57   unsigned char viterbi_in[16];
58   viterbi_chunks_init(state0);  
59
60   while (!feof(stdin)) {
61     unsigned int n = fread(syms, 1, MAXENCSIZE, stdin);
62     unsigned char *out = data;
63     
64     for (unsigned int i = 0; i < n; i++) {
65
66       // FIXME: This implements hard decoding by slicing the input stream
67       unsigned char sym = syms[i] > 0 ? -amp : amp;
68
69       // Write the symbol to the decoder input
70       viterbi_in[count % 4] = sym;
71
72       // Every four symbols, perform the butterfly2 operation
73       if ((count % 4) == 3) {
74         viterbi_butterfly2(viterbi_in, mettab, state0, state1);      
75
76         // Every sixteen symbols, perform the readback operation
77         if ((count > 64) && (count % 16) == 11) {
78           viterbi_get_output(state0, out);
79           fwrite(out++, 1, 1, stdout);
80         }
81       }
82       
83       count++;
84     }    
85   }
86
87   return 0;
88 }