blob: 56fead917345b1557b0e768632c4f1286dd7f60a [file] [log] [blame]
piotr6b78abc2014-07-08 23:29:13 +02001#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3#
4# Copyright 2014 Piotr Krysik pkrysik@elka.pw.edu.pl
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 pylab import *
24from gnuradio import gr
25import pmt
piotr299ff0e2014-07-20 23:49:48 +020026from gsm.chirpz import ZoomFFT
piotr6b78abc2014-07-08 23:29:13 +020027
28class fcch_burst_tagger(gr.sync_block):
29 """
30 docstring for block fcch_burst_tagger
31 """
32 def __init__(self, OSR):
33 gr.sync_block.__init__(self,
34 name="fcch_burst_tagger",
35 in_sig=[complex64, float32],
36 out_sig=[complex64])
37
38 self.state=False
39 self.symbol_rate = 1625000/6
40 self.OSR=OSR
41 self.samp_rate = self.symbol_rate*OSR
42 self.burst_size = int(156.25*self.OSR)
43 self.guard_period = int(round(8.25*self.OSR))
44 self.block_size = self.burst_size+self.guard_period
45 self.processed_block_size = int(142*self.OSR)
piotr75c2f5c2014-07-20 23:51:28 +020046 self.set_history(self.block_size)
piotr6b78abc2014-07-08 23:29:13 +020047 self.set_output_multiple(self.guard_period)
48 self.prev_offset=0
49
50 #parameters of zoomfft frequency estimator
51 f1 = self.symbol_rate/4*0.9
52 f2 = self.symbol_rate/4*1.1
53 m=5000*self.OSR
54 self.zoomfft = ZoomFFT(self.processed_block_size, f1, f2, m, Fs=self.samp_rate)
55 self.f_axis = linspace(f1,f2,m)
56
57 def work(self, input_items, output_items):
58 in0=input_items[0]
59 output_items[0][:] = in0[self.history()-1:]
60
61 threshold = input_items[1][self.history()-1:]
62 threshold_diff = diff(concatenate([[0],threshold]))
63 up_to_high_indexes = nonzero(threshold_diff>0)[0]
64
65 up_to_high_idx=[]
66
piotr75c2f5c2014-07-20 23:51:28 +020067 for up_to_high_idx in up_to_high_indexes: #look for "high" value at the trigger
piotr6b78abc2014-07-08 23:29:13 +020068 if up_to_high_idx==0 and self.state==True: #if it's not transition from "low" to "high"
piotr75c2f5c2014-07-20 23:51:28 +020069 continue #then continue
piotr6b78abc2014-07-08 23:29:13 +020070 self.state=True #if found - change state
71
72 if self.state==True and up_to_high_idx and any(threshold_diff<0): #and look for transition from high to low
73 last_up_to_high_idx = up_to_high_idx
74 last_high_to_low_idx = nonzero(threshold_diff<0)[0][-1]
75
76 if last_high_to_low_idx-last_up_to_high_idx>0:
piotr75c2f5c2014-07-20 23:51:28 +020077 coarse_idx = int(last_high_to_low_idx+self.history()-self.block_size)
piotr6b78abc2014-07-08 23:29:13 +020078 inst_freq = angle(in0[coarse_idx:coarse_idx+self.block_size]*in0[coarse_idx-self.OSR:coarse_idx+self.block_size-self.OSR].conj())/(2*pi)*self.symbol_rate #instantaneus frequency estimate
79 precise_idx = self.find_best_position(inst_freq)
80# measured_freq = mean(inst_freq[precise_idx:precise_idx+self.processed_block_size])
81 expected_freq = self.symbol_rate/4
piotr75c2f5c2014-07-20 23:51:28 +020082
83 print "input_items:",len(in0)
84 print "coarse_idx",coarse_idx
85 print "coarse_idx+precise_idx",coarse_idx+precise_idx
86
piotr6b78abc2014-07-08 23:29:13 +020087 zoomed_spectrum = abs(self.zoomfft(in0[coarse_idx+precise_idx:coarse_idx+precise_idx+self.processed_block_size]))
88 measured_freq = self.f_axis[argmax(zoomed_spectrum)]
89 freq_offset = measured_freq - expected_freq
90 offset = self.nitems_written(0) + coarse_idx + precise_idx - self.guard_period
91 key = pmt.string_to_symbol("fcch")
92 value = pmt.from_double(freq_offset)
93 self.add_item_tag(0,offset, key, value)
94 self.state=False
95
96# Some additional plots and prints for debugging
97# print "coarse_idx+precise_idx",coarse_idx+precise_idx
98# print "offset-self.nitems_written(0):",offset-self.nitems_written(0)
99 print offset-self.prev_offset
100 self.prev_offset=offset
101 print "freq offset", freq_offset
102# freq_offset = measured_freq - expected_freq
103# plot(self.f_axis, zoomed_spectrum)
104# show()
105# plot(inst_freq[precise_idx:precise_idx+self.burst_size])
106# show()
107# plot(unwrap(angle(in0[coarse_idx+precise_idx:coarse_idx+precise_idx+self.burst_size])))
108# show()
109#
110 return len(output_items[0])
111
112 def find_best_position(self, inst_freq):
113 lowest_max_min_diff = 1e6 #1e6 - just some large value
114 start_pos = 0
115
piotr75c2f5c2014-07-20 23:51:28 +0200116 for ii in xrange(0,int(2*self.guard_period)):
piotr6b78abc2014-07-08 23:29:13 +0200117 min_inst_freq = min(inst_freq[ii:self.processed_block_size+ii-1]);
118 max_inst_freq = max(inst_freq[ii:self.processed_block_size+ii-1]);
119
120 if (lowest_max_min_diff > max_inst_freq - min_inst_freq):
121 lowest_max_min_diff = max_inst_freq - min_inst_freq;
122 start_pos = ii
123# print 'start_pos',start_pos
124
125# plot(xrange(start_pos,start_pos+self.processed_block_size),inst_freq[start_pos:start_pos+self.processed_block_size],'r.')
126# hold(True)
127# plot(inst_freq)
128# show()
129
130 return start_pos