piotr | 4089c1a | 2014-08-06 14:10:56 +0200 | [diff] [blame] | 1 | #!/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 | |
| 22 | from numpy import * |
| 23 | from gnuradio import gr |
| 24 | import pmt |
| 25 | |
| 26 | class 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)) |
| 52 | |
piotr | d6d6687 | 2014-08-06 15:20:33 +0200 | [diff] [blame] | 53 | if state == "fcch_search": |
piotr | 4089c1a | 2014-08-06 14:10:56 +0200 | [diff] [blame] | 54 | msg_ppm = pmt.from_double(ppm) |
| 55 | self.message_port_pub(pmt.intern("ppm"), msg_ppm) |
| 56 | |
| 57 | if state == "synchronized": |
| 58 | if self.first_measurement: |
| 59 | self.ppm_estimate = ppm |
| 60 | self.first_measurement = False |
| 61 | else: |
| 62 | self.ppm_estimate = (1-self.alfa)*self.ppm_estimate+self.alfa*ppm |
| 63 | |
| 64 | if self.counter == 5: |
| 65 | self.counter = 0 |
| 66 | msg_ppm = pmt.from_double(ppm) |
| 67 | self.message_port_pub(pmt.intern("ppm"), msg_ppm) |
| 68 | else: |
| 69 | self.counter=self.counter+1 |
| 70 | |
| 71 | if state == "sync_loss": |
| 72 | self.ppm_estimate = -1e6 |
| 73 | self.counter = 0 |
| 74 | self.first_measurement = True |
| 75 | msg_ppm = pmt.from_double(0.0) |
| 76 | self.message_port_pub(pmt.intern("ppm"), msg_ppm) |
piotr | d6d6687 | 2014-08-06 15:20:33 +0200 | [diff] [blame] | 77 | |