为了测试我正在尝试使用 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
这个块产生正弦波,但音频真的很不稳定,就像我不能足够快地产生正弦波或其他东西(sample_rate 应该不是问题,因为如果我用信号源替换我的块,相同的流程图可以正常工作)
我也在使用 Null 源(因为我还没有弄清楚如何创建一个没有任何源的 python 块(一旦我设置 in_sig=None 就没有音频从我的块中出来,所以我不确定信号源在没有任何源连接到其输入的情况下生成音频)
感谢您的回复和最诚挚的问候