# Simple block with constant rate

**URL:** <https://www.ruby-forum.com/t/simple-block-with-constant-rate/230592>\
**Category:** GNU Radio\
**Created:** [June 25, 2014, 11:16am UTC](https://www.ruby-forum.com/t/simple-block-with-constant-rate/230592 "2014-06-25T11:16:59Z")\
**Posts on this page:** 10\
**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 25, 2014, 11:16am UTC](https://www.ruby-forum.com/t/simple-block-with-constant-rate/230592/1 "2014-06-25T11:16:59Z")

</div>

Hello,

I’m doing a block with two inputs (signal and noise) and one output.

Signal input block generates samples sometimes. For example:  
2,3,-,-,4,-,-,-,5,1,1,-,4,55…  
Noise block generates samples always. For example:  
1,1,0,2,2,1,2,3,4,2…

I want to create a block that adds noise to the signal and generate  
samples always:

Signal 2,3,-,-,4,-,-,-,5,1,1,-,4,55…  
Noise 1,1,0,2,2,1,2,3,4,2,3,4,1,12…

Output 3,4,0,2,6,1,2,3,9,3,4,4,5,67…

I have written this code, what is the fail?

Thank you 😉

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

#include \<gnuradio/io\_signature.h\>  
#include “add\_noise\_fff\_impl.h”

namespace gr {  
namespace howto {

```
 add_noise_fff::sptr
 add_noise_fff::make()
 {
   return gnuradio::get_initial_sptr
     (new add_noise_fff_impl());
 }

 /*
  * The private constructor
  */
 add_noise_fff_impl::add_noise_fff_impl()
   : gr::block("add_noise_fff",
           gr::io_signature::make(2,2, sizeof(float)),
           gr::io_signature::make(1,1, sizeof(float)))
 {}

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

 void
 add_noise_fff_impl::forecast (int noutput_items, gr_vector_int

```

&ninput\_items\_required)  
{  
ninput\_items\_required[1] = noutput\_items;  
//ninput\_items\_required[0] = 0;  
}

```
 int
 add_noise_fff_impl::general_work (int noutput_items,
                    gr_vector_int &ninput_items,
                    gr_vector_const_void_star &input_items,
                    gr_vector_void_star &output_items)
 {
     const float *in = (const float *) input_items[0];
     const float *in_noise = (const float *) input_items[1];

     float *out = (float *) output_items[0];

     int j=0;
     int i=0;
     for(;i<noutput_items;++i){
         if(i<ninput_items[0]){
             ++j;
             out[i]=in[i]+in_noise[i];
         }
         else
             out[i]=in_noise[i];
     }

     //consume the inputs
     this->consume(0, j); //consume port 0 input
     this->consume(1, i); //consume port 1 input

     // Tell runtime system how many output items we produced.
     return noutput_items;
 }

```

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

* * *

#ifndef INCLUDED\_HOWTO\_ADD\_NOISE\_FFF\_IMPL\_H  
#define INCLUDED\_HOWTO\_ADD\_NOISE\_FFF\_IMPL\_H

#include \<howto/add\_noise\_fff.h\>

namespace gr {  
namespace howto {

```
 class add_noise_fff_impl : public add_noise_fff
 {
  private:
   // Nothing to declare in this block.

  public:
   add_noise_fff_impl();
   ~add_noise_fff_impl();

   // Where all the action really happens
   void forecast (int noutput_items, gr_vector_int

```

&ninput\_items\_required);

```
   int general_work(int noutput_items,
       gr_vector_int &ninput_items,
       gr_vector_const_void_star &input_items,
       gr_vector_void_star &output_items);
 };

```

} // namespace howto  
} // namespace gr

#endif /\* INCLUDED\_HOWTO\_ADD\_NOISE\_FFF\_IMPL\_H \*/

---

<div class="post-metadata">

**Author:** ![caruiz.ext](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7ba0ec/32.png) [@caruiz.ext](https://www.ruby-forum.com/u/caruiz.ext)\
**Post date:** [June 25, 2014, 11:54am UTC](https://www.ruby-forum.com/t/simple-block-with-constant-rate/230592/2 "2014-06-25T11:54:27Z")

</div>

Hi Caruiz,

you don’t even tell us what goes wrong, so it might be hard to answer  
your question 😉

Anyway, that’s not how GNU Radio really works. For the sample streams,  
real time just happens to pass by and has no meaning. So the concept  
“generates samples sometimes” doesn’t exist in GNU Radio – it just may  
take longer.  
There are various factors that determine when your block’s general\_work  
is called. You were right to have the forecast with the  
nitems\_required[0] = 0; so why did you comment that out?  
I guess it was because your block started to run free and basically  
never consumed on input stream 1; and that’s exactly the problem here:  
GNU Radio will always try to call your blocks as often as possible, i.e.  
whenever there is space free to put output into and when there is enough  
input. Assuming the noise source is fast, and your signal source takes  
relatively long times, especially when your CPU is busy generating noise  
and passing it through your block, then you will always never consume  
signal samples. Even with throttle between the noise source and your  
block (beware: NEVER use throttle – UNLESS you really just need to  
reduce the _average_ sample rate of a stream), there is absolutely no  
reliable correlation between the duration of no-signal samples and real  
time passing with signal present.  
Just an example: Assume your source produces 100 samples with a sample  
rate (that only you know about and GNU Radio doesn’t even care about) of  
1kS/s, and the next work call of the source takes 0.9s, to produce 100  
samples again:  
s - - - - - - - - - s - - - - - - - - - s - - - - - - - - - … [REAL  
TIME axis]  
There will never ever be 100 samples signal + noise, 900 samples noise  
only etc… but  
s+n[100] n[121200] s+n[100] n[80000] s+n[100] …

Because the noise source will produce as fast as possible, and that  
means it’s only bound by CPU time available and the speed of samples  
going through your flow graph.

Please note: real world sampling rate is a concept utterly meaningless  
in GNU Radio and is only occasionally used to calculate relative  
frequencies.

Greetings,  
Marcus

---

<div class="post-metadata">

**Author:** ![caruiz.ext](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7ba0ec/32.png) [@caruiz.ext](https://www.ruby-forum.com/u/caruiz.ext)\
**Post date:** [June 25, 2014, 12:36pm UTC](https://www.ruby-forum.com/t/simple-block-with-constant-rate/230592/3 "2014-06-25T12:36:42Z")

</div>

On Wed, Jun 25, 2014 at 5:15 PM, caruiz.ext [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Signal 2,3,-,-,4,-,-,-,5,1,1,-,4,55…  
> Noise 1,1,0,2,2,1,2,3,4,2,3,4,1,12…
> 
> Output 3,4,0,2,6,1,2,3,9,3,4,4,5,67…

I believe your problem could be easily solved.  
Let’s show us the code that generates the Signal (2,3,-,-,4,…), or  
does  
it come from a UHD source?  
Meanwhile your code above is not correct.

---

<div class="post-metadata">

**Author:** ![caruiz.ext](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7ba0ec/32.png) [@caruiz.ext](https://www.ruby-forum.com/u/caruiz.ext)\
**Post date:** [June 25, 2014, 12:55pm UTC](https://www.ruby-forum.com/t/simple-block-with-constant-rate/230592/4 "2014-06-25T12:55:08Z")

</div>

Really clear your explanation Marcus 🙂

Activecat, my input block  
is a variable delay.

- I have a signal recorded in a file.

- Signal  
enters in a variable delay (delay is controlled by another block).

- 

Signal enters in Add noise block.

File --\> Variable delay --\> Add  
noise --\> out

My problem is that if variable delay is, for example, 5  
samples: a sample enters, the block “waits” 5 samples, the sample  
leaves. Then my output is: sample\_3,- , - , - , - , - ,sample\_2 , - , -  
, - , - , - , sample\_1

Greetings

---

<div class="post-metadata">

**Author:** ![caruiz.ext](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7ba0ec/32.png) [@caruiz.ext](https://www.ruby-forum.com/u/caruiz.ext)\
**Post date:** [June 25, 2014, 1:33pm UTC](https://www.ruby-forum.com/t/simple-block-with-constant-rate/230592/5 "2014-06-25T13:33:48Z")

</div>

Ahh, the problem! :S

When I run the flow graph, it dies…

\*\*I  
use GRC and my final block is a GUI Scope Sink.

---

<div class="post-metadata">

**Author:** ![caruiz.ext](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7ba0ec/32.png) [@caruiz.ext](https://www.ruby-forum.com/u/caruiz.ext)\
**Post date:** [June 25, 2014, 6:05pm UTC](https://www.ruby-forum.com/t/simple-block-with-constant-rate/230592/6 "2014-06-25T18:05:04Z")

</div>

Monday I will post the code (I have it in the lab).

Thank you 😉

El 25-06-2014 17:29, Activecat escribió:

> On Wed, Jun 25, 2014 at  
> 6:53 PM, caruiz.ext [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Activecat, my  
> > input block is a variable delay.
> > 
> > - I have a signal recorded in a  
> > file.
> > 
> > - Signal enters in a variable delay (delay is controlled  
> > by another block).
> > 
> > - Signal enters in Add noise block. File →   
> > Variable delay → Add noise → out
> > 
> > My problem is that if  
> > variable delay is, for example, 5 samples: a sample enters, the block  
> > “waits” 5 samples, the sample leaves. Then my output is: sample\_3,- , -  
> > , - , - , - ,sample\_2 , - , - , - , - , - , sample\_1
> 
> Could you  
> please explain how did you build the “variable delay”, do you use the  
> built-in “delay” block?  
> If this is a custom delay block, please show  
> your code.
> 
> Meanwhile your understanding about the delay block is  
> wrong.  
> Says, the input is: s1, s2, s3, s4, s5.  
> After going through  
> the delay block (with delay=5), the output becomes: 0, 0, 0, 0, 0, s1,  
> s2, s3, s4, s5.  
> The output of delay block will never be “-” but it  
> could be “0” (zero).
> 
> Probably you just need the standard “Add”  
> block instead of your custom block above.  
> Could you try this  
> first..?

---

<div class="post-metadata">

**Author:** ![caruiz.ext](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7ba0ec/32.png) [@caruiz.ext](https://www.ruby-forum.com/u/caruiz.ext)\
**Post date:** [June 25, 2014, 5:31pm UTC](https://www.ruby-forum.com/t/simple-block-with-constant-rate/230592/7 "2014-06-25T17:31:10Z")

</div>

On Wed, Jun 25, 2014 at 6:53 PM, caruiz.ext [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> sample\_3,- , - , - , - , - ,sample\_2 , - , - , - , - , - , sample\_1

Could you please explain how did you build the “variable delay”, do you  
use  
the built-in “delay” block?  
If this is a custom delay block, please show your code.

Meanwhile your understanding about the delay block is wrong.  
Says, the input is: s1, s2, s3, s4, s5.  
After going through the delay block (with delay=5), the output becomes:  
0,  
0, 0, 0, 0, s1, s2, s3, s4, s5.  
The output of delay block will never be “-” but it could be “0” (zero).

Probably you just need the standard “Add” block instead of your custom  
block above.  
Could you try this first..?

---

<div class="post-metadata">

**Author:** ![caruiz.ext](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7ba0ec/32.png) [@caruiz.ext](https://www.ruby-forum.com/u/caruiz.ext)\
**Post date:** [July 1, 2014, 12:19pm UTC](https://www.ruby-forum.com/t/simple-block-with-constant-rate/230592/8 "2014-07-01T12:19:12Z")

</div>

Hi there,

I have modified my block. Now it works well for 10-15  
seconds and then dies.

My block have two inputs:

- Signal

- Control  
delay: number of samples to delay

And one output:

- New signal

In the  
image example:

\*\* Current delay = 0

—\> Delay = 1

current\_delay \<  
delay -\>delay of 1 sample

I insert a virtual sample (linear  
interpolation)

I sent the next sample (2)

current\_delay = 1

—\>  
Delay = 1

current\_delay = delay -\> nothing

I send the next sample  
(3)  
current\_delay = 1

—\> Delay = 1

current\_delay = delay -\>  
nothing

I send the next sample (4)  
current\_delay = 1

—\> Delay =  
2

current\_delay \< delay -\> delay of 1 sample (delay-current\_delay)

I  
insert a virtual sample  
I send the next sample (5)  
current\_delay =  
2

—\> Delay = 1

current\_delay \> delay -\>ADVANCE 1 sample

I delete a  
sample (6)

I send the next sample (7)  
current\_delay = 1

Attach the  
code.

\*\*\* The value of control block is about 400000

Thank you 😉

---

<div class="post-metadata">

**Author:** ![caruiz.ext](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7ba0ec/32.png) [@caruiz.ext](https://www.ruby-forum.com/u/caruiz.ext)\
**Post date:** [July 1, 2014, 4:15pm UTC](https://www.ruby-forum.com/t/simple-block-with-constant-rate/230592/9 "2014-07-01T16:15:41Z")

</div>

On Tue, Jul 1, 2014 at 9:30 PM, Activecat [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I don’t understand what this means.  
> My comments:  
> You don’t need to create a std::queue to perform delay.  
> I also don’t understand your requirement.

Meanwhile, you shouldn’t have skipped the unittest from the very  
beginning.  
The unittest will show you that this block (delay\_cola\_fff) doesn’t work  
accordingly.  
There is no point to go further unless this block has passed its  
unittest.

---

<div class="post-metadata">

**Author:** ![caruiz.ext](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7ba0ec/32.png) [@caruiz.ext](https://www.ruby-forum.com/u/caruiz.ext)\
**Post date:** [July 1, 2014, 3:31pm UTC](https://www.ruby-forum.com/t/simple-block-with-constant-rate/230592/10 "2014-07-01T15:31:37Z")

</div>

On Tue, Jul 1, 2014 at 6:17 PM, caruiz.ext [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> —\> Delay = 1  
> current\_delay \< delay -\>delay of 1 sample  
> I insert a virtual sample (linear interpolation)  
> I sent the next sample (2)  
> current\_delay = 1

I don’t understand what this means.

> —\> Delay = 1  
> current\_delay = delay → nothing  
> I send the next sample (3)  
> current\_delay = 1

My comments:

1). I see no reason that “q”, “inicio”, “sample\_rate” and “delay”  
should  
be defined as public class variables, rather than private.

2). You make this a general block that always consumes and returns the  
same value. This is similar to a sync block.  
But a delay block is not a sync block.

3). Basically what a delay block does is just to copy “input[i]” to  
“output[i + delay]”, this creates an effect of delaying the signal.  
You don’t need to create a std::queue to perform delay.

4). The definition of “Sample” class, if it is really needed, should be  
placed in separate file.

5). Your always forecast 1 input item, but in general\_work() you always  
consumes and access the input buffer as if its length is always not  
smaller  
than noutput\_items.  
(you forget the memory boundary) This is fatal.

6). I don’t understand your attached picture.  
I also don’t understand your requirement.

You have written a code (delay\_cola\_fff) that is wrong from the ground  
up.  
The only way to help you is to understand your requirement and suggest  
something else from scratch.  
Unfortunately I fail to understand your requirement.

I suggest you to look into the source code of the existing “Delay”  
block,  
and start from there.
