 # Convolution integral export as animation

This example is taken from a tutorial
<https://dspillustrations.com/pages/posts/misc/convolution-examples-and-the-convolution-integral.html>
related
to convolution integral.

I would like to export this example as animation in mp4 format. So far, the
code looks like this :

import scipy.integrateimport numpy as npimport matplotlib.pyplot as
pltimport 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()

When I run the program I see the output of see the output of convolution
integral at t0 = -0.20.

[image: convolution.png]

Is there a way to change t0 so that I'd like to able to save it as
animation as in the tutorial
<https://dspillustrations.com/pages/posts/misc/convolution-examples-and-the-convolution-integral.html>
In
the example t0 decreases from -2.0 to -1.95 etc the green curve is shifted
right, and the area between curves, product increases. In the tutorial
there is an html animation and I would like to save as mp4 file.

I've also added source code as an attachment.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20190519/01592354/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: convolution.png
Type: image/png
Size: 36111 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20190519/01592354/attachment-0001.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: convolution.py
Type: text/x-python
Size: 2029 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20190519/01592354/attachment-0001.py>

Erdem,

I do not understand your question. If I run that code I do get an mp4 file
of the animation.

Tom

···

On Sat, May 18, 2019 at 6:05 PM Erdem <farukerdemoncel at gmail.com> wrote:

This example is taken from a tutorial
<https://dspillustrations.com/pages/posts/misc/convolution-examples-and-the-convolution-integral.html> related
to convolution integral.

I would like to export this example as animation in mp4 format. So far,
the code looks like this :

import scipy.integrateimport numpy as npimport matplotlib.pyplot as pltimport 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()

When I run the program I see the output of see the output of convolution
integral at t0 = -0.20.

[image: convolution.png]

Is there a way to change t0 so that I'd like to able to save it as
animation as in the tutorial
<https://dspillustrations.com/pages/posts/misc/convolution-examples-and-the-convolution-integral.html> In
the example t0 decreases from -2.0 to -1.95 etc the green curve is shifted
right, and the area between curves, product increases. In the tutorial
there is an html animation and I would like to save as mp4 file.

I've also added source code as an attachment.
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users at python.org
https://mail.python.org/mailman/listinfo/matplotlib-users

--
Thomas Caswell
tcaswell at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
-------------- next part --------------
A non-text attachment was scrubbed...
Name: convolution.png
Type: image/png
Size: 36111 bytes
Desc: not available

Please don't drop the mailing list from the replies.

Ah, I ran it by invoking the script in an IPython session where plt.ion()
has been called and plt.show() in non-blocking. If I run it as as script
I see what you descirbe!

Removing the plt.show() calls from inside of the re-draw function allows
it to run end-to-end and write out the animation.

Tom

···

On Sun, May 19, 2019 at 5:59 PM Erdem <farukerdemoncel at gmail.com> wrote:

Thank you very much.

My operating system is Ubuntu 18.04 LTS 64 bit. I've set up a virtual
python environment and to activate I use this command.

$. venv/bin/activate I've saved source file as convolution py. To run the source code I use this command$ python convolution.py

But rather than an animation I see only a plot of integral at t0=-2.00

[image: convolution.png]

I wait some time and nothing happens. I've also tried to close window a
few times and the result is same.

Since you may be able to get an mp4 file of the animation may it be
because of some codecs are missing or may be a bug?

Erdem

Thomas Caswell <tcaswell at gmail.com>, 20 May 2019 Pzt, 00:17 tarihinde
?unu yazd?:

Erdem,

I do not understand your question. If I run that code I do get an mp4
file of the animation.

Tom

On Sat, May 18, 2019 at 6:05 PM Erdem <farukerdemoncel at gmail.com> wrote:

This example is taken from a tutorial
<https://dspillustrations.com/pages/posts/misc/convolution-examples-and-the-convolution-integral.html> related
to convolution integral.

I would like to export this example as animation in mp4 format. So far,
the code looks like this :

import scipy.integrateimport numpy as npimport matplotlib.pyplot as pltimport 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()

When I run the program I see the output of see the output of convolution
integral at t0 = -0.20.

[image: convolution.png]

Is there a way to change t0 so that I'd like to able to save it as
animation as in the tutorial
<https://dspillustrations.com/pages/posts/misc/convolution-examples-and-the-convolution-integral.html> In
the example t0 decreases from -2.0 to -1.95 etc the green curve is shifted
right, and the area between curves, product increases. In the tutorial
there is an html animation and I would like to save as mp4 file.

I've also added source code as an attachment.
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users at python.org
https://mail.python.org/mailman/listinfo/matplotlib-users

--
Thomas Caswell
tcaswell at gmail.com

--
Thomas Caswell
tcaswell at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20190519/07471a2a/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: convolution.png
Type: image/png
Size: 36111 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20190519/07471a2a/attachment-0002.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: convolution.png
Type: image/png
Size: 36111 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20190519/07471a2a/attachment-0003.png>

Yes, that is controlled by the fps kwarg to anim.save.

Tom

···

On Sun, May 19, 2019 at 6:23 PM Erdem <farukerdemoncel at gmail.com> wrote:

My reply does not show in the mail list. I have no idea why this happened
but I simply use Gmail to reply. I used to use Gnus message reader with
Emacs but I think it is not installed for now.

Thanks now it works as expected!

Is there a way to slow down animation a bit.

Erdem

Thomas Caswell <tcaswell at gmail.com>, 20 May 2019 Pzt, 01:05 tarihinde
?unu yazd?:

Please don't drop the mailing list from the replies.

Ah, I ran it by invoking the script in an IPython session where
plt.ion() has been called and plt.show() in non-blocking. If I run it
as as script I see what you descirbe!

Removing the plt.show() calls from inside of the re-draw function
allows it to run end-to-end and write out the animation.

Tom

On Sun, May 19, 2019 at 5:59 PM Erdem <farukerdemoncel at gmail.com> wrote:

Thank you very much.

My operating system is Ubuntu 18.04 LTS 64 bit. I've set up a virtual
python environment and to activate I use this command.

$. venv/bin/activate I've saved source file as convolution py. To run the source code I use this command$ python convolution.py

But rather than an animation I see only a plot of integral at t0=-2.00

[image: convolution.png]

I wait some time and nothing happens. I've also tried to close window a
few times and the result is same.

Since you may be able to get an mp4 file of the animation may it be
because of some codecs are missing or may be a bug?

Erdem

Thomas Caswell <tcaswell at gmail.com>, 20 May 2019 Pzt, 00:17 tarihinde
?unu yazd?:

Erdem,

I do not understand your question. If I run that code I do get an mp4
file of the animation.

Tom

On Sat, May 18, 2019 at 6:05 PM Erdem <farukerdemoncel at gmail.com> >>>> wrote:

This example is taken from a tutorial
<https://dspillustrations.com/pages/posts/misc/convolution-examples-and-the-convolution-integral.html> related
to convolution integral.

I would like to export this example as animation in mp4 format. So
far, the code looks like this :

import scipy.integrateimport numpy as npimport matplotlib.pyplot as pltimport 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()

When I run the program I see the output of see the output of
convolution integral at t0 = -0.20.

[image: convolution.png]

Is there a way to change t0 so that I'd like to able to save it as
animation as in the tutorial
<https://dspillustrations.com/pages/posts/misc/convolution-examples-and-the-convolution-integral.html> In
the example t0 decreases from -2.0 to -1.95 etc the green curve is shifted
right, and the area between curves, product increases. In the tutorial
there is an html animation and I would like to save as mp4 file.

I've also added source code as an attachment.
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users at python.org
https://mail.python.org/mailman/listinfo/matplotlib-users

--
Thomas Caswell
tcaswell at gmail.com

--
Thomas Caswell
tcaswell at gmail.com

--
Thomas Caswell
tcaswell at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20190519/d86ee279/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: convolution.png
Type: image/png
Size: 36111 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20190519/d86ee279/attachment-0002.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: convolution.png
Type: image/png
Size: 36111 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20190519/d86ee279/attachment-0003.png>