blob: f3445cf85dd0b0f6951c88b8d2b82324a3cc8164 [file] [log] [blame]
piotr437f5462014-02-04 17:57:25 +01001/* -*- c++ -*- */
2/*
3 * @file
4 * @author Piotr Krysik <pkrysik@stud.elka.pw.edu.pl>
5 * @section LICENSE
6 *
7 * This program 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 * This program 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 this program; 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/*
24 * viterbi_detector:
25 * This part does the detection of received sequnece.
26 * Employed algorithm is viterbi Maximum Likehood Sequence Estimation.
27 * At this moment it gives hard decisions on the output, but
28 * it was designed with soft decisions in mind.
29 *
30 * SYNTAX: void viterbi_detector(
31 * const gr_complex * input,
32 * unsigned int samples_num,
33 * gr_complex * rhh,
34 * unsigned int start_state,
35 * const unsigned int * stop_states,
36 * unsigned int stops_num,
37 * float * output)
38 *
39 * INPUT: input: Complex received signal afted matched filtering.
40 * samples_num: Number of samples in the input table.
41 * rhh: The autocorrelation of the estimated channel
42 * impulse response.
43 * start_state: Number of the start point. In GSM each burst
44 * starts with sequence of three bits (0,0,0) which
45 * indicates start point of the algorithm.
46 * stop_states: Table with numbers of possible stop states.
47 * stops_num: Number of possible stop states
48 *
49 *
50 * OUTPUT: output: Differentially decoded hard output of the algorithm:
51 * -1 for logical "0" and 1 for logical "1"
52 *
53 * SUB_FUNC: none
54 *
55 * TEST(S): Tested with real world normal burst.
56 */
57
58#include <gnuradio/gr_complex.h>
59#include <gsm_constants.h>
60#define PATHS_NUM (1 << (CHAN_IMP_RESP_LENGTH-1))
61
62void 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)
63{
64 float increment[8];
65 float path_metrics1[16];
66 float path_metrics2[16];
67 float * new_path_metrics;
68 float * old_path_metrics;
69 float * tmp;
70 float trans_table[BURST_SIZE][16];
71 float pm_candidate1, pm_candidate2;
72 bool real_imag;
73 float input_symbol_real, input_symbol_imag;
74 unsigned int i, sample_nr;
75
76/*
77* Setup first path metrics, so only state pointed by start_state is possible.
78* Start_state metric is equal to zero, the rest is written with some very low value,
79* which makes them practically impossible to occur.
80*/
81 for(i=0; i<PATHS_NUM; i++){
82 path_metrics1[i]=(-10e30);
83 }
84 path_metrics1[start_state]=0;
85
86/*
87* Compute Increment - a table of values which does not change for subsequent input samples.
88* Increment is table of reference levels for computation of branch metrics:
89* branch metric = (+/-)received_sample (+/-) reference_level
90*/
91 increment[0] = -rhh[1].imag() -rhh[2].real() -rhh[3].imag() +rhh[4].real();
92 increment[1] = rhh[1].imag() -rhh[2].real() -rhh[3].imag() +rhh[4].real();
93 increment[2] = -rhh[1].imag() +rhh[2].real() -rhh[3].imag() +rhh[4].real();
94 increment[3] = rhh[1].imag() +rhh[2].real() -rhh[3].imag() +rhh[4].real();
95 increment[4] = -rhh[1].imag() -rhh[2].real() +rhh[3].imag() +rhh[4].real();
96 increment[5] = rhh[1].imag() -rhh[2].real() +rhh[3].imag() +rhh[4].real();
97 increment[6] = -rhh[1].imag() +rhh[2].real() +rhh[3].imag() +rhh[4].real();
98 increment[7] = rhh[1].imag() +rhh[2].real() +rhh[3].imag() +rhh[4].real();
99
100
101/*
102* Computation of path metrics and decisions (Add-Compare-Select).
103* It's composed of two parts: one for odd input samples (imaginary numbers)
104* and one for even samples (real numbers).
105* Each part is composed of independent (parallelisable) statements like
106* this one:
107* pm_candidate1 = old_path_metrics[0] - input_symbol_real - increment[7];
108* pm_candidate2 = old_path_metrics[8] - input_symbol_real + increment[0];
109* if(pm_candidate1 > pm_candidate2){
110* new_path_metrics[0] = pm_candidate1;
111* trans_table[sample_nr][0] = -1.0;
112* }
113* else{
114* new_path_metrics[0] = pm_candidate2;
115* trans_table[sample_nr][0] = 1.0;
116* }
117* This is very good point for optimisations (SIMD or OpenMP) as it's most time
118* consuming part of this function.
119*/
120 sample_nr=0;
121 old_path_metrics=path_metrics1;
122 new_path_metrics=path_metrics2;
123 while(sample_nr<samples_num){
124 //Processing imag states
125 real_imag=1;
126 input_symbol_imag = input[sample_nr].imag();
127
128 pm_candidate1 = old_path_metrics[0] + input_symbol_imag - increment[2];
129 pm_candidate2 = old_path_metrics[8] + input_symbol_imag + increment[5];
130 if(pm_candidate1 > pm_candidate2){
131 new_path_metrics[0] = pm_candidate1;
132 trans_table[sample_nr][0] = -1.0;
133 }
134 else{
135 new_path_metrics[0] = pm_candidate2;
136 trans_table[sample_nr][0] = 1.0;
137 }
138
139 pm_candidate1 = old_path_metrics[0] - input_symbol_imag + increment[2];
140 pm_candidate2 = old_path_metrics[8] - input_symbol_imag - increment[5];
141 if(pm_candidate1 > pm_candidate2){
142 new_path_metrics[1] = pm_candidate1;
143 trans_table[sample_nr][1] = -1.0;
144 }
145 else{
146 new_path_metrics[1] = pm_candidate2;
147 trans_table[sample_nr][1] = 1.0;
148 }
149
150 pm_candidate1 = old_path_metrics[1] + input_symbol_imag - increment[3];
151 pm_candidate2 = old_path_metrics[9] + input_symbol_imag + increment[4];
152 if(pm_candidate1 > pm_candidate2){
153 new_path_metrics[2] = pm_candidate1;
154 trans_table[sample_nr][2] = -1.0;
155 }
156 else{
157 new_path_metrics[2] = pm_candidate2;
158 trans_table[sample_nr][2] = 1.0;
159 }
160
161 pm_candidate1 = old_path_metrics[1] - input_symbol_imag + increment[3];
162 pm_candidate2 = old_path_metrics[9] - input_symbol_imag - increment[4];
163 if(pm_candidate1 > pm_candidate2){
164 new_path_metrics[3] = pm_candidate1;
165 trans_table[sample_nr][3] = -1.0;
166 }
167 else{
168 new_path_metrics[3] = pm_candidate2;
169 trans_table[sample_nr][3] = 1.0;
170 }
171
172 pm_candidate1 = old_path_metrics[2] + input_symbol_imag - increment[0];
173 pm_candidate2 = old_path_metrics[10] + input_symbol_imag + increment[7];
174 if(pm_candidate1 > pm_candidate2){
175 new_path_metrics[4] = pm_candidate1;
176 trans_table[sample_nr][4] = -1.0;
177 }
178 else{
179 new_path_metrics[4] = pm_candidate2;
180 trans_table[sample_nr][4] = 1.0;
181 }
182
183 pm_candidate1 = old_path_metrics[2] - input_symbol_imag + increment[0];
184 pm_candidate2 = old_path_metrics[10] - input_symbol_imag - increment[7];
185 if(pm_candidate1 > pm_candidate2){
186 new_path_metrics[5] = pm_candidate1;
187 trans_table[sample_nr][5] = -1.0;
188 }
189 else{
190 new_path_metrics[5] = pm_candidate2;
191 trans_table[sample_nr][5] = 1.0;
192 }
193
194 pm_candidate1 = old_path_metrics[3] + input_symbol_imag - increment[1];
195 pm_candidate2 = old_path_metrics[11] + input_symbol_imag + increment[6];
196 if(pm_candidate1 > pm_candidate2){
197 new_path_metrics[6] = pm_candidate1;
198 trans_table[sample_nr][6] = -1.0;
199 }
200 else{
201 new_path_metrics[6] = pm_candidate2;
202 trans_table[sample_nr][6] = 1.0;
203 }
204
205 pm_candidate1 = old_path_metrics[3] - input_symbol_imag + increment[1];
206 pm_candidate2 = old_path_metrics[11] - input_symbol_imag - increment[6];
207 if(pm_candidate1 > pm_candidate2){
208 new_path_metrics[7] = pm_candidate1;
209 trans_table[sample_nr][7] = -1.0;
210 }
211 else{
212 new_path_metrics[7] = pm_candidate2;
213 trans_table[sample_nr][7] = 1.0;
214 }
215
216 pm_candidate1 = old_path_metrics[4] + input_symbol_imag - increment[6];
217 pm_candidate2 = old_path_metrics[12] + input_symbol_imag + increment[1];
218 if(pm_candidate1 > pm_candidate2){
219 new_path_metrics[8] = pm_candidate1;
220 trans_table[sample_nr][8] = -1.0;
221 }
222 else{
223 new_path_metrics[8] = pm_candidate2;
224 trans_table[sample_nr][8] = 1.0;
225 }
226
227 pm_candidate1 = old_path_metrics[4] - input_symbol_imag + increment[6];
228 pm_candidate2 = old_path_metrics[12] - input_symbol_imag - increment[1];
229 if(pm_candidate1 > pm_candidate2){
230 new_path_metrics[9] = pm_candidate1;
231 trans_table[sample_nr][9] = -1.0;
232 }
233 else{
234 new_path_metrics[9] = pm_candidate2;
235 trans_table[sample_nr][9] = 1.0;
236 }
237
238 pm_candidate1 = old_path_metrics[5] + input_symbol_imag - increment[7];
239 pm_candidate2 = old_path_metrics[13] + input_symbol_imag + increment[0];
240 if(pm_candidate1 > pm_candidate2){
241 new_path_metrics[10] = pm_candidate1;
242 trans_table[sample_nr][10] = -1.0;
243 }
244 else{
245 new_path_metrics[10] = pm_candidate2;
246 trans_table[sample_nr][10] = 1.0;
247 }
248
249 pm_candidate1 = old_path_metrics[5] - input_symbol_imag + increment[7];
250 pm_candidate2 = old_path_metrics[13] - input_symbol_imag - increment[0];
251 if(pm_candidate1 > pm_candidate2){
252 new_path_metrics[11] = pm_candidate1;
253 trans_table[sample_nr][11] = -1.0;
254 }
255 else{
256 new_path_metrics[11] = pm_candidate2;
257 trans_table[sample_nr][11] = 1.0;
258 }
259
260 pm_candidate1 = old_path_metrics[6] + input_symbol_imag - increment[4];
261 pm_candidate2 = old_path_metrics[14] + input_symbol_imag + increment[3];
262 if(pm_candidate1 > pm_candidate2){
263 new_path_metrics[12] = pm_candidate1;
264 trans_table[sample_nr][12] = -1.0;
265 }
266 else{
267 new_path_metrics[12] = pm_candidate2;
268 trans_table[sample_nr][12] = 1.0;
269 }
270
271 pm_candidate1 = old_path_metrics[6] - input_symbol_imag + increment[4];
272 pm_candidate2 = old_path_metrics[14] - input_symbol_imag - increment[3];
273 if(pm_candidate1 > pm_candidate2){
274 new_path_metrics[13] = pm_candidate1;
275 trans_table[sample_nr][13] = -1.0;
276 }
277 else{
278 new_path_metrics[13] = pm_candidate2;
279 trans_table[sample_nr][13] = 1.0;
280 }
281
282 pm_candidate1 = old_path_metrics[7] + input_symbol_imag - increment[5];
283 pm_candidate2 = old_path_metrics[15] + input_symbol_imag + increment[2];
284 if(pm_candidate1 > pm_candidate2){
285 new_path_metrics[14] = pm_candidate1;
286 trans_table[sample_nr][14] = -1.0;
287 }
288 else{
289 new_path_metrics[14] = pm_candidate2;
290 trans_table[sample_nr][14] = 1.0;
291 }
292
293 pm_candidate1 = old_path_metrics[7] - input_symbol_imag + increment[5];
294 pm_candidate2 = old_path_metrics[15] - input_symbol_imag - increment[2];
295 if(pm_candidate1 > pm_candidate2){
296 new_path_metrics[15] = pm_candidate1;
297 trans_table[sample_nr][15] = -1.0;
298 }
299 else{
300 new_path_metrics[15] = pm_candidate2;
301 trans_table[sample_nr][15] = 1.0;
302 }
303 tmp=old_path_metrics;
304 old_path_metrics=new_path_metrics;
305 new_path_metrics=tmp;
306
307 sample_nr++;
308 if(sample_nr==samples_num)
309 break;
310
311 //Processing real states
312 real_imag=0;
313 input_symbol_real = input[sample_nr].real();
314
315 pm_candidate1 = old_path_metrics[0] - input_symbol_real - increment[7];
316 pm_candidate2 = old_path_metrics[8] - input_symbol_real + increment[0];
317 if(pm_candidate1 > pm_candidate2){
318 new_path_metrics[0] = pm_candidate1;
319 trans_table[sample_nr][0] = -1.0;
320 }
321 else{
322 new_path_metrics[0] = pm_candidate2;
323 trans_table[sample_nr][0] = 1.0;
324 }
325
326 pm_candidate1 = old_path_metrics[0] + input_symbol_real + increment[7];
327 pm_candidate2 = old_path_metrics[8] + input_symbol_real - increment[0];
328 if(pm_candidate1 > pm_candidate2){
329 new_path_metrics[1] = pm_candidate1;
330 trans_table[sample_nr][1] = -1.0;
331 }
332 else{
333 new_path_metrics[1] = pm_candidate2;
334 trans_table[sample_nr][1] = 1.0;
335 }
336
337 pm_candidate1 = old_path_metrics[1] - input_symbol_real - increment[6];
338 pm_candidate2 = old_path_metrics[9] - input_symbol_real + increment[1];
339 if(pm_candidate1 > pm_candidate2){
340 new_path_metrics[2] = pm_candidate1;
341 trans_table[sample_nr][2] = -1.0;
342 }
343 else{
344 new_path_metrics[2] = pm_candidate2;
345 trans_table[sample_nr][2] = 1.0;
346 }
347
348 pm_candidate1 = old_path_metrics[1] + input_symbol_real + increment[6];
349 pm_candidate2 = old_path_metrics[9] + input_symbol_real - increment[1];
350 if(pm_candidate1 > pm_candidate2){
351 new_path_metrics[3] = pm_candidate1;
352 trans_table[sample_nr][3] = -1.0;
353 }
354 else{
355 new_path_metrics[3] = pm_candidate2;
356 trans_table[sample_nr][3] = 1.0;
357 }
358
359 pm_candidate1 = old_path_metrics[2] - input_symbol_real - increment[5];
360 pm_candidate2 = old_path_metrics[10] - input_symbol_real + increment[2];
361 if(pm_candidate1 > pm_candidate2){
362 new_path_metrics[4] = pm_candidate1;
363 trans_table[sample_nr][4] = -1.0;
364 }
365 else{
366 new_path_metrics[4] = pm_candidate2;
367 trans_table[sample_nr][4] = 1.0;
368 }
369
370 pm_candidate1 = old_path_metrics[2] + input_symbol_real + increment[5];
371 pm_candidate2 = old_path_metrics[10] + input_symbol_real - increment[2];
372 if(pm_candidate1 > pm_candidate2){
373 new_path_metrics[5] = pm_candidate1;
374 trans_table[sample_nr][5] = -1.0;
375 }
376 else{
377 new_path_metrics[5] = pm_candidate2;
378 trans_table[sample_nr][5] = 1.0;
379 }
380
381 pm_candidate1 = old_path_metrics[3] - input_symbol_real - increment[4];
382 pm_candidate2 = old_path_metrics[11] - input_symbol_real + increment[3];
383 if(pm_candidate1 > pm_candidate2){
384 new_path_metrics[6] = pm_candidate1;
385 trans_table[sample_nr][6] = -1.0;
386 }
387 else{
388 new_path_metrics[6] = pm_candidate2;
389 trans_table[sample_nr][6] = 1.0;
390 }
391
392 pm_candidate1 = old_path_metrics[3] + input_symbol_real + increment[4];
393 pm_candidate2 = old_path_metrics[11] + input_symbol_real - increment[3];
394 if(pm_candidate1 > pm_candidate2){
395 new_path_metrics[7] = pm_candidate1;
396 trans_table[sample_nr][7] = -1.0;
397 }
398 else{
399 new_path_metrics[7] = pm_candidate2;
400 trans_table[sample_nr][7] = 1.0;
401 }
402
403 pm_candidate1 = old_path_metrics[4] - input_symbol_real - increment[3];
404 pm_candidate2 = old_path_metrics[12] - input_symbol_real + increment[4];
405 if(pm_candidate1 > pm_candidate2){
406 new_path_metrics[8] = pm_candidate1;
407 trans_table[sample_nr][8] = -1.0;
408 }
409 else{
410 new_path_metrics[8] = pm_candidate2;
411 trans_table[sample_nr][8] = 1.0;
412 }
413
414 pm_candidate1 = old_path_metrics[4] + input_symbol_real + increment[3];
415 pm_candidate2 = old_path_metrics[12] + input_symbol_real - increment[4];
416 if(pm_candidate1 > pm_candidate2){
417 new_path_metrics[9] = pm_candidate1;
418 trans_table[sample_nr][9] = -1.0;
419 }
420 else{
421 new_path_metrics[9] = pm_candidate2;
422 trans_table[sample_nr][9] = 1.0;
423 }
424
425 pm_candidate1 = old_path_metrics[5] - input_symbol_real - increment[2];
426 pm_candidate2 = old_path_metrics[13] - input_symbol_real + increment[5];
427 if(pm_candidate1 > pm_candidate2){
428 new_path_metrics[10] = pm_candidate1;
429 trans_table[sample_nr][10] = -1.0;
430 }
431 else{
432 new_path_metrics[10] = pm_candidate2;
433 trans_table[sample_nr][10] = 1.0;
434 }
435
436 pm_candidate1 = old_path_metrics[5] + input_symbol_real + increment[2];
437 pm_candidate2 = old_path_metrics[13] + input_symbol_real - increment[5];
438 if(pm_candidate1 > pm_candidate2){
439 new_path_metrics[11] = pm_candidate1;
440 trans_table[sample_nr][11] = -1.0;
441 }
442 else{
443 new_path_metrics[11] = pm_candidate2;
444 trans_table[sample_nr][11] = 1.0;
445 }
446
447 pm_candidate1 = old_path_metrics[6] - input_symbol_real - increment[1];
448 pm_candidate2 = old_path_metrics[14] - input_symbol_real + increment[6];
449 if(pm_candidate1 > pm_candidate2){
450 new_path_metrics[12] = pm_candidate1;
451 trans_table[sample_nr][12] = -1.0;
452 }
453 else{
454 new_path_metrics[12] = pm_candidate2;
455 trans_table[sample_nr][12] = 1.0;
456 }
457
458 pm_candidate1 = old_path_metrics[6] + input_symbol_real + increment[1];
459 pm_candidate2 = old_path_metrics[14] + input_symbol_real - increment[6];
460 if(pm_candidate1 > pm_candidate2){
461 new_path_metrics[13] = pm_candidate1;
462 trans_table[sample_nr][13] = -1.0;
463 }
464 else{
465 new_path_metrics[13] = pm_candidate2;
466 trans_table[sample_nr][13] = 1.0;
467 }
468
469 pm_candidate1 = old_path_metrics[7] - input_symbol_real - increment[0];
470 pm_candidate2 = old_path_metrics[15] - input_symbol_real + increment[7];
471 if(pm_candidate1 > pm_candidate2){
472 new_path_metrics[14] = pm_candidate1;
473 trans_table[sample_nr][14] = -1.0;
474 }
475 else{
476 new_path_metrics[14] = pm_candidate2;
477 trans_table[sample_nr][14] = 1.0;
478 }
479
480 pm_candidate1 = old_path_metrics[7] + input_symbol_real + increment[0];
481 pm_candidate2 = old_path_metrics[15] + input_symbol_real - increment[7];
482 if(pm_candidate1 > pm_candidate2){
483 new_path_metrics[15] = pm_candidate1;
484 trans_table[sample_nr][15] = -1.0;
485 }
486 else{
487 new_path_metrics[15] = pm_candidate2;
488 trans_table[sample_nr][15] = 1.0;
489 }
490 tmp=old_path_metrics;
491 old_path_metrics=new_path_metrics;
492 new_path_metrics=tmp;
493
494 sample_nr++;
495 }
496
497/*
498* Find the best from the stop states by comparing their path metrics.
499* Not every stop state is always possible, so we are searching in
500* a subset of them.
501*/
502 unsigned int best_stop_state;
503 float stop_state_metric, max_stop_state_metric;
504 best_stop_state = stop_states[0];
505 max_stop_state_metric = old_path_metrics[best_stop_state];
506 for(i=1; i< stops_num; i++){
507 stop_state_metric = old_path_metrics[stop_states[i]];
508 if(stop_state_metric > max_stop_state_metric){
509 max_stop_state_metric = stop_state_metric;
510 best_stop_state = stop_states[i];
511 }
512 }
513
514/*
515* This table was generated with hope that it gives a litle speedup during
516* traceback stage.
517* Received bit is related to the number of state in the trellis.
518* I've numbered states so their parity (number of ones) is related
519* to a received bit.
520*/
521 static const unsigned int parity_table[PATHS_NUM] = { 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, };
522
523/*
524* Table of previous states in the trellis diagram.
525* For GMSK modulation every state has two previous states.
526* Example:
527* previous_state_nr1 = prev_table[current_state_nr][0]
528* previous_state_nr2 = prev_table[current_state_nr][1]
529*/
530 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}, };
531
532/*
533* Traceback and differential decoding of received sequence.
534* Decisions stored in trans_table are used to restore best path in the trellis.
535*/
536 sample_nr=samples_num;
537 unsigned int state_nr=best_stop_state;
538 unsigned int decision;
539 bool out_bit=0;
540
541 while(sample_nr>0){
542 sample_nr--;
543 decision = (trans_table[sample_nr][state_nr]>0);
544
545 if(decision != out_bit)
546 output[sample_nr]=-trans_table[sample_nr][state_nr];
547 else
548 output[sample_nr]=trans_table[sample_nr][state_nr];
549
550 out_bit = out_bit ^ real_imag ^ parity_table[state_nr];
551 state_nr = prev_table[state_nr][decision];
552 real_imag = !real_imag;
553 }
554}