.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/extensions/model_convolution_steps.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_extensions_model_convolution_steps.py: .. _convolution_steps-label: ===================== Component convolution ===================== This example illustrates the convolution steps in the implementation of convolution in model fitting. .. GENERATED FROM PYTHON SOURCE LINES 12-18 .. code-block:: Python import hyperspy.api as hs import numpy as np from hyperspy.misc.axis_tools import calculate_convolution1D_axis .. GENERATED FROM PYTHON SOURCE LINES 19-20 Create a signal containing a Lorentzian peak: .. GENERATED FROM PYTHON SOURCE LINES 20-26 .. code-block:: Python f = hs.model.components1D.Lorentzian(centre=220) f_signal = hs.signals.Signal1D(f.function(np.arange(200, 300))) f_signal.axes_manager.signal_axes.set(offset=200) f_signal.plot() .. image-sg:: /auto_examples/extensions/images/sphx_glr_model_convolution_steps_001.png :alt: Signal :srcset: /auto_examples/extensions/images/sphx_glr_model_convolution_steps_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 27-28 Create a second signal, for example a detector response: .. GENERATED FROM PYTHON SOURCE LINES 28-34 .. code-block:: Python g = hs.model.components1D.Gaussian(sigma=3) g_signal = hs.signals.Signal1D(g.function(np.arange(-20, 20))) g_signal.axes_manager.signal_axes.set(offset=-20) g_signal.plot() .. image-sg:: /auto_examples/extensions/images/sphx_glr_model_convolution_steps_002.png :alt: Signal :srcset: /auto_examples/extensions/images/sphx_glr_model_convolution_steps_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 35-36 Create the "convolution axis" which adds the necessary padding: .. GENERATED FROM PYTHON SOURCE LINES 36-40 .. code-block:: Python convolution_axis = calculate_convolution1D_axis( f_signal.axes_manager.signal_axes[0], g_signal.axes_manager.signal_axes[0] ) .. GENERATED FROM PYTHON SOURCE LINES 41-43 Extend the data over the full range of the "convolution axis" and take the convolution with ``g_signal``: .. GENERATED FROM PYTHON SOURCE LINES 43-49 .. code-block:: Python f_padded_data = f.function(convolution_axis) convolved_data = np.convolve(f_padded_data, g_signal.data, mode="valid") convolved_signal = hs.signals.Signal1D(convolved_data) convolved_signal.axes_manager.signal_axes.set(offset=f_signal.axes_manager[-1].offset) .. GENERATED FROM PYTHON SOURCE LINES 50-51 Display the comparison of both signals: .. GENERATED FROM PYTHON SOURCE LINES 51-52 .. code-block:: Python hs.plot.plot_spectra([f_signal, convolved_signal]) .. image-sg:: /auto_examples/extensions/images/sphx_glr_model_convolution_steps_003.png :alt: model convolution steps :srcset: /auto_examples/extensions/images/sphx_glr_model_convolution_steps_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.969 seconds) .. _sphx_glr_download_auto_examples_extensions_model_convolution_steps.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: model_convolution_steps.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: model_convolution_steps.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: model_convolution_steps.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_