我正在Python中构建一个GNURadio块,以将输入信号与根升余弦信号进行卷积。我注意到发送到函数 general_work 的输入信号具有许多样本变化,这对我来说很烦人。实际上,对于n_in个样本的输入信号和n个样本的根升余弦滤波器,输出信号的长度将为n_in + n-1(卷积信号的性质)。
我无法使用插值块,因为我不知道n_in,所以我也不知道插值因子(n_in + n-1)/ n_in的值。 因此,由于提供了 gr_modtool ,我使用了这种形式的通用块:
import numpy as np
from gnuradio import gr
class Convolve(gr.basic_block):
"""
docstring for block Convolve
"""
n=45 #Number of samples in the Root-raised cosine signal
def __init__(self):
gr.basic_block.__init__(self,
name="Convolve",
in_sig=[np.complex64],
out_sig=[np.complex64])
def forecast(self, noutput_items, ninput_items_required):
#setup size of input_items[i] for work call
print("---------- Forecast function --------------")
n_in = noutput_items-self.n+1
if n_in<=0:
ninput_items_required[0]=1
else:
ninput_items_required[0]=n_in
print("n_in : ", n_in)
print("ninput_items_required[0] : ", ninput_items_required[0])
print("noutput_items : ", noutput_items)
def general_work(self, input_items, output_items):
print("---------- General work function ----------------")
output_items[0][:] = 1 #Output is not important for the example
print("input_items[0] : ", len(input_items[0]))
print("output_items[0] : ", len(output_items[0]))
self.consume(0, len(input_items[0]))
#self.consume_each(len(input_items[0]))
return len(output_items[0])
在此示例中,我选择了根升余弦的样本数为n = 45。这会在终端上向我输出:
---------- Forecast function --------------
('n_in : ', 4052)
('ninput_items_required[0] : ', 4052)
('noutput_items : ', 4096)
---------- Forecast function --------------
('n_in : ', 2004)
('ninput_items_required[0] : ', 2004)
('noutput_items : ', 2048)
---------- Forecast function --------------
('n_in : ', 980)
('ninput_items_required[0] : ', 980)
('noutput_items : ', 1024)
---------- Forecast function --------------
('n_in : ', 468)
('ninput_items_required[0] : ', 468)
('noutput_items : ', 512)
---------- Forecast function --------------
('n_in : ', 212)
('ninput_items_required[0] : ', 212)
('noutput_items : ', 256)
---------- Forecast function --------------
('n_in : ', 84)
('ninput_items_required[0] : ', 84)
('noutput_items : ', 128)
---------- Forecast function --------------
('n_in : ', 20)
('ninput_items_required[0] : ', 20)
('noutput_items : ', 64)
---------- Forecast function --------------
('n_in : ', -12)
('ninput_items_required[0] : ', 1)
('noutput_items : ', 32)
---------- Forecast function --------------
('n_in : ', -28)
('ninput_items_required[0] : ', 1)
('noutput_items : ', 16)
---------- Forecast function --------------
('n_in : ', -36)
('ninput_items_required[0] : ', 1)
('noutput_items : ', 8)
---------- Forecast function --------------
('n_in : ', -40)
('ninput_items_required[0] : ', 1)
('noutput_items : ', 4)
---------- Forecast function --------------
('n_in : ', -42)
('ninput_items_required[0] : ', 1)
('noutput_items : ', 2)
---------- Forecast function --------------
('n_in : ', -43)
('ninput_items_required[0] : ', 1)
('noutput_items : ', 1)
---------- Forecast function --------------
('n_in : ', 4052)
('ninput_items_required[0] : ', 4052)
('noutput_items : ', 4096)
似乎多次调用了函数Forecast以测试输入和输出长度的不同值。它似乎正常工作,但是在此示例中,调用了通用工作函数,并且在此示例中,输入和输出的长度相同...这与通过Forecast函数获得的值不符。
---------- General work function ----------------
('input_items[0] : ', 4096)
('output_items[0] : ', 4096)
有人可以解释一下为什么我在一般的工作功能中有相同长度的信号吗? 如何更轻松地处理输入和输出信号的长度? 有没有更合适的方法来处理n_in为负的情况,而不是这种方式:
[...]
if n_in<=0:
ninput_items_required[0]=1
else:
ninput_items_required[0]=n_in
[...]