blob: 0cf85ed0ee50d4cd7794ce224a58f2b477938f1f [file] [log] [blame]
Piotr Krysikdf978692017-09-27 21:58:24 +02001/* -*- c++ -*- */
2/*
3 * @file
4 * @author (C) 2017 by 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#include <math.h>
24#include "time_sample_ref.h"
25
26namespace gr {
27 namespace gsm {
28 time_sample_ref::time_sample_ref(double samp_rate): d_samp_rate(samp_rate)
29 {
30 }
31
32 time_sample_ref::~time_sample_ref()
33 {
34 }
35
36 void time_sample_ref::update(time_spec_t last_rx_time, uint64_t current_start_offset)
37 {
38 d_last_rx_time = last_rx_time;
39 d_current_start_offset = current_start_offset;
40 }
41
42 time_spec_t time_sample_ref::offset_to_time(uint64_t offset)
43 {
44 uint64_t samples_from_last_rx_time = offset - d_current_start_offset;
45 time_spec_t time = time_spec_t(static_cast<double>(samples_from_last_rx_time)/d_samp_rate) + d_last_rx_time;
46
47 return time;
48 }
49
50 uint64_t time_sample_ref::time_to_offset(time_spec_t time)
51 {
52 double samples_since_last_rx_time_tag = (time-d_last_rx_time).get_real_secs()*d_samp_rate;
53// double fractional_part = round(samples_since_last_rx_time_tag) - samples_since_last_rx_time_tag;
54 uint64_t offset = static_cast<uint64_t>(round(samples_since_last_rx_time_tag)) + d_current_start_offset;
55
56 return offset;
57 }
58 } // namespace gsm
59} // namespace gr
60