diff options
Diffstat (limited to 'grc/src/grc_gnuradio/blks2')
-rw-r--r-- | grc/src/grc_gnuradio/blks2/Makefile.am | 4 | ||||
-rw-r--r-- | grc/src/grc_gnuradio/blks2/__init__.py | 11 | ||||
-rw-r--r-- | grc/src/grc_gnuradio/blks2/error_rate.py | 45 | ||||
-rw-r--r-- | grc/src/grc_gnuradio/blks2/packet.py | 72 | ||||
-rw-r--r-- | grc/src/grc_gnuradio/blks2/queue.py | 71 | ||||
-rw-r--r-- | grc/src/grc_gnuradio/blks2/selector.py | 85 |
6 files changed, 141 insertions, 147 deletions
diff --git a/grc/src/grc_gnuradio/blks2/Makefile.am b/grc/src/grc_gnuradio/blks2/Makefile.am index 307ec863f..e3a0b8a38 100644 --- a/grc/src/grc_gnuradio/blks2/Makefile.am +++ b/grc/src/grc_gnuradio/blks2/Makefile.am @@ -19,9 +19,9 @@ # Boston, MA 02110-1301, USA. # -include $(top_srcdir)/Makefile.common +include $(top_srcdir)/grc/Makefile.inc -ourpythondir = $(pythondir)/grc_gnuradio/blks2 +ourpythondir = $(grc_gnuradio_prefix)/blks2 ourpython_PYTHON = \ __init__.py \ diff --git a/grc/src/grc_gnuradio/blks2/__init__.py b/grc/src/grc_gnuradio/blks2/__init__.py index cd1b793c4..a26228f88 100644 --- a/grc/src/grc_gnuradio/blks2/__init__.py +++ b/grc/src/grc_gnuradio/blks2/__init__.py @@ -1,22 +1,22 @@ # Copyright 2008 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 queue import queue_sink_thread from queue import queue_sink_c, queue_sink_f, queue_sink_i, queue_sink_s, queue_sink_b @@ -25,4 +25,3 @@ from queue import queue_source_c, queue_source_f, queue_source_i, queue_source_s from selector import selector, valve from packet import packet_encoder, packet_decoder from error_rate import error_rate - diff --git a/grc/src/grc_gnuradio/blks2/error_rate.py b/grc/src/grc_gnuradio/blks2/error_rate.py index eb09940cb..9b2df58ef 100644 --- a/grc/src/grc_gnuradio/blks2/error_rate.py +++ b/grc/src/grc_gnuradio/blks2/error_rate.py @@ -1,22 +1,22 @@ # Copyright 2008 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. -# +# default_win_size = 1000 @@ -31,7 +31,7 @@ class input_watcher(_threading.Thread): """ Read samples from the message queue and hand them to the callback. """ - + def __init__(self, msgq, callback): self._msgq = msgq self._callback = callback @@ -39,7 +39,7 @@ class input_watcher(_threading.Thread): self.setDaemon(1) self.keep_running = True self.start() - + def run(self): r = '' while True: @@ -49,27 +49,27 @@ class input_watcher(_threading.Thread): s = r + msg.to_string() i = (nitems-nitems%2)*itemsize r = s[i:] - s = s[:i] + s = s[:i] samples = numpy.fromstring(s, numpy.int8) - self._callback(samples) - + self._callback(samples) + class error_rate(gr.hier_block2): """ Sample the incoming data streams (byte) and calculate the bit or symbol error rate. Write the running rate to the output data stream (float). """ - - def __init__(self, type='BER', win_size=default_win_size, bits_per_symbol=2): - """! + + def __init__(self, type='BER', win_size=default_win_size, bits_per_symbol=2): + """ Error rate constructor. @param type a string 'BER' or 'SER' @param win_size the number of samples to calculate over @param bits_per_symbol the number of information bits per symbol (BER only) - """ + """ #init gr.hier_block2.__init__( - self, 'error_rate', - gr.io_signature(2, 2, gr.sizeof_char), + self, 'error_rate', + gr.io_signature(2, 2, gr.sizeof_char), gr.io_signature(1, 1, gr.sizeof_float), ) assert type in ('BER', 'SER') @@ -95,12 +95,12 @@ class error_rate(gr.hier_block2): self.connect((self, 0), (inter, 0)) self.connect((self, 1), (inter, 1)) self.connect(inter, msg_sink) - + def _handler_ber(self, samples): num = len(samples)/2 arr = numpy.zeros(num, numpy.float32) for i in range(num): - old_err = self._err_array[self._err_index] + old_err = self._err_array[self._err_index] #record error self._err_array[self._err_index] = _1s_counts[samples[i*2] ^ samples[i*2 + 1]] self._num_errs = self._num_errs + self._err_array[self._err_index] - old_err @@ -111,13 +111,13 @@ class error_rate(gr.hier_block2): arr[i] = float(self._num_errs)/float(self._num_samps*self._bits_per_symbol) #write message msg = gr.message_from_string(arr.tostring(), 0, gr.sizeof_float, num) - self._msgq_source.insert_tail(msg) - + self._msgq_source.insert_tail(msg) + def _handler_ser(self, samples): num = len(samples)/2 arr = numpy.zeros(num, numpy.float32) for i in range(num): - old_err = self._err_array[self._err_index] + old_err = self._err_array[self._err_index] #record error ref = samples[i*2] res = samples[i*2 + 1] @@ -134,5 +134,4 @@ class error_rate(gr.hier_block2): arr[i] = float(self._num_errs)/float(self._num_samps) #write message msg = gr.message_from_string(arr.tostring(), 0, gr.sizeof_float, num) - self._msgq_source.insert_tail(msg) - + self._msgq_source.insert_tail(msg) diff --git a/grc/src/grc_gnuradio/blks2/packet.py b/grc/src/grc_gnuradio/blks2/packet.py index 5276de109..fcdfb9ae2 100644 --- a/grc/src/grc_gnuradio/blks2/packet.py +++ b/grc/src/grc_gnuradio/blks2/packet.py @@ -1,22 +1,22 @@ # Copyright 2008 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, packet_utils import gnuradio.gr.gr_threading as _threading @@ -31,11 +31,11 @@ DEFAULT_MSGQ_LIMIT = 2 DEFAULT_THRESHOLD = 12 ####################################################################################### -## Packet Encoder +## Packet Encoder ####################################################################################### class _packet_encoder_thread(_threading.Thread): - + def __init__(self, msgq, payload_length, send): self._msgq = msgq self._payload_length = payload_length @@ -44,24 +44,24 @@ class _packet_encoder_thread(_threading.Thread): self.setDaemon(1) self.keep_running = True self.start() - + def run(self): sample = '' #residual sample while self.keep_running: msg = self._msgq.delete_head() #blocking read of message queue - sample = sample + msg.to_string() #get the body of the msg as a string - while len(sample) >= self._payload_length: + sample = sample + msg.to_string() #get the body of the msg as a string + while len(sample) >= self._payload_length: payload = sample[0:self._payload_length] - sample = sample[self._payload_length:] - self._send(payload) + sample = sample[self._payload_length:] + self._send(payload) class packet_encoder(gr.hier_block2): """ Hierarchical block for wrapping packet-based modulators. """ - + def __init__(self, item_size_in, samples_per_symbol, bits_per_symbol, access_code='', pad_for_usrp=True, payload_length=-1): - """! + """ packet_mod constructor. @param item_size_in the size of the input data stream in bytes @param samples_per_symbol number of samples per symbol @@ -79,8 +79,8 @@ class packet_encoder(gr.hier_block2): access_code = packet_utils.default_access_code if not packet_utils.is_1_0_string(access_code): raise ValueError, "Invalid access_code %r. Must be string of 1's and 0's" % (access_code,) - self._access_code = access_code - self._pad_for_usrp = pad_for_usrp + self._access_code = access_code + self._pad_for_usrp = pad_for_usrp if payload_length < 0: #get payload length payload_length = DEFAULT_PAYLOAD_LEN if payload_length%self._item_size_in != 0: #verify that packet length is a multiple of the stream size @@ -88,12 +88,12 @@ class packet_encoder(gr.hier_block2): self._payload_length = payload_length #create blocks msg_source = gr.message_source(gr.sizeof_char, DEFAULT_MSGQ_LIMIT) - self._msgq_out = msg_source.msgq() + self._msgq_out = msg_source.msgq() self._msgq_in = gr.msg_queue(DEFAULT_MSGQ_LIMIT) - msg_sink = gr.message_sink(self._item_size_in, self._msgq_in, False) #False -> blocking + msg_sink = gr.message_sink(self._item_size_in, self._msgq_in, False) #False -> blocking #initialize hier2 gr.hier_block2.__init__( - self, + self, "packet_encoder", gr.io_signature(1, 1, self._item_size_in), # Input signature gr.io_signature(1, 1, gr.sizeof_char) # Output signature @@ -103,12 +103,12 @@ class packet_encoder(gr.hier_block2): self.connect(msg_source, self) #start thread _packet_encoder_thread(self._msgq_in, self._payload_length, self._send_packet) - + def _send_packet(self, payload): - """! + """ Wrap the payload in a packet and push onto the message queue. @param payload string, data to send - """ + """ packet = packet_utils.make_packet( payload, self._samples_per_symbol, @@ -118,13 +118,13 @@ class packet_encoder(gr.hier_block2): ) msg = gr.message_from_string(packet) self._msgq_out.insert_tail(msg) - + ####################################################################################### -## Packet Decoder +## Packet Decoder ####################################################################################### class _packet_decoder_thread(_threading.Thread): - + def __init__(self, msgq, callback): _threading.Thread.__init__(self) self.setDaemon(1) @@ -144,9 +144,9 @@ class packet_decoder(gr.hier_block2): """ Hierarchical block for wrapping packet-based demodulators. """ - + def __init__(self, item_size_out, access_code='', threshold=-1): - """! + """ packet_demod constructor. @param item_size_out the size of the output data stream in bytes @param access_code AKA sync vector @@ -159,19 +159,19 @@ class packet_decoder(gr.hier_block2): access_code = packet_utils.default_access_code if not packet_utils.is_1_0_string(access_code): raise ValueError, "Invalid access_code %r. Must be string of 1's and 0's" % (access_code,) - self._access_code = access_code + self._access_code = access_code #threshold - if threshold < 0: threshold = DEFAULT_THRESHOLD - self._threshold = threshold + if threshold < 0: threshold = DEFAULT_THRESHOLD + self._threshold = threshold #blocks self._msgq_in = gr.msg_queue(DEFAULT_MSGQ_LIMIT) #holds packets from the PHY correlator = gr.correlate_access_code_bb(self._access_code, self._threshold) - framer_sink = gr.framer_sink_1(self._msgq_in) + framer_sink = gr.framer_sink_1(self._msgq_in) msg_source = gr.message_source(self._item_size_out, DEFAULT_MSGQ_LIMIT) - self._msgq_out = msg_source.msgq() + self._msgq_out = msg_source.msgq() #initialize hier2 gr.hier_block2.__init__( - self, + self, "packet_decoder", gr.io_signature(1, 1, gr.sizeof_char), # Input signature gr.io_signature(1, 1, self._item_size_out) # Output signature @@ -183,12 +183,10 @@ class packet_decoder(gr.hier_block2): _packet_decoder_thread(self._msgq_in, self._recv_packet) def _recv_packet(self, ok, payload): - """! + """ Extract the payload from the packet and push onto message queue. @param ok boolean ok @param payload data received """ msg = gr.message_from_string(payload, 0, self._item_size_out, len(payload)/self._item_size_out) - if ok: self._msgq_out.insert_tail(msg) - - + if ok: self._msgq_out.insert_tail(msg) diff --git a/grc/src/grc_gnuradio/blks2/queue.py b/grc/src/grc_gnuradio/blks2/queue.py index cec35e52a..25ea1c616 100644 --- a/grc/src/grc_gnuradio/blks2/queue.py +++ b/grc/src/grc_gnuradio/blks2/queue.py @@ -1,37 +1,37 @@ # Copyright 2008 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 import gnuradio.gr.gr_threading as _threading import numpy ####################################################################################### -## Queue Sink Thread +## Queue Sink Thread ####################################################################################### class queue_sink_thread(_threading.Thread): - """! + """ Read samples from the queue sink and execute the callback. """ - + def __init__(self, queue_sink, callback): - """! + """ Queue sink thread contructor. @param queue_sink the queue to pop messages from @param callback the function of one argument @@ -42,31 +42,31 @@ class queue_sink_thread(_threading.Thread): self.setDaemon(1) self.keep_running = True self.start() - + def run(self): while self.keep_running: self._callback(self._queue_sink.pop()) ####################################################################################### -## Queue Sink +## Queue Sink ####################################################################################### class _queue_sink_base(gr.hier_block2): - """! + """ Queue sink base, a queue sink for any size queue. Easy read access to a gnuradio data stream from python. Call pop to read a sample from a gnuradio data stream. Samples are cast as python data types, complex, float, or int. """ - + def __init__(self, vlen=1): - """! + """ Queue sink base contructor. @param vlen the vector length """ self._vlen = vlen #initialize hier2 gr.hier_block2.__init__( - self, + self, "queue_sink", gr.io_signature(1, 1, self._item_size*self._vlen), # Input signature gr.io_signature(0, 0, 0) # Output signature @@ -77,9 +77,9 @@ class _queue_sink_base(gr.hier_block2): #connect self.connect(self, message_sink) self.arr = '' - + def pop(self): - """! + """ Pop a new sample off the front of the queue. @return a new sample """ @@ -95,59 +95,59 @@ class _queue_sink_base(gr.hier_block2): class queue_sink_c(_queue_sink_base): _item_size = gr.sizeof_gr_complex _numpy = numpy.complex64 - def _cast(self, arg): return complex(arg.real, arg.imag) - + def _cast(self, arg): return complex(arg.real, arg.imag) + class queue_sink_f(_queue_sink_base): _item_size = gr.sizeof_float _numpy = numpy.float32 _cast = float - + class queue_sink_i(_queue_sink_base): _item_size = gr.sizeof_int _numpy = numpy.int32 _cast = int - + class queue_sink_s(_queue_sink_base): _item_size = gr.sizeof_short _numpy = numpy.int16 _cast = int - + class queue_sink_b(_queue_sink_base): _item_size = gr.sizeof_char _numpy = numpy.int8 _cast = int - + ####################################################################################### -## Queue Source +## Queue Source ####################################################################################### class _queue_source_base(gr.hier_block2): - """! + """ Queue source base, a queue source for any size queue. Easy write access to a gnuradio data stream from python. Call push to to write a sample into the gnuradio data stream. """ - + def __init__(self, vlen=1): - """! + """ Queue source base contructor. @param vlen the vector length """ self._vlen = vlen #initialize hier2 gr.hier_block2.__init__( - self, + self, "queue_source", gr.io_signature(0, 0, 0), # Input signature gr.io_signature(1, 1, self._item_size*self._vlen) # Output signature ) #create message sink - message_source = gr.message_source(self._item_size*self._vlen, 1) + message_source = gr.message_source(self._item_size*self._vlen, 1) self._msgq = message_source.msgq() #connect self.connect(message_source, self) - + def push(self, item): - """! + """ Push an item into the back of the queue. @param item the item """ @@ -159,20 +159,19 @@ class _queue_source_base(gr.hier_block2): class queue_source_c(_queue_source_base): _item_size = gr.sizeof_gr_complex _numpy = numpy.complex64 - + class queue_source_f(_queue_source_base): _item_size = gr.sizeof_float _numpy = numpy.float32 - + class queue_source_i(_queue_source_base): _item_size = gr.sizeof_int _numpy = numpy.int32 - + class queue_source_s(_queue_source_base): _item_size = gr.sizeof_short _numpy = numpy.int16 - + class queue_source_b(_queue_source_base): _item_size = gr.sizeof_char _numpy = numpy.int8 - diff --git a/grc/src/grc_gnuradio/blks2/selector.py b/grc/src/grc_gnuradio/blks2/selector.py index 787f6547f..0997a5ce7 100644 --- a/grc/src/grc_gnuradio/blks2/selector.py +++ b/grc/src/grc_gnuradio/blks2/selector.py @@ -1,92 +1,92 @@ #!/usr/bin/env python # # Copyright 2008 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 - + class selector(gr.hier_block2): """A hier2 block with N inputs and M outputs, where data is only forwarded through input n to output m.""" - def __init__(self, item_size, num_inputs, num_outputs, input_index, output_index): - """! + def __init__(self, item_size, num_inputs, num_outputs, input_index, output_index): + """ SelectorHelper constructor. @param item_size the size of the gr data stream in bytes @param num_inputs the number of inputs (integer) @param num_outputs the number of outputs (integer) @param input_index the index for the source data @param output_index the index for the destination data - """ + """ gr.hier_block2.__init__( - self, 'selector', - gr.io_signature(num_inputs, num_inputs, item_size), + self, 'selector', + gr.io_signature(num_inputs, num_inputs, item_size), gr.io_signature(num_outputs, num_outputs, item_size), ) - #terminator blocks for unused inputs and outputs + #terminator blocks for unused inputs and outputs self.input_terminators = [gr.null_sink(item_size)] * num_inputs self.output_terminators = [gr.head(item_size, 0)] * num_outputs self.copy = None - #connections + #connections for i in range(num_inputs): self.connect((self, i), self.input_terminators[i]) - for i in range(num_outputs): self.connect(gr.null_source(item_size), self.output_terminators[i], (self, i)) - self.item_size = item_size + for i in range(num_outputs): self.connect(gr.null_source(item_size), self.output_terminators[i], (self, i)) + self.item_size = item_size self.input_index = input_index - self.output_index = output_index + self.output_index = output_index self.num_inputs = num_inputs self.num_outputs = num_outputs self._connect_current() - + def _indexes_valid(self): - """! + """ Are the input and output indexes within range of the number of inputs and outputs? @return true if input index and output index are in range """ return self.input_index in range(self.num_inputs) and self.output_index in range(self.num_outputs) - + def _connect_current(self): - """If the input and output indexes are valid: - disconnect the blocks at the input and output index from their terminators, + """If the input and output indexes are valid: + disconnect the blocks at the input and output index from their terminators, and connect them to one another. Then connect the terminators to one another.""" if self._indexes_valid(): - self.disconnect((self, self.input_index), self.input_terminators[self.input_index]) + self.disconnect((self, self.input_index), self.input_terminators[self.input_index]) self.disconnect(self.output_terminators[self.output_index], (self, self.output_index)) self.copy = gr.skiphead(self.item_size, 0) self.connect((self, self.input_index), self.copy) - self.connect(self.copy, (self, self.output_index)) - self.connect(self.output_terminators[self.output_index], self.input_terminators[self.input_index]) - + self.connect(self.copy, (self, self.output_index)) + self.connect(self.output_terminators[self.output_index], self.input_terminators[self.input_index]) + def _disconnect_current(self): - """If the input and output indexes are valid: - disconnect the blocks at the input and output index from one another, + """If the input and output indexes are valid: + disconnect the blocks at the input and output index from one another, and the terminators at the input and output index from one another. Reconnect the blocks to the terminators.""" if self._indexes_valid(): self.disconnect((self, self.input_index), self.copy) self.disconnect(self.copy, (self, self.output_index)) self.disconnect(self.output_terminators[self.output_index], self.input_terminators[self.input_index]) - del self.copy - self.copy = None + del self.copy + self.copy = None self.connect((self, self.input_index), self.input_terminators[self.input_index]) self.connect(self.output_terminators[self.output_index], (self, self.output_index)) - + def set_input_index(self, input_index): - """! + """ Change the block to the new input index if the index changed. @param input_index the new input index """ @@ -95,25 +95,25 @@ class selector(gr.hier_block2): self._disconnect_current() self.input_index = input_index self._connect_current() - self.unlock() - + self.unlock() + def set_output_index(self, output_index): - """! + """ Change the block to the new output index if the index changed. @param output_index the new output index - """ - if self.output_index != output_index: + """ + if self.output_index != output_index: self.lock() self._disconnect_current() self.output_index = output_index - self._connect_current() - self.unlock() + self._connect_current() + self.unlock() class valve(selector): """Wrapper for selector with 1 input and 1 output.""" - + def __init__(self, item_size, open): - """! + """ Constructor for valve. @param item_size the size of the gr data stream in bytes @param open true if initial valve state is open @@ -121,13 +121,12 @@ class valve(selector): if open: output_index = -1 else: output_index = 0 selector.__init__(self, item_size, 1, 1, 0, output_index) - + def set_open(self, open): - """! + """ Callback to set open state. @param open true to set valve state to open """ if open: output_index = -1 else: output_index = 0 self.set_output_index(output_index) - |