# Testing block written in block

**URL:** <https://www.ruby-forum.com/t/testing-block-written-in-block/230441>\
**Category:** GNU Radio\
**Created:** [June 18, 2014, 11:01am UTC](https://www.ruby-forum.com/t/testing-block-written-in-block/230441 "2014-06-18T11:01:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jason\_sam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7993a0/32.png) [@jason\_sam](https://www.ruby-forum.com/u/jason_sam)\
**Post date:** [June 18, 2014, 11:01am UTC](https://www.ruby-forum.com/t/testing-block-written-in-block/230441/1 "2014-06-18T11:01:44Z")

</div>

Hi

I’m new to GNU radio and is trying to develop my own block. I want to  
develop a block that intake a vector and out put sum of the vector  
elements. I used the out of tree module and followed tutorial using  
python code. How should be the arguments of blocks.vector\_source\_f to be  
given if i am inputting vector stream of four integers is used. the  
python code and test file is given below.

Code:

def **init** (self, in\_arg):  
gr.sync\_block. **init** (self,  
name=“sream\_ff”,  
in\_sig=[(numpy.float32, 4)],  
out\_sig=[numpy.float32])  
def work(self, input\_items, output\_items):  
in0 = input\_items[0]  
out = output\_items[0]  
# \<+signal processing here+\>  
out[:] = sum(in0)\*in\_arg  
return len(output\_items[0])

Test code:

def test\_001\_t (self):  
src\_data = (([1,1,1,1],4), ([1,2,3,4],4))  
expected\_result = (8, 20)  
src = blocks.vector\_source\_f(src\_data)  
fxn = sream\_ff(2)  
snk = blocks.vector\_sink\_f()  
# set up fg  
self.tb.connect(src, fxn)  
self.tb.connect(fxn, snk)  
self.tb.run ()  
# check data  
result\_data = snk.data ()  
self.assertFloatTuplesAlmostEqual (expected\_result, result\_data, 6)

---

<div class="post-metadata">

**Author:** ![sreena\_p\_h](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/47e85d/32.png) [@sreena\_p\_h](https://www.ruby-forum.com/u/sreena_p_h)\
**Post date:** [June 18, 2014, 11:31am UTC](https://www.ruby-forum.com/t/testing-block-written-in-block/230441/2 "2014-06-18T11:31:27Z")

</div>

Hi Sreena,

a few quick remarks:

- where does in\_arg come from?
- in0 should be seen as a vector of vectors. It’s often useful to “print  
in0.shape”, if in0 is a numpy array
- potential mistake: just returning len(input\_items[0]). Be _very_ aware  
of what the return value of a work call should be (i.e. the number of  
items produced). I think you meant to calculate but one sum, is that  
right?

Greetings,  
Marcus

---

<div class="post-metadata">

**Author:** ![sreena\_p\_h](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/47e85d/32.png) [@sreena\_p\_h](https://www.ruby-forum.com/u/sreena_p_h)\
**Post date:** [June 18, 2014, 12:01pm UTC](https://www.ruby-forum.com/t/testing-block-written-in-block/230441/3 "2014-06-18T12:01:10Z")

</div>

On Wed, Jun 18, 2014 at 4:57 PM, sreena p h [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi
> 
> I’m new to GNU radio and is trying to develop my own block. I want to  
> develop a block that intake a vector and out put sum of the vector  
> elements. I used the out of tree module and followed tutorial using python  
> code. How should be the arguments of blocks.vector\_source\_f to be given if  
> i am inputting vector stream of four integers is used. the python code and  
> test file is given below.

Does your intake vectors have fixed-length or variable length?

If they are variable length, then your sream\_ff block won’t be very  
useful  
in graphical GRC flowgraph, because there the vector streams are usually  
fixed-length.

If they are fixed-length, then your problem could be easily solved; just  
create sream\_ff as a decimator block.

---

<div class="post-metadata">

**Author:** ![sreena\_p\_h](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/47e85d/32.png) [@sreena\_p\_h](https://www.ruby-forum.com/u/sreena_p_h)\
**Post date:** [June 18, 2014, 3:15pm UTC](https://www.ruby-forum.com/t/testing-block-written-in-block/230441/4 "2014-06-18T15:15:29Z")

</div>

From: discuss-gnuradio-bounces+sean.nowlan=removed\_email\_address@domain.invalid  
[[mailto:discuss-gnuradio-bounces+sean.nowlan=removed\_email\_address@domain.invalid](mailto:discuss-gnuradio-bounces+sean.nowlan=removed_email_address@domain.invalid)] On  
Behalf Of Activecat  
Sent: Wednesday, June 18, 2014 5:59 AM  
To: removed\_email\_address@domain.invalid  
Cc: sreena p h  
Subject: Re: [Discuss-gnuradio] testing block written in block

On Wed, Jun 18, 2014 at 4:57 PM, sreena p h  
\<removed\_email\_address@domain.invalid[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> wrote:  
Hi

I’m new to GNU radio and is trying to develop my own block. I want to  
develop a block that intake a vector and out put sum of the vector  
elements. I used the out of tree module and followed tutorial using  
python code. How should be the arguments of blocks.vector\_source\_f to be  
given if i am inputting vector stream of four integers is used. the  
python code and test file is given below.

Does your intake vectors have fixed-length or variable length?

If they are variable length, then your sream\_ff block won’t be very  
useful in graphical GRC flowgraph, because there the vector streams are  
usually fixed-length.  
If they are fixed-length, then your problem could be easily solved; just  
create sream\_ff as a decimator block.

If you’d rather use stock GR blocks or you would like a flowgraph with  
identical functionality, you can hook up a Vector to Stream block (with  
num\_items=vector\_length) to an Integrate block (with  
decimation=vector\_length).

…  
vector\_length = 10  
self.vec2stream = blocks.vector\_to\_stream(gr.sizeof\_gr\_complex,  
vector\_length)  
self.integrate = blocks.integrate\_cc(vector\_length)  
self.connect((self.vec2stream, 0), (self.integrate, 0))  
…

Sean

---

<div class="post-metadata">

**Author:** ![sreena\_p\_h](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/47e85d/32.png) [@sreena\_p\_h](https://www.ruby-forum.com/u/sreena_p_h)\
**Post date:** [June 19, 2014, 5:37am UTC](https://www.ruby-forum.com/t/testing-block-written-in-block/230441/5 "2014-06-19T05:37:50Z")

</div>

On Wed, Jun 18, 2014 at 9:14 PM, Nowlan, Sean  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Sean

I agree, that the Integrate block is a decimator block that implement  
this  
perfectly.  
Just that she may want to use integrate\_ff rather than integrate\_cc,  
depending on her needs.  
Also we are assuming that the intake vectors are fixed in length.

```
int
integrate_ff_impl::work(int noutput_items,
          gr_vector_const_void_star &input_items,
          gr_vector_void_star &output_items)
{
  const float *in = (const float *)input_items[0];
  float *out = (float *)output_items[0];

  for (int i = 0; i < noutput_items; i++) {
    out[i] = (float)0;
    for (int j = 0; j < d_decim; j++)
      out[i] += in[i*d_decim+j];
  }

  return noutput_items;
}

```
