# Gnuradio block behaves strange.....please have a look at this

**URL:** <https://www.ruby-forum.com/t/gnuradio-block-behaves-strange-please-have-a-look-at-this/149919>\
**Category:** GNU Radio\
**Created:** [October 1, 2008, 6:23am UTC](https://www.ruby-forum.com/t/gnuradio-block-behaves-strange-please-have-a-look-at-this/149919 "2008-10-01T06:23:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Murtuza](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@Murtuza](https://www.ruby-forum.com/u/Murtuza)\
**Post date:** [October 1, 2008, 6:23am UTC](https://www.ruby-forum.com/t/gnuradio-block-behaves-strange-please-have-a-look-at-this/149919/1 "2008-10-01T06:23:23Z")

</div>

Hi friends,

I wrote a gnuradio block that has 2 input streams.

1. The first stream is the stream of PN sequence data coming out of an  
gr.xor\_bb block. (It is a gold sequence generated by XOR two M-seq  
generated  
using gr.glfsr\_source\_b).
2. The second stream is the data read from a file. I use a test data  
"0xAA "  
stored in a file and read it using  
gr.file\_source(gr.sizeof\_char,“datafile”,True).

The output is written to a file. The output written to the file always  
has  
some data missing. I tested the spreading block by printing its output  
to  
“stdout” and found that the output is alright, but when I write it to  
the  
file I always see some data missing. Any ideas why this might be  
happening ?

Below is the code for the block that spreads the data. I spread each bit  
of  
every byte read to the lenth of PN sequence. e.g. If PN sequence is 7  
bit  
long, the output of the block will be 7\*8 bits for each byte read from  
the  
file. The PN sequence bit is the LSB of every byte that output out of  
the  
XOR block.

I suspect something is wrong with the way I call the “consume” function.

Any kind of help will be greatly appreciated.

Thanks  
Ali.

The code.

// dsss\_spreading\_blk\_b.h

/\* -_- c++ -_- _/  
/_

- Copyright 2004 Free Software Foundation, Inc.
- 
- This file is part of GNU Radio
- 
- GNU Radio 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.
- 
- GNU Radio 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 GNU Radio; see the file COPYING. If not, write to
- the Free Software Foundation, Inc., 51 Franklin Street,
- Boston, MA 02110-1301, USA.  
\*/

#ifndef INCLUDED\_DSSS\_SPREADING\_BLK\_B\_H  
#define INCLUDED\_DSSS\_SPREADING\_BLK\_B\_H

#include \<gr\_block.h\>

class dsss\_spreading\_blk\_b;  
typedef boost::shared\_ptr\<dsss\_spreading\_blk\_b\>  
dsss\_spreading\_blk\_b\_sptr;

dsss\_spreading\_blk\_b\_sptr dsss\_make\_spreading\_blk\_b (int degree\_PN);

class dsss\_spreading\_blk\_b : public gr\_block  
{  
int d\_length\_PN;  
friend dsss\_spreading\_blk\_b\_sptr dsss\_make\_spreading\_blk\_b(int  
degree\_PN);  
dsss\_spreading\_blk\_b(int degree\_PN);

```
public:
    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);  
};

#endif /\* INCLUDED\_DSSS\_SPREADING\_BLK\_B\_H _/  
//_\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

// dsss\_spreading\_blk\_b.cc

/\* -_- c++ -_- _/  
/_

- Copyright 2004 Free Software Foundation, Inc.
- 
- This file is part of GNU Radio
- 
- GNU Radio 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.
- 
- GNU Radio 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 GNU Radio; see the file COPYING. If not, write to
- the Free Software Foundation, Inc., 51 Franklin Street,
- Boston, MA 02110-1301, USA.  
\*/

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

#include \<dsss\_spreading\_blk\_b.h\>  
#include \<gr\_io\_signature.h\>  
#include \<assert.h\>  
#include   
#include   
dsss\_spreading\_blk\_b\_sptr  
dsss\_make\_spreading\_blk\_b (int degree\_PN)  
{  
return dsss\_spreading\_blk\_b\_sptr (new dsss\_spreading\_blk\_b  
(degree\_PN));  
}

/\*  
Input\_stream\_2 = data\_source to be spread. The input data size is 1  
byte.(1 item is needed to produce 1 output item)  
Input\_stream\_1 = PN\_sequence\_generator. The input data size is 1  
byte.(length\_PN items are needed to produce 1 output item)  
\*/

dsss\_spreading\_blk\_b::dsss\_spreading\_blk\_b(int  
degree\_PN):gr\_block(“spreading\_blk”,  
gr\_make\_io\_signature(2,2,sizeof(unsigned char)),  
gr\_make\_io\_signature(1,1,sizeof(unsigned char))),  
d\_length\_PN((int)(pow(2,degree\_PN)-1))  
{  
set\_output\_multiple(8\*d\_length\_PN); // noutput\_items == multiple of  
length of PN sequence  
}

void  
dsss\_spreading\_blk\_b::forecast(int noutput\_items,gr\_vector\_int  
&ninput\_items\_required){  
assert(noutput\_items % d\_length\_PN==0);

```
//unsigned int ninputs = ninput_items_required.size();
int items_on_first_input_stream = noutput_items; // the first_stream 

```

is  
the PN sequence stream  
int items\_on\_data\_input\_stream = noutput\_items/(8\*d\_length\_PN);  
ninput\_items\_required[0]=items\_on\_first\_input\_stream;  
ninput\_items\_required[1]=items\_on\_data\_input\_stream;  
}

int  
dsss\_spreading\_blk\_b::general\_work(int noutput\_items,  
gr\_vector\_int &ninput\_items,  
gr\_vector\_const\_void\_star &input\_items,  
gr\_vector\_void\_star &output\_items)  
{  
//std::cout \<\< d\_length\_PN \<\< std::endl;  
const unsigned char \*in\_1 = (const unsigned char \*)input\_items[0];//  
PN  
const unsigned char \*in\_2 = (const unsigned char \*)input\_items[1];//  
data  
unsigned char \*out = (unsigned char \*)output\_items[0];  
int pn\_input\_cnt=0;  
int items\_on\_1 = ninput\_items[0];  
int items\_on\_2 = ninput\_items[1];  
int i=0;  
int data\_cnt=0;  
int cc=0;  
for(i=0;(i\<noutput\_items)&&(data\_cnt\<items\_on\_2);i++){  
data\_cnt=data\_cnt+1;  
//pn\_input\_cnt=0;  
unsigned char temp = in\_2[data\_cnt-1];  
//std::cout \<\< “int\_2=” \<\< (int)temp \<\< std::endl;  
//std::cout \<\< " in \t" \<\< " out" \<\< std::endl;  
for(int j=0;j\<8;j++){  
pn\_input\_cnt=0;  
if((temp\>\>j)&0x01){  
while(pn\_input\_cnt\<d\_length\_PN){  
//std::cout \<\< " “\<\< (int)in\_1[i] \<\< “\t”;  
out[i]=in\_1[i];  
// std::cout \<\< " \t”\<\< (int)out[i] \<\< std::endl;  
i=i+1;  
pn\_input\_cnt=pn\_input\_cnt+1;

```
            }
        }else{
            while(pn_input_cnt<d_length_PN){
                //std::cout << " "<< (int)in_1[i] << "\t";
                out[i]=in_1[i] ^ 0x01;
            // std::cout << " \t"<< (int)out[i] << std::endl;
                i=i+1;
                pn_input_cnt=pn_input_cnt+1;

            }
        }
    }
    //cc++;
    //if(cc==1)
        //exit(0);
}
consume(0,i);
consume(1,data_cnt);

```

}

//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

[//spread.i](https://spread.i)  
/\* -_- c++ -_- _/  
/_

- Copyright 2007 Free Software Foundation, Inc.
- 
- This file is part of GNU Radio
- 
- GNU Radio 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.
- 
- GNU Radio 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 GNU Radio; see the file COPYING. If not, write to
- the Free Software Foundation, Inc., 51 Franklin Street,
- Boston, MA 02110-1301, USA.  
\*/

%feature(“autodoc”, “1”); // generate python docstrings

%include “exception.i”  
%import “gnuradio.i”  
// the common stuff  
%{  
#include “gnuradio\_swig\_bug\_workaround.h” // mandatory bug fix  
#include “dsss\_spreading\_blk\_b.h”  
#include   
%}

GR\_SWIG\_BLOCK\_MAGIC(dsss,spreading\_blk\_b);

dsss\_spreading\_blk\_b\_sptr  
dsss\_make\_spreading\_blk\_b(int degree\_PN)  
throw (std::runtime\_error);

class dsss\_spreading\_blk\_b:public gr\_block  
{  
protected:  
dsss\_spreading\_blk\_b(int degree\_PN);  
};

---

<div class="post-metadata">

**Author:** ![Murtuza](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@Murtuza](https://www.ruby-forum.com/u/Murtuza)\
**Post date:** [October 1, 2008, 10:31pm UTC](https://www.ruby-forum.com/t/gnuradio-block-behaves-strange-please-have-a-look-at-this/149919/2 "2008-10-01T22:31:37Z")

</div>

On Tue, Sep 30, 2008 at 11:19:54PM -0500, Murtuza wrote:

> The output is written to a file. The output written to the file always has
> 
> I suspect something is wrong with the way I call the “consume” function.  
> Any kind of help will be greatly appreciated.

First off, I think this would be easier to understand (and get right)  
if both inputs are defined to look at only the LSB. You can use the  
gr.packed\_to\_unpacked block to unpack your data bytes.

Assuming you make this change, then

The PN code is coming in on 0, and it requires 1 input for every  
output.  
The data is coming in on 1, and it requires 1 input for every  
d\_length\_PN outputs.

Do

set\_output\_multiple(d\_length\_PN)

in your constructor

void  
dsss\_spreading\_blk\_b::forecast(int noutput\_items,  
gr\_vector\_int &ninput\_items\_required)  
{  
assert(noutput\_items % d\_length\_PN == 0);  
ninput\_items\_required[0] = noutput\_items;  
ninput\_items\_required[1] = noutput\_items / d\_length\_PN;  
}

int  
dsss\_spreading\_blk\_b::general\_work(int noutput\_items,  
gr\_vector\_int &ninput\_items,  
gr\_vector\_const\_void\_star  
&input\_items,  
gr\_vector\_void\_star &output\_items)  
{  
assert(noutput\_items % d\_length\_PN == 0);

const unsigned char \*pn = (const unsigned char \*)input\_items[0]; //  
PN  
const unsigned char \*data = (const unsigned char \*)input\_items[1]; //  
data  
unsigned char \*out = (unsigned char \*)output\_items[0];

for(int pi=0, di=0; pi \< noutput\_items; pi += d\_length\_PN, di++){  
int current\_data = pn[di]  
for (int j=0; j \< d\_length\_PN; j++){  
out[pi + j] = f(current\_data, pn[pi])  
}  
}

consume(0, noutput\_items);  
consume(1, noutput\_items / d\_length\_PN)  
}

---

<div class="post-metadata">

**Author:** ![Murtuza](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@Murtuza](https://www.ruby-forum.com/u/Murtuza)\
**Post date:** [October 1, 2008, 10:35pm UTC](https://www.ruby-forum.com/t/gnuradio-block-behaves-strange-please-have-a-look-at-this/149919/3 "2008-10-01T22:35:43Z")

</div>

On Wed, Oct 01, 2008 at 01:30:59PM -0700, Eric B. wrote:

> unsigned char \*out = (unsigned char \*)output\_items[0];
> 
> for(int pi=0, di=0; pi \< noutput\_items; pi += d\_length\_PN, di++){  
> int current\_data = pn[di]  
> for (int j=0; j \< d\_length\_PN; j++){  
> out[pi + j] = f(current\_data, pn[pi])

Sorry, should be:

> ```
> out[pi + j] = f(current_data, pn[pi + j])
> 
> ```

---

<div class="post-metadata">

**Author:** ![Murtuza](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@Murtuza](https://www.ruby-forum.com/u/Murtuza)\
**Post date:** [October 5, 2008, 11:24am UTC](https://www.ruby-forum.com/t/gnuradio-block-behaves-strange-please-have-a-look-at-this/149919/4 "2008-10-05T11:24:17Z")

</div>

Hi Eric

I did exactly as you told me to do but still I face the same problem. I  
use  
gr.packed\_to\_unpacked\_bb(1,gr.GR\_LSB\_FIRST) to send one bit each time  
but I  
still find some data missing in the destination file. When I print the  
output\_items to stdout I find no error and the displayed output is  
coherent  
with the expected output. The problem arises only when I try to write  
this  
out to a file. I write it to the file in order to test it.

What could be wrong with this code. This is what I do in python.  
spread is the gnruadio block that does spreading according to my C++  
block.  
The input file “/home/murtuza/f” contains only 1 byte which is 0xAA. I  
repeatedly read this using “True” in the gr.file\_source.

> > > t.connect(b,(s,1))  
> > > t.connect(c,(x,0))  
> > > t.connect(d,(x,1))  
> > > t.connect(x,(s,0))  
> > > t.connect(s,ds)  
> > > t.start()  
> > > t.stop()

What do you think can cause this confusing problem ?

Thanks  
Ali

---

<div class="post-metadata">

**Author:** ![Murtuza](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@Murtuza](https://www.ruby-forum.com/u/Murtuza)\
**Post date:** [October 6, 2008, 7:43pm UTC](https://www.ruby-forum.com/t/gnuradio-block-behaves-strange-please-have-a-look-at-this/149919/5 "2008-10-06T19:43:22Z")

</div>

On Sun, Oct 05, 2008 at 04:23:10AM -0500, Murtuza wrote:

> Hi Eric
> 
> I did exactly as you told me to do but still I face the same problem. I use  
> gr.packed\_to\_unpacked\_bb(1,gr.GR\_LSB\_FIRST) to send one bit each time but I  
> still find some data missing in the destination file.

What, where, and how much data is missing? Is the data incorrect, or  
are there gaps, or zeros, or what? You not giving us much to work  
with…

Eric

---

<div class="post-metadata">

**Author:** ![Murtuza](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@Murtuza](https://www.ruby-forum.com/u/Murtuza)\
**Post date:** [October 6, 2008, 11:21pm UTC](https://www.ruby-forum.com/t/gnuradio-block-behaves-strange-please-have-a-look-at-this/149919/6 "2008-10-06T23:21:26Z")

</div>

Hi Eric ,

This is the detail.

File source : I have a binary file with only one byte in it. The byte  
value  
is 0x5B in hex.  
PN Sequence : By using  
c = gr.glfsr\_source\_b(3,True,0x05,0x01)  
d = gr.glfsr\_source\_b(3,True,0x06,0x01)  
as the m-sequence generators and XORing them together I get a gold-code  
sequence of [0 1 0 1 0 0 0],which when written in HEX should appear as  
[00 01 00 01 00 00 00] as the sequence of inputs on the input\_stream[0]  
of  
the spread.spreading\_blk\_b.

As I am reading the data repeatedly from file\_source and unpacking it  
bit-by-bit using gr.packed\_to\_unpacked\_bb(1,gr.GR\_LSB\_FIRST) the input  
on  
input\_stream[1] is sequence of either 0x01 or 0x00. In my case one full  
sequence of a byte should look like this in HEX at the input\_Stream[1]  
 →   
[01 01 00 01 01 00 01 00] which is 0x5B with the first entry  
representing  
the LSB of 0x5B .

For each input on input\_stream[1] I must get 7 items on output\_stream  
which  
is the gold-code sequence. If I have 0x01 on input\_stream[1] I must get  
[00  
01 00 01 00 00 00 ] and if I have 0x00 on input\_stream[1] I must get [01  
00  
01 00 01 01 01] which is the complement of the PN-sequence.

I check the output\_stream on stdout and the values for out[i] each time  
is  
exactly as it has to be. But the data written to the file is not right.  
This  
is the data in the output file. The gr.file\_sink block takes output from  
the  
spread.spreading\_blk\_b block as the input. This is the data I got in the  
output file in HEX (arranged in blocks of length of PN-sequence = 2^3 -  
1 =  
7)

00 00 01 00 00 01 00 01 00 00 01 00 00 01  
00 01 00 00 01 01 01 00 01 00 00 00 00 00  
00 00 00 00 00 00 01 01 01 01 00 01 00 00  
00 01 01 01 00 01 00 00 00 00 00 00 00 00  
00 01 01 01 01 01 00 00 00 00 00 01 01 00  
01 00 00 00 00 01 00 01 00 01 01 00 00 00  
00 00 01 01 01 01 00 01 00 00 01 00 01 00  
00 00 00 00 01 00 01 01 00 00 00 00 01 01  
01 01 00 00 00 00 00 01 01 00 01 00 01 00  
00 01 . . . . . . .

Clearly, this data is not right as I must either get a complete  
PN-sequence  
or the complement of it. The data observed above doesn’t show [00 01 00  
01  
00 00 00 ] (output when input is 0x01)or [01 00 01 00 01 01 01](output  
when  
input is 0x00). The correct data should look as shown below for 1-byte  
of  
input i.e. 8 items on input\_stream[1].

00 01 00 01 00 00 00 00 01 00 01 00 00 00  
01 00 01 00 01 01 01 00 01 00 01 00 00 00  
00 01 00 01 00 00 00 01 00 01 00 01 01 01  
00 01 00 01 00 00 00 01 00 01 00 01 01 01

I hope this information is good enough for you to help me. Do you want  
me to  
send my files as a “.tar.gz” compressed file ?

This is what I do in Python(mentioning it again)

> > > t.connect(b,(s,1))  
> > > t.connect(c,(x,0))  
> > > t.connect(d,(x,1))  
> > > t.connect(x,(s,0))  
> > > t.connect(s,ds)  
> > > t.start()

Thanks once again,  
Ali

---

<div class="post-metadata">

**Author:** ![Murtuza](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@Murtuza](https://www.ruby-forum.com/u/Murtuza)\
**Post date:** [October 7, 2008, 1:34am UTC](https://www.ruby-forum.com/t/gnuradio-block-behaves-strange-please-have-a-look-at-this/149919/7 "2008-10-07T01:34:31Z")

</div>

This is the corrected C++ source code.

/\* -_- c++ -_- _/  
/_

- Copyright 2004 Free Software Foundation, Inc.
- 
- This file is part of GNU Radio
- 
- GNU Radio 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.
- 
- GNU Radio 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 GNU Radio; see the file COPYING. If not, write to
- the Free Software Foundation, Inc., 51 Franklin Street,
- Boston, MA 02110-1301, USA.  
\*/

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

#include \<dsss\_spreading\_blk\_b.h\>  
#include \<gr\_io\_signature.h\>  
#include \<assert.h\>  
#include   
#include   
dsss\_spreading\_blk\_b\_sptr  
dsss\_make\_spreading\_blk\_b (int degree\_PN)  
{  
return dsss\_spreading\_blk\_b\_sptr (new dsss\_spreading\_blk\_b  
(degree\_PN));  
}

/\*  
Input\_stream\_2 = data\_source to be spread.  
Input\_stream\_1 = PN\_sequence  
\*/

dsss\_spreading\_blk\_b::dsss\_spreading\_blk\_b(int  
degree\_PN):gr\_block(“spreading\_blk”,  
gr\_make\_io\_signature(2,2,sizeof(unsigned char)),  
gr\_make\_io\_signature(1,1,sizeof(unsigned char))),  
d\_length\_PN((int)(pow(2,degree\_PN)-1))  
{  
set\_output\_multiple(d\_length\_PN); // noutput\_items == multiple of  
length  
of PN sequence  
}

void  
dsss\_spreading\_blk\_b::forecast(int noutput\_items,gr\_vector\_int  
&ninput\_items\_required){  
assert(noutput\_items % d\_length\_PN==0);

```
ninput_items_required[0]=noutput_items;
ninput_items_required[1]=noutput_items/d_length_PN;

```

}

int  
dsss\_spreading\_blk\_b::general\_work(int noutput\_items,  
gr\_vector\_int &ninput\_items,  
gr\_vector\_const\_void\_star &input\_items,  
gr\_vector\_void\_star &output\_items)  
{  
//std::cout \<\< "length of PN = "\<\< d\_length\_PN \<\< std::endl;  
const unsigned char \*pn = (const unsigned char \*)input\_items[0];//  
PN  
const unsigned char \*data = (const unsigned char \*)input\_items[1];//  
data  
unsigned char \*out = (unsigned char \*)output\_items[0];  
int data\_items = ninput\_items[1];  
int pi,di;  
for(pi=0,di=0;(pi\<noutput\_items);pi=pi+d\_length\_PN,di++){  
unsigned char temp = data[di];  
// std::cout \<\< "input " \<\< (int)temp \<\< std::endl;  
bool lsb=false;  
if(temp & 0x01)  
lsb=true;  
else  
lsb=false;  
for(int j=0;j\<d\_length\_PN;j++){  
if(lsb)  
out[pi+j] = pn[pi+j];  
else  
out[pi+j] = pn[pi+j]^0x01;  
// std::cout \<\< (int)out[pi+j] \<\< std::endl;  
}  
//if(di==7)  
// exit(0);  
}  
consume(0,noutput\_items);  
consume(1,noutput\_items/d\_length\_PN);  
}
