blob: 4cf1e6f2ad44ababd7830e99c0264dba6a36d467 [file] [log] [blame]
piotr4089c1a2014-08-06 14:10:56 +02001#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3#
4# Copyright 2014 <+YOU OR YOUR COMPANY+>.
5#
6# This is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 3, or (at your option)
9# any later version.
10#
11# This software is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this software; see the file COPYING. If not, write to
18# the Free Software Foundation, Inc., 51 Franklin Street,
19# Boston, MA 02110-1301, USA.
20#
21
22from numpy import *
23from gnuradio import gr
24import pmt
25
26class clock_offset_control(gr.basic_block):
27 """
28 docstring for block clock_offset_control
29 """
30 def __init__(self, fc, samp_rate):
31 gr.basic_block.__init__(self,
32 name="clock_offset_control",
33 in_sig=[],
34 out_sig=[])
35 self.fc = fc
36 self.samp_rate = samp_rate
37 self.message_port_register_in(pmt.intern("measurements"))
38 self.set_msg_handler(pmt.intern("measurements"), self.process_measurement)
39 self.message_port_register_out(pmt.intern("ppm"))
40 self.alfa = 0.6
41 self.ppm_estimate = -1e6
42 self.first_measurement = True
43 self.counter = 0
44
45 def process_measurement(self,msg):
46 if pmt.is_tuple(msg):
47 key = pmt.symbol_to_string(pmt.tuple_ref(msg,0))
48 if key == "freq_offset":
49 freq_offset = pmt.to_double(pmt.tuple_ref(msg,1))
50 ppm = -freq_offset/self.fc*1.0e6
51 state = pmt.symbol_to_string(pmt.tuple_ref(msg,2))
piotr9b59e822014-08-06 20:14:46 +020052
53 if abs(ppm) > 50:
54 ppm = 0
55 self.reset()
56
piotrd6d66872014-08-06 15:20:33 +020057 if state == "fcch_search":
piotr4089c1a2014-08-06 14:10:56 +020058 msg_ppm = pmt.from_double(ppm)
59 self.message_port_pub(pmt.intern("ppm"), msg_ppm)
60
61 if state == "synchronized":
62 if self.first_measurement:
63 self.ppm_estimate = ppm
64 self.first_measurement = False
65 else:
66 self.ppm_estimate = (1-self.alfa)*self.ppm_estimate+self.alfa*ppm
67
68 if self.counter == 5:
69 self.counter = 0
70 msg_ppm = pmt.from_double(ppm)
71 self.message_port_pub(pmt.intern("ppm"), msg_ppm)
72 else:
73 self.counter=self.counter+1
74
75 if state == "sync_loss":
piotr9b59e822014-08-06 20:14:46 +020076 self.reset()
piotr4089c1a2014-08-06 14:10:56 +020077 msg_ppm = pmt.from_double(0.0)
78 self.message_port_pub(pmt.intern("ppm"), msg_ppm)
piotr9b59e822014-08-06 20:14:46 +020079 def reset(self):
80 self.ppm_estimate = -1e6
81 self.counter = 0
82 self.first_measurement = True