Need help to understand code of sync:decimator block

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

-----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
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-----

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

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

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

-----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

Greetings,

Marcus

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs