# Need help to understand code of sync:decimator block

**URL:** <https://www.ruby-forum.com/t/need-help-to-understand-code-of-sync-decimator-block/239639>\
**Category:** GNU Radio\
**Created:** [March 8, 2014, 8:50am UTC](https://www.ruby-forum.com/t/need-help-to-understand-code-of-sync-decimator-block/239639 "2014-03-08T08:50:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![musicdenotation](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea666f/32.png) [@musicdenotation](https://www.ruby-forum.com/u/musicdenotation)\
**Post date:** [March 8, 2014, 8:50am UTC](https://www.ruby-forum.com/t/need-help-to-understand-code-of-sync-decimator-block/239639/1 "2014-03-08T08:50:17Z")

</div>

Hello,

I am using GNU Radio version 3.6.5 and trying to write a block to  
calculate  
average of 5 samples. I want to produce one average output corresponding  
to  
5 input samples. I am using set\_history(5) to remember previous data. I  
used gr\_modtool script of type decimator to create file.

eg. for Vector source {1,2,3,4,5,6,7,8,9,10}, it should generate average  
{3,8}. Instead I am getting {.2, .6}.

My requirement is to produce output, it should take 5 inputs. Please  
suggest me where I need to do modification.

In function gr\_sync\_decimator(“average”,  
gr\_make\_io\_signature(1, 1, sizeof(float)),  
gr\_make\_io\_signature(1, 1, sizeof(float)), 5)

what role value 5 will play?

Below is code snapshot.

#ifdef HAVE\_CONFIG\_H  
#include “config.h”  
#endif

#include \<gr\_io\_signature.h\>  
#include “average\_impl.h”

namespace gr {  
namespace howto {

```
average::sptr
average::make()
{
  return gnuradio::get_initial_sptr
    (new average_impl());
}

/*
 * The private constructor
 */
average_impl::average_impl()
  : gr_sync_decimator("average",
          gr_make_io_signature(1, 1, sizeof(float)),
          gr_make_io_signature(1, 1, sizeof(float)), 5)
{
set_history(5);
}

/*
 * Our virtual destructor.
 */
average_impl::~average_impl()
{
}

int
average_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];
in +=4;

for(int i=0; i<noutput_items; i++){
out[i]=(in[i]+in[i-1]+in[i-2]+in[i-3]+in[i-4])/5;
}

    return noutput_items;
}

```

} /\* namespace howto _/  
} /_ namespace gr \*/

Thanks and regards,  
Kunal Sankhe

---

<div class="post-metadata">

**Author:** ![kunal\_sankhe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9e8a1a/32.png) [@kunal\_sankhe](https://www.ruby-forum.com/u/kunal_sankhe)\
**Post date:** [March 8, 2014, 10:54am UTC](https://www.ruby-forum.com/t/need-help-to-understand-code-of-sync-decimator-block/239639/2 "2014-03-08T10:54:39Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA256

Hello Kunal,

You don’t want to use set\_history in this case: you want to produce 1  
output for 5 input, so you use a decimator; that’s all you need to do.  
Drop the set\_history.  
For reference: history is when for every input item you need the last 5  
items before,  
[http://gnuradio.org/redmine/projects/gnuradio/wiki/OutOfTreeModules#set\_history](http://gnuradio.org/redmine/projects/gnuradio/wiki/OutOfTreeModules#set_history)  
Greetings,  
Marcus

* * *

Sent from my Android device with K-9 Mail. Please excuse my brevity.  
-----BEGIN PGP SIGNATURE-----  
Version: APG v1.0.9

iQFABAEBCAAqBQJTGuigIxxNYXJjdXMgTfxsbGVyIDxtYXJjdXNAaG9zdGFsaWEu  
ZGU+AAoJEBKNLRrpJvfoIRUH/0Orh65WsPeli99oQrbMpb40FVG+cytjmuUS8mKX  
s0PHoDcQ/HXC8KIKxAOo+JQ26X/AK8Qnf3WCgfYB2/dvA7sz7JmYUbXgGFry1S9D  
dtPUzAW2D0AASXG19rFBtCq6u1nhVDsIwxQ1yASNwx2wKuQEZyRFA1tjHiUqjpFf  
0V+S1nB81jWw0WUJTpRMRGOe1tvhG0IDKmeBptMbKw6ShM979pPM3JgDsFIhn2MS  
UWIM7Qjvnul4g8dnq/t7DpNe+lguHFgFes3hB8cHtTRGBvy+8QPJWn5lnCRBZz+4  
ZKFrkWdsslzKjUHP67ZIQ5hn104P6MQZER+RpFdtcO3BJqg=  
=9KAs  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![kunal\_sankhe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9e8a1a/32.png) [@kunal\_sankhe](https://www.ruby-forum.com/u/kunal_sankhe)\
**Post date:** [March 8, 2014, 12:05pm UTC](https://www.ruby-forum.com/t/need-help-to-understand-code-of-sync-decimator-block/239639/3 "2014-03-08T12:05:02Z")

</div>

Hello Marcus,

Sorry for my vague explanation. I want to write a block which will  
calculate moving average for a block of samples. I have only one input  
in  
this case. I am providing my data serially using vector to stream  
converter. I want to calculate average of a block of 5 samples.

eg. from vector source {1,2,3,4,5,6,7,8,9,10}, i will get stream of  
integers.

Now I want to calculate average for first 5 samples i.e. 3 and then it  
should calculate average of {6,7,8,9,10} which is 8. So i should get  
desired output as {3,8}.

So I should get output for a block containing 5 samples. It should again  
consider block of 5 next samples and calculate average.

Please let me what changes do I need to make.

Thanks,  
Kunal

---

<div class="post-metadata">

**Author:** ![kunal\_sankhe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9e8a1a/32.png) [@kunal\_sankhe](https://www.ruby-forum.com/u/kunal_sankhe)\
**Post date:** [March 8, 2014, 12:14pm UTC](https://www.ruby-forum.com/t/need-help-to-understand-code-of-sync-decimator-block/239639/4 "2014-03-08T12:14:10Z")

</div>

Hi Kunal,

Use a decimating FIR filter with taps [0.2, 0.2, 0.2, 0.2, 0.2] and  
decimation 5. It will be faster than anything you are about to write.

Miklos

---

<div class="post-metadata">

**Author:** ![kunal\_sankhe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9e8a1a/32.png) [@kunal\_sankhe](https://www.ruby-forum.com/u/kunal_sankhe)\
**Post date:** [March 9, 2014, 2:53am UTC](https://www.ruby-forum.com/t/need-help-to-understand-code-of-sync-decimator-block/239639/5 "2014-03-09T02:53:31Z")

</div>

Dear Kunal,

This is not “moving average”, this is normal “average”.  
I agree with Marcus’ explanation, you don’t need the “set\_history()”.

Moving average is:  
1st output = average of 1,2,3,4,5  
2nd output = average of 2,3,4,5,6  
3rd output = average of 3,4,5,6,7  
4th output = average of 4,5,6,7,8

Obviously what you want is not “moving average”.  
If you really want moving average, then only you need “set\_history()”.

Regards,  
Activecat

---

<div class="post-metadata">

**Author:** ![kunal\_sankhe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9e8a1a/32.png) [@kunal\_sankhe](https://www.ruby-forum.com/u/kunal_sankhe)\
**Post date:** [March 8, 2014, 12:26pm UTC](https://www.ruby-forum.com/t/need-help-to-understand-code-of-sync-decimator-block/239639/6 "2014-03-08T12:26:17Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Sorry I was unclear: “5 inputs” means “5input items from the same  
(only one) input stream”.  
Now, Miklos is most probably very right, the GNU Radio filters are  
highly optimized. But let’s take this as an exercise in understanding  
decimators:

A decimator is just a normal block that “knows” that for every N input  
items, it produces 1 output item. So if you produce but 1 output, GNU  
Radio will now that you’ve seen input 0…4, and you will be presented  
with 5…9 next time.

You’re work’s for loop does something like a moving average for _each_  
input item, so that’s what’s wrong with it.  
So, as I said: just drop the set\_history call, and change your for  
loop like following and your block should work:

for(int i = 0; i/5 \< noutput\_items; i += 5)  
out[i / 5] = (in[i]+ in[i+1]…+in[i+4]) / 5.0f;  
return i/5;

Greetings,  
Marcus

On 08.03.2014 12:03, kunal sankhe wrote:

> [http://gnuradio.org/redmine/projects/gnuradio/wiki/OutOfTreeModules#set\_history](http://gnuradio.org/redmine/projects/gnuradio/wiki/OutOfTreeModules#set_history)

Greetings,

> Marcus
> 
> >

-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1  
Comment: Using GnuPG with Thunderbird - [http://www.enigmail.net/](http://www.enigmail.net/)

iQEcBAEBAgAGBQJTGv3kAAoJEBQ6EdjyzlHtEXkH/i3Z8NnM9sQP3lZLFRU7SEsT  
NedPJD7hcCGVlDUY16h7fdtuweHesDIP51vzrLJfmbC/swRS2gWhps5KrY7w8+vq  
LwHZjBNSa1tBZ5e0OlrW+mCv2PXlrbC0kGOdMSnyoptWNVm8NNJgTw3VxVKHpWFK  
FEpBfaIdoNt873qZdWnSxaCDeusrdeIDmNiE68bGwRd3solH4CSQe4Dve7Q7N45a  
wEUn9UhHk/jOKRZ9hEqDM/sF1Bp2JL2zmZOj46YT82tvFjGNpFyftu2wei/LByst  
/s0n8h8w0l0TjkEIE7ehIwYIEzBtuUv7Op8ePrDtKEBRy7BBcw/25e8ChlOKe1U=  
=HpzM  
-----END PGP SIGNATURE-----
