两个矩形脉冲的卷积Python
Convolution of two rectangular pulses Python
我试图找到两个矩形脉冲的卷积。
没有错误被抛出 - 我得到了一个合适形状的波形输出 - 但是,我的答案的幅度似乎太大了,我也不确定如何适应正确的 x/time 轴到这个卷积。
此外,卷积的幅度似乎取决于两个脉冲中的样本数量(本质上是采样频率)——我认为这是不正确的。
由于我试图模拟连续时间信号而不是离散时间信号,因此我将采样频率设置得非常高。
很明显我做错了什么 - 但它是什么,我该如何纠正它?
非常感谢 - 如果某些代码不是很抱歉,我们深表歉意 "pythonic"(最近 Java 转换)!
编辑:在尝试手动评估时,我发现时间轴太小了 2 倍;再次,我不知道为什么会这样
import numpy as np
import matplotlib.pyplot as plt
from sympy.functions.special import delta_functions as dlta
def stepFunction(t): #create pulses below from step-functions
retval = 0
if t == 0:
retval = 1
else:
retval = dlta.Heaviside(t)
return retval
def hT (t=0, start=0, dur=8, samples=1000):
time = np.linspace(start, start + dur, samples, True)
data = np.zeros(len(time))
hTArray = np.column_stack((time, data))
for row in range(len(hTArray)):
hTArray[row][1] = 2 * (stepFunction(hTArray[row][0] - 4) - stepFunction(hTArray[row][0] - 6))
return hTArray
def xT (t=0, start=0, dur=8, samples=1000):
time = np.linspace(start, start + dur, samples, True)
data = np.zeros(len(time))
hTArray = np.column_stack((time, data))
for row in range(len(hTArray)):
hTArray[row][1] = (stepFunction(hTArray[row][0]) - stepFunction(hTArray[row][0] - 4))
return hTArray
hTArray = hT() #populate two arrays with functions
xTArray = xT()
resCon = np.convolve(hTArray[:, 1], xTArray[:, 1]) #convolute signals/array data
Xaxis = np.linspace(hTArray[0][0], hTArray[len(hTArray) - 1][0],
len(resCon), endpoint=True) # create time axis, with same intervals as original functions
#Plot the functions & convolution
plt.plot(hTArray[:, 0], hTArray[:, 1], label=r'$x1(t)$')
plt.plot(xTArray[:, 0], xTArray[:, 1], label=r'$x2(t)$')
plt.plot(Xaxis, resCon)
plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3,
ncol=2, mode="expand", borderaxespad=0.)
ax = plt.gca()
ax.grid(True)
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', 0))
plt.show()
当您对离散信号进行卷积时,您需要适当地缩放以保持信号的能量(|x(t)|² 上的积分)恒定:
import numpy as np
import matplotlib.pyplot as plt
n = 1000
t = np.linspace(0, 8, n)
T = t[1] - t[0] # sampling width
x1 = np.where(t<=4, 1, 0) # build input functions
x2 = np.where(np.logical_and(t>=4, t<=6), 2, 0)
y = np.convolve(x1, x2, mode='full') * T # scaled convolution
ty = np.linspace(0, 2*8, n*2-1) # double time interval
# plot results:
fg, ax = plt.subplots(1, 1)
ax.plot(t, x1, label="$x_1$")
ax.plot(t, x2, label="$x_2$")
ax.plot(ty, y, label="$x_1\star x_2$")
ax.legend(loc='best')
ax.grid(True)
fg.canvas.draw()
我试图找到两个矩形脉冲的卷积。
没有错误被抛出 - 我得到了一个合适形状的波形输出 - 但是,我的答案的幅度似乎太大了,我也不确定如何适应正确的 x/time 轴到这个卷积。
此外,卷积的幅度似乎取决于两个脉冲中的样本数量(本质上是采样频率)——我认为这是不正确的。
由于我试图模拟连续时间信号而不是离散时间信号,因此我将采样频率设置得非常高。
很明显我做错了什么 - 但它是什么,我该如何纠正它? 非常感谢 - 如果某些代码不是很抱歉,我们深表歉意 "pythonic"(最近 Java 转换)!
编辑:在尝试手动评估时,我发现时间轴太小了 2 倍;再次,我不知道为什么会这样
import numpy as np
import matplotlib.pyplot as plt
from sympy.functions.special import delta_functions as dlta
def stepFunction(t): #create pulses below from step-functions
retval = 0
if t == 0:
retval = 1
else:
retval = dlta.Heaviside(t)
return retval
def hT (t=0, start=0, dur=8, samples=1000):
time = np.linspace(start, start + dur, samples, True)
data = np.zeros(len(time))
hTArray = np.column_stack((time, data))
for row in range(len(hTArray)):
hTArray[row][1] = 2 * (stepFunction(hTArray[row][0] - 4) - stepFunction(hTArray[row][0] - 6))
return hTArray
def xT (t=0, start=0, dur=8, samples=1000):
time = np.linspace(start, start + dur, samples, True)
data = np.zeros(len(time))
hTArray = np.column_stack((time, data))
for row in range(len(hTArray)):
hTArray[row][1] = (stepFunction(hTArray[row][0]) - stepFunction(hTArray[row][0] - 4))
return hTArray
hTArray = hT() #populate two arrays with functions
xTArray = xT()
resCon = np.convolve(hTArray[:, 1], xTArray[:, 1]) #convolute signals/array data
Xaxis = np.linspace(hTArray[0][0], hTArray[len(hTArray) - 1][0],
len(resCon), endpoint=True) # create time axis, with same intervals as original functions
#Plot the functions & convolution
plt.plot(hTArray[:, 0], hTArray[:, 1], label=r'$x1(t)$')
plt.plot(xTArray[:, 0], xTArray[:, 1], label=r'$x2(t)$')
plt.plot(Xaxis, resCon)
plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3,
ncol=2, mode="expand", borderaxespad=0.)
ax = plt.gca()
ax.grid(True)
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', 0))
plt.show()
当您对离散信号进行卷积时,您需要适当地缩放以保持信号的能量(|x(t)|² 上的积分)恒定:
import numpy as np
import matplotlib.pyplot as plt
n = 1000
t = np.linspace(0, 8, n)
T = t[1] - t[0] # sampling width
x1 = np.where(t<=4, 1, 0) # build input functions
x2 = np.where(np.logical_and(t>=4, t<=6), 2, 0)
y = np.convolve(x1, x2, mode='full') * T # scaled convolution
ty = np.linspace(0, 2*8, n*2-1) # double time interval
# plot results:
fg, ax = plt.subplots(1, 1)
ax.plot(t, x1, label="$x_1$")
ax.plot(t, x2, label="$x_2$")
ax.plot(ty, y, label="$x_1\star x_2$")
ax.legend(loc='best')
ax.grid(True)
fg.canvas.draw()