~ubuntu-branches/ubuntu/precise/gnuradio/precise

« back to all changes in this revision

Viewing changes to gr-trellis/src/lib/trellis_viterbi_combined_ss.cc

  • Committer: Bazaar Package Importer
  • Author(s): Kamal Mostafa
  • Date: 2010-03-13 07:46:01 UTC
  • mfrom: (2.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20100313074601-zjsa893a87bozyh7
Tags: 3.2.2.dfsg-1ubuntu1
* Fix build for Ubuntu lucid (LP: #260406)
  - add binary package dep for libusrp0, libusrp2-0: adduser
  - debian/rules clean: remove pre-built Qt moc files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 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
// WARNING: this file is machine generated.  Edits will be over written
 
24
 
 
25
#ifdef HAVE_CONFIG_H
 
26
#include "config.h"
 
27
#endif
 
28
 
 
29
#include <trellis_viterbi_combined_ss.h>
 
30
#include <gr_io_signature.h>
 
31
#include <assert.h>
 
32
#include <iostream>
 
33
  
 
34
static const float INF = 1.0e9;
 
35
 
 
36
trellis_viterbi_combined_ss_sptr 
 
37
trellis_make_viterbi_combined_ss (
 
38
    const fsm &FSM,
 
39
    int K,
 
40
    int S0,
 
41
    int SK,
 
42
    int D,
 
43
    const std::vector<short> &TABLE,
 
44
    trellis_metric_type_t TYPE)
 
45
{
 
46
  return trellis_viterbi_combined_ss_sptr (new trellis_viterbi_combined_ss (FSM,K,S0,SK,D,TABLE,TYPE));
 
47
}
 
48
 
 
49
trellis_viterbi_combined_ss::trellis_viterbi_combined_ss (
 
50
    const fsm &FSM,
 
51
    int K,
 
52
    int S0,
 
53
    int SK,
 
54
    int D,
 
55
    const std::vector<short> &TABLE,
 
56
    trellis_metric_type_t TYPE)
 
57
  : gr_block ("viterbi_combined_ss",
 
58
                          gr_make_io_signature (1, -1, sizeof (short)),
 
59
                          gr_make_io_signature (1, -1, sizeof (short))),  
 
60
  d_FSM (FSM),
 
61
  d_K (K),
 
62
  d_S0 (S0),
 
63
  d_SK (SK),
 
64
  d_D (D),
 
65
  d_TABLE (TABLE),
 
66
  d_TYPE (TYPE)//,
 
67
  //d_trace(FSM.S()*K)
 
68
{
 
69
    set_relative_rate (1.0 / ((double) d_D));
 
70
    set_output_multiple (d_K);
 
71
}
 
72
 
 
73
 
 
74
void trellis_viterbi_combined_ss::set_TABLE(const std::vector<short> &table) 
 
75
{
 
76
  d_TABLE = table;
 
77
}
 
78
 
 
79
void
 
80
trellis_viterbi_combined_ss::forecast (int noutput_items, gr_vector_int &ninput_items_required)
 
81
{
 
82
  assert (noutput_items % d_K == 0);
 
83
  int input_required =  d_D * noutput_items ;
 
84
  unsigned ninputs = ninput_items_required.size();
 
85
  for (unsigned int i = 0; i < ninputs; i++) {
 
86
    ninput_items_required[i] = input_required;
 
87
  }
 
88
}
 
89
 
 
90
 
 
91
 
 
92
 
 
93
void viterbi_algorithm_combined(int I, int S, int O, 
 
94
             const std::vector<int> &NS,
 
95
             const std::vector<int> &OS,
 
96
             const std::vector< std::vector<int> > &PS,
 
97
             const std::vector< std::vector<int> > &PI,
 
98
             int K,
 
99
             int S0,int SK,
 
100
             int D,
 
101
             const std::vector<short> &TABLE,
 
102
             trellis_metric_type_t TYPE,
 
103
             const short *in, short *out)//,
 
104
             //std::vector<int> &trace) 
 
105
{
 
106
  std::vector<int> trace(S*K);
 
107
  std::vector<float> alpha(S*2);
 
108
  float *metric = new float[O];
 
109
  int alphai;
 
110
  float norm,mm,minm;
 
111
  int minmi;
 
112
  int st;
 
113
 
 
114
  if(S0<0) { // initial state not specified
 
115
      for(int i=0;i<S;i++) alpha[0*S+i]=0;
 
116
  }
 
117
  else {
 
118
      for(int i=0;i<S;i++) alpha[0*S+i]=INF;
 
119
      alpha[0*S+S0]=0.0;
 
120
  }
 
121
 
 
122
  alphai=0;
 
123
  for(int k=0;k<K;k++) {
 
124
      calc_metric(O, D, TABLE, &(in[k*D]), metric,TYPE); // calc metrics
 
125
      norm=INF;
 
126
      for(int j=0;j<S;j++) { // for each next state do ACS
 
127
          minm=INF;
 
128
          minmi=0;
 
129
          for(unsigned int i=0;i<PS[j].size();i++) {
 
130
              //int i0 = j*I+i;
 
131
              if((mm=alpha[alphai*S+PS[j][i]]+metric[OS[PS[j][i]*I+PI[j][i]]])<minm)
 
132
                  minm=mm,minmi=i;
 
133
          }
 
134
          trace[k*S+j]=minmi;
 
135
          alpha[((alphai+1)%2)*S+j]=minm;
 
136
          if(minm<norm) norm=minm;
 
137
      }
 
138
      for(int j=0;j<S;j++) 
 
139
          alpha[((alphai+1)%2)*S+j]-=norm; // normalize total metrics so they do not explode
 
140
      alphai=(alphai+1)%2;
 
141
  }
 
142
 
 
143
  if(SK<0) { // final state not specified
 
144
      minm=INF;
 
145
      minmi=0;
 
146
      for(int i=0;i<S;i++)
 
147
          if((mm=alpha[alphai*S+i])<minm) minm=mm,minmi=i;
 
148
      st=minmi;
 
149
  }
 
150
  else {
 
151
      st=SK;
 
152
  }
 
153
 
 
154
  for(int k=K-1;k>=0;k--) { // traceback
 
155
      int i0=trace[k*S+st];
 
156
      out[k]= (short) PI[st][i0];
 
157
      st=PS[st][i0];
 
158
  }
 
159
  
 
160
  delete [] metric;
 
161
 
 
162
}
 
163
 
 
164
 
 
165
 
 
166
 
 
167
 
 
168
 
 
169
int
 
170
trellis_viterbi_combined_ss::general_work (int noutput_items,
 
171
                        gr_vector_int &ninput_items,
 
172
                        gr_vector_const_void_star &input_items,
 
173
                        gr_vector_void_star &output_items)
 
174
{
 
175
  assert (input_items.size() == output_items.size());
 
176
  int nstreams = input_items.size();
 
177
  assert (noutput_items % d_K == 0);
 
178
  int nblocks = noutput_items / d_K;
 
179
 
 
180
  for (int m=0;m<nstreams;m++) {
 
181
    const short *in = (const short *) input_items[m];
 
182
    short *out = (short *) output_items[m];
 
183
    for (int n=0;n<nblocks;n++) {
 
184
      viterbi_algorithm_combined(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,d_D,d_TABLE,d_TYPE,&(in[n*d_K*d_D]),&(out[n*d_K]));//,d_trace);
 
185
    }
 
186
  }
 
187
 
 
188
  consume_each (d_D * noutput_items );
 
189
  return noutput_items;
 
190
}