blob: 506b0fd1d43f945f6e19c08fbd2d5cc6eb67f894 [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
Piotr K8cfe4c42014-08-07 17:03:10 +020025from threading import Timer
piotr4089c1a2014-08-06 14:10:56 +020026
27class clock_offset_control(gr.basic_block):
28 """
29 docstring for block clock_offset_control
30 """
31 def __init__(self, fc, samp_rate):
32 gr.basic_block.__init__(self,
Piotr K66bb3cd2014-08-13 19:04:57 +020033 name="gsm_clock_offset_control",
piotr4089c1a2014-08-06 14:10:56 +020034 in_sig=[],
35 out_sig=[])
36 self.fc = fc
37 self.samp_rate = samp_rate
38 self.message_port_register_in(pmt.intern("measurements"))
39 self.set_msg_handler(pmt.intern("measurements"), self.process_measurement)
40 self.message_port_register_out(pmt.intern("ppm"))
Piotr K66bb3cd2014-08-13 19:04:57 +020041 self.alfa = 0.3
piotr4089c1a2014-08-06 14:10:56 +020042 self.ppm_estimate = -1e6
43 self.first_measurement = True
44 self.counter = 0
Piotr K8cfe4c42014-08-07 17:03:10 +020045 self.last_state = ""
Piotr K77f3b062014-08-07 23:04:43 +020046 self.timer = Timer(0.5, self.timed_reset)
Piotr K66bb3cd2014-08-13 19:04:57 +020047 self.last_ppm = -1e6
piotr4089c1a2014-08-06 14:10:56 +020048
49 def process_measurement(self,msg):
50 if pmt.is_tuple(msg):
51 key = pmt.symbol_to_string(pmt.tuple_ref(msg,0))
52 if key == "freq_offset":
53 freq_offset = pmt.to_double(pmt.tuple_ref(msg,1))
54 ppm = -freq_offset/self.fc*1.0e6
55 state = pmt.symbol_to_string(pmt.tuple_ref(msg,2))
piotr9b59e822014-08-06 20:14:46 +020056
Piotr K8cfe4c42014-08-07 17:03:10 +020057 self.last_state = state
58
59 if abs(ppm) > 100: #safeguard against flawed measurements
piotr9b59e822014-08-06 20:14:46 +020060 ppm = 0
61 self.reset()
62
piotrd6d66872014-08-06 15:20:33 +020063 if state == "fcch_search":
piotr4089c1a2014-08-06 14:10:56 +020064 msg_ppm = pmt.from_double(ppm)
65 self.message_port_pub(pmt.intern("ppm"), msg_ppm)
Piotr K8cfe4c42014-08-07 17:03:10 +020066 self.timer.cancel()
Piotr K77f3b062014-08-07 23:04:43 +020067 self.timer = Timer(0.5, self.timed_reset)
Piotr K8cfe4c42014-08-07 17:03:10 +020068 self.timer.start()
69 elif state == "synchronized":
Piotr K77f3b062014-08-07 23:04:43 +020070 self.timer.cancel()
piotr4089c1a2014-08-06 14:10:56 +020071 if self.first_measurement:
72 self.ppm_estimate = ppm
73 self.first_measurement = False
74 else:
75 self.ppm_estimate = (1-self.alfa)*self.ppm_estimate+self.alfa*ppm
76
77 if self.counter == 5:
78 self.counter = 0
Piotr K66bb3cd2014-08-13 19:04:57 +020079 if abs(self.last_ppm-self.ppm_estimate) > 0.1:
80 msg_ppm = pmt.from_double(ppm)
81 self.message_port_pub(pmt.intern("ppm"), msg_ppm)
82 self.last_ppm = self.ppm_estimate
piotr4089c1a2014-08-06 14:10:56 +020083 else:
84 self.counter=self.counter+1
Piotr K8cfe4c42014-08-07 17:03:10 +020085 elif state == "sync_loss":
piotr9b59e822014-08-06 20:14:46 +020086 self.reset()
piotr4089c1a2014-08-06 14:10:56 +020087 msg_ppm = pmt.from_double(0.0)
88 self.message_port_pub(pmt.intern("ppm"), msg_ppm)
Piotr K8cfe4c42014-08-07 17:03:10 +020089
Piotr K66bb3cd2014-08-13 19:04:57 +020090
Piotr K77f3b062014-08-07 23:04:43 +020091 def timed_reset(self):
Piotr K8cfe4c42014-08-07 17:03:10 +020092 if self.last_state != "synchronized":
93# print "conditional reset"
94 self.reset()
95 msg_ppm = pmt.from_double(0.0)
96 self.message_port_pub(pmt.intern("ppm"), msg_ppm)
97
piotr9b59e822014-08-06 20:14:46 +020098 def reset(self):
99 self.ppm_estimate = -1e6
100 self.counter = 0
101 self.first_measurement = True
Piotr K77f3b062014-08-07 23:04:43 +0200102