3 from gnuradio import gr, gru, modulation_utils
4 from gnuradio import eng_notation
5 from gnuradio.eng_option import eng_option
6 from optparse import OptionParser
7 import random, time, struct, sys, os, math
9 from threading import Thread
12 from transmit_path import transmit_path
13 from receive_path import receive_path
16 from gnuradio.qtgui import qtgui
17 from PyQt4 import QtGui, QtCore
20 print "Please install gr-qtgui."
24 from qt_digital_window import Ui_DigitalWindow
26 print "Error: could not find qt_digital_window.py:"
27 print "\t\"pyuic4 qt_digital_window.ui -o qt_digital_window.py\""
35 # ////////////////////////////////////////////////////////////////////
36 # Define the QT Interface and Control Dialog
37 # ////////////////////////////////////////////////////////////////////
40 class dialog_box(QtGui.QMainWindow):
41 def __init__(self, snkTx, snkRx, fg, parent=None):
43 QtGui.QWidget.__init__(self, parent)
44 self.gui = Ui_DigitalWindow()
45 self.gui.setupUi(self)
49 self.set_sample_rate(self.fg.sample_rate())
51 self.set_snr(self.fg.snr())
52 self.set_frequency(self.fg.frequency_offset())
53 self.set_time_offset(self.fg.timing_offset())
55 self.set_gain_mu(self.fg.rx_gain_mu())
56 self.set_alpha(self.fg.rx_alpha())
58 # Add the qtsnk widgets to the hlayout box
59 self.gui.sinkLayout.addWidget(snkTx)
60 self.gui.sinkLayout.addWidget(snkRx)
63 # Connect up some signals
64 self.connect(self.gui.pauseButton, QtCore.SIGNAL("clicked()"),
67 self.connect(self.gui.sampleRateEdit, QtCore.SIGNAL("editingFinished()"),
68 self.sampleRateEditText)
70 self.connect(self.gui.snrEdit, QtCore.SIGNAL("editingFinished()"),
72 self.connect(self.gui.freqEdit, QtCore.SIGNAL("editingFinished()"),
74 self.connect(self.gui.timeEdit, QtCore.SIGNAL("editingFinished()"),
77 self.connect(self.gui.gainMuEdit, QtCore.SIGNAL("editingFinished()"),
79 self.connect(self.gui.alphaEdit, QtCore.SIGNAL("editingFinished()"),
82 # Build a timer to update the packet number and PER fields
83 self.update_delay = 250 # time between updating packet rate fields
84 self.pkt_timer = QtCore.QTimer(self)
85 self.connect(self.pkt_timer, QtCore.SIGNAL("timeout()"),
86 self.updatePacketInfo)
87 self.pkt_timer.start(self.update_delay)
90 if(self.gui.pauseButton.text() == "Pause"):
93 self.gui.pauseButton.setText("Unpause")
96 self.gui.pauseButton.setText("Pause")
98 # Accessor functions for Gui to manipulate system parameters
99 def set_sample_rate(self, sr):
100 ssr = eng_notation.num_to_str(sr)
101 self.gui.sampleRateEdit.setText(QtCore.QString("%1").arg(ssr))
103 def sampleRateEditText(self):
105 rate = self.gui.sampleRateEdit.text().toAscii()
106 srate = eng_notation.str_to_num(rate)
107 #self.fg.set_sample_rate(srate)
112 # Accessor functions for Gui to manipulate channel model
113 def set_snr(self, snr):
114 self.gui.snrEdit.setText(QtCore.QString("%1").arg(snr))
116 def set_frequency(self, fo):
117 self.gui.freqEdit.setText(QtCore.QString("%1").arg(fo))
119 def set_time_offset(self, to):
120 self.gui.timeEdit.setText(QtCore.QString("%1").arg(to))
122 def snrEditText(self):
124 snr = self.gui.snrEdit.text().toDouble()[0]
129 def freqEditText(self):
131 freq = self.gui.freqEdit.text().toDouble()[0]
132 self.fg.set_frequency_offset(freq)
136 def timeEditText(self):
138 to = self.gui.timeEdit.text().toDouble()[0]
139 self.fg.set_timing_offset(to)
144 # Accessor functions for Gui to manipulate receiver parameters
145 def set_gain_mu(self, gain):
146 self.gui.gainMuEdit.setText(QtCore.QString("%1").arg(gain))
148 def set_alpha(self, alpha):
149 self.gui.alphaEdit.setText(QtCore.QString("%1").arg(alpha))
151 def alphaEditText(self):
153 alpha = self.gui.alphaEdit.text().toDouble()[0]
154 self.fg.set_rx_alpha(alpha)
158 def gainMuEditText(self):
160 gain = self.gui.gainMuEdit.text().toDouble()[0]
161 self.fg.set_rx_gain_mu(gain)
165 # Accessor functions for packet error reporting
166 def updatePacketInfo(self):
167 # Pull these globals in from the main thread
168 global n_rcvd, n_right, pktno
170 per = float(n_rcvd - n_right)/float(pktno)
171 self.gui.pktsRcvdEdit.setText(QtCore.QString("%1").arg(n_rcvd))
172 self.gui.pktsCorrectEdit.setText(QtCore.QString("%1").arg(n_right))
173 self.gui.perEdit.setText(QtCore.QString("%1").arg(per))
177 # ////////////////////////////////////////////////////////////////////
178 # Define the GNU Radio Top Block
179 # ////////////////////////////////////////////////////////////////////
183 class my_top_block(gr.top_block):
184 def __init__(self, mod_class, demod_class, rx_callback, options):
185 gr.top_block.__init__(self)
187 self._sample_rate = options.sample_rate
191 self.gui_on = options.gui
193 self._frequency_offset = options.frequency_offset
194 self._timing_offset = options.timing_offset
195 self._tx_amplitude = options.tx_amplitude
196 self._snr_dB = options.snr
198 self._noise_voltage = self.get_noise_voltage(self._snr_dB)
200 self.txpath = transmit_path(mod_class, options)
201 self.throttle = gr.throttle(gr.sizeof_gr_complex, self.sample_rate())
202 self.rxpath = receive_path(demod_class, rx_callback, options)
204 # FIXME: do better exposure to lower issues for control
205 self._gain_mu = self.rxpath.packet_receiver._demodulator._mm_gain_mu
206 self._alpha = self.rxpath.packet_receiver._demodulator._costas_alpha
209 self.channel = gr.channel_model(self._noise_voltage,
210 self.frequency_offset(),
211 self.timing_offset())
213 if options.discontinuous:
215 self.zeros = gr.vector_source_c(z, True)
216 packet_size = 5*((4+8+4+1500+4) * 8)
217 self.mux = gr.stream_mux(gr.sizeof_gr_complex, [packet_size-0, int(9e5)])
220 self.connect(self.txpath, self.throttle, (self.mux,0))
221 self.connect(self.zeros, (self.mux,1))
222 self.connect(self.mux, self.channel, self.rxpath)
225 self.connect(self.txpath, self.throttle, self.channel, self.rxpath)
228 self.qapp = QtGui.QApplication(sys.argv)
231 self.snk_tx = qtgui.sink_c(fftsize, gr.firdes.WIN_BLACKMAN_hARRIS,
233 "Tx", True, True, False, True, True)
234 self.snk_rx = qtgui.sink_c(fftsize, gr.firdes.WIN_BLACKMAN_hARRIS,
236 "Rx", True, True, False, True, True)
237 self.snk_err = qtgui.sink_f(fftsize, gr.firdes.WIN_BLACKMAN_hARRIS,
239 "Error", True, True, False, False, False)
241 self.snk_tx.set_frequency_axis(-80, 0)
242 self.snk_rx.set_frequency_axis(-60, 20)
244 # Connect to the QT sinks
245 # FIXME: make better exposure to receiver from rxpath
246 #self.freq_recov = self.rxpath.packet_receiver._demodulator.clock_recov
247 self.time_recov = self.rxpath.packet_receiver._demodulator.time_recov
248 self.connect(self.channel, self.snk_tx)
249 self.connect(self.time_recov, self.snk_rx)
250 self.connect((self.time_recov, 1), self.snk_err)
252 pyTxQt = self.snk_tx.pyqwidget()
253 pyTx = sip.wrapinstance(pyTxQt, QtGui.QWidget)
255 pyRxQt = self.snk_rx.pyqwidget()
256 pyRx = sip.wrapinstance(pyRxQt, QtGui.QWidget)
258 pyErrQt = self.snk_err.pyqwidget()
259 pyErr = sip.wrapinstance(pyRxQt, QtGui.QWidget)
261 self.main_box = dialog_box(pyTx, pyRx, self)
266 self.connect(self.txpath, self.throttle, self.rxpath)
271 def sample_rate(self):
272 return self._sample_rate
274 def set_sample_rate(self, sr):
275 self._sample_rate = sr
276 #self.throttle.set_samples_per_second(self._sample_rate)
278 # Channel Model Parameters
282 def set_snr(self, snr):
284 self._noise_voltage = self.get_noise_voltage(self._snr_dB)
285 self.channel.set_noise_voltage(self._noise_voltage)
287 def get_noise_voltage(self, SNR):
288 snr = 10.0**(SNR/10.0)
289 power_in_signal = abs(self._tx_amplitude)**2
290 noise_power = power_in_signal/snr
291 noise_voltage = math.sqrt(noise_power)
294 def frequency_offset(self):
295 return self._frequency_offset * self.sample_rate()
297 def set_frequency_offset(self, fo):
298 self._frequency_offset = fo / self.sample_rate()
299 self.channel.set_frequency_offset(self._frequency_offset)
301 def timing_offset(self):
302 return self._timing_offset
304 def set_timing_offset(self, to):
305 self._timing_offset = to
306 self.channel.set_timing_offset(self._timing_offset)
309 # Receiver Parameters
310 def rx_gain_mu(self):
313 def rx_gain_omega(self):
314 return self.gain_omega
316 def set_rx_gain_mu(self, gain):
318 self.gain_omega = .25 * self._gain_mu * self._gain_mu
319 #self.time_recov.set_gain_mu(self._gain_mu)
320 #self.time_recov.set_gain_omega(self.gain_omega)
321 self.time_recov.set_gain(self._gain_mu)
329 def set_rx_alpha(self, alpha):
331 self.beta = .25 * self._alpha * self._alpha
332 #self.freq_recov.set_alpha(self._alpha)
333 #self.freq_recov.set_beta(self.beta)
337 # /////////////////////////////////////////////////////////////////////////////
338 # Thread to handle the packet sending procedure
339 # Operates in parallel with qApp.exec_()
340 # /////////////////////////////////////////////////////////////////////////////
344 class th_send(Thread):
345 def __init__(self, send_fnc, megs, sz):
346 Thread.__init__(self)
348 self.nbytes = int(1e6 * megs)
349 self.pkt_size = int(sz)
352 # generate and send packets
356 while n < self.nbytes:
357 self.send(struct.pack('!H', pktno & 0xffff) +
358 (self.pkt_size - 2) * chr(pktno & 0xff))
369 # /////////////////////////////////////////////////////////////////////////////
371 # /////////////////////////////////////////////////////////////////////////////
377 global n_rcvd, n_right, pktno
383 def rx_callback(ok, payload):
384 global n_rcvd, n_right, pktno
385 (pktno,) = struct.unpack('!H', payload[0:2])
391 print "ok = %5s pktno = %4d n_rcvd = %4d n_right = %4d" % (
392 ok, pktno, n_rcvd, n_right)
395 def send_pkt(payload='', eof=False):
396 return tb.txpath.send_pkt(payload, eof)
398 mods = modulation_utils.type_1_mods()
399 demods = modulation_utils.type_1_demods()
401 parser = OptionParser(option_class=eng_option, conflict_handler="resolve")
402 expert_grp = parser.add_option_group("Expert")
403 channel_grp = parser.add_option_group("Channel")
405 parser.add_option("-m", "--modulation", type="choice", choices=mods.keys(),
407 help="Select modulation from: %s [default=%%default]"
408 % (', '.join(mods.keys()),))
410 parser.add_option("-s", "--size", type="eng_float", default=1500,
411 help="set packet size [default=%default]")
412 parser.add_option("-M", "--megabytes", type="eng_float", default=1.0,
413 help="set megabytes to transmit [default=%default]")
414 parser.add_option("","--discontinuous", action="store_true", default=False,
415 help="enable discontinous transmission (bursts of 5 packets)")
416 parser.add_option("-G", "--gui", action="store_true", default=False,
417 help="Turn on the GUI [default=%default]")
419 channel_grp.add_option("", "--sample-rate", type="eng_float", default=1e5,
420 help="set speed of channel/simulation rate to RATE [default=%default]")
421 channel_grp.add_option("", "--snr", type="eng_float", default=30,
422 help="set the SNR of the channel in dB [default=%default]")
423 channel_grp.add_option("", "--frequency-offset", type="eng_float", default=0,
424 help="set frequency offset introduced by channel [default=%default]")
425 channel_grp.add_option("", "--timing-offset", type="eng_float", default=1.0,
426 help="set timing offset introduced by channel [default=%default]")
427 channel_grp.add_option("", "--seed", action="store_true", default=False,
428 help="use a random seed for AWGN noise [default=%default]")
430 transmit_path.add_options(parser, expert_grp)
431 receive_path.add_options(parser, expert_grp)
433 for mod in mods.values():
434 mod.add_options(expert_grp)
435 for demod in demods.values():
436 demod.add_options(expert_grp)
438 (options, args) = parser.parse_args ()
444 r = gr.enable_realtime_scheduling()
446 print "Warning: failed to enable realtime scheduling"
448 # Create an instance of a hierarchical block
449 tb = my_top_block(mods[options.modulation],
450 demods[options.modulation],
451 rx_callback, options)
454 packet_sender = th_send(send_pkt, options.megabytes, options.size)
455 packet_sender.start()
461 # Process until done; hack in to the join to stop on an interrupt
462 while(packet_sender.isAlive()):
464 packet_sender.join(1)
465 except KeyboardInterrupt:
469 if __name__ == '__main__':
472 except KeyboardInterrupt: