blob: 200ba48c2c242112e77bce154506fb90ced8c7be [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#ifndef INCLUDED_TIME_SAMPLE_REF_IMPL_H
24#define INCLUDED_TIME_SAMPLE_REF_IMPL_H
25
26#include <stdint.h>
Piotr Krysik1dd2afe2017-10-31 12:57:26 +010027#include <grgsm/misc_utils/time_spec.h>
Piotr Krysikdf978692017-09-27 21:58:24 +020028
29namespace gr {
30 namespace gsm {
31 /*
Piotr Krysikc5ed0452019-07-16 16:17:43 +020032 Class for storing time reference and for conversions time<->sample number.
33 N - number of sample before the resampler
34 M - number of sample after the resampler
35 t - time before the resampler
Piotr Krysikdf978692017-09-27 21:58:24 +020036 */
37 class time_sample_ref
38 {
39 private:
Piotr Krysikc5ed0452019-07-16 16:17:43 +020040 double d_samp_rate;
41 double d_resamp_rate;
42
43 time_spec_t d_ref_time;
44 uint64_t d_ref_offset;/**< sample offset of the reference point */
45 uint64_t d_original_ref_offset;/**< sample offset of the
46 reference point before resampler */
Piotr Krysikdf978692017-09-27 21:58:24 +020047 public:
Piotr Krysikc5ed0452019-07-16 16:17:43 +020048 time_sample_ref(double samp_rate, double resamp_rate = 1.0);
49 ~time_sample_ref();
50
51 void update(uint64_t M, uint64_t N){
52 d_ref_time = time_spec_t::from_ticks(N - d_original_ref_offset, d_samp_rate*d_resamp_rate) + d_ref_time;
53 d_original_ref_offset = N;
54 d_ref_offset = M;
55 }
56
57 void update(uint64_t M, uint64_t N, double resamp_rate){
58// std::cout << "M:" << M << " N:" << N << " resamp_rate:" << resamp_rate << std::endl;
59 update(M, N);
60 d_resamp_rate = resamp_rate;
61 }
62 void update(uint64_t M, uint64_t N, time_spec_t ref_time){
63// std::cout << "M:" << M << " N:" << N << " ref_time" << ref_time.get_real_secs() << std::endl;
64 update(M, N);
65 d_ref_time = ref_time;
66 }
67
68 void set_samp_rate(double samp_rate){
69 d_samp_rate = samp_rate;
70 }
71
72 uint64_t convert_M_to_N(uint64_t M){
73 time_spec_t new_N_tspec =
74 time_spec_t::from_ticks(M - d_ref_offset, 1.0/d_resamp_rate) +
75 time_spec_t::from_ticks(d_original_ref_offset, 1.0);
76 uint64_t new_N = (round(new_N_tspec.get_real_secs()));
77// std::cout << "d_ref_offset:" << d_ref_offset << " d_resamp_rate:" << d_resamp_rate << " d_ref_offset" << d_ref_offset << " d_original_ref_offset:" << d_original_ref_offset << std::endl;
78 return new_N;
79 }
80
81 time_spec_t convert_M_to_t(uint64_t M){
82 return time_spec_t::from_ticks(convert_M_to_N(M) -
83 d_original_ref_offset, d_samp_rate*d_resamp_rate) + d_ref_time;
84 }
85
86 time_spec_t convert_M_to_ideal_t(uint64_t M){
87 return time_spec_t::from_ticks(M, d_samp_rate);
88 }
89
90 time_spec_t get_ref_time(){
91 return d_ref_time;
92 }
93
94 uint64_t convert_t_to_M(time_spec_t time)
95 {
96 uint64_t samples_since_ref_time_tag = (time-d_ref_time).to_ticks(d_samp_rate);
97 uint64_t offset = samples_since_ref_time_tag + d_ref_offset;
98 return offset;
99 }
100
101 uint64_t convert_t_to_N(time_spec_t time)
102 {
103 return convert_M_to_N(convert_t_to_M(time));
104 }
Piotr Krysikdf978692017-09-27 21:58:24 +0200105 };
106 } // namespace gsm
107} // namespace gr
108#endif// INCLUDED_TIME_SAMPLE_REF_IMPL_H