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> |
| 59 | #include <gsm_constants.h> |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 60 | |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 61 | #define PATHS_NUM (1 << (CHAN_IMP_RESP_LENGTH-1)) |
| 62 | |
| 63 | 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) |
| 64 | { |
| 65 | float increment[8]; |
| 66 | float path_metrics1[16]; |
| 67 | float path_metrics2[16]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 68 | float paths_difference; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 69 | 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: |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 109 | * 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 114 | * 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 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 125 | pm_candidate1 = old_path_metrics[0] +input_symbol_imag -increment[2]; |
| 126 | pm_candidate2 = old_path_metrics[8] +input_symbol_imag +increment[5]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 127 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 130 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 131 | pm_candidate1 = old_path_metrics[0] -input_symbol_imag +increment[2]; |
| 132 | pm_candidate2 = old_path_metrics[8] -input_symbol_imag -increment[5]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 133 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 136 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 137 | pm_candidate1 = old_path_metrics[1] +input_symbol_imag -increment[3]; |
| 138 | pm_candidate2 = old_path_metrics[9] +input_symbol_imag +increment[4]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 139 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 142 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 143 | pm_candidate1 = old_path_metrics[1] -input_symbol_imag +increment[3]; |
| 144 | pm_candidate2 = old_path_metrics[9] -input_symbol_imag -increment[4]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 145 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 148 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 149 | pm_candidate1 = old_path_metrics[2] +input_symbol_imag -increment[0]; |
| 150 | pm_candidate2 = old_path_metrics[10] +input_symbol_imag +increment[7]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 151 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 154 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 155 | pm_candidate1 = old_path_metrics[2] -input_symbol_imag +increment[0]; |
| 156 | pm_candidate2 = old_path_metrics[10] -input_symbol_imag -increment[7]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 157 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 160 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 161 | pm_candidate1 = old_path_metrics[3] +input_symbol_imag -increment[1]; |
| 162 | pm_candidate2 = old_path_metrics[11] +input_symbol_imag +increment[6]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 163 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 166 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 167 | pm_candidate1 = old_path_metrics[3] -input_symbol_imag +increment[1]; |
| 168 | pm_candidate2 = old_path_metrics[11] -input_symbol_imag -increment[6]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 169 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 172 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 173 | pm_candidate1 = old_path_metrics[4] +input_symbol_imag -increment[6]; |
| 174 | pm_candidate2 = old_path_metrics[12] +input_symbol_imag +increment[1]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 175 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 178 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 179 | pm_candidate1 = old_path_metrics[4] -input_symbol_imag +increment[6]; |
| 180 | pm_candidate2 = old_path_metrics[12] -input_symbol_imag -increment[1]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 181 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 184 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 185 | pm_candidate1 = old_path_metrics[5] +input_symbol_imag -increment[7]; |
| 186 | pm_candidate2 = old_path_metrics[13] +input_symbol_imag +increment[0]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 187 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 190 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 191 | pm_candidate1 = old_path_metrics[5] -input_symbol_imag +increment[7]; |
| 192 | pm_candidate2 = old_path_metrics[13] -input_symbol_imag -increment[0]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 193 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 196 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 197 | pm_candidate1 = old_path_metrics[6] +input_symbol_imag -increment[4]; |
| 198 | pm_candidate2 = old_path_metrics[14] +input_symbol_imag +increment[3]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 199 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 202 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 203 | pm_candidate1 = old_path_metrics[6] -input_symbol_imag +increment[4]; |
| 204 | pm_candidate2 = old_path_metrics[14] -input_symbol_imag -increment[3]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 205 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 208 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 209 | pm_candidate1 = old_path_metrics[7] +input_symbol_imag -increment[5]; |
| 210 | pm_candidate2 = old_path_metrics[15] +input_symbol_imag +increment[2]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 211 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 214 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 215 | pm_candidate1 = old_path_metrics[7] -input_symbol_imag +increment[5]; |
| 216 | pm_candidate2 = old_path_metrics[15] -input_symbol_imag -increment[2]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 217 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 220 | 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 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 232 | pm_candidate1 = old_path_metrics[0] -input_symbol_real -increment[7]; |
| 233 | pm_candidate2 = old_path_metrics[8] -input_symbol_real +increment[0]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 234 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 237 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 238 | pm_candidate1 = old_path_metrics[0] +input_symbol_real +increment[7]; |
| 239 | pm_candidate2 = old_path_metrics[8] +input_symbol_real -increment[0]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 240 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 243 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 244 | pm_candidate1 = old_path_metrics[1] -input_symbol_real -increment[6]; |
| 245 | pm_candidate2 = old_path_metrics[9] -input_symbol_real +increment[1]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 246 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 249 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 250 | pm_candidate1 = old_path_metrics[1] +input_symbol_real +increment[6]; |
| 251 | pm_candidate2 = old_path_metrics[9] +input_symbol_real -increment[1]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 252 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 255 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 256 | pm_candidate1 = old_path_metrics[2] -input_symbol_real -increment[5]; |
| 257 | pm_candidate2 = old_path_metrics[10] -input_symbol_real +increment[2]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 258 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 261 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 262 | pm_candidate1 = old_path_metrics[2] +input_symbol_real +increment[5]; |
| 263 | pm_candidate2 = old_path_metrics[10] +input_symbol_real -increment[2]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 264 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 267 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 268 | pm_candidate1 = old_path_metrics[3] -input_symbol_real -increment[4]; |
| 269 | pm_candidate2 = old_path_metrics[11] -input_symbol_real +increment[3]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 270 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 273 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 274 | pm_candidate1 = old_path_metrics[3] +input_symbol_real +increment[4]; |
| 275 | pm_candidate2 = old_path_metrics[11] +input_symbol_real -increment[3]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 276 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 279 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 280 | pm_candidate1 = old_path_metrics[4] -input_symbol_real -increment[3]; |
| 281 | pm_candidate2 = old_path_metrics[12] -input_symbol_real +increment[4]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 282 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 285 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 286 | pm_candidate1 = old_path_metrics[4] +input_symbol_real +increment[3]; |
| 287 | pm_candidate2 = old_path_metrics[12] +input_symbol_real -increment[4]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 288 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 291 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 292 | pm_candidate1 = old_path_metrics[5] -input_symbol_real -increment[2]; |
| 293 | pm_candidate2 = old_path_metrics[13] -input_symbol_real +increment[5]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 294 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 297 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 298 | pm_candidate1 = old_path_metrics[5] +input_symbol_real +increment[2]; |
| 299 | pm_candidate2 = old_path_metrics[13] +input_symbol_real -increment[5]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 300 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 303 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 304 | pm_candidate1 = old_path_metrics[6] -input_symbol_real -increment[1]; |
| 305 | pm_candidate2 = old_path_metrics[14] -input_symbol_real +increment[6]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 306 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 309 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 310 | pm_candidate1 = old_path_metrics[6] +input_symbol_real +increment[1]; |
| 311 | pm_candidate2 = old_path_metrics[14] +input_symbol_real -increment[6]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 312 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 315 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 316 | pm_candidate1 = old_path_metrics[7] -input_symbol_real -increment[0]; |
| 317 | pm_candidate2 = old_path_metrics[15] -input_symbol_real +increment[7]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 318 | 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; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 321 | |
piotr | ebd0623 | 2014-05-02 17:28:21 +0200 | [diff] [blame] | 322 | pm_candidate1 = old_path_metrics[7] +input_symbol_real +increment[0]; |
| 323 | pm_candidate2 = old_path_metrics[15] +input_symbol_real -increment[7]; |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 324 | 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 | |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 328 | 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); |
piotr | f0039b4 | 2014-05-05 16:45:48 +0200 | [diff] [blame] | 382 | |
| 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 | |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 388 | 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 | } |