为了测试一些东西,我正在尝试使用 gnuradio 中的 python 块开发我自己的 SineWave 发生器
块在这里
"""
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 np
from 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])
并在我的流程图中使用 https://imgur.com/28kE1Sj
这个块产生正弦波 corecly 但音频真的很糟糕,就像我不能足够快地生成正弦波或其他东西(sample_rate 应该不是问题,因为如果我用 Signal 替换我的块,那么相同的流程图可以corecly 工作来源)
我也在使用 Null 源(因为我还没有弄清楚如何在没有任何源的情况下创建一个 python 块(一旦我设置了 in_sig=None,我的块就没有声音了,所以我不确定信号源如何在没有任何源连接到其输入的情况下生成音频)
感谢您的回答和问候