Thursday, March 15, 2018

Re: [Discuss-gnuradio] Vector average with decimate block

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2018 <+YOU OR YOUR COMPANY+>.
#
# 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, gr_unittest
from gnuradio import blocks
from vave import vave

class qa_vave (gr_unittest.TestCase):

def setUp (self):
self.tb = gr.top_block ()

def tearDown (self):
self.tb = None

def test_001_t (self):
vsize = 1024
vdecimate = 4
vin = numpy.zeros(vsize*vdecimate)
for i in range(vsize):
vin[i] = float(i)
# create a set of vectors
src = blocks.vector_source_f( vin.tolist())
s2v = blocks.stream_to_vector(gr.sizeof_float, vsize)
# block we're testing
vblock = vave( vsize, vdecimate)

v2s = blocks.vector_to_stream( gr.sizeof_float, vsize)
snk = blocks.vector_sink_f(vsize)

self.tb.connect (src, s2v)
self.tb.connect (s2v, vblock)
self.tb.connect (vblock, snk)
# self.tb.connect (v2s, snk)
expected = vin[0:vsize]/4.
print 'Expected: ', expected[0:7]
outdata = None
waittime = 0.01

self.tb.run ()
outdata = snk.data()
print 'Output: ', outdata[0:7]
# check data
self.assertFloatTuplesAlmostEqual (expected, outdata, 6)

if __name__ == '__main__':
gr_unittest.run(qa_vave, "qa_vave.xml")
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2018 <+YOU OR YOUR COMPANY+>.
#
# 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 vave(gr.decim_block):
"""
docstring for block vave
"""
def __init__(self, vsize, vdecimate):
gr.decim_block.__init__(self,
name="vave",
in_sig=[(numpy.float32, int(vsize))],
out_sig=[(numpy.float32, int(vsize))],
decim=int(vdecimate))
self.vsize = int(vsize)
self.vdecimate = int(vdecimate)
self.sum = numpy.zeros(self.vsize)
self.count = 0
self.oneovern = 1./float(self.vdecimate)

def forecast( self, noutput_items, ninput_items):
return self.vdecimate

def work(self, input_items, output_items):
"""
Work averages all input vectors and outputs one vector for all inputs
"""
out = output_items[0]
in0 = input_items[0]

# get the number of input vectors
n = len( input_items)
print 'Number of work inputs: ',n
nout = len( output_items)
print 'Number of work outputs: ',n

nv = len(in0)
for j in range(nv):
# get the lenght of one input
vin = in0[j]
print 'Length of input ', len(vin),' for vectors: ',j

# indicate consumption of a vector from input
self.consume(0, len(vin))

# now save this vector until all are received
self.sum = self.sum + vin
self.count = self.count + 1

if self.count >= self.vdecimate:
# normalize output average
out[:] = self.oneovern * self.sum[:]
# now reset the count and restart the sum
self.count = 0
self.sum = numpy.zeros( self.vdecimate)
return len(output_items[0])
# end for all input vectors
# if here, then not enough vectors input to produce an output
return 0
# end vave()

<?xml version="1.0"?>
<block>
<name>vave</name>
<key>vave_vave</key>
<category>[vave]</category>
<import>import vave</import>
<make>vave.vave($vlen, $vdecimate)</make>
<!-- Make one 'param' node for every Parameter you want settable from the GUI.
Sub-nodes:
* name
* key (makes the value accessible as $keyname, e.g. in the make node)
* type -->
<param>
<name>vector_size</name>
<key>vlen</key>
<value>10</value>
<type>int</type>
</param>
<param>
<name>vdecimate</name>
<key>vdecimate</key>
<value>4</value>
<type>int</type>
</param>

<!-- Make one 'sink' node per input. Sub-nodes:
* name (an identifier for the GUI)
* type
* vlen
* optional (set to 1 for optional inputs) -->
<sink>
<name>in</name>
<type>float</type>
<vlen>$vlen</vlen>
</sink>

<!-- Make one 'source' node per output. Sub-nodes:
* name (an identifier for the GUI)
* type
* vlen
* optional (set to 1 for optional inputs) -->
<source>
<name>out</name>
<type>float</type>
<vlen>$vlen</vlen>
</source>
</block>
Hello Marcus and all,

Sorry to keep dragging on with this vector decimate.

I've gotten "vave" into grc but it is not running properly.

The grc messages I get are below:

Generating: '/Users/glangsto/Desktop/Research/vdecimate/keep5.py'

Executing: /opt/local/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python -u /Users/glangsto/Desktop/Research/vdecimate/keep5.py

Number work inputs : 1 Length: 10
Number work outputs: 1 Length: 10
Number work inputs : 1 Length: 10
Number work outputs: 1 Length: 10
handler caught exception: operands could not be broadcast together with shapes (4,) (10,)
Traceback (most recent call last):
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/gnuradio/gr/gateway.py", line 55, in eval
try: self._callback()
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/gnuradio/gr/gateway.py", line 160, in __gr_block_handle
) for i in self.__out_indexes],
File "/Users/glangsto/Desktop/Research/gr-vave/python/vave.py", line 73, in work
self.sum[0:ncp] = self.sum[0:ncp] + vin[0:ncp]
ValueError: operands could not be broadcast together with shapes (4,) (10,)
thread[thread-per-block[4]: <block vave (1)>]: SWIG director method error. Error detected when calling 'feval_ll.eval'

I've tweaked the python and attached the .xml etc

No comments:

Post a Comment