aboutsummaryrefslogtreecommitdiffstats
path: root/python/clock_offset_control.py
blob: 133368cb201f51cca3f71401460a445a9fa859da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 
# Copyright 2014 <+YOU OR YOUR COMPANY+>.
# 
# This is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
# 
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this software; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
# 

from numpy import *
from gnuradio import gr
import pmt

class clock_offset_control(gr.basic_block):
    """
    docstring for block clock_offset_control
    """
    def __init__(self, fc, samp_rate):
        gr.basic_block.__init__(self,
            name="clock_offset_control",
            in_sig=[],
            out_sig=[])
        self.fc = fc
        self.samp_rate = samp_rate
        self.message_port_register_in(pmt.intern("measurements"))
        self.set_msg_handler(pmt.intern("measurements"), self.process_measurement)
        self.message_port_register_out(pmt.intern("ppm"))
        self.alfa = 0.6
        self.ppm_estimate = -1e6
        self.first_measurement = True
        self.counter = 0
        
    def process_measurement(self,msg):
        if pmt.is_tuple(msg):
            key = pmt.symbol_to_string(pmt.tuple_ref(msg,0))
            if key == "freq_offset":
                freq_offset = pmt.to_double(pmt.tuple_ref(msg,1))
                ppm = -freq_offset/self.fc*1.0e6
                state = pmt.symbol_to_string(pmt.tuple_ref(msg,2))

                if state == "first_fcch_search" or state == "next_fcch_search":
                    msg_ppm = pmt.from_double(ppm)
                    self.message_port_pub(pmt.intern("ppm"), msg_ppm)
                
                if state == "synchronized":
                    if self.first_measurement:
                        self.ppm_estimate = ppm
                        self.first_measurement = False
                    else:
                        self.ppm_estimate = (1-self.alfa)*self.ppm_estimate+self.alfa*ppm
                    
                    if self.counter == 5:
                        self.counter = 0
                        msg_ppm = pmt.from_double(ppm)
                        self.message_port_pub(pmt.intern("ppm"), msg_ppm)
                    else:
                        self.counter=self.counter+1
                    
                if state == "sync_loss":
                    self.ppm_estimate = -1e6
                    self.counter = 0
                    self.first_measurement = True
                    msg_ppm = pmt.from_double(0.0)
                    self.message_port_pub(pmt.intern("ppm"), msg_ppm)