此示例摘自与卷积积分有关的tutorial。
我想将此示例导出为mp4格式的动画。到目前为止,代码看起来像这样:
span
据我了解,我应该能够以0.05的步长在-2.00和2.00之间更改t0值。乍一看,我尝试使用numpy的arange函数。
import scipy.integrate
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def showConvolution(f1, f2, t0):
# Calculate the overall convolution result using Simpson integration
convolution = np.zeros(len(t))
for n, t_ in enumerate(t):
prod = lambda tau: f1(tau) * f2(t_-tau)
convolution[n] = scipy.integrate.simps(prod(t), t)
# Create the shifted and flipped function
f_shift = lambda t: f2(t0-t)
prod = lambda tau: f1(tau) * f2(t0-tau)
# Plot the curves
plt.gcf().clear() # il
plt.subplot(211)
plt.gca().set_ymargin(0.05) # il
plt.plot(t, f1(t), label=r'$f_1(\tau)$')
plt.plot(t, f_shift(t), label=r'$f_2(t_0-\tau)$')
plt.fill(t, prod(t), color='r', alpha=0.5, edgecolor='black', hatch='//') # il
plt.plot(t, prod(t), 'r-', label=r'$f_1(\tau)f_2(t_0-\tau)$')
plt.grid(True); plt.xlabel(r'$\tau$'); plt.ylabel(r'$x(\tau)$') # il
plt.legend(fontsize=10) # il
plt.text(-4, 0.6, '$t_0=%.2f$' % t0, bbox=dict(fc='white')) # il
# plot the convolution curve
plt.subplot(212)
plt.gca().set_ymargin(0.05) # il
plt.plot(t, convolution, label='$(f_1*f_2)(t)$')
# recalculate the value of the convolution integral at the current time-shift t0
current_value = scipy.integrate.simps(prod(t), t)
plt.plot(t0, current_value, 'ro') # plot the point
plt.grid(True); plt.xlabel('$t$'); plt.ylabel('$(f_1*f_2)(t)$') # il
plt.legend(fontsize=10) # il
plt.show() # il
Fs = 50 # our sampling frequency for the plotting
T = 5 # the time range we are interested in
t = np.arange(-T, T, 1/Fs) # the time samples
f1 = lambda t: np.maximum(0, 1-abs(t))
f2 = lambda t: (t>0) * np.exp(-2*t)
t0 = np.arange(-2.0,2.0, 0.05)
fig = plt.figure(figsize=(8,3))
anim = animation.FuncAnimation(fig, showConvolution(f1,f2, t0), frames=np.linspace(0, 50, 500), interval=80)
anim.save('animation.mp4', fps=30) # fps = frames per second
plt.show()
但是它给出了一条错误消息:
ValueError:操作数不能与形状一起广播(80,) (500,)
如何更改t0值,以便能够生成动画视频?
编辑: 我尝试了建议的更改。我用
运行该示例t0 = np.arange(-2.0,2.0, 0.05)
我没有看到动画,而是看到了t0 = -0.20时卷积积分的输出。
是否有一种方法可以更改t0,以便像the tutorial中一样将其保存为动画,在示例中,t0从-2.0减小到-1.95,因此绿色曲线向右移动,并且曲线之间的面积,乘积增加。在该示例中,有一个html动画,我想另存为mp4文件。
编辑2:
从重绘功能内部删除python convolution.py
调用允许
它可以端到端运行并编写动画。
答案 0 :(得分:1)
该示例似乎是错误的。
FuncAnimation中的第二个参数采用一个可调用对象,每个循环中的第一个参数将从'frames'关键字参数中获取新值。请查看matplotlib文档,以查找有关可调用对象的必需签名的更多信息。
我只是简单地改变了showConvolution()参数,以便t0是第一个参数。范围t0用作所需的帧参数。 λ函数f1和f2在“ fargs”中的元组中传递。
希望它对您有帮助,
干杯
BdeG
import scipy.integrate
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def showConvolution(t0,f1, f2):
# Calculate the overall convolution result using Simpson integration
convolution = np.zeros(len(t))
for n, t_ in enumerate(t):
prod = lambda tau: f1(tau) * f2(t_-tau)
convolution[n] = scipy.integrate.simps(prod(t), t)
# Create the shifted and flipped function
f_shift = lambda t: f2(t0-t)
prod = lambda tau: f1(tau) * f2(t0-tau)
# Plot the curves
plt.gcf().clear() # il
plt.subplot(211)
plt.gca().set_ymargin(0.05) # il
plt.plot(t, f1(t), label=r'$f_1(\tau)$')
plt.plot(t, f_shift(t), label=r'$f_2(t_0-\tau)$')
plt.fill(t, prod(t), color='r', alpha=0.5, edgecolor='black', hatch='//') # il
plt.plot(t, prod(t), 'r-', label=r'$f_1(\tau)f_2(t_0-\tau)$')
plt.grid(True); plt.xlabel(r'$\tau$'); plt.ylabel(r'$x(\tau)$') # il
plt.legend(fontsize=10) # il
plt.text(-4, 0.6, '$t_0=%.2f$' % t0, bbox=dict(fc='white')) # il
# plot the convolution curve
plt.subplot(212)
plt.gca().set_ymargin(0.05) # il
plt.plot(t, convolution, label='$(f_1*f_2)(t)$')
# recalculate the value of the convolution integral at the current time-shift t0
current_value = scipy.integrate.simps(prod(t), t)
plt.plot(t0, current_value, 'ro') # plot the point
plt.grid(True); plt.xlabel('$t$'); plt.ylabel('$(f_1*f_2)(t)$') # il
plt.legend(fontsize=10) # il
plt.show() # il
Fs = 50 # our sampling frequency for the plotting
T = 5 # the time range we are interested in
t = np.arange(-T, T, 1/Fs) # the time samples
f1 = lambda t: np.maximum(0, 1-abs(t))
f2 = lambda t: (t>0) * np.exp(-2*t)
t0 = np.arange(-2.0,2.0, 0.05)
fig = plt.figure(figsize=(8,3))
anim = animation.FuncAnimation(fig, showConvolution, frames=t0, fargs=(f1,f2),interval=80)
anim.save('animation.mp4', fps=30) # fps = frames per second
plt.show()