blob: 3dce379a44797d30e9a58ff7f7d0687e1fe2f96f [file] [log] [blame]
piotr437f5462014-02-04 17:57:25 +01001/* -*- c++ -*- */
2/*
3 * @file
Piotr Krysika6268a52017-08-23 16:02:19 +02004 * @author (C) 2009 by Piotr Krysik <ptrkrysik@gmail.com>
piotr437f5462014-02-04 17:57:25 +01005 * @section LICENSE
6 *
ptrkrysik529895b2014-12-02 18:07:38 +01007 * Gr-gsm is free software; you can redistribute it and/or modify
piotr437f5462014-02-04 17:57:25 +01008 * 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 *
ptrkrysik529895b2014-12-02 18:07:38 +010012 * Gr-gsm is distributed in the hope that it will be useful,
piotr437f5462014-02-04 17:57:25 +010013 * 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
ptrkrysik529895b2014-12-02 18:07:38 +010018 * along with gr-gsm; see the file COPYING. If not, write to
piotr437f5462014-02-04 17:57:25 +010019 * 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>
Piotr Krysikd7efc052017-11-07 19:33:22 +010059#include <grgsm/gsm_constants.h>
piotrebd06232014-05-02 17:28:21 +020060#include <cmath>
61
piotr437f5462014-02-04 17:57:25 +010062#define PATHS_NUM (1 << (CHAN_IMP_RESP_LENGTH-1))
63
64void 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)
65{
66 float increment[8];
67 float path_metrics1[16];
68 float path_metrics2[16];
piotrf0039b42014-05-05 16:45:48 +020069 float paths_difference;
piotr437f5462014-02-04 17:57:25 +010070 float * new_path_metrics;
71 float * old_path_metrics;
72 float * tmp;
73 float trans_table[BURST_SIZE][16];
74 float pm_candidate1, pm_candidate2;
75 bool real_imag;
76 float input_symbol_real, input_symbol_imag;
77 unsigned int i, sample_nr;
78
79/*
80* Setup first path metrics, so only state pointed by start_state is possible.
81* Start_state metric is equal to zero, the rest is written with some very low value,
82* which makes them practically impossible to occur.
83*/
84 for(i=0; i<PATHS_NUM; i++){
85 path_metrics1[i]=(-10e30);
86 }
87 path_metrics1[start_state]=0;
88
89/*
90* Compute Increment - a table of values which does not change for subsequent input samples.
91* Increment is table of reference levels for computation of branch metrics:
92* branch metric = (+/-)received_sample (+/-) reference_level
93*/
94 increment[0] = -rhh[1].imag() -rhh[2].real() -rhh[3].imag() +rhh[4].real();
95 increment[1] = rhh[1].imag() -rhh[2].real() -rhh[3].imag() +rhh[4].real();
96 increment[2] = -rhh[1].imag() +rhh[2].real() -rhh[3].imag() +rhh[4].real();
97 increment[3] = rhh[1].imag() +rhh[2].real() -rhh[3].imag() +rhh[4].real();
98 increment[4] = -rhh[1].imag() -rhh[2].real() +rhh[3].imag() +rhh[4].real();
99 increment[5] = rhh[1].imag() -rhh[2].real() +rhh[3].imag() +rhh[4].real();
100 increment[6] = -rhh[1].imag() +rhh[2].real() +rhh[3].imag() +rhh[4].real();
101 increment[7] = rhh[1].imag() +rhh[2].real() +rhh[3].imag() +rhh[4].real();
102
103
104/*
105* Computation of path metrics and decisions (Add-Compare-Select).
106* It's composed of two parts: one for odd input samples (imaginary numbers)
107* and one for even samples (real numbers).
108* Each part is composed of independent (parallelisable) statements like
109* this one:
piotrf0039b42014-05-05 16:45:48 +0200110* pm_candidate1 = old_path_metrics[0] -input_symbol_imag +increment[2];
111* pm_candidate2 = old_path_metrics[8] -input_symbol_imag -increment[5];
112* paths_difference=pm_candidate2-pm_candidate1;
113* new_path_metrics[1]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
114* trans_table[sample_nr][1] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100115* This is very good point for optimisations (SIMD or OpenMP) as it's most time
116* consuming part of this function.
117*/
118 sample_nr=0;
119 old_path_metrics=path_metrics1;
120 new_path_metrics=path_metrics2;
121 while(sample_nr<samples_num){
122 //Processing imag states
123 real_imag=1;
124 input_symbol_imag = input[sample_nr].imag();
125
piotrebd06232014-05-02 17:28:21 +0200126 pm_candidate1 = old_path_metrics[0] +input_symbol_imag -increment[2];
127 pm_candidate2 = old_path_metrics[8] +input_symbol_imag +increment[5];
piotrf0039b42014-05-05 16:45:48 +0200128 paths_difference=pm_candidate2-pm_candidate1;
129 new_path_metrics[0]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
130 trans_table[sample_nr][0] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100131
piotrebd06232014-05-02 17:28:21 +0200132 pm_candidate1 = old_path_metrics[0] -input_symbol_imag +increment[2];
133 pm_candidate2 = old_path_metrics[8] -input_symbol_imag -increment[5];
piotrf0039b42014-05-05 16:45:48 +0200134 paths_difference=pm_candidate2-pm_candidate1;
135 new_path_metrics[1]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
136 trans_table[sample_nr][1] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100137
piotrebd06232014-05-02 17:28:21 +0200138 pm_candidate1 = old_path_metrics[1] +input_symbol_imag -increment[3];
139 pm_candidate2 = old_path_metrics[9] +input_symbol_imag +increment[4];
piotrf0039b42014-05-05 16:45:48 +0200140 paths_difference=pm_candidate2-pm_candidate1;
141 new_path_metrics[2]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
142 trans_table[sample_nr][2] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100143
piotrebd06232014-05-02 17:28:21 +0200144 pm_candidate1 = old_path_metrics[1] -input_symbol_imag +increment[3];
145 pm_candidate2 = old_path_metrics[9] -input_symbol_imag -increment[4];
piotrf0039b42014-05-05 16:45:48 +0200146 paths_difference=pm_candidate2-pm_candidate1;
147 new_path_metrics[3]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
148 trans_table[sample_nr][3] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100149
piotrebd06232014-05-02 17:28:21 +0200150 pm_candidate1 = old_path_metrics[2] +input_symbol_imag -increment[0];
151 pm_candidate2 = old_path_metrics[10] +input_symbol_imag +increment[7];
piotrf0039b42014-05-05 16:45:48 +0200152 paths_difference=pm_candidate2-pm_candidate1;
153 new_path_metrics[4]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
154 trans_table[sample_nr][4] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100155
piotrebd06232014-05-02 17:28:21 +0200156 pm_candidate1 = old_path_metrics[2] -input_symbol_imag +increment[0];
157 pm_candidate2 = old_path_metrics[10] -input_symbol_imag -increment[7];
piotrf0039b42014-05-05 16:45:48 +0200158 paths_difference=pm_candidate2-pm_candidate1;
159 new_path_metrics[5]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
160 trans_table[sample_nr][5] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100161
piotrebd06232014-05-02 17:28:21 +0200162 pm_candidate1 = old_path_metrics[3] +input_symbol_imag -increment[1];
163 pm_candidate2 = old_path_metrics[11] +input_symbol_imag +increment[6];
piotrf0039b42014-05-05 16:45:48 +0200164 paths_difference=pm_candidate2-pm_candidate1;
165 new_path_metrics[6]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
166 trans_table[sample_nr][6] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100167
piotrebd06232014-05-02 17:28:21 +0200168 pm_candidate1 = old_path_metrics[3] -input_symbol_imag +increment[1];
169 pm_candidate2 = old_path_metrics[11] -input_symbol_imag -increment[6];
piotrf0039b42014-05-05 16:45:48 +0200170 paths_difference=pm_candidate2-pm_candidate1;
171 new_path_metrics[7]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
172 trans_table[sample_nr][7] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100173
piotrebd06232014-05-02 17:28:21 +0200174 pm_candidate1 = old_path_metrics[4] +input_symbol_imag -increment[6];
175 pm_candidate2 = old_path_metrics[12] +input_symbol_imag +increment[1];
piotrf0039b42014-05-05 16:45:48 +0200176 paths_difference=pm_candidate2-pm_candidate1;
177 new_path_metrics[8]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
178 trans_table[sample_nr][8] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100179
piotrebd06232014-05-02 17:28:21 +0200180 pm_candidate1 = old_path_metrics[4] -input_symbol_imag +increment[6];
181 pm_candidate2 = old_path_metrics[12] -input_symbol_imag -increment[1];
piotrf0039b42014-05-05 16:45:48 +0200182 paths_difference=pm_candidate2-pm_candidate1;
183 new_path_metrics[9]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
184 trans_table[sample_nr][9] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100185
piotrebd06232014-05-02 17:28:21 +0200186 pm_candidate1 = old_path_metrics[5] +input_symbol_imag -increment[7];
187 pm_candidate2 = old_path_metrics[13] +input_symbol_imag +increment[0];
piotrf0039b42014-05-05 16:45:48 +0200188 paths_difference=pm_candidate2-pm_candidate1;
189 new_path_metrics[10]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
190 trans_table[sample_nr][10] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100191
piotrebd06232014-05-02 17:28:21 +0200192 pm_candidate1 = old_path_metrics[5] -input_symbol_imag +increment[7];
193 pm_candidate2 = old_path_metrics[13] -input_symbol_imag -increment[0];
piotrf0039b42014-05-05 16:45:48 +0200194 paths_difference=pm_candidate2-pm_candidate1;
195 new_path_metrics[11]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
196 trans_table[sample_nr][11] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100197
piotrebd06232014-05-02 17:28:21 +0200198 pm_candidate1 = old_path_metrics[6] +input_symbol_imag -increment[4];
199 pm_candidate2 = old_path_metrics[14] +input_symbol_imag +increment[3];
piotrf0039b42014-05-05 16:45:48 +0200200 paths_difference=pm_candidate2-pm_candidate1;
201 new_path_metrics[12]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
202 trans_table[sample_nr][12] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100203
piotrebd06232014-05-02 17:28:21 +0200204 pm_candidate1 = old_path_metrics[6] -input_symbol_imag +increment[4];
205 pm_candidate2 = old_path_metrics[14] -input_symbol_imag -increment[3];
piotrf0039b42014-05-05 16:45:48 +0200206 paths_difference=pm_candidate2-pm_candidate1;
207 new_path_metrics[13]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
208 trans_table[sample_nr][13] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100209
piotrebd06232014-05-02 17:28:21 +0200210 pm_candidate1 = old_path_metrics[7] +input_symbol_imag -increment[5];
211 pm_candidate2 = old_path_metrics[15] +input_symbol_imag +increment[2];
piotrf0039b42014-05-05 16:45:48 +0200212 paths_difference=pm_candidate2-pm_candidate1;
213 new_path_metrics[14]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
214 trans_table[sample_nr][14] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100215
piotrebd06232014-05-02 17:28:21 +0200216 pm_candidate1 = old_path_metrics[7] -input_symbol_imag +increment[5];
217 pm_candidate2 = old_path_metrics[15] -input_symbol_imag -increment[2];
piotrf0039b42014-05-05 16:45:48 +0200218 paths_difference=pm_candidate2-pm_candidate1;
219 new_path_metrics[15]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
220 trans_table[sample_nr][15] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100221 tmp=old_path_metrics;
222 old_path_metrics=new_path_metrics;
223 new_path_metrics=tmp;
224
225 sample_nr++;
226 if(sample_nr==samples_num)
227 break;
228
229 //Processing real states
230 real_imag=0;
231 input_symbol_real = input[sample_nr].real();
232
piotrebd06232014-05-02 17:28:21 +0200233 pm_candidate1 = old_path_metrics[0] -input_symbol_real -increment[7];
234 pm_candidate2 = old_path_metrics[8] -input_symbol_real +increment[0];
piotrf0039b42014-05-05 16:45:48 +0200235 paths_difference=pm_candidate2-pm_candidate1;
236 new_path_metrics[0]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
237 trans_table[sample_nr][0] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100238
piotrebd06232014-05-02 17:28:21 +0200239 pm_candidate1 = old_path_metrics[0] +input_symbol_real +increment[7];
240 pm_candidate2 = old_path_metrics[8] +input_symbol_real -increment[0];
piotrf0039b42014-05-05 16:45:48 +0200241 paths_difference=pm_candidate2-pm_candidate1;
242 new_path_metrics[1]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
243 trans_table[sample_nr][1] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100244
piotrebd06232014-05-02 17:28:21 +0200245 pm_candidate1 = old_path_metrics[1] -input_symbol_real -increment[6];
246 pm_candidate2 = old_path_metrics[9] -input_symbol_real +increment[1];
piotrf0039b42014-05-05 16:45:48 +0200247 paths_difference=pm_candidate2-pm_candidate1;
248 new_path_metrics[2]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
249 trans_table[sample_nr][2] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100250
piotrebd06232014-05-02 17:28:21 +0200251 pm_candidate1 = old_path_metrics[1] +input_symbol_real +increment[6];
252 pm_candidate2 = old_path_metrics[9] +input_symbol_real -increment[1];
piotrf0039b42014-05-05 16:45:48 +0200253 paths_difference=pm_candidate2-pm_candidate1;
254 new_path_metrics[3]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
255 trans_table[sample_nr][3] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100256
piotrebd06232014-05-02 17:28:21 +0200257 pm_candidate1 = old_path_metrics[2] -input_symbol_real -increment[5];
258 pm_candidate2 = old_path_metrics[10] -input_symbol_real +increment[2];
piotrf0039b42014-05-05 16:45:48 +0200259 paths_difference=pm_candidate2-pm_candidate1;
260 new_path_metrics[4]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
261 trans_table[sample_nr][4] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100262
piotrebd06232014-05-02 17:28:21 +0200263 pm_candidate1 = old_path_metrics[2] +input_symbol_real +increment[5];
264 pm_candidate2 = old_path_metrics[10] +input_symbol_real -increment[2];
piotrf0039b42014-05-05 16:45:48 +0200265 paths_difference=pm_candidate2-pm_candidate1;
266 new_path_metrics[5]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
267 trans_table[sample_nr][5] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100268
piotrebd06232014-05-02 17:28:21 +0200269 pm_candidate1 = old_path_metrics[3] -input_symbol_real -increment[4];
270 pm_candidate2 = old_path_metrics[11] -input_symbol_real +increment[3];
piotrf0039b42014-05-05 16:45:48 +0200271 paths_difference=pm_candidate2-pm_candidate1;
272 new_path_metrics[6]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
273 trans_table[sample_nr][6] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100274
piotrebd06232014-05-02 17:28:21 +0200275 pm_candidate1 = old_path_metrics[3] +input_symbol_real +increment[4];
276 pm_candidate2 = old_path_metrics[11] +input_symbol_real -increment[3];
piotrf0039b42014-05-05 16:45:48 +0200277 paths_difference=pm_candidate2-pm_candidate1;
278 new_path_metrics[7]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
279 trans_table[sample_nr][7] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100280
piotrebd06232014-05-02 17:28:21 +0200281 pm_candidate1 = old_path_metrics[4] -input_symbol_real -increment[3];
282 pm_candidate2 = old_path_metrics[12] -input_symbol_real +increment[4];
piotrf0039b42014-05-05 16:45:48 +0200283 paths_difference=pm_candidate2-pm_candidate1;
284 new_path_metrics[8]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
285 trans_table[sample_nr][8] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100286
piotrebd06232014-05-02 17:28:21 +0200287 pm_candidate1 = old_path_metrics[4] +input_symbol_real +increment[3];
288 pm_candidate2 = old_path_metrics[12] +input_symbol_real -increment[4];
piotrf0039b42014-05-05 16:45:48 +0200289 paths_difference=pm_candidate2-pm_candidate1;
290 new_path_metrics[9]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
291 trans_table[sample_nr][9] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100292
piotrebd06232014-05-02 17:28:21 +0200293 pm_candidate1 = old_path_metrics[5] -input_symbol_real -increment[2];
294 pm_candidate2 = old_path_metrics[13] -input_symbol_real +increment[5];
piotrf0039b42014-05-05 16:45:48 +0200295 paths_difference=pm_candidate2-pm_candidate1;
296 new_path_metrics[10]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
297 trans_table[sample_nr][10] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100298
piotrebd06232014-05-02 17:28:21 +0200299 pm_candidate1 = old_path_metrics[5] +input_symbol_real +increment[2];
300 pm_candidate2 = old_path_metrics[13] +input_symbol_real -increment[5];
piotrf0039b42014-05-05 16:45:48 +0200301 paths_difference=pm_candidate2-pm_candidate1;
302 new_path_metrics[11]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
303 trans_table[sample_nr][11] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100304
piotrebd06232014-05-02 17:28:21 +0200305 pm_candidate1 = old_path_metrics[6] -input_symbol_real -increment[1];
306 pm_candidate2 = old_path_metrics[14] -input_symbol_real +increment[6];
piotrf0039b42014-05-05 16:45:48 +0200307 paths_difference=pm_candidate2-pm_candidate1;
308 new_path_metrics[12]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
309 trans_table[sample_nr][12] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100310
piotrebd06232014-05-02 17:28:21 +0200311 pm_candidate1 = old_path_metrics[6] +input_symbol_real +increment[1];
312 pm_candidate2 = old_path_metrics[14] +input_symbol_real -increment[6];
piotrf0039b42014-05-05 16:45:48 +0200313 paths_difference=pm_candidate2-pm_candidate1;
314 new_path_metrics[13]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
315 trans_table[sample_nr][13] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100316
piotrebd06232014-05-02 17:28:21 +0200317 pm_candidate1 = old_path_metrics[7] -input_symbol_real -increment[0];
318 pm_candidate2 = old_path_metrics[15] -input_symbol_real +increment[7];
piotrf0039b42014-05-05 16:45:48 +0200319 paths_difference=pm_candidate2-pm_candidate1;
320 new_path_metrics[14]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
321 trans_table[sample_nr][14] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100322
piotrebd06232014-05-02 17:28:21 +0200323 pm_candidate1 = old_path_metrics[7] +input_symbol_real +increment[0];
324 pm_candidate2 = old_path_metrics[15] +input_symbol_real -increment[7];
piotrf0039b42014-05-05 16:45:48 +0200325 paths_difference=pm_candidate2-pm_candidate1;
326 new_path_metrics[15]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
327 trans_table[sample_nr][15] = paths_difference;
328
piotr437f5462014-02-04 17:57:25 +0100329 tmp=old_path_metrics;
330 old_path_metrics=new_path_metrics;
331 new_path_metrics=tmp;
332
333 sample_nr++;
334 }
335
336/*
337* Find the best from the stop states by comparing their path metrics.
338* Not every stop state is always possible, so we are searching in
339* a subset of them.
340*/
341 unsigned int best_stop_state;
342 float stop_state_metric, max_stop_state_metric;
343 best_stop_state = stop_states[0];
344 max_stop_state_metric = old_path_metrics[best_stop_state];
345 for(i=1; i< stops_num; i++){
346 stop_state_metric = old_path_metrics[stop_states[i]];
347 if(stop_state_metric > max_stop_state_metric){
348 max_stop_state_metric = stop_state_metric;
349 best_stop_state = stop_states[i];
350 }
351 }
352
353/*
354* This table was generated with hope that it gives a litle speedup during
355* traceback stage.
356* Received bit is related to the number of state in the trellis.
357* I've numbered states so their parity (number of ones) is related
358* to a received bit.
359*/
360 static const unsigned int parity_table[PATHS_NUM] = { 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, };
361
362/*
363* Table of previous states in the trellis diagram.
364* For GMSK modulation every state has two previous states.
365* Example:
366* previous_state_nr1 = prev_table[current_state_nr][0]
367* previous_state_nr2 = prev_table[current_state_nr][1]
368*/
369 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}, };
370
371/*
372* Traceback and differential decoding of received sequence.
373* Decisions stored in trans_table are used to restore best path in the trellis.
374*/
375 sample_nr=samples_num;
376 unsigned int state_nr=best_stop_state;
377 unsigned int decision;
378 bool out_bit=0;
379
380 while(sample_nr>0){
381 sample_nr--;
382 decision = (trans_table[sample_nr][state_nr]>0);
piotrf0039b42014-05-05 16:45:48 +0200383
384 if(decision != out_bit)
385 output[sample_nr]=-trans_table[sample_nr][state_nr];
386 else
387 output[sample_nr]=trans_table[sample_nr][state_nr];
388
piotr437f5462014-02-04 17:57:25 +0100389 out_bit = out_bit ^ real_imag ^ parity_table[state_nr];
390 state_nr = prev_table[state_nr][decision];
391 real_imag = !real_imag;
392 }
393}