.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/convolution.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_convolution.py: Sympy notebook with the convolution =================================== In this notebook we use *sympy*, a CAS for python, to calculate the convolution of an gaussian with an one-sided decaying exponential. Since *sympy* is not an requirement of *skultrafast*, it may be manually installed. .. GENERATED FROM PYTHON SOURCE LINES 9-14 .. code-block:: Python import sympy from sympy import (symbols, Heaviside, exp, sqrt, oo, integrate, simplify, Eq, plot, pi, init_printing, solve) from sympy import erfc, erf init_printing() .. GENERATED FROM PYTHON SOURCE LINES 15-16 First, we need to define sympy symbols. .. GENERATED FROM PYTHON SOURCE LINES 16-21 .. code-block:: Python A, t, ti = symbols('A t ti', real=True) tau, sigma = symbols('tau sigma', positive=True) step = Heaviside .. GENERATED FROM PYTHON SOURCE LINES 22-23 Define :math:`y=A\exp(-t/\tau)` and the Gaussian IRF. .. GENERATED FROM PYTHON SOURCE LINES 25-28 .. code-block:: Python y = step(t) * A * exp(-t / tau) y .. GENERATED FROM PYTHON SOURCE LINES 29-32 .. code-block:: Python irf = 1 / sqrt(2 * pi * sigma**2) * exp(-t**2 / (2 * sigma**2)) irf .. GENERATED FROM PYTHON SOURCE LINES 33-34 Calculate the covolution integral. .. GENERATED FROM PYTHON SOURCE LINES 36-40 .. code-block:: Python func = integrate((irf.subs(t, t - ti) * y.subs(t, ti)), (ti, -oo, oo)) func = simplify(func) func .. GENERATED FROM PYTHON SOURCE LINES 41-43 Rewrite the `erf` with the `erfc` function: erfc, erf = special.error_functions.erfc, special.error_functions.erf .. GENERATED FROM PYTHON SOURCE LINES 43-46 .. code-block:: Python func2 = func.rewrite(erfc) func2 .. GENERATED FROM PYTHON SOURCE LINES 47-48 Plot the result for visualization: .. GENERATED FROM PYTHON SOURCE LINES 48-50 .. code-block:: Python plot(func2.subs(sigma, 0.2).subs(tau, 2).subs(A, 1), (t, -1, 10)) .. GENERATED FROM PYTHON SOURCE LINES 51-54 Normalized derivatives of a Gaussian ------------------------------------ These are used to model coherent contributions. .. GENERATED FROM PYTHON SOURCE LINES 54-68 .. code-block:: Python irf, irf.diff(t, 1), irf.diff(t, 2) p1 = None for i in range(0, 3): f = irf.diff(t, i) sol = solve(f.diff(t, 1), t) ext = f.subs(t, sol[0]) if not p1: p1 = plot((f/ext).subs(sigma, 1), (t, -4, 4), show=False) else: p1.append(plot((f/ext).subs(sigma, 1), (t, -4, 4), show=False)[0]) print((f/ext)) p1.show() .. GENERATED FROM PYTHON SOURCE LINES 69-71 .. code-block:: Python plot(irf.diff(t).subs(sigma, 0.2).subs(tau, 2), (t, -1, 1)) plot(irf.diff(t, 2).subs(sigma, 0.2).subs(tau, 2), (t, -1, 1)) .. _sphx_glr_download_auto_examples_convolution.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: convolution.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: convolution.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: convolution.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_