OOT Module with shape error

2021-05-25 Thread Elmore's
I am attempting to add an OOT module to my flowgraph. I have generated a 
General block which I want to place between an Audio Source and a Multiply 
Constant.

The Audio Source is set at a 48K sample rate. Input and Output are float.

As a test (I have never tried using an OOT module before) I said:
out[:] = input_items[0]
in an attempt to simply feed the input to the output unmodified.

I get a run time error:
ValueError: Could not broadcast input array from shape(4800) into shape(256)

Obviously I don’t know how to perform the simplest possible operation. What is 
wrong here?

I have tried looking on forums and reading some of the GNU docs but no joy.

Thanks for any help.

Jim



--
This email has been checked for viruses by AVG.
https://www.avg.com


Re: OOT Module with shape error

2021-05-25 Thread Jeff Long
The framework can give you different size input and output buffers. In this
case, you would:

nitems = len(input_items[0])
out[:nitems] = input_items[0]

You may have to do more checks, of course, because input could be larger
than output.

On Tue, May 25, 2021 at 5:03 PM Elmore's  wrote:

> I am attempting to add an OOT module to my flowgraph. I have generated a
> General block which I want to place between an Audio Source and a Multiply
> Constant.
>
> The Audio Source is set at a 48K sample rate. Input and Output are float.
>
> As a test (I have never tried using an OOT module before) I said:
> out[:] = input_items[0]
> in an attempt to simply feed the input to the output unmodified.
>
> I get a run time error:
> ValueError: Could not broadcast input array from shape(4800) into
> shape(256)
>
> Obviously I don’t know how to perform the simplest possible operation.
> What is wrong here?
>
> I have tried looking on forums and reading some of the GNU docs but no joy.
>
> Thanks for any help.
>
> Jim
>
>
>
>
> 
>  Virus-free.
> www.avg.com
> 
> <#m_-109177683565946075_DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
>


Gnuradio: SineWave generator python block choppy audio out

2021-05-25 Thread Mitja kocjančič
for testing something I am trying to develop my own SineWave generator
using python block in gnuradio

the block is here

"""
Embedded Python Blocks:

Each time this file is saved, GRC will instantiate the first class it finds
to get ports and parameters of your block. The arguments to __init__  will
be the parameters. All of them are required to have default values!
"""
import numpy as npfrom gnuradio import gr

class blk(gr.sync_block):  # other base classes are basic_block,
decim_block, interp_block
"""Embedded Python Block example - a simple multiply const"""

def __init__(self, sample_rate=192e3, frequency=1e3, amplitude=1):
 # only default arguments here
"""arguments to this function show up as parameters in GRC"""
gr.sync_block.__init__(
self,
name='SineWave Generator',   # will show up in GRC
in_sig=[np.float32],
#in_sig=None,
out_sig=[np.float32]
)
# if an attribute with the same name as a parameter is found,
# a callback is registered (properties work, too).
self.sample_rate = sample_rate
self.frequency = frequency
self.amplitude = amplitude


def work(self, input_items, output_items):
"""example: multiply with constant"""
print "length of input =",  len(input_items[0])
print "length of output =",  len(output_items[0])

for i in range(0, len(output_items[0])): #8192
output_items[0][i] = np.sin(2 * np.pi * self.frequency *
(i / self.sample_rate)) * self.amplitude



return len(output_items[0])


and is used in my flowgraph like so https://imgur.com/28kE1Sj


this block produces sine wave corecly but the audio is realy choopy like I
woulldn't be able to generate sinewave fast enough or something
(sample_rate shouldn't be a problem as the same flowgraph works corecly if
I substitute my block with Signal Source)

also I am using Null source (because I didn't yet figure out how to create
a python block without any source (as soon as I set in_sig=None no audio
ever comes out of my block, so I am not sure how Signal source gets away
with generating audio without any source connected to its input)

Thanks for Anwsering and Best Regards


Re: Gnuradio: SineWave generator python block choppy audio out

2021-05-25 Thread Marcus Müller
Hi Mitja!

Great to have you here :)


Couple of things up front:

1. This is GNU Radio 3.7. Especially if you want to implement things in Python 
blocks, 3.8
or later is a *must*. GNU Radio 3.7 is really just in legacy keepalive mode, 
not actively
developed. Please update to GNU Radio 3.8 or 3.9. You really should not start 
developing
for GNU Radio 3.7 in 2021!
2. WX GUI is dead. We stopped being able to support it a long time ago. Please 
use Qt GUI
instead.

All in all, please update your version of GNU Radio. In case any bugs occur, we 
won't be
able to help you with GNU Radio 3.7. GNU Radio 3.8 can be natively installed on 
almost all
modern Linux distros directly from their package managers, and on Windows, and 
OS X using
conda and other tools, without any complications.

With that out of the way, let's look at your code:

for i in range(0, len(output_items[0])): #8192
output_items[0][i] = np.sin(2 * np.pi * self.frequency * (i /
self.sample_rate)) * self.amplitude

No surprise the sound is choppy! You reset your i to 0 for every new call to 
work();
however, GNU Radio doesn't guarantee that work is called with number of items 
equal to a
multiple of your sine period.
So, this is a bug in your design. What you'd want is really something like

def work(self, input_items, output_items):
   # get rid of the for loop!
   startnr = self.ninputs_written(0)
   f_rel = 2 * np.pi * self.frequency / self.sample_rate
   number = len(output_items[0])
   output_items[0][:] = self.amplitude*np.sin(f_rel * np.arange(startnr, 
startnr+number))
   return number

Best regards
Marcus