piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 1 | /* -*- c++ -*- */ |
| 2 | /* |
| 3 | * @file |
Piotr Krysik | a6268a5 | 2017-08-23 16:02:19 +0200 | [diff] [blame] | 4 | * @author (C) 2009 by Piotr Krysik <ptrkrysik@gmail.com> |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 5 | * @section LICENSE |
| 6 | * |
ptrkrysik | 529895b | 2014-12-02 18:07:38 +0100 | [diff] [blame] | 7 | * Gr-gsm is free software; you can redistribute it and/or modify |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 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 | * |
ptrkrysik | 529895b | 2014-12-02 18:07:38 +0100 | [diff] [blame] | 12 | * Gr-gsm is distributed in the hope that it will be useful, |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 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 |
ptrkrysik | 529895b | 2014-12-02 18:07:38 +0100 | [diff] [blame] | 18 | * along with gr-gsm; see the file COPYING. If not, write to |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 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> |
Piotr Krysik | d7efc05 | 2017-11-07 19:33:22 +0100 | [diff] [blame] | 59 | #include <grgsm/gsm_constants.h> |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 60 | #include <cmath> |
| 61 | |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 62 | #define PATHS_NUM (1 << (CHAN_IMP_RESP_LENGTH-1)) |
| 63 | |
| 64 | 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) |
| 65 | { |
| 66 | float increment[8]; |
| 67 | float path_metrics1[16]; |
| 68 | float path_metrics2[16]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 69 | float paths_difference; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 70 | 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: |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 110 | * 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 115 | * 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 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 126 | pm_candidate1 = old_path_metrics[0] +input_symbol_imag -increment[2]; |
| 127 | pm_candidate2 = old_path_metrics[8] +input_symbol_imag +increment[5]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 128 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 131 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 132 | pm_candidate1 = old_path_metrics[0] -input_symbol_imag +increment[2]; |
| 133 | pm_candidate2 = old_path_metrics[8] -input_symbol_imag -increment[5]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 134 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 137 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 138 | pm_candidate1 = old_path_metrics[1] +input_symbol_imag -increment[3]; |
| 139 | pm_candidate2 = old_path_metrics[9] +input_symbol_imag +increment[4]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 140 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 143 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 144 | pm_candidate1 = old_path_metrics[1] -input_symbol_imag +increment[3]; |
| 145 | pm_candidate2 = old_path_metrics[9] -input_symbol_imag -increment[4]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 146 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 149 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 150 | pm_candidate1 = old_path_metrics[2] +input_symbol_imag -increment[0]; |
| 151 | pm_candidate2 = old_path_metrics[10] +input_symbol_imag +increment[7]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 152 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 155 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 156 | pm_candidate1 = old_path_metrics[2] -input_symbol_imag +increment[0]; |
| 157 | pm_candidate2 = old_path_metrics[10] -input_symbol_imag -increment[7]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 158 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 161 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 162 | pm_candidate1 = old_path_metrics[3] +input_symbol_imag -increment[1]; |
| 163 | pm_candidate2 = old_path_metrics[11] +input_symbol_imag +increment[6]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 164 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 167 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 168 | pm_candidate1 = old_path_metrics[3] -input_symbol_imag +increment[1]; |
| 169 | pm_candidate2 = old_path_metrics[11] -input_symbol_imag -increment[6]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 170 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 173 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 174 | pm_candidate1 = old_path_metrics[4] +input_symbol_imag -increment[6]; |
| 175 | pm_candidate2 = old_path_metrics[12] +input_symbol_imag +increment[1]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 176 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 179 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 180 | pm_candidate1 = old_path_metrics[4] -input_symbol_imag +increment[6]; |
| 181 | pm_candidate2 = old_path_metrics[12] -input_symbol_imag -increment[1]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 182 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 185 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 186 | pm_candidate1 = old_path_metrics[5] +input_symbol_imag -increment[7]; |
| 187 | pm_candidate2 = old_path_metrics[13] +input_symbol_imag +increment[0]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 188 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 191 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 192 | pm_candidate1 = old_path_metrics[5] -input_symbol_imag +increment[7]; |
| 193 | pm_candidate2 = old_path_metrics[13] -input_symbol_imag -increment[0]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 194 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 197 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 198 | pm_candidate1 = old_path_metrics[6] +input_symbol_imag -increment[4]; |
| 199 | pm_candidate2 = old_path_metrics[14] +input_symbol_imag +increment[3]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 200 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 203 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 204 | pm_candidate1 = old_path_metrics[6] -input_symbol_imag +increment[4]; |
| 205 | pm_candidate2 = old_path_metrics[14] -input_symbol_imag -increment[3]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 206 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 209 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 210 | pm_candidate1 = old_path_metrics[7] +input_symbol_imag -increment[5]; |
| 211 | pm_candidate2 = old_path_metrics[15] +input_symbol_imag +increment[2]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 212 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 215 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 216 | pm_candidate1 = old_path_metrics[7] -input_symbol_imag +increment[5]; |
| 217 | pm_candidate2 = old_path_metrics[15] -input_symbol_imag -increment[2]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 218 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 221 | 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 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 233 | pm_candidate1 = old_path_metrics[0] -input_symbol_real -increment[7]; |
| 234 | pm_candidate2 = old_path_metrics[8] -input_symbol_real +increment[0]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 235 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 238 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 239 | pm_candidate1 = old_path_metrics[0] +input_symbol_real +increment[7]; |
| 240 | pm_candidate2 = old_path_metrics[8] +input_symbol_real -increment[0]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 241 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 244 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 245 | pm_candidate1 = old_path_metrics[1] -input_symbol_real -increment[6]; |
| 246 | pm_candidate2 = old_path_metrics[9] -input_symbol_real +increment[1]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 247 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 250 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 251 | pm_candidate1 = old_path_metrics[1] +input_symbol_real +increment[6]; |
| 252 | pm_candidate2 = old_path_metrics[9] +input_symbol_real -increment[1]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 253 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 256 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 257 | pm_candidate1 = old_path_metrics[2] -input_symbol_real -increment[5]; |
| 258 | pm_candidate2 = old_path_metrics[10] -input_symbol_real +increment[2]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 259 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 262 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 263 | pm_candidate1 = old_path_metrics[2] +input_symbol_real +increment[5]; |
| 264 | pm_candidate2 = old_path_metrics[10] +input_symbol_real -increment[2]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 265 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 268 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 269 | pm_candidate1 = old_path_metrics[3] -input_symbol_real -increment[4]; |
| 270 | pm_candidate2 = old_path_metrics[11] -input_symbol_real +increment[3]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 271 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 274 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 275 | pm_candidate1 = old_path_metrics[3] +input_symbol_real +increment[4]; |
| 276 | pm_candidate2 = old_path_metrics[11] +input_symbol_real -increment[3]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 277 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 280 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 281 | pm_candidate1 = old_path_metrics[4] -input_symbol_real -increment[3]; |
| 282 | pm_candidate2 = old_path_metrics[12] -input_symbol_real +increment[4]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 283 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 286 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 287 | pm_candidate1 = old_path_metrics[4] +input_symbol_real +increment[3]; |
| 288 | pm_candidate2 = old_path_metrics[12] +input_symbol_real -increment[4]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 289 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 292 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 293 | pm_candidate1 = old_path_metrics[5] -input_symbol_real -increment[2]; |
| 294 | pm_candidate2 = old_path_metrics[13] -input_symbol_real +increment[5]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 295 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 298 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 299 | pm_candidate1 = old_path_metrics[5] +input_symbol_real +increment[2]; |
| 300 | pm_candidate2 = old_path_metrics[13] +input_symbol_real -increment[5]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 301 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 304 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 305 | pm_candidate1 = old_path_metrics[6] -input_symbol_real -increment[1]; |
| 306 | pm_candidate2 = old_path_metrics[14] -input_symbol_real +increment[6]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 307 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 310 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 311 | pm_candidate1 = old_path_metrics[6] +input_symbol_real +increment[1]; |
| 312 | pm_candidate2 = old_path_metrics[14] +input_symbol_real -increment[6]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 313 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 316 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 317 | pm_candidate1 = old_path_metrics[7] -input_symbol_real -increment[0]; |
| 318 | pm_candidate2 = old_path_metrics[15] -input_symbol_real +increment[7]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 319 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 322 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 323 | pm_candidate1 = old_path_metrics[7] +input_symbol_real +increment[0]; |
| 324 | pm_candidate2 = old_path_metrics[15] +input_symbol_real -increment[7]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 325 | 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 | |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 329 | 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); |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 383 | |
| 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 | |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 389 | 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 | } |