3 * Copyright 2004 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
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)
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.
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., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
27 #include <trellis_siso_f.h>
28 #include <gr_io_signature.h>
33 static const float INF = 1.0e9;
43 trellis_siso_type_t SISO_TYPE)
45 return trellis_siso_f_sptr (new trellis_siso_f (FSM,K,S0,SK,POSTI,POSTO,SISO_TYPE));
48 trellis_siso_f::trellis_siso_f (
55 trellis_siso_type_t SISO_TYPE)
57 gr_make_io_signature (1, -1, sizeof (float)),
58 gr_make_io_signature (1, -1, sizeof (float))),
65 d_SISO_TYPE (SISO_TYPE),
66 d_alpha(FSM.S()*(K+1)),
70 if (d_POSTI && d_POSTO)
71 multiple = d_FSM.I()+d_FSM.O();
77 throw std::runtime_error ("Not both POSTI and POSTO can be false.");
78 //printf("constructor: Multiple = %d\n",multiple);
79 set_output_multiple (d_K*multiple);
80 //what is the meaning of relative rate for this?
81 // it was suggested to use the one furthest from 1.0
83 set_relative_rate ( multiple / ((double) d_FSM.I()) );
88 trellis_siso_f::forecast (int noutput_items, gr_vector_int &ninput_items_required)
91 if (d_POSTI && d_POSTO)
92 multiple = d_FSM.I()+d_FSM.O();
98 throw std::runtime_error ("Not both POSTI and POSTO can be false.");
99 //printf("forecast: Multiple = %d\n",multiple);
100 assert (noutput_items % (d_K*multiple) == 0);
101 int input_required1 = d_FSM.I() * (noutput_items/multiple) ;
102 int input_required2 = d_FSM.O() * (noutput_items/multiple) ;
103 //printf("forecast: Output requirements: %d\n",noutput_items);
104 //printf("forecast: Input requirements: %d %d\n",input_required1,input_required2);
105 unsigned ninputs = ninput_items_required.size();
106 assert(ninputs % 2 == 0);
107 for (unsigned int i = 0; i < ninputs/2; i++) {
108 ninput_items_required[2*i] = input_required1;
109 ninput_items_required[2*i+1] = input_required2;
113 inline float min(float a, float b)
115 return a <= b ? a : b;
118 inline float min_star(float a, float b)
120 return (a <= b ? a : b)-log(1+exp(a <= b ? a-b : b-a));
123 void siso_algorithm(int I, int S, int O,
124 const std::vector<int> &NS,
125 const std::vector<int> &OS,
126 const std::vector<int> &PS,
127 const std::vector<int> &PI,
130 bool POSTI, bool POSTO,
131 float (*p2mymin)(float,float),
132 const float *priori, const float *prioro, float *post,
133 std::vector<float> &alpha,
134 std::vector<float> &beta)
139 if(S0<0) { // initial state not specified
140 for(int i=0;i<S;i++) alpha[0*S+i]=0;
143 for(int i=0;i<S;i++) alpha[0*S+i]=INF;
147 for(int k=0;k<K;k++) { // forward recursion
149 for(int j=0;j<S;j++) {
151 for(int i=0;i<I;i++) {
153 mm=alpha[k*S+PS[i0]]+priori[k*I+PI[i0]]+prioro[k*O+OS[PS[i0]*I+PI[i0]]];
154 minm=(*p2mymin)(minm,mm);
156 alpha[(k+1)*S+j]=minm;
157 if(minm<norm) norm=minm;
160 alpha[(k+1)*S+j]-=norm; // normalize total metrics so they do not explode
163 if(SK<0) { // final state not specified
164 for(int i=0;i<S;i++) beta[K*S+i]=0;
167 for(int i=0;i<S;i++) beta[K*S+i]=INF;
171 for(int k=K-1;k>=0;k--) { // backward recursion
173 for(int j=0;j<S;j++) {
175 for(int i=0;i<I;i++) {
177 mm=beta[(k+1)*S+NS[i0]]+priori[k*I+i]+prioro[k*O+OS[i0]];
178 minm=(*p2mymin)(minm,mm);
181 if(minm<norm) norm=minm;
184 beta[k*S+j]-=norm; // normalize total metrics so they do not explode
190 for(int k=0;k<K;k++) { // input combining
192 for(int i=0;i<I;i++) {
194 for(int j=0;j<S;j++) {
195 mm=alpha[k*S+j]+prioro[k*O+OS[j*I+i]]+beta[(k+1)*S+NS[j*I+i]];
196 minm=(*p2mymin)(minm,mm);
198 post[k*(I+O)+i]=minm;
199 if(minm<norm) norm=minm;
202 post[k*(I+O)+i]-=norm; // normalize metrics
206 for(int k=0;k<K;k++) { // output combining
208 for(int n=0;n<O;n++) {
210 for(int j=0;j<S;j++) {
211 for(int i=0;i<I;i++) {
212 mm= (n==OS[j*I+i] ? alpha[k*S+j]+priori[k*I+i]+beta[(k+1)*S+NS[j*I+i]] : INF);
213 minm=(*p2mymin)(minm,mm);
216 post[k*(I+O)+I+n]=minm;
217 if(minm<norm) norm=minm;
220 post[k*(I+O)+I+n]-=norm; // normalize metrics
225 for(int k=0;k<K;k++) { // input combining
227 for(int i=0;i<I;i++) {
229 for(int j=0;j<S;j++) {
230 mm=alpha[k*S+j]+prioro[k*O+OS[j*I+i]]+beta[(k+1)*S+NS[j*I+i]];
231 minm=(*p2mymin)(minm,mm);
234 if(minm<norm) norm=minm;
237 post[k*I+i]-=norm; // normalize metrics
242 for(int k=0;k<K;k++) { // output combining
244 for(int n=0;n<O;n++) {
246 for(int j=0;j<S;j++) {
247 for(int i=0;i<I;i++) {
248 mm= (n==OS[j*I+i] ? alpha[k*S+j]+priori[k*I+i]+beta[(k+1)*S+NS[j*I+i]] : INF);
249 minm=(*p2mymin)(minm,mm);
253 if(minm<norm) norm=minm;
256 post[k*O+n]-=norm; // normalize metrics
260 throw std::runtime_error ("Not both POSTI and POSTO can be false.");
270 trellis_siso_f::general_work (int noutput_items,
271 gr_vector_int &ninput_items,
272 gr_vector_const_void_star &input_items,
273 gr_vector_void_star &output_items)
275 assert (input_items.size() == 2*output_items.size());
276 int nstreams = output_items.size();
277 //printf("general_work:Streams: %d\n",nstreams);
279 if (d_POSTI && d_POSTO)
280 multiple = d_FSM.I()+d_FSM.O();
282 multiple = d_FSM.I();
284 multiple = d_FSM.O();
286 throw std::runtime_error ("Not both POSTI and POSTO can be false.");
288 assert (noutput_items % (d_K*multiple) == 0);
289 int nblocks = noutput_items / (d_K*multiple);
290 //printf("general_work:Blocks: %d\n",nblocks);
291 //for(int i=0;i<ninput_items.size();i++)
292 //printf("general_work:Input items available: %d\n",ninput_items[i]);
294 float (*p2min)(float, float) = NULL;
295 if(d_SISO_TYPE == TRELLIS_MIN_SUM)
297 else if(d_SISO_TYPE == TRELLIS_SUM_PRODUCT)
301 for (int m=0;m<nstreams;m++) {
302 const float *in1 = (const float *) input_items[2*m];
303 const float *in2 = (const float *) input_items[2*m+1];
304 float *out = (float *) output_items[m];
305 for (int n=0;n<nblocks;n++) {
306 siso_algorithm(d_FSM.I(),d_FSM.S(),d_FSM.O(),
307 d_FSM.NS(),d_FSM.OS(),d_FSM.PS(),d_FSM.PI(),
311 &(in1[n*d_K*d_FSM.I()]),&(in2[n*d_K*d_FSM.O()]),
312 &(out[n*d_K*multiple]),
317 for (unsigned int i = 0; i < input_items.size()/2; i++) {
318 consume(2*i,d_FSM.I() * noutput_items / multiple );
319 consume(2*i+1,d_FSM.O() * noutput_items / multiple );
322 return noutput_items;