-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsense_path.py
More file actions
217 lines (170 loc) · 8.56 KB
/
sense_path.py
File metadata and controls
217 lines (170 loc) · 8.56 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env python
#
# Copyright 2005,2007 Free Software Foundation, Inc.
#
# This file is part of GNU Radio
#
# GNU Radio is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# GNU Radio is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Radio; see the file COPYING. If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
#
from gnuradio import gr, gru, eng_notation, optfir, window
from gnuradio.eng_option import eng_option
#from usrpm import usrp_dbid
import sys, struct
import math
class tune(gr.feval_dd):
"""
This class allows C++ code to callback into python.
"""
def __init__(self, tb):
gr.feval_dd.__init__(self)
self.tb = tb
def eval(self, ignore):
"""
This method is called from gr.bin_statistics_f when it wants to change
the center frequency. This method tunes the front end to the new center
frequency, and returns the new frequency as its result.
"""
try:
# We use this try block so that if something goes wrong from here
# down, at least we'll have a prayer of knowing what went wrong.
# Without this, you get a very mysterious:
#
# terminate called after throwing an instance of 'Swig::DirectorMethodException'
# Aborted
#
# message on stderr. Not exactly helpful ;)
new_freq = self.tb.set_next_freq()
return new_freq
except Exception, e:
print "tune: Exception: ", e
class parse_msg(object):
def __init__(self, msg):
self.center_freq = msg.arg1()
self.vlen = int(msg.arg2())
assert(msg.length() == self.vlen * gr.sizeof_float)
# FIXME consider using Numarray or NumPy vector
t = msg.to_string()
self.raw_data = t
self.data = struct.unpack('%df' % (self.vlen,), t)
class sense_path(gr.hier_block2):
def __init__(self, tuner_callback, options):
gr.hier_block2.__init__(self, "sense_path",
gr.io_signature(1, 1, gr.sizeof_gr_complex), # Input signature
gr.io_signature(0, 0, 0)) # Output signature
self.usrp_rate = options.channel_rate
self.usrp_tune = tuner_callback
self.threshold = options.threshold
#self.freq_step = options.chan_bandwidth
#self.min_freq = options.start_freq
#self.max_freq = options.end_freq
self.hold_freq = False
self.channels = [600000000, 620000000, 625000000, 640000000, 645000000, 650000000]
self.current_chan = 0
self.num_channels = len(self.channels) #(self.max_freq - self.min_freq)/self.freq_step
#if self.min_freq > self.max_freq:
# self.min_freq, self.max_freq = self.max_freq, self.min_freq # swap them
self.fft_size = options.sense_fft_size
if not options.real_time:
realtime = False
else:
# Attempt to enable realtime scheduling
r = gr.enable_realtime_scheduling()
if r == gr.RT_OK:
realtime = True
else:
realtime = False
print "Note: failed to enable realtime scheduling"
# build graph
s2v = gr.stream_to_vector(gr.sizeof_gr_complex, self.fft_size)
mywindow = window.blackmanharris(self.fft_size)
fft = gr.fft_vcc(self.fft_size, True, mywindow)
power = 0
for tap in mywindow:
power += tap*tap
c2mag = gr.complex_to_mag_squared(self.fft_size)
# FIXME the log10 primitive is dog slow
log = gr.nlog10_ff(10, self.fft_size,
-20*math.log10(self.fft_size)-10*math.log10(power/self.fft_size))
# Set the freq_step to 75% of the actual data throughput.
# This allows us to discard the bins on both ends of the spectrum.
#changed on 2011 May 31, MR -- maybe change back at some point
#self.min_center_freq = self.min_freq + self.freq_step/2
#nsteps = math.ceil((self.max_freq - self.min_freq) / self.freq_step)
#self.max_center_freq = self.min_center_freq + (nsteps * self.freq_step)
self.next_freq = self.channels[self.current_chan] #self.min_center_freq
tune_delay = max(0, int(round(options.tune_delay * self.usrp_rate / self.fft_size))) # in fft_frames
dwell_delay = max(1, int(round(options.dwell_delay * self.usrp_rate / self.fft_size))) # in fft_frames
self.msgq = gr.msg_queue(16)
self._tune_callback = tune(self) # hang on to this to keep it from being GC'd
self.stats = gr.bin_statistics_f(self.fft_size, self.msgq,
self._tune_callback, tune_delay, dwell_delay)
# FIXME leave out the log10 until we speed it up
#self.connect(self, s2v, fft, c2mag, log, stats)
self.connect(self, s2v, fft, c2mag, self.stats)
def set_next_freq(self):
if self.hold_freq:
return 0 #current_freq
target_freq = self.next_freq
self.current_chan = (self.current_chan + 1) % self.num_channels
self.next_freq = self.channels[self.current_chan] #self.next_freq + self.freq_step
#if self.next_freq >= self.max_center_freq:
# self.next_freq = self.min_center_freq
if not self.set_freq(target_freq):
print "Failed to set frequency to", target_freq
return target_freq
def set_freq(self, target_freq):
"""
Set the center frequency we're interested in.
@param target_freq: frequency in Hz
@rypte: bool
Tuning is a two step process. First we ask the front-end to
tune as close to the desired frequency as it can. Then we use
the result of that operation and our target_frequency to
determine the value for the digital down converter.
"""
#updated 2011 May 31, MR
#return self.u.tune(0, self.subdev, target_freq)
return self.usrp_tune(target_freq)
def set_hold_freq(self, hold):
self.hold_freq = hold
self.set_next_freq()
def update_samp_rate(self, samp_rate):
self.usrp_rate = samp_rate
#self.freq_step = samp_rate
def add_options(normal, expert):
"""
Add sense-path specific options to the Options parser
"""
normal.add_option("", "--tune-delay", type="eng_float", default=.02, metavar="SECS",
help="time to delay (in seconds) after changing frequency [default=%default]")
normal.add_option("", "--dwell-delay", type="eng_float", default=.04, metavar="SECS",
help="time to dwell (in seconds) at a given frequncy [default=%default]")
normal.add_option("-F", "--sense-fft-size", type="int", default=512,
help="specify number of FFT bins [default=%default]")
normal.add_option("", "--threshold", type="eng_float", default=-54,
help="set detection threshold [default=%default]")
expert.add_option("", "--real-time", action="store_true", default=False,
help="Attempt to enable real-time scheduling")
normal.add_option("", "--num-tests", type="intx", default=1,
help="set the number of times to test the frequency band [default=%default]")
#normal.add_option("", "--start-freq", type="eng_float", default="631M",
# help="set the start of the frequency band to sense over [default=%default]")
#normal.add_option("", "--end-freq", type="eng_float", default="671M",
# help="set the end of the frequency band to sense over [default=%default]")
expert.add_option("", "--chan-bandwidth", type="eng_float", default=6000000,
help="set the sample rate of each 6MHz channel [default=%default]")
# Make a static method to call before instantiation
add_options = staticmethod(add_options)