aboutsummaryrefslogtreecommitdiffstats
path: root/lib/receiver/viterbi_detector.cc
blob: 4e8fb3a19035f5600bbac7a82b3be483096d3563 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/* -*- c++ -*- */
/*
 * @file
 * @author (C) 2009 by Piotr Krysik <ptrkrysik@gmail.com>
 * @section LICENSE
 *
 * Gr-gsm is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * Gr-gsm is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gr-gsm; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */

/*
 * viterbi_detector:
 *           This part does the detection of received sequnece.
 *           Employed algorithm is viterbi Maximum Likehood Sequence Estimation.
 *           At this moment it gives hard decisions on the output, but
 *           it was designed with soft decisions in mind.
 *
 * SYNTAX:   void viterbi_detector(
 *                                  const gr_complex * input, 
 *                                  unsigned int samples_num, 
 *                                  gr_complex * rhh, 
 *                                  unsigned int start_state, 
 *                                  const unsigned int * stop_states, 
 *                                  unsigned int stops_num, 
 *                                  float * output)
 *
 * INPUT:    input:       Complex received signal afted matched filtering.
 *           samples_num: Number of samples in the input table.
 *           rhh:         The autocorrelation of the estimated channel 
 *                        impulse response.
 *           start_state: Number of the start point. In GSM each burst 
 *                        starts with sequence of three bits (0,0,0) which 
 *                        indicates start point of the algorithm.
 *           stop_states: Table with numbers of possible stop states.
 *           stops_num:   Number of possible stop states
 *                     
 *
 * OUTPUT:   output:      Differentially decoded hard output of the algorithm: 
 *                        -1 for logical "0" and 1 for logical "1"
 *
 * SUB_FUNC: none
 *
 * TEST(S):  Tested with real world normal burst.
 */

#include <gnuradio/gr_complex.h>
#include <gsm_constants.h>
#include <cmath>

#define PATHS_NUM (1 << (CHAN_IMP_RESP_LENGTH-1))

void viterbi_detector(const gr_complex * input, unsigned int samples_num, gr_complex * rhh, unsigned int start_state, const unsigned int * stop_states, unsigned int stops_num, float * output)
{
   float increment[8];
   float path_metrics1[16];
   float path_metrics2[16];
   float paths_difference;
   float * new_path_metrics;
   float * old_path_metrics;
   float * tmp;
   float trans_table[BURST_SIZE][16];
   float pm_candidate1, pm_candidate2;
   bool real_imag;
   float input_symbol_real, input_symbol_imag;
   unsigned int i, sample_nr;

/*
* Setup first path metrics, so only state pointed by start_state is possible.
* Start_state metric is equal to zero, the rest is written with some very low value,
* which makes them practically impossible to occur.
*/
   for(i=0; i<PATHS_NUM; i++){
      path_metrics1[i]=(-10e30);
   }
   path_metrics1[start_state]=0;

/*
* Compute Increment - a table of values which does not change for subsequent input samples.
* Increment is table of reference levels for computation of branch metrics:
*    branch metric = (+/-)received_sample (+/-) reference_level
*/
   increment[0] = -rhh[1].imag() -rhh[2].real() -rhh[3].imag() +rhh[4].real();
   increment[1] = rhh[1].imag() -rhh[2].real() -rhh[3].imag() +rhh[4].real();
   increment[2] = -rhh[1].imag() +rhh[2].real() -rhh[3].imag() +rhh[4].real();
   increment[3] = rhh[1].imag() +rhh[2].real() -rhh[3].imag() +rhh[4].real();
   increment[4] = -rhh[1].imag() -rhh[2].real() +rhh[3].imag() +rhh[4].real();
   increment[5] = rhh[1].imag() -rhh[2].real() +rhh[3].imag() +rhh[4].real();
   increment[6] = -rhh[1].imag() +rhh[2].real() +rhh[3].imag() +rhh[4].real();
   increment[7] = rhh[1].imag() +rhh[2].real() +rhh[3].imag() +rhh[4].real();


/*
* Computation of path metrics and decisions (Add-Compare-Select).
* It's composed of two parts: one for odd input samples (imaginary numbers)
* and one for even samples (real numbers).
* Each part is composed of independent (parallelisable) statements like  
* this one:
*      pm_candidate1 = old_path_metrics[0] -input_symbol_imag +increment[2];
*      pm_candidate2 = old_path_metrics[8] -input_symbol_imag -increment[5];
*      paths_difference=pm_candidate2-pm_candidate1;
*      new_path_metrics[1]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
*      trans_table[sample_nr][1] = paths_difference;
* This is very good point for optimisations (SIMD or OpenMP) as it's most time 
* consuming part of this function. 
*/
   sample_nr=0;
   old_path_metrics=path_metrics1;
   new_path_metrics=path_metrics2;
   while(sample_nr<samples_num){
      //Processing imag states
      real_imag=1;
      input_symbol_imag = input[sample_nr].imag();

      pm_candidate1 = old_path_metrics[0] +input_symbol_imag -increment[2];
      pm_candidate2 = old_path_metrics[8] +input_symbol_imag +increment[5];
      paths_difference=pm_candidate2-pm_candidate1;
      new_path_metrics[0]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
      trans_table[sample_nr][0] = paths_difference;

      pm_candidate1 = old_path_metrics[0] -input_symbol_imag +increment[2];
      pm_candidate2 = old_path_metrics[8] -input_symbol_imag -increment[5];
      paths_difference=pm_candidate2-pm_candidate1;
      new_path_metrics[1]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
      trans_table[sample_nr][1] = paths_difference;

      pm_candidate1 = old_path_metrics[1] +input_symbol_imag -increment[3];
      pm_candidate2 = old_path_metrics[9] +input_symbol_imag +increment[4];
      paths_difference=pm_candidate2-pm_candidate1;
      new_path_metrics[2]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
      trans_table[sample_nr][2] = paths_difference;

      pm_candidate1 = old_path_metrics[1] -input_symbol_imag +increment[3];
      pm_candidate2 = old_path_metrics[9] -input_symbol_imag -increment[4];
      paths_difference=pm_candidate2-pm_candidate1;
      new_path_metrics[3]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
      trans_table[sample_nr][3] = paths_difference;

      pm_candidate1 = old_path_metrics[2] +input_symbol_imag -increment[0];
      pm_candidate2 = old_path_metrics[10] +input_symbol_imag +increment[7];
      paths_difference=pm_candidate2-pm_candidate1;
      new_path_metrics[4]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
      trans_table[sample_nr][4] = paths_difference;

      pm_candidate1 = old_path_metrics[2] -input_symbol_imag +increment[0];
      pm_candidate2 = old_path_metrics[10] -input_symbol_imag -increment[7];
      paths_difference=pm_candidate2-pm_candidate1;
      new_path_metrics[5]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
      trans_table[sample_nr][5] = paths_difference;

      pm_candidate1 = old_path_metrics[3] +input_symbol_imag -increment[1];
      pm_candidate2 = old_path_metrics[11] +input_symbol_imag +increment[6];
      paths_difference=pm_candidate2-pm_candidate1;
      new_path_metrics[6]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
      trans_table[sample_nr][6] = paths_difference;

      pm_candidate1 = old_path_metrics[3] -input_symbol_imag +increment[1];
      pm_candidate2 = old_path_metrics[11] -input_symbol_imag -increment[6];
      paths_difference=pm_candidate2-pm_candidate1;
      new_path_metrics[7]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
      trans_table[sample_nr][7] = paths_difference;

      pm_candidate1 = old_path_metrics[4] +input_symbol_imag -increment[6];
      pm_candidate2 = old_path_metrics[12] +input_symbol_imag +increment[1];
      paths_difference=pm_candidate2-pm_candidate1;
      new_path_metrics[8]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
      trans_table[sample_nr][8] = paths_difference;

      pm_candidate1 = old_path_metrics[4] -input_symbol_imag +increment[6];
      pm_candidate2 = old_path_metrics[12] -input_symbol_imag -increment[1];
      paths_difference=pm_candidate2-pm_candidate1;
      new_path_metrics[9]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
      trans_table[sample_nr][9] = paths_difference;

      pm_candidate1 = old_path_metrics[5] +input_symbol_imag -increment[7];
      pm_candidate2 = old_path_metrics[13] +input_symbol_imag +increment[0];
      paths_difference=pm_candidate2-pm_candidate1;
      new_path_metrics[10]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
      trans_table[sample_nr][10] = paths_difference;

      pm_candidate1 = old_path_metrics[5] -input_symbol_imag +increment[7];
      pm_candidate2 = old_path_metrics[13] -input_symbol_imag -increment[0];
      paths_difference=pm_candidate2-pm_candidate1;
      new_path_metrics[11]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
      trans_table[sample_nr][11] = paths_difference;

      pm_candidate1 = old_path_metrics[6] +input_symbol_imag -increment[4];
      pm_candidate2 = old_path_metrics[14] +input_symbol_imag +increment[3];
      paths_difference=pm_candidate2-pm_candidate1;
      new_path_metrics[12]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
      trans_table[sample_nr][12] = paths_difference;

      pm_candidate1 = old_path_metrics[6] -input_symbol_imag +increment[4];
      pm_candidate2 = old_path_metrics[14] -input_symbol_imag -increment[3];
      paths_difference=pm_candidate2-pm_candidate1;
      new_path_metrics[13]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
      trans_table[sample_nr][13] = paths_difference;

      pm_candidate1 = old_path_metrics[7] +input_symbol_imag -increment[5];
      pm_candidate2 = old_path_metrics[15] +input_symbol_imag +increment[2];
      paths_difference=pm_candidate2-pm_candidate1;
      new_path_metrics[14]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
      trans_table[sample_nr][14] = paths_difference;

      pm_candidate1 = old_path_metrics[7] -input_symbol_imag +increment[5];
      pm_candidate2 = old_path_metrics[15] -input_symbol_imag -increment[2];
      paths_difference=pm_candidate2-pm_candidate1;
      new_path_metrics[15]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
      trans_table[sample_nr][15] = paths_difference;
      tmp=old_path_metrics;
      old_path_metrics=new_path_metrics;
      new_path_metrics=tmp;

      sample_nr++;
      if(sample_nr==samples_num)
         break;

      //Processing real states
      real_imag=0;
      input_symbol_real = input[sample_nr].real();

      pm_candidate1 = old_path_metrics[0] -input_symbol_real -increment[7];
      pm_candidate2 = old_path_metrics[8] -input_symbol_real +increment[0];
      paths_difference=pm_candidate2-pm_candidate1;
      new_path_metrics[0]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
      trans_table[sample_nr][0] = paths_difference;

      pm_candidate1 = old_path_metrics[0] +input_symbol_real +increment[7];
      pm_candidate2 = old_path_metrics[8] +input_symbol_real -increment[0];
      paths_difference=pm_candidate2-pm_candidate1;
      new_path_metrics[1]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
      trans_table[sample_nr][1] = paths_difference;

      pm_candidate1 = old_path_metrics[1] -input_symbol_real -increment[6];
      pm_candidate2 = old_path_metrics[9] -input_symbol_real +increment[1];
      paths_difference=pm_candidate2-pm_candidate1;
      new_path_metrics[2]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
      trans_table[sample_nr][2] = paths_difference;

      pm_candidate1 = old_path_metrics[1] +input_symbol_real +increment[6];
      pm_candidate2 = old_path_metrics[9] +input_symbol_real -increment[1];
      paths_difference=pm_candidate2-pm_candidate1;
      new_path_metrics[3]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
      trans_table[sample_nr][3] = paths_difference;

      pm_candidate1 = old_path_metrics[2] -input_symbol_real -increment[5];
      pm_candidate2 = old_path_metrics[10] -input_symbol_real +increment[2];
      paths_difference=pm_candidate2-pm_candidate1;
      new_path_metrics[4]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
      trans_table[sample_nr][4] = paths_difference;

      pm_candidate1 = old_path_metrics[2] +input_symbol_real +increment[5];
      pm_candidate2 = old_path_metrics[10] +input_symbol_real -increment[2];
      paths_difference=pm_candidate2-pm_candidate1;
      new_path_metrics[5]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
      trans_table[sample_nr][5] = paths_difference;

      pm_candidate1 = old_path_metrics[3] -input_symbol_real -increment[4];
      pm_candidate2 = old_path_metrics[11] -input_symbol_real +increment[3];
      paths_difference=pm_candidate2-pm_candidate1;
      new_path_metrics[6]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
      trans_table[sample_nr][6] = paths_difference;

      pm_candidate1 = old_path_metrics[3] +input_symbol_real +increment[4];
      pm_candidate2 = old_path_metrics[11] +input_symbol_real -increment[3];
      paths_difference=pm_candidate2-pm_candidate1;
      new_path_metrics[7]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
      trans_table[sample_nr][7] = paths_difference;

      pm_candidate1 = old_path_metrics[4] -input_symbol_real -increment[3];
      pm_candidate2 = old_path_metrics[12] -input_symbol_real +increment[4];
      paths_difference=pm_candidate2-pm_candidate1;
      new_path_metrics[8]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
      trans_table[sample_nr][8] = paths_difference;

      pm_candidate1 = old_path_metrics[4] +input_symbol_real +increment[3];
      pm_candidate2 = old_path_metrics[12] +input_symbol_real -increment[4];
      paths_difference=pm_candidate2-pm_candidate1;
      new_path_metrics[9]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
      trans_table[sample_nr][9] = paths_difference;

      pm_candidate1 = old_path_metrics[5] -input_symbol_real -increment[2];
      pm_candidate2 = old_path_metrics[13] -input_symbol_real +increment[5];
      paths_difference=pm_candidate2-pm_candidate1;
      new_path_metrics[10]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
      trans_table[sample_nr][10] = paths_difference;

      pm_candidate1 = old_path_metrics[5] +input_symbol_real +increment[2];
      pm_candidate2 = old_path_metrics[13] +input_symbol_real -increment[5];
      paths_difference=pm_candidate2-pm_candidate1;
      new_path_metrics[11]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
      trans_table[sample_nr][11] = paths_difference;

      pm_candidate1 = old_path_metrics[6] -input_symbol_real -increment[1];
      pm_candidate2 = old_path_metrics[14] -input_symbol_real +increment[6];
      paths_difference=pm_candidate2-pm_candidate1;
      new_path_metrics[12]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
      trans_table[sample_nr][12] = paths_difference;

      pm_candidate1 = old_path_metrics[6] +input_symbol_real +increment[1];
      pm_candidate2 = old_path_metrics[14] +input_symbol_real -increment[6];
      paths_difference=pm_candidate2-pm_candidate1;
      new_path_metrics[13]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
      trans_table[sample_nr][13] = paths_difference;

      pm_candidate1 = old_path_metrics[7] -input_symbol_real -increment[0];
      pm_candidate2 = old_path_metrics[15] -input_symbol_real +increment[7];
      paths_difference=pm_candidate2-pm_candidate1;
      new_path_metrics[14]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
      trans_table[sample_nr][14] = paths_difference;

      pm_candidate1 = old_path_metrics[7] +input_symbol_real +increment[0];
      pm_candidate2 = old_path_metrics[15] +input_symbol_real -increment[7];
      paths_difference=pm_candidate2-pm_candidate1;
      new_path_metrics[15]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
      trans_table[sample_nr][15] = paths_difference;

      tmp=old_path_metrics;
      old_path_metrics=new_path_metrics;
      new_path_metrics=tmp;

      sample_nr++;
   }

/*
* Find the best from the stop states by comparing their path metrics.
* Not every stop state is always possible, so we are searching in
* a subset of them.
*/
   unsigned int best_stop_state;
   float stop_state_metric, max_stop_state_metric;
   best_stop_state = stop_states[0];
   max_stop_state_metric = old_path_metrics[best_stop_state];
   for(i=1; i< stops_num; i++){
      stop_state_metric = old_path_metrics[stop_states[i]];
      if(stop_state_metric > max_stop_state_metric){
         max_stop_state_metric = stop_state_metric;
         best_stop_state = stop_states[i];
      }
   }

/*
* This table was generated with hope that it gives a litle speedup during
* traceback stage. 
* Received bit is related to the number of state in the trellis.
* I've numbered states so their parity (number of ones) is related
* to a received bit. 
*/
   static const unsigned int parity_table[PATHS_NUM] = { 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0,  };

/*
* Table of previous states in the trellis diagram.
* For GMSK modulation every state has two previous states.
* Example:
*   previous_state_nr1 = prev_table[current_state_nr][0]
*   previous_state_nr2 = prev_table[current_state_nr][1]
*/
   static const unsigned int prev_table[PATHS_NUM][2] = { {0,8}, {0,8}, {1,9}, {1,9}, {2,10}, {2,10}, {3,11}, {3,11}, {4,12}, {4,12}, {5,13}, {5,13}, {6,14}, {6,14}, {7,15}, {7,15},  };

/*
* Traceback and differential decoding of received sequence.
* Decisions stored in trans_table are used to restore best path in the trellis.
*/
   sample_nr=samples_num;
   unsigned int state_nr=best_stop_state;
   unsigned int decision;
   bool out_bit=0;

   while(sample_nr>0){
      sample_nr--;
      decision = (trans_table[sample_nr][state_nr]>0);

      if(decision != out_bit)
         output[sample_nr]=-trans_table[sample_nr][state_nr];
      else
         output[sample_nr]=trans_table[sample_nr][state_nr];

      out_bit = out_bit ^ real_imag ^ parity_table[state_nr];
      state_nr = prev_table[state_nr][decision];
      real_imag = !real_imag;
   }
}