Piotr Krysik | d890cd3 | 2017-09-19 07:52:34 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # -*- coding: utf-8 -*- |
| 3 | # @file |
| 4 | # @author Piotr Krysik <ptrkrysik@gmail.com> |
| 5 | # @section LICENSE |
| 6 | # |
| 7 | # Gr-gsm is free software; you can redistribute it and/or modify |
| 8 | # it under the terms of the GNU General Public License as published by |
| 9 | # the Free Software Foundation; either version 3, or (at your option) |
| 10 | # any later version. |
| 11 | # |
| 12 | # Gr-gsm is distributed in the hope that it will be useful, |
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | # GNU General Public License for more details. |
| 16 | # |
| 17 | # You should have received a copy of the GNU General Public License |
| 18 | # along with gr-gsm; see the file COPYING. If not, write to |
| 19 | # the Free Software Foundation, Inc., 51 Franklin Street, |
| 20 | # Boston, MA 02110-1301, USA. |
| 21 | # |
| 22 | # |
| 23 | from math import floor, ceil |
| 24 | from random import uniform |
Piotr Krysik | 6e41d06 | 2017-11-03 09:56:49 +0100 | [diff] [blame] | 25 | from grgsm import fn_time_delta_cpp |
Piotr Krysik | d890cd3 | 2017-09-19 07:52:34 +0200 | [diff] [blame] | 26 | |
| 27 | __hyper_frame = 26*51*2048 |
| 28 | __symb_rate = 13.0e6/48.0 |
| 29 | __ts_period = 156.25/__symb_rate |
| 30 | __frame_period = 8*__ts_period |
| 31 | |
| 32 | #The functions below are crucial part of synchronization between |
| 33 | #transmitter and receiver. |
| 34 | |
| 35 | #fnmod_delta computes difference between two frame numbers modulo |
| 36 | #__hyper_frame. The result is correct if difference between the frame |
| 37 | #numbers is not bigger than __hyper_frame/2. |
| 38 | def fnmod_delta(fn1, fn2): |
| 39 | h2 = __hyper_frame/2 |
| 40 | delta = (fn1%__hyper_frame)-(fn2%__hyper_frame) |
Piotr Krysik | 6e41d06 | 2017-11-03 09:56:49 +0100 | [diff] [blame] | 41 | |
Piotr Krysik | d890cd3 | 2017-09-19 07:52:34 +0200 | [diff] [blame] | 42 | if delta >= h2: |
| 43 | delta = delta - __hyper_frame |
| 44 | elif delta < -h2: |
| 45 | delta = delta + __hyper_frame |
| 46 | |
| 47 | return delta |
Piotr Krysik | eb62dc1 | 2017-09-27 21:59:19 +0200 | [diff] [blame] | 48 | |
| 49 | def fn_time_diff_delta(fn_x, fn_ref, time_diff_hint=0): |
| 50 | frames_diff = int(round(time_diff_hint/__frame_period)) |
| 51 | fn_ref_hint = (fn_ref + frames_diff) |
| 52 | fn_delta = fnmod_delta(fn_x,fn_ref_hint)+frames_diff |
Piotr Krysik | 6e41d06 | 2017-11-03 09:56:49 +0100 | [diff] [blame] | 53 | |
Piotr Krysik | eb62dc1 | 2017-09-27 21:59:19 +0200 | [diff] [blame] | 54 | return fn_delta |
| 55 | |
Piotr Krysik | d890cd3 | 2017-09-19 07:52:34 +0200 | [diff] [blame] | 56 | #fn_time_delta computes difference between reference frame number and a second frame number. It also computes timestamp of the second frame number. The full list of parameters is following: |
| 57 | #* fn_ref - reference frame number modulo __hyper_frame |
| 58 | #* time_ref - precise timestamp of the first sample in the frame fn_ref |
| 59 | #* fn_x - second frame number modulo __hyper_frame, |
| 60 | #* time_hint - coarse time for fn_x that is used as a hint to avoid ambiguities caused by modulo operation applied to frame numbers. The correct are values from range <time2_precise-__hiperframe/2, time2_precise+__hiperframe/2> (where time2_precise is exact time of the first sample in frame fn_x) |
| 61 | #* ts_num - number of timeslot in the frame |
| 62 | |
Piotr Krysik | ffd09ac | 2017-10-16 15:47:38 +0200 | [diff] [blame] | 63 | def fn_time_delta(fn_ref, time_ref, fn_x, time_hint=None, ts_num=0, ts_ref=0): |
Piotr Krysik | d890cd3 | 2017-09-19 07:52:34 +0200 | [diff] [blame] | 64 | if time_hint is None: |
| 65 | time_hint = time_ref |
| 66 | |
Piotr Krysik | eb62dc1 | 2017-09-27 21:59:19 +0200 | [diff] [blame] | 67 | time_diff_hint = time_hint-time_ref |
| 68 | fn_delta = fn_time_diff_delta(fn_x,fn_ref, time_diff_hint) |
Piotr Krysik | ffd09ac | 2017-10-16 15:47:38 +0200 | [diff] [blame] | 69 | time_x_precise = fn_delta*__frame_period+time_ref+(ts_num-ts_ref)*__ts_period |
Piotr Krysik | d890cd3 | 2017-09-19 07:52:34 +0200 | [diff] [blame] | 70 | |
Piotr Krysik | eb62dc1 | 2017-09-27 21:59:19 +0200 | [diff] [blame] | 71 | return fn_delta, time_x_precise |
Piotr Krysik | d890cd3 | 2017-09-19 07:52:34 +0200 | [diff] [blame] | 72 | |
| 73 | |
| 74 | if __name__ == "__main__": |
| 75 | fn1 = 10000 |
Piotr Krysik | ffd09ac | 2017-10-16 15:47:38 +0200 | [diff] [blame] | 76 | ts_ref = 4 |
Piotr Krysik | 6e41d06 | 2017-11-03 09:56:49 +0100 | [diff] [blame] | 77 | time1 = 10.5 |
| 78 | for fn2 in xrange(__hyper_frame/2+fn1-10,__hyper_frame/2*10+fn1+100,10): |
Piotr Krysik | ffd09ac | 2017-10-16 15:47:38 +0200 | [diff] [blame] | 79 | ts_x = int(uniform(0,8)) |
Piotr Krysik | 6e41d06 | 2017-11-03 09:56:49 +0100 | [diff] [blame] | 80 | time2 = time1 + (fn2-fn1)*__frame_period + (ts_x-ts_ref)*__ts_period |
Piotr Krysik | d890cd3 | 2017-09-19 07:52:34 +0200 | [diff] [blame] | 81 | error = uniform(-6200,6200) |
| 82 | time2_err = time2+error |
Piotr Krysik | ffd09ac | 2017-10-16 15:47:38 +0200 | [diff] [blame] | 83 | fn_delta, time2_precise = fn_time_delta(fn1, time1, fn2, time2_err, ts_x, ts_ref) |
Piotr Krysik | 6e41d06 | 2017-11-03 09:56:49 +0100 | [diff] [blame] | 84 | time2_precise_cpp = fn_time_delta_cpp(fn1, (int(time1),time1-int(time1)), fn2, (int(time2_err),time2_err-int(time2_err)), ts_x, ts_ref) |
Piotr Krysik | d890cd3 | 2017-09-19 07:52:34 +0200 | [diff] [blame] | 85 | if fn_delta != fn2-fn1: |
Piotr Krysik | ffd09ac | 2017-10-16 15:47:38 +0200 | [diff] [blame] | 86 | print "bad fn:", fn2, error#, 'fn_delta:'+str(fn_delta), time2, error, frames_diff_h4, (time2-time1)/(__hyper_frame*__frame_period), time_diff_hint_h4_prev, time_diff_hint |
Piotr Krysik | 6e41d06 | 2017-11-03 09:56:49 +0100 | [diff] [blame] | 87 | # time_diff_hint = time2 - time2_precise |
| 88 | time_diff_hint = time2_precise_cpp[0]+time2_precise_cpp[1] - time2_precise |
Piotr Krysik | d890cd3 | 2017-09-19 07:52:34 +0200 | [diff] [blame] | 89 | |
Piotr Krysik | 6e41d06 | 2017-11-03 09:56:49 +0100 | [diff] [blame] | 90 | if abs(time_diff_hint) > 0.0001: |
| 91 | print "time2_precise_cpp",time2_precise_cpp," time2_precise",time2_precise," time_ref",time1," time_hint",time2_err |
| 92 | print "" |