blob: 6a64a2c2cb7a3e5c6ba65eb4012316a7eb50417f [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>
59#include <gsm_constants.h>
piotrebd06232014-05-02 17:28:21 +020060
piotr437f5462014-02-04 17:57:25 +010061#define PATHS_NUM (1 << (CHAN_IMP_RESP_LENGTH-1))
62
63void 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)
64{
65 float increment[8];
66 float path_metrics1[16];
67 float path_metrics2[16];
piotrf0039b42014-05-05 16:45:48 +020068 float paths_difference;
piotr437f5462014-02-04 17:57:25 +010069 float * new_path_metrics;
70 float * old_path_metrics;
71 float * tmp;
72 float trans_table[BURST_SIZE][16];
73 float pm_candidate1, pm_candidate2;
74 bool real_imag;
75 float input_symbol_real, input_symbol_imag;
76 unsigned int i, sample_nr;
77
78/*
79* Setup first path metrics, so only state pointed by start_state is possible.
80* Start_state metric is equal to zero, the rest is written with some very low value,
81* which makes them practically impossible to occur.
82*/
83 for(i=0; i<PATHS_NUM; i++){
84 path_metrics1[i]=(-10e30);
85 }
86 path_metrics1[start_state]=0;
87
88/*
89* Compute Increment - a table of values which does not change for subsequent input samples.
90* Increment is table of reference levels for computation of branch metrics:
91* branch metric = (+/-)received_sample (+/-) reference_level
92*/
93 increment[0] = -rhh[1].imag() -rhh[2].real() -rhh[3].imag() +rhh[4].real();
94 increment[1] = rhh[1].imag() -rhh[2].real() -rhh[3].imag() +rhh[4].real();
95 increment[2] = -rhh[1].imag() +rhh[2].real() -rhh[3].imag() +rhh[4].real();
96 increment[3] = rhh[1].imag() +rhh[2].real() -rhh[3].imag() +rhh[4].real();
97 increment[4] = -rhh[1].imag() -rhh[2].real() +rhh[3].imag() +rhh[4].real();
98 increment[5] = rhh[1].imag() -rhh[2].real() +rhh[3].imag() +rhh[4].real();
99 increment[6] = -rhh[1].imag() +rhh[2].real() +rhh[3].imag() +rhh[4].real();
100 increment[7] = rhh[1].imag() +rhh[2].real() +rhh[3].imag() +rhh[4].real();
101
102
103/*
104* Computation of path metrics and decisions (Add-Compare-Select).
105* It's composed of two parts: one for odd input samples (imaginary numbers)
106* and one for even samples (real numbers).
107* Each part is composed of independent (parallelisable) statements like
108* this one:
piotrf0039b42014-05-05 16:45:48 +0200109* pm_candidate1 = old_path_metrics[0] -input_symbol_imag +increment[2];
110* pm_candidate2 = old_path_metrics[8] -input_symbol_imag -increment[5];
111* paths_difference=pm_candidate2-pm_candidate1;
112* new_path_metrics[1]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
113* trans_table[sample_nr][1] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100114* This is very good point for optimisations (SIMD or OpenMP) as it's most time
115* consuming part of this function.
116*/
117 sample_nr=0;
118 old_path_metrics=path_metrics1;
119 new_path_metrics=path_metrics2;
120 while(sample_nr<samples_num){
121 //Processing imag states
122 real_imag=1;
123 input_symbol_imag = input[sample_nr].imag();
124
piotrebd06232014-05-02 17:28:21 +0200125 pm_candidate1 = old_path_metrics[0] +input_symbol_imag -increment[2];
126 pm_candidate2 = old_path_metrics[8] +input_symbol_imag +increment[5];
piotrf0039b42014-05-05 16:45:48 +0200127 paths_difference=pm_candidate2-pm_candidate1;
128 new_path_metrics[0]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
129 trans_table[sample_nr][0] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100130
piotrebd06232014-05-02 17:28:21 +0200131 pm_candidate1 = old_path_metrics[0] -input_symbol_imag +increment[2];
132 pm_candidate2 = old_path_metrics[8] -input_symbol_imag -increment[5];
piotrf0039b42014-05-05 16:45:48 +0200133 paths_difference=pm_candidate2-pm_candidate1;
134 new_path_metrics[1]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
135 trans_table[sample_nr][1] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100136
piotrebd06232014-05-02 17:28:21 +0200137 pm_candidate1 = old_path_metrics[1] +input_symbol_imag -increment[3];
138 pm_candidate2 = old_path_metrics[9] +input_symbol_imag +increment[4];
piotrf0039b42014-05-05 16:45:48 +0200139 paths_difference=pm_candidate2-pm_candidate1;
140 new_path_metrics[2]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
141 trans_table[sample_nr][2] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100142
piotrebd06232014-05-02 17:28:21 +0200143 pm_candidate1 = old_path_metrics[1] -input_symbol_imag +increment[3];
144 pm_candidate2 = old_path_metrics[9] -input_symbol_imag -increment[4];
piotrf0039b42014-05-05 16:45:48 +0200145 paths_difference=pm_candidate2-pm_candidate1;
146 new_path_metrics[3]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
147 trans_table[sample_nr][3] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100148
piotrebd06232014-05-02 17:28:21 +0200149 pm_candidate1 = old_path_metrics[2] +input_symbol_imag -increment[0];
150 pm_candidate2 = old_path_metrics[10] +input_symbol_imag +increment[7];
piotrf0039b42014-05-05 16:45:48 +0200151 paths_difference=pm_candidate2-pm_candidate1;
152 new_path_metrics[4]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
153 trans_table[sample_nr][4] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100154
piotrebd06232014-05-02 17:28:21 +0200155 pm_candidate1 = old_path_metrics[2] -input_symbol_imag +increment[0];
156 pm_candidate2 = old_path_metrics[10] -input_symbol_imag -increment[7];
piotrf0039b42014-05-05 16:45:48 +0200157 paths_difference=pm_candidate2-pm_candidate1;
158 new_path_metrics[5]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
159 trans_table[sample_nr][5] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100160
piotrebd06232014-05-02 17:28:21 +0200161 pm_candidate1 = old_path_metrics[3] +input_symbol_imag -increment[1];
162 pm_candidate2 = old_path_metrics[11] +input_symbol_imag +increment[6];
piotrf0039b42014-05-05 16:45:48 +0200163 paths_difference=pm_candidate2-pm_candidate1;
164 new_path_metrics[6]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
165 trans_table[sample_nr][6] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100166
piotrebd06232014-05-02 17:28:21 +0200167 pm_candidate1 = old_path_metrics[3] -input_symbol_imag +increment[1];
168 pm_candidate2 = old_path_metrics[11] -input_symbol_imag -increment[6];
piotrf0039b42014-05-05 16:45:48 +0200169 paths_difference=pm_candidate2-pm_candidate1;
170 new_path_metrics[7]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
171 trans_table[sample_nr][7] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100172
piotrebd06232014-05-02 17:28:21 +0200173 pm_candidate1 = old_path_metrics[4] +input_symbol_imag -increment[6];
174 pm_candidate2 = old_path_metrics[12] +input_symbol_imag +increment[1];
piotrf0039b42014-05-05 16:45:48 +0200175 paths_difference=pm_candidate2-pm_candidate1;
176 new_path_metrics[8]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
177 trans_table[sample_nr][8] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100178
piotrebd06232014-05-02 17:28:21 +0200179 pm_candidate1 = old_path_metrics[4] -input_symbol_imag +increment[6];
180 pm_candidate2 = old_path_metrics[12] -input_symbol_imag -increment[1];
piotrf0039b42014-05-05 16:45:48 +0200181 paths_difference=pm_candidate2-pm_candidate1;
182 new_path_metrics[9]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
183 trans_table[sample_nr][9] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100184
piotrebd06232014-05-02 17:28:21 +0200185 pm_candidate1 = old_path_metrics[5] +input_symbol_imag -increment[7];
186 pm_candidate2 = old_path_metrics[13] +input_symbol_imag +increment[0];
piotrf0039b42014-05-05 16:45:48 +0200187 paths_difference=pm_candidate2-pm_candidate1;
188 new_path_metrics[10]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
189 trans_table[sample_nr][10] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100190
piotrebd06232014-05-02 17:28:21 +0200191 pm_candidate1 = old_path_metrics[5] -input_symbol_imag +increment[7];
192 pm_candidate2 = old_path_metrics[13] -input_symbol_imag -increment[0];
piotrf0039b42014-05-05 16:45:48 +0200193 paths_difference=pm_candidate2-pm_candidate1;
194 new_path_metrics[11]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
195 trans_table[sample_nr][11] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100196
piotrebd06232014-05-02 17:28:21 +0200197 pm_candidate1 = old_path_metrics[6] +input_symbol_imag -increment[4];
198 pm_candidate2 = old_path_metrics[14] +input_symbol_imag +increment[3];
piotrf0039b42014-05-05 16:45:48 +0200199 paths_difference=pm_candidate2-pm_candidate1;
200 new_path_metrics[12]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
201 trans_table[sample_nr][12] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100202
piotrebd06232014-05-02 17:28:21 +0200203 pm_candidate1 = old_path_metrics[6] -input_symbol_imag +increment[4];
204 pm_candidate2 = old_path_metrics[14] -input_symbol_imag -increment[3];
piotrf0039b42014-05-05 16:45:48 +0200205 paths_difference=pm_candidate2-pm_candidate1;
206 new_path_metrics[13]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
207 trans_table[sample_nr][13] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100208
piotrebd06232014-05-02 17:28:21 +0200209 pm_candidate1 = old_path_metrics[7] +input_symbol_imag -increment[5];
210 pm_candidate2 = old_path_metrics[15] +input_symbol_imag +increment[2];
piotrf0039b42014-05-05 16:45:48 +0200211 paths_difference=pm_candidate2-pm_candidate1;
212 new_path_metrics[14]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
213 trans_table[sample_nr][14] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100214
piotrebd06232014-05-02 17:28:21 +0200215 pm_candidate1 = old_path_metrics[7] -input_symbol_imag +increment[5];
216 pm_candidate2 = old_path_metrics[15] -input_symbol_imag -increment[2];
piotrf0039b42014-05-05 16:45:48 +0200217 paths_difference=pm_candidate2-pm_candidate1;
218 new_path_metrics[15]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
219 trans_table[sample_nr][15] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100220 tmp=old_path_metrics;
221 old_path_metrics=new_path_metrics;
222 new_path_metrics=tmp;
223
224 sample_nr++;
225 if(sample_nr==samples_num)
226 break;
227
228 //Processing real states
229 real_imag=0;
230 input_symbol_real = input[sample_nr].real();
231
piotrebd06232014-05-02 17:28:21 +0200232 pm_candidate1 = old_path_metrics[0] -input_symbol_real -increment[7];
233 pm_candidate2 = old_path_metrics[8] -input_symbol_real +increment[0];
piotrf0039b42014-05-05 16:45:48 +0200234 paths_difference=pm_candidate2-pm_candidate1;
235 new_path_metrics[0]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
236 trans_table[sample_nr][0] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100237
piotrebd06232014-05-02 17:28:21 +0200238 pm_candidate1 = old_path_metrics[0] +input_symbol_real +increment[7];
239 pm_candidate2 = old_path_metrics[8] +input_symbol_real -increment[0];
piotrf0039b42014-05-05 16:45:48 +0200240 paths_difference=pm_candidate2-pm_candidate1;
241 new_path_metrics[1]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
242 trans_table[sample_nr][1] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100243
piotrebd06232014-05-02 17:28:21 +0200244 pm_candidate1 = old_path_metrics[1] -input_symbol_real -increment[6];
245 pm_candidate2 = old_path_metrics[9] -input_symbol_real +increment[1];
piotrf0039b42014-05-05 16:45:48 +0200246 paths_difference=pm_candidate2-pm_candidate1;
247 new_path_metrics[2]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
248 trans_table[sample_nr][2] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100249
piotrebd06232014-05-02 17:28:21 +0200250 pm_candidate1 = old_path_metrics[1] +input_symbol_real +increment[6];
251 pm_candidate2 = old_path_metrics[9] +input_symbol_real -increment[1];
piotrf0039b42014-05-05 16:45:48 +0200252 paths_difference=pm_candidate2-pm_candidate1;
253 new_path_metrics[3]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
254 trans_table[sample_nr][3] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100255
piotrebd06232014-05-02 17:28:21 +0200256 pm_candidate1 = old_path_metrics[2] -input_symbol_real -increment[5];
257 pm_candidate2 = old_path_metrics[10] -input_symbol_real +increment[2];
piotrf0039b42014-05-05 16:45:48 +0200258 paths_difference=pm_candidate2-pm_candidate1;
259 new_path_metrics[4]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
260 trans_table[sample_nr][4] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100261
piotrebd06232014-05-02 17:28:21 +0200262 pm_candidate1 = old_path_metrics[2] +input_symbol_real +increment[5];
263 pm_candidate2 = old_path_metrics[10] +input_symbol_real -increment[2];
piotrf0039b42014-05-05 16:45:48 +0200264 paths_difference=pm_candidate2-pm_candidate1;
265 new_path_metrics[5]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
266 trans_table[sample_nr][5] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100267
piotrebd06232014-05-02 17:28:21 +0200268 pm_candidate1 = old_path_metrics[3] -input_symbol_real -increment[4];
269 pm_candidate2 = old_path_metrics[11] -input_symbol_real +increment[3];
piotrf0039b42014-05-05 16:45:48 +0200270 paths_difference=pm_candidate2-pm_candidate1;
271 new_path_metrics[6]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
272 trans_table[sample_nr][6] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100273
piotrebd06232014-05-02 17:28:21 +0200274 pm_candidate1 = old_path_metrics[3] +input_symbol_real +increment[4];
275 pm_candidate2 = old_path_metrics[11] +input_symbol_real -increment[3];
piotrf0039b42014-05-05 16:45:48 +0200276 paths_difference=pm_candidate2-pm_candidate1;
277 new_path_metrics[7]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
278 trans_table[sample_nr][7] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100279
piotrebd06232014-05-02 17:28:21 +0200280 pm_candidate1 = old_path_metrics[4] -input_symbol_real -increment[3];
281 pm_candidate2 = old_path_metrics[12] -input_symbol_real +increment[4];
piotrf0039b42014-05-05 16:45:48 +0200282 paths_difference=pm_candidate2-pm_candidate1;
283 new_path_metrics[8]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
284 trans_table[sample_nr][8] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100285
piotrebd06232014-05-02 17:28:21 +0200286 pm_candidate1 = old_path_metrics[4] +input_symbol_real +increment[3];
287 pm_candidate2 = old_path_metrics[12] +input_symbol_real -increment[4];
piotrf0039b42014-05-05 16:45:48 +0200288 paths_difference=pm_candidate2-pm_candidate1;
289 new_path_metrics[9]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
290 trans_table[sample_nr][9] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100291
piotrebd06232014-05-02 17:28:21 +0200292 pm_candidate1 = old_path_metrics[5] -input_symbol_real -increment[2];
293 pm_candidate2 = old_path_metrics[13] -input_symbol_real +increment[5];
piotrf0039b42014-05-05 16:45:48 +0200294 paths_difference=pm_candidate2-pm_candidate1;
295 new_path_metrics[10]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
296 trans_table[sample_nr][10] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100297
piotrebd06232014-05-02 17:28:21 +0200298 pm_candidate1 = old_path_metrics[5] +input_symbol_real +increment[2];
299 pm_candidate2 = old_path_metrics[13] +input_symbol_real -increment[5];
piotrf0039b42014-05-05 16:45:48 +0200300 paths_difference=pm_candidate2-pm_candidate1;
301 new_path_metrics[11]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
302 trans_table[sample_nr][11] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100303
piotrebd06232014-05-02 17:28:21 +0200304 pm_candidate1 = old_path_metrics[6] -input_symbol_real -increment[1];
305 pm_candidate2 = old_path_metrics[14] -input_symbol_real +increment[6];
piotrf0039b42014-05-05 16:45:48 +0200306 paths_difference=pm_candidate2-pm_candidate1;
307 new_path_metrics[12]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
308 trans_table[sample_nr][12] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100309
piotrebd06232014-05-02 17:28:21 +0200310 pm_candidate1 = old_path_metrics[6] +input_symbol_real +increment[1];
311 pm_candidate2 = old_path_metrics[14] +input_symbol_real -increment[6];
piotrf0039b42014-05-05 16:45:48 +0200312 paths_difference=pm_candidate2-pm_candidate1;
313 new_path_metrics[13]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
314 trans_table[sample_nr][13] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100315
piotrebd06232014-05-02 17:28:21 +0200316 pm_candidate1 = old_path_metrics[7] -input_symbol_real -increment[0];
317 pm_candidate2 = old_path_metrics[15] -input_symbol_real +increment[7];
piotrf0039b42014-05-05 16:45:48 +0200318 paths_difference=pm_candidate2-pm_candidate1;
319 new_path_metrics[14]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
320 trans_table[sample_nr][14] = paths_difference;
piotr437f5462014-02-04 17:57:25 +0100321
piotrebd06232014-05-02 17:28:21 +0200322 pm_candidate1 = old_path_metrics[7] +input_symbol_real +increment[0];
323 pm_candidate2 = old_path_metrics[15] +input_symbol_real -increment[7];
piotrf0039b42014-05-05 16:45:48 +0200324 paths_difference=pm_candidate2-pm_candidate1;
325 new_path_metrics[15]=(paths_difference<0) ? pm_candidate1 : pm_candidate2;
326 trans_table[sample_nr][15] = paths_difference;
327
piotr437f5462014-02-04 17:57:25 +0100328 tmp=old_path_metrics;
329 old_path_metrics=new_path_metrics;
330 new_path_metrics=tmp;
331
332 sample_nr++;
333 }
334
335/*
336* Find the best from the stop states by comparing their path metrics.
337* Not every stop state is always possible, so we are searching in
338* a subset of them.
339*/
340 unsigned int best_stop_state;
341 float stop_state_metric, max_stop_state_metric;
342 best_stop_state = stop_states[0];
343 max_stop_state_metric = old_path_metrics[best_stop_state];
344 for(i=1; i< stops_num; i++){
345 stop_state_metric = old_path_metrics[stop_states[i]];
346 if(stop_state_metric > max_stop_state_metric){
347 max_stop_state_metric = stop_state_metric;
348 best_stop_state = stop_states[i];
349 }
350 }
351
352/*
353* This table was generated with hope that it gives a litle speedup during
354* traceback stage.
355* Received bit is related to the number of state in the trellis.
356* I've numbered states so their parity (number of ones) is related
357* to a received bit.
358*/
359 static const unsigned int parity_table[PATHS_NUM] = { 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, };
360
361/*
362* Table of previous states in the trellis diagram.
363* For GMSK modulation every state has two previous states.
364* Example:
365* previous_state_nr1 = prev_table[current_state_nr][0]
366* previous_state_nr2 = prev_table[current_state_nr][1]
367*/
368 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}, };
369
370/*
371* Traceback and differential decoding of received sequence.
372* Decisions stored in trans_table are used to restore best path in the trellis.
373*/
374 sample_nr=samples_num;
375 unsigned int state_nr=best_stop_state;
376 unsigned int decision;
377 bool out_bit=0;
378
379 while(sample_nr>0){
380 sample_nr--;
381 decision = (trans_table[sample_nr][state_nr]>0);
piotrf0039b42014-05-05 16:45:48 +0200382
383 if(decision != out_bit)
384 output[sample_nr]=-trans_table[sample_nr][state_nr];
385 else
386 output[sample_nr]=trans_table[sample_nr][state_nr];
387
piotr437f5462014-02-04 17:57:25 +0100388 out_bit = out_bit ^ real_imag ^ parity_table[state_nr];
389 state_nr = prev_table[state_nr][decision];
390 real_imag = !real_imag;
391 }
392}