Imported Upstream version 3.0
[debian/gnuradio] / gr-trellis / src / lib / trellis_viterbi_i.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004 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 2, 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 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <trellis_viterbi_i.h>
28 #include <gr_io_signature.h>
29 #include <assert.h>
30 #include <iostream>
31   
32 static const float INF = 1.0e9;
33
34 trellis_viterbi_i_sptr 
35 trellis_make_viterbi_i (
36     const fsm &FSM,
37     int K,
38     int S0,
39     int SK)
40 {
41   return trellis_viterbi_i_sptr (new trellis_viterbi_i (FSM,K,S0,SK));
42 }
43
44 trellis_viterbi_i::trellis_viterbi_i (
45     const fsm &FSM,
46     int K,
47     int S0,
48     int SK)
49   : gr_block ("viterbi_i",
50                           gr_make_io_signature (1, -1, sizeof (float)),
51                           gr_make_io_signature (1, -1, sizeof (int))),  
52   d_FSM (FSM),
53   d_K (K),
54   d_S0 (S0),
55   d_SK (SK)//,
56   //d_trace(FSM.S()*K)
57 {
58     set_relative_rate (1.0 / ((double) d_FSM.O()));
59     set_output_multiple (d_K);
60 }
61
62
63 void
64 trellis_viterbi_i::forecast (int noutput_items, gr_vector_int &ninput_items_required)
65 {
66   assert (noutput_items % d_K == 0);
67   int input_required =  d_FSM.O() * noutput_items ;
68   unsigned ninputs = ninput_items_required.size();
69   for (unsigned int i = 0; i < ninputs; i++) {
70     ninput_items_required[i] = input_required;
71   }
72 }
73
74
75
76
77 void viterbi_algorithm(int I, int S, int O, 
78              const std::vector<int> &NS,
79              const std::vector<int> &OS,
80              const std::vector<int> &PS,
81              const std::vector<int> &PI,
82              int K,
83              int S0,int SK,
84              const float *in, int *out)//,
85              //std::vector<int> &trace) 
86 {
87   std::vector<int> trace(S*K);
88   std::vector<float> alpha(S*2);
89   int alphai;
90   float norm,mm,minm;
91   int minmi;
92   int st;
93
94
95   if(S0<0) { // initial state not specified
96       for(int i=0;i<S;i++) alpha[0*S+i]=0;
97   }
98   else {
99       for(int i=0;i<S;i++) alpha[0*S+i]=INF;
100       alpha[0*S+S0]=0.0;
101   }
102
103   alphai=0;
104   for(int k=0;k<K;k++) {
105       norm=INF;
106       for(int j=0;j<S;j++) { // for each next state do ACS
107           minm=INF;
108           minmi=0;
109           for(int i=0;i<I;i++) {
110               int i0 = j*I+i;
111               if((mm=alpha[alphai*S+PS[i0]]+in[k*O+OS[PS[i0]*I+PI[i0]]])<minm)
112                   minm=mm,minmi=i;
113           }
114           trace[k*S+j]=minmi;
115           alpha[((alphai+1)%2)*S+j]=minm;
116           if(minm<norm) norm=minm;
117       }
118       for(int j=0;j<S;j++) 
119           alpha[((alphai+1)%2)*S+j]-=norm; // normalize total metrics so they do not explode
120       alphai=(alphai+1)%2;
121   }
122
123   if(SK<0) { // final state not specified
124       minm=INF;
125       minmi=0;
126       for(int i=0;i<S;i++)
127           if((mm=alpha[alphai*S+i])<minm) minm=mm,minmi=i;
128       st=minmi;
129   }
130   else {
131       st=SK;
132   }
133
134   for(int k=K-1;k>=0;k--) { // traceback
135       int i0=st*I+trace[k*S+st];
136       out[k]= (int) PI[i0];
137       st=PS[i0];
138   }
139
140 }
141
142
143
144
145
146
147 int
148 trellis_viterbi_i::general_work (int noutput_items,
149                         gr_vector_int &ninput_items,
150                         gr_vector_const_void_star &input_items,
151                         gr_vector_void_star &output_items)
152 {
153   assert (input_items.size() == output_items.size());
154   int nstreams = input_items.size();
155   assert (noutput_items % d_K == 0);
156   int nblocks = noutput_items / d_K;
157
158   for (int m=0;m<nstreams;m++) {
159     const float *in = (const float *) input_items[m];
160     int *out = (int *) output_items[m];
161     for (int n=0;n<nblocks;n++) {
162       viterbi_algorithm(d_FSM.I(),d_FSM.S(),d_FSM.O(),d_FSM.NS(),d_FSM.OS(),d_FSM.PS(),d_FSM.PI(),d_K,d_S0,d_SK,&(in[n*d_K*d_FSM.O()]),&(out[n*d_K]));//,d_trace);
163     }
164   }
165
166   consume_each (d_FSM.O() * noutput_items );
167   return noutput_items;
168 }