3

我想在 scipy 中执行正弦信号和矩形脉冲的卷积运算。我将正弦信号与余弦信号进行卷积并将其绘制在图表上,但我想知道如何使用矩形脉冲创建阵列,类似于这个 matlab 表达式

y = rectpulse(x,nsamp)

所以我可以对它们进行卷积。我用它来创建我的正弦和余弦信号

x=r_[0:50] (my array)
y01=sin(2*pi*x/49)
y02=cos(2*pi*x/49)

所以我尝试创建一个 nu.zeros(50),并手动将位置 15-25 的零从 0.0 更改。到 0.9 所以它看起来像矩形,但正弦阵列上的卷积和这个“矩形”阵列很奇怪,当没有交叉点但我得到正弦信号时,它应该为零,这是代码:

from scipy import *
from pylab import *

x = r_[0:50]
y1 = sin(2*pi*x/49)
#y2 = cos(2*pi*x/49)
y2 = np.zeros(50)
for i in range(15,25):
    y2[i] = 0.9
#print len(y1),len(y2)
y3 = convolve(y2,y1,mode="same")
subplot(2,2,1)
plot(x,y1)
hold(True)
plot(x,y2)
hold(True)
subplot(2,2,2)
print len(x),len(y3)
plot(x,y3)
hold(True)
show()

我提前道歉,我觉得这是最简单的事情,但我找不到任何关于如何创建矩形脉冲的参考。

4

1 回答 1

2

您已经很好地绘制了卷积,然后对其进行分析以发现它不是您所期望的,+1!但是,我认为您的担忧与convolve功能有关。由于您将参数传递给它,mode="same"它会切断等于零的卷积部分,并留下“有趣”的部分。你已经rect()很好地构建了这个函数,虽然我同意你的观点,但应该有另一种内置的方法来 scipy 某处。当我允许绘制整个卷积时,我得到:

卷积

从这段代码:

from scipy import *
from pylab import *

x = r_[0:50]
y1 = sin(2*pi*x/49)
#y2 = cos(2*pi*x/49)
y2 = np.zeros((y1.shape))
for i in range(15,25):
    y2[i] = 0.9
#print len(y1),len(y2)
#y3 = convolve(y2,y1,mode="same")
y3 = convolve(y2,y1)
subplot(2,2,1)
plot(x,y1)
hold(True)
plot(x,y2)
hold(True)
subplot(2,2,2)
print len(x),len(y3)
xx = r_[0:len(y3)]
print len(xx),len(y3)
plot(xx,y3)
hold(True)
show()

鉴于此功能的文档,这是我所期望的。让我知道我是否还有其他遗漏!

于 2012-03-05T14:51:00.820 回答