discuss-gnuradio
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Problem initializing parameters in Gnuradio Python Block?


From: George Edwards
Subject: Re: Problem initializing parameters in Gnuradio Python Block?
Date: Thu, 14 Jan 2021 09:42:12 -0600

Hell Tim and Jeff,

Problem, so when I make v global in my_init() method and use it is the work() method, it works good if I do not try to change the values in the vector v, let's say v came down as v = np.array([1.0 , 1.0 , 1.0]). If I try to change the values say  v[0] += v[0], etc. the program breaks with the message local variable v changes before assignment. I thought it was global and already assigned.

Thanks for your help.

George 


On Thu, Jan 14, 2021 at 9:17 AM George Edwards <gedwards.eng@gmail.com> wrote:
Hi Tim and Jeff,

Thanks for your help and insights. Being new to Python, I was looking at some Python videos and found my solution, I will make the numpy parameter vectors in the my_init() method "global" and that should solve the problem.

Thank you!

Regards,
George

On Thu, Jan 14, 2021 at 9:57 AM Tim Huggins <huggins.timothy@yahoo.com> wrote:
George,

"My understanding is the self.x, etc. is used to initialize param arguments in the def __init__() method and in my case, the only argument in this method is start"


This is not correct, you can add a self.v in there so it looks like:

def __init__(self, start = True):  # only default arguments here
        gr.sync_block.__init__(
            self,
            name='text',   # will show up in GRC
            in_sig=[()],
            out_sig=[()]
        )
        # if an attribute with the same name as a parameter is found,
        # a callback is registered (properties work, too).
        self.start = start
        self.v = self.my_init() #or whatever you want the  initial value to be

Tim


On Thursday, January 14, 2021, 10:38:36 AM EST, George Edwards <gedwards.eng@gmail.com> wrote:


Hi Jeff,

So I am new to Python programming, so some of the nuances I am not up on yet. My understanding is the self.x, etc. is used to initialize param arguments in the def __init__() method and in my case, the only argument in this method is start, there is no v for me to use self.v = v. v is used the work() and my_init() methods. At program startup work() calls my_init() method to get the initial values for the numpy array v (my_init() function will be called once only at startup when self.start is True). After initialization and v is passed back to the work(), the values in the numpy array v inside work will change on a sample to sample computation basis. My problem is how do I assign v its initial seed value at start up. If my program was simple with only one parameter v, I would initialize it inside the work() method, however, I have a bunch of other vector variables to initialize which is why I want to initialize all parameters inside a my_init() method that work() calls at startup. 

Thanks for the help.

George

On Thu, Jan 14, 2021 at 8:14 AM Jeff Long <willcode4@gmail.com> wrote:
You are already initializing self.start in exactly the same way you should be initializing self.v, right?

On Wed, Jan 13, 2021 at 10:46 PM George Edwards <gedwards.eng@gmail.com> wrote:
Hi Jeff,

Thanks for your answer. You are right, it crashes on the second call.

So how do I write the program to initialize a bunch of vectors in a "method strictly for initialization" when it first starts running? If this cannot be done, then I guess the only solution is to initialize them in the work() method even though it would make the work() method bulky?

Thanks again for your help.

Regards,
George

On Wed, Jan 13, 2021 at 8:12 PM Jeff Long <willcode4@gmail.com> wrote:
'v' is a local variable in work(). It is probably crashing on the second call, where my_init() is not called, and thus there is no 'v'.

On Wed, Jan 13, 2021 at 7:38 PM George Edwards <gedwards.eng@gmail.com> wrote:
Hello,

I am using a Gnuradio Python Block in my GRC signal processing and am having problems initializing my parameters. My system has a number of vector parameters to be initialized at startup. I will provide the gist of my goal in a scaled down version of my work. 
1. In the def __init__(self, start = True)  method, "start" is the parameter that will be used in the program to run the initialization process and is set as follows:
         self.start = start 
2. In the work(self, input_items, output_items)  method, I have the following at the start of the method:
         if self.start == True:
                v = self.my_init()    # go initialize all vectors

         output_items[0][:] = in0*v[0] + in1*v[1] + in2*v[2]  #computation using v
                                                     # with 3-inputs to the block 

3. In the my_init(self) method I have:
         self.start = False            # set start to False
         v = np.array([1., 2., 3.])  #hypothetical to make this simple
         return v

When I run the GRC model, it tells me that "v" is referenced before assignment. I am confused because I thought that the method my_init() would have been called before the computation and would return the values for "v". On the other hand if I do the assignment in the work(...) method as  v = np.array([1., 2., 3.]), it works perfectly.
Question: Why was the my_init() method not called properly to get  the values for the numpy array v?

Thanks for the help!

Regards,
George

reply via email to

[Prev in Thread] Current Thread [Next in Thread]