Created
April 25, 2014 16:11
-
-
Save marcusmueller/11294819 to your computer and use it in GitHub Desktop.
Max index filter block
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# | |
# Copyright 2014 Marcus Müller. | |
# | |
# This 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. | |
# | |
# This software 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 this software; see the file COPYING. If not, write to | |
# the Free Software Foundation, Inc., 51 Franklin Street, | |
# Boston, MA 02110-1301, USA. | |
# | |
import numpy | |
from gnuradio import gr | |
class max_index(gr.sync_block): | |
""" | |
finds the maximum index in a vector and outputs it on the output stream | |
""" | |
def __init__(self, vlen, dtype=numpy.float32): | |
gr.sync_block.__init__(self, | |
name="max_index", | |
in_sig=[(numpy.dtype(dtype),vlen)], | |
out_sig=[numpy.short]) | |
def work(self, input_items, output_items): | |
in0 = input_items[0] | |
out = output_items[0] | |
out[:] = [ numpy.argmax(vector) for vector in in0 ] | |
return len(output_items[0]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# | |
# Copyright 2014 Marcus Müller. | |
# | |
# This 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. | |
# | |
# This software 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 this software; 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, gr_unittest | |
from gnuradio import blocks | |
from max_index import max_index | |
import numpy | |
class qa_max_index (gr_unittest.TestCase): | |
def setUp (self): | |
self.tb = gr.top_block () | |
def tearDown (self): | |
self.tb = None | |
def test_max_idx (self): | |
# set up fg | |
N = 100 | |
self.data = numpy.identity(N).ravel() | |
self.src = blocks.vector_source_f(self.data, vlen=N, repeat=False) | |
self.sink = blocks.vector_sink_s() | |
self.maxarg = max_index(N) | |
self.tb.connect(self.src, self.maxarg, self.sink) | |
self.tb.run () | |
self.tb.wait() | |
testdata = self.sink.data() | |
self.assertTupleEqual(testdata, tuple(range(N))) | |
print testdata | |
# check data | |
if __name__ == '__main__': | |
gr_unittest.run(qa_max_index, "qa_max_index.xml") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment