Newer
Older
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
##################################################
# GNU Radio Python Flow Graph
# Title: Top Block
# Generated: Wed Feb 17 15:45:00 2016
##################################################
from gnuradio import blocks
from gnuradio import eng_notation
from gnuradio import filter
from gnuradio import gr
from gnuradio.eng_option import eng_option
from gnuradio.filter import firdes
from optparse import OptionParser
import osmosdr
import numpy as np
import time
class top_block(gr.top_block):
def __init__(self):
gr.top_block.__init__(self, "Top Block")
##################################################
# Variables
##################################################
self.cutoff_freq = cutoff_freq = 4e5
self.transition_width = transition_width = cutoff_freq/2
self.samp_rate = samp_rate = 2e6
self.freq = freq = 868e6
##################################################
# Message Queues
##################################################
# Queue
self.msgq_out = blocks_message_sink_0_msgq_out = gr.msg_queue(0)
##################################################
# Blocks
##################################################
self.osmosdr_source_0 = osmosdr.source( args="numchan=" + str(1) + " " + "" )
self.osmosdr_source_0.set_sample_rate(samp_rate)
self.osmosdr_source_0.set_center_freq(freq, 0)
self.osmosdr_source_0.set_freq_corr(0, 0)
self.osmosdr_source_0.set_dc_offset_mode(0, 0)
self.osmosdr_source_0.set_iq_balance_mode(0, 0)
self.osmosdr_source_0.set_gain_mode(False, 0)
self.osmosdr_source_0.set_gain(10, 0)
self.osmosdr_source_0.set_if_gain(20, 0)
self.osmosdr_source_0.set_bb_gain(20, 0)
self.osmosdr_source_0.set_antenna("", 0)
self.osmosdr_source_0.set_bandwidth(0, 0)
self.freq_xlating_fir_filter_xxx_0 = filter.freq_xlating_fir_filter_ccc(1, (firdes.low_pass(1, samp_rate, transition_width, cutoff_freq, firdes.WIN_HAMMING, 6.76)), 3.5e5, samp_rate)
self.blocks_threshold_ff_0 = blocks.threshold_ff(0.00686, 0.00686, 0)
self.blocks_message_sink_0 = blocks.message_sink(gr.sizeof_float*1, blocks_message_sink_0_msgq_out, False)
self.blocks_complex_to_mag_squared_0 = blocks.complex_to_mag_squared(1)
##################################################
# Connections
##################################################
self.connect((self.blocks_complex_to_mag_squared_0, 0), (self.blocks_threshold_ff_0, 0))
self.connect((self.blocks_threshold_ff_0, 0), (self.blocks_message_sink_0, 0))
self.connect((self.freq_xlating_fir_filter_xxx_0, 0), (self.blocks_complex_to_mag_squared_0, 0))
self.connect((self.osmosdr_source_0, 0), (self.freq_xlating_fir_filter_xxx_0, 0))
def get_cutoff_freq(self):
return self.cutoff_freq
def set_cutoff_freq(self, cutoff_freq):
self.cutoff_freq = cutoff_freq
self.set_transition_width(self.cutoff_freq/2)
self.freq_xlating_fir_filter_xxx_0.set_taps((firdes.low_pass(1, self.samp_rate, self.transition_width, self.cutoff_freq, firdes.WIN_HAMMING, 6.76)))
def get_transition_width(self):
return self.transition_width
def set_transition_width(self, transition_width):
self.transition_width = transition_width
self.freq_xlating_fir_filter_xxx_0.set_taps((firdes.low_pass(1, self.samp_rate, self.transition_width, self.cutoff_freq, firdes.WIN_HAMMING, 6.76)))
def get_samp_rate(self):
return self.samp_rate
def set_samp_rate(self, samp_rate):
self.samp_rate = samp_rate
self.freq_xlating_fir_filter_xxx_0.set_taps((firdes.low_pass(1, self.samp_rate, self.transition_width, self.cutoff_freq, firdes.WIN_HAMMING, 6.76)))
self.osmosdr_source_0.set_sample_rate(self.samp_rate)
def get_freq(self):
return self.freq
def set_freq(self, freq):
self.freq = freq
self.osmosdr_source_0.set_center_freq(self.freq, 0)
def main(top_block_cls=top_block, options=None):
tb = top_block_cls()
tb.start()
samples_count = 0
messages = []
time_added = 0
samples_len = 0
while True:
msg = tb.msgq_out.delete_head()
samples = np.fromstring(msg.to_string(), dtype='float32')
samples_len = len(samples)
if np.count_nonzero(samples) != 0:
string = ""
for i in samples:
string += str(int(i))
if len(messages) == 0:
block_samples_start = samples_count
messages.append(string)
time_added = time.time()
if time_added != 0 and (time.time()-time_added) > 0.5 :
msg = "".join(messages)
transmission_time = round(1/tb.samp_rate*(int(msg.count("1"))*1000), 2)
first_one = msg.index("1")
last_one = msg.rfind("1")
real_transmission_time = round(1/tb.samp_rate*(int(last_one-first_one)*1000), 2)
print(msg)
print("Transmission time: \t\t" + str(transmission_time) + "ms")
print("Real transmission time: \t" + str(real_transmission_time) + "ms\n")
print("Transmission block starts at sample: \t" + str(block_samples_start))
print("Transmission starts at sample: \t\t" + str(block_samples_start + first_one))
print("Sample count: \t\t\t\t" + str(samples_count))
print("\n\n")
messages = []
block_samples_start = 0
time_added = 0
samples_count += samples_len
if __name__ == '__main__':
main()