title: How to use Viterbi decoder from GNU Radio library
body: <p>I need to make Viterbi decoding of some <a href="https://en.wikipedia.org/wiki/Convolutional_code" rel="nofollow noreferrer">convolutional-encoded</a> signal. My application shall work with large files, therefore I cannot insert all the signal into a heap, so I need to process a data by a sequence of separate buffers. I have found a good library for Viterbi decoding - <a href="http://www.drdobbs.com/cpp/convolutional-error-control-codes/184410317" rel="nofollow noreferrer"><strong>Encoder and a Viterbi decoder in C++ on the dr. Dobbs</strong></a>. I have applied the decoder from the libarary, it works correct, but doesn't provide a function for continuous use (call a function many times for each signal buffer with considering of previous calculations). Then I have found the  <a href="http://gnuradio.org/redmine/projects/gnuradio/wiki/Guided_Tutorials" rel="nofollow noreferrer">GNU Radio C++ library</a> which provide <a href="http://www.gnuradio.org/redmine/projects/gnuradio/repository/revisions/a52f9a19581901beabc9111917965b9817231014/show/gnuradio-core/src/lib/viterbi" rel="nofollow noreferrer">the necessary functions</a>. But I don't understand how to use its functions, because it doesn't provide a documentation. It contains <a href="http://www.gnuradio.org/redmine/projects/gnuradio/repository/revisions/a52f9a19581901beabc9111917965b9817231014/entry/gnuradio-core/src/lib/viterbi/decode.cc" rel="nofollow noreferrer">the example of Viterbi decoding</a> with is present below:</p>
  
  <pre><code>extern "C" {
  #include &lt;gnuradio/fec/viterbi.h&gt;
  }
  
  #include &lt;cstdio&gt;
  #include &lt;cmath&gt;
  
  #define MAXCHUNKSIZE 4096
  #define MAXENCSIZE MAXCHUNKSIZE*16
  
  int main()
  {
    unsigned char data[MAXCHUNKSIZE];
    signed char syms[MAXENCSIZE];
    int count = 0;
  
    // Initialize metric table
    int mettab[2][256];
    int amp = 100;  // What is it? ***
    float RATE=0.5;
    float ebn0 = 12.0;
    float esn0 = RATE*pow(10.0, ebn0/10);
    gen_met(mettab, amp, esn0, 0.0, 4);
  
    // Initialize decoder state
    struct viterbi_state state0[64];
    struct viterbi_state state1[64];
    unsigned char viterbi_in[16];
    viterbi_chunks_init(state0);
  
    while (!feof(stdin)) {
      unsigned int n = fread(syms, 1, MAXENCSIZE, stdin);
      unsigned char *out = data;
  
      for (unsigned int i = 0; i &lt; n; i++) {
  
        // FIXME: This implements hard decoding by slicing the input stream
        unsigned char sym = syms[i] &gt; 0 ? -amp : amp; // What is it? ***
  
        // Write the symbol to the decoder input
        viterbi_in[count % 4] = sym;
  
        // Every four symbols, perform the butterfly2 operation
        if ((count % 4) == 3) {
          viterbi_butterfly2(viterbi_in, mettab, state0, state1);
  
      // Every sixteen symbols, perform the readback operation
          if ((count &gt; 64) &amp;&amp; (count % 16) == 11) {
            viterbi_get_output(state0, out);
        fwrite(out++, 1, 1, stdout);
      }
        }
  
        count++;
      }
    }
  
    return 0;
  }
  </code></pre>
  
  <p>File <a href="http://www.gnuradio.org/redmine/projects/gnuradio/repository/revisions/a52f9a19581901beabc9111917965b9817231014/entry/gnuradio-core/src/lib/viterbi/viterbi.c" rel="nofollow noreferrer">viterbi.c</a> from it also contains the next function <code>viterbi()</code>, without a declaration:</p>
  
  <pre><code>/* Viterbi decoder */
  int viterbi(unsigned long *metric,  /* Final path metric (returned value) */
      unsigned char *data,    /* Decoded output data */
      unsigned char *symbols, /* Raw deinterleaved input symbols */
      unsigned int nbits, /* Number of output bits */
      int mettab[2][256]  /* Metric table, [sent sym][rx symbol] */
      ) { ...
  </code></pre>
  
  <p>Also I found one more implementation for Viterbi decoding - <a href="http://www.spiral.net/software/viterbi.html" rel="nofollow noreferrer">The Spiral project</a>. But it also doesn't contain a normal description and doesn't want to compile. And two more implementation on the <a href="http://www.expertcore.org/viewtopic.php?t=866" rel="nofollow noreferrer">ExpertCore</a> and <a href="http://www.ka9q.net/code/fec/" rel="nofollow noreferrer">Forward Error Correction DSP library</a>.</p>
  
  <p><strong>My question:</strong> Can anyone understand how to use the above GNU Radio's implementation of the Viterbi algorithm for continuous use for binary interleaved digital signal (Encoder parameters of my signal: K=7 rate=1/2, every bit in my file is a demodulated sample of a signal)?</p>