discuss-gnuradio
[Top][All Lists]
Advanced

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

Re: [Discuss-gnuradio] Python block with vector input and vector output


From: Marcus Müller
Subject: Re: [Discuss-gnuradio] Python block with vector input and vector output
Date: Fri, 30 Oct 2015 14:26:43 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0

Hi Chad,

so in that case, the trick would probably be writing good unit tests!
For example, you could take the `for` loop out of your work, and put it into a method of its own:

import numpy
from gnuradio import gr
from yall1 import *

class yall1_reconstruction_cc(gr.sync_block):
    """
    Yall1_reconstruction_block
    """
    def __init__(self,n,m):
	self.N = n
	self.M = m
	phi = np.load("/home/chad/Desktop/PROJECT/Python/Matrices/phi_mtx%(M)dx%(N)d.npy" %{"M":self.M,"N":self.N})
	psi = np.load("/home/chad/Desktop/PROJECT/Python/Matrices/psi_mtx%(N)dx%(N)d.npy" %{"N":self.N})
	self.alpha = np.dot(phi,psi)
        gr.sync_block.__init__(self,
            name="yall1_reconstruction",
            in_sig=[(np.complex64,self.M)],
            out_sig=[(np.complex64,self.N)])

    def work(self, input_items, output_items):
        in0 = input_items[0]
        output_items[0][:] = self._algo(in0)
        return len(output_items[0])


    def _algo(self, in):
	out = np.zeros((size,self.N),dtype = np.complex64)
	for i, vector in enumerate(in):
		recon = yall1(self.alpha, vector.reshape(self.M,1))*4.6
		out[i] = recon.reshape(self.N,)
        return out 
        
That way, you can write a unit test that first checks whether your for loop does exactly what you think it does for "artificial" input, without having to use the block in a GNU Radio flow graph.
It is does, the next step would be to write a unit test that uses well-known input in a vector source, connects that to your block, and connects that to a vector sink; validate .data() of that sink.

Best regards,
Marcus

On 10/30/2015 11:38 AM, Chad R wrote:
Hi Marcus

It's and adaption of the YALL1 Basis Pursuit Algorithm[1]. I simplified it a lot as I knew the case of signals I would be using so I have no need to parse it opts at run-time and can just leave those parameters constant. So I've adapted it that all I need to pass is the compressed vector and compression matrix. My Python implementation of the YALL1 algorithm works fine when I run it in a Python program but gives funny results in the GRC implementation. I'll look at what you said and get back to you if I don't understand something. Thanks for your help.

Kind Regards
Chad Richts

[1] User’s Guide for YALL1: Your ALgorithms for L1 Optimization: http://www.caam.rice.edu/~zhang/reports/tr0917.pdf

On Thu, Oct 29, 2015 at 9:04 PM, Marcus Müller <address@hidden> wrote:
Hi Chad,

thanks for re-posting your code; it's much clearer to read now.
I'm not quite sure what I'm looking at now: Is this your own implementation of the yall1 minimization function, or something that can be tested?
If I remember correctly, the matlab call has a matrix input, and a "target" vector, and an options argument. Is there some accessible documentation.

Other than my confusion whether this is a GNU Radio or a general yall1 question, your code looks pretty good. A few thing's I'd note:
  • self.alpha.shape  should be right
  • check that input_items.shape and in0.shape are as you expect them

Best regards,
Marcus


On 10/29/2015 01:27 PM, Chad R wrote:
Sorry. Something went wrong when I copied pasted it but my actual code is:

import numpy
from gnuradio import gr
from yall1 import *

class yall1_reconstruction_cc(gr.sync_block):
    """
    Yall1_reconstruction_block
    """
    def __init__(self,n,m):
	self.N = n
	self.M = m
	phi = np.load("/home/chad/Desktop/PROJECT/Python/Matrices/phi_mtx%(M)dx%(N)d.npy" %{"M":self.M,"N":self.N})
	psi = np.load("/home/chad/Desktop/PROJECT/Python/Matrices/psi_mtx%(N)dx%(N)d.npy" %{"N":self.N})
	self.alpha = np.dot(phi,psi)
        gr.sync_block.__init__(self,
            name="yall1_reconstruction",
            in_sig=[(np.complex64,self.M)],
            out_sig=[(np.complex64,self.N)])

    def work(self, input_items, output_items):
        in0 = input_items[0]
	size = np.shape(in0)[0]
	out = np.zeros((size,self.N),dtype = np.complex64)
	#out = yall1(self.alpha,in0[0]).reshape(self.N,)
	for i in range(0,size):
		recon = yall1(self.alpha, in0[i].reshape(self.M,1))*4.6
		out[i] = recon.reshape(self.N,)
        output_items[0][:] = out
        return len(output_items[0])


On Thu, Oct 29, 2015 at 2:08 PM, Marcus Müller <address@hidden> wrote:
Hi Chad,

there's something wrong with the indention of the lines between "def __init__" and "g.sync_block", and the same goes for your work function; so that's my first stab at explaining misbehaviour.


Best regards,
Marcus


On 29.10.2015 13:01, Chad R wrote:
Good day every one

I have implemented a Python block but I am not getting the results I expected. I get the results I expect at any frequency=samp_rate/2^n where n is any integer. My block makes use of a yall1 reconstruction algorithm to reconstruct a signal from M=100 to N=1024 vector.
The code for my block is shown below:

class yall1_reconstruction_cc(gr.sync_block):
    """
    Yall1_reconstruction_block
    """
    def __init__(self,n,m):
    self.N = n
    self.M = m
    phi = np.load("/home/chad/Desktop/PROJECT/Python/Matrices/phi_mtx%(M)dx%(N)d.npy" %{"M":self.M,"N":self.N})
    psi = np.load("/home/chad/Desktop/PROJECT/Python/Matrices/psi_mtx%(N)dx%(N)d.npy" %{"N":self.N})
    self.alpha = np.dot(phi,psi)
        gr.sync_block.__init__(self,
            name="yall1_reconstruction",
            in_sig=[(np.complex64,self.M)],
            out_sig=[(np.complex64,self.N)])

    def work(self, input_items, output_items):
        in0 = input_items[0]
    size = np.shape(in0)[0]
    out = np.zeros((size,self.N),dtype = np.complex64)
    #out = yall1(self.alpha,in0[0]).reshape(self.N,)
    for i in range(0,size):
        recon = yall1(self.alpha, in0[i].reshape(self.M,1))*4.7
        out[i] = recon.reshape(self.N,)
        output_items[0][:] = out
        return len(output_items[0])

Have I implemented it right? or is my issue with my implementation? I read through the tutorials but I feel some aspects are quiet hard to follow.

Thank you in advance for your help

Chad Richs


_______________________________________________
Discuss-gnuradio mailing list
address@hidden
https://lists.gnu.org/mailman/listinfo/discuss-gnuradio


_______________________________________________
Discuss-gnuradio mailing list
address@hidden
https://lists.gnu.org/mailman/listinfo/discuss-gnuradio






reply via email to

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