.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "generated/examples/morphological/plot_half_window_effects.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_generated_examples_morphological_plot_half_window_effects.py: `half_window` effects --------------------- This example shows the influence of the `half_window` parameter that is used when fitting any morphological algorithm. For this example, the :meth:`~.Baseline.mor` algorithm will be used, which is a relatively robust baseline algorithm. .. GENERATED FROM PYTHON SOURCE LINES 13-38 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from pybaselines import Baseline from pybaselines.utils import gaussian x = np.linspace(0, 1000, 2000) signal = ( gaussian(x, 9, 100, 12) + gaussian(x, 6, 180, 5) + gaussian(x, 8, 300, 11) + gaussian(x, 15, 400, 12) + gaussian(x, 6, 550, 6) + gaussian(x, 13, 700, 8) + gaussian(x, 9, 800, 9) + gaussian(x, 9, 880, 7) ) baseline = 5 + gaussian(x, 10, 650, 150) noise = np.random.default_rng(0).normal(0, 0.1, len(x)) y = signal + baseline + noise baseline_fitter = Baseline(x_data=x) .. GENERATED FROM PYTHON SOURCE LINES 40-50 For many morphology-based baseline algorithms, the optimal `half_window` value is approximately equal to the full-width-at-half-maximum (FWHM) of the widest peak. We can calculate the FWHM of our synthetic data from the largest :math:`\sigma` value. The FWHM of a Gaussian distribution is related to its :math:`\sigma` value by the relationship: :math:`FWHM = 2 \sigma \sqrt{2 ln{2}} \approx 2.3548 \sigma`. The spacing of the x-values, :math:`dx` also has to be taken into account since the `half_window` value is index-based, while :math:`\sigma` is based on the x-values. Thus, the approximate FWHM is: :math:`FWHM \approx 2.3548 \sigma / dx`. Also note that `half_window` has to be an integer since it is index-based. .. GENERATED FROM PYTHON SOURCE LINES 50-53 .. code-block:: Python dx = np.diff(x).mean() # set dx as the average of all the x-spacings print(int(2.3548 * 12 / dx)) .. rst-class:: sphx-glr-script-out .. code-block:: none 56 .. GENERATED FROM PYTHON SOURCE LINES 54-61 Thus, a good `half_window` value is ~60. To investigate the effect of the `half_window` value, we will use 30, 60, and 120. Note that the actual :math:`\sigma` value for a peak is often unknown, so the FWHM value is usually estimated simply by looking at a plot of the data (using plt.plot(y)). .. GENERATED FROM PYTHON SOURCE LINES 63-64 Using a small `half_window` value makes the baseline cut into the larger peaks. .. GENERATED FROM PYTHON SOURCE LINES 64-71 .. code-block:: Python plt.figure() plt.plot(y, label='data') half_window = 30 plt.plot(baseline_fitter.mor(y, half_window=half_window)[0], label=f'half_window={half_window}') plt.legend() .. image-sg:: /generated/examples/morphological/images/sphx_glr_plot_half_window_effects_001.png :alt: plot half window effects :srcset: /generated/examples/morphological/images/sphx_glr_plot_half_window_effects_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 72-74 Setting the `half_window` value as the approximate FWHM now fits the expected baseline without reducing the peak area. .. GENERATED FROM PYTHON SOURCE LINES 74-81 .. code-block:: Python plt.figure() plt.plot(y, label='data') half_window = 60 plt.plot(baseline_fitter.mor(y, half_window=half_window)[0], label=f'half_window={half_window}') plt.legend() .. image-sg:: /generated/examples/morphological/images/sphx_glr_plot_half_window_effects_002.png :alt: plot half window effects :srcset: /generated/examples/morphological/images/sphx_glr_plot_half_window_effects_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 82-84 Further increasing the `half_window` value then makes the baseline unable to follow the curve of the data. .. GENERATED FROM PYTHON SOURCE LINES 84-90 .. code-block:: Python plt.figure() plt.plot(y, label='data') half_window = 120 plt.plot(baseline_fitter.mor(y, half_window=half_window)[0], label=f'half_window={half_window}') plt.legend() .. image-sg:: /generated/examples/morphological/images/sphx_glr_plot_half_window_effects_003.png :alt: plot half window effects :srcset: /generated/examples/morphological/images/sphx_glr_plot_half_window_effects_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 91-97 Now, put together all the results to show the change in the baseline as `half_window` increases. Note how the `half_window` value can be considered as the approximate "stiffness" of the baseline. For small `half_window` values, the baseline has more flexibility to fit inbetween the peaks, while the largest `half_window` value is too stiff to account for the localized curvature of the baseline. .. GENERATED FROM PYTHON SOURCE LINES 97-103 .. code-block:: Python plt.figure() plt.plot(y, label='data') for half_window in (30, 60, 120): plt.plot(baseline_fitter.mor(y, half_window=half_window)[0], label=f'half_window={half_window}') plt.legend() .. image-sg:: /generated/examples/morphological/images/sphx_glr_plot_half_window_effects_004.png :alt: plot half window effects :srcset: /generated/examples/morphological/images/sphx_glr_plot_half_window_effects_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 104-107 Finally, note that the effect of the larger `half_window` value is decreased if the baseline is relatively flat, so there is less of a penalty for using a larger `half_window` value on flat baselines. .. GENERATED FROM PYTHON SOURCE LINES 107-116 .. code-block:: Python flat_baseline = 5 + gaussian(x, 10, 650, 400) y = y - baseline + flat_baseline # replace the old baseline with the flat baseline plt.figure() plt.plot(y, label='data') for half_window in (30, 60, 120): plt.plot(baseline_fitter.mor(y, half_window=half_window)[0], label=f'half_window={half_window}') plt.legend() plt.show() .. image-sg:: /generated/examples/morphological/images/sphx_glr_plot_half_window_effects_005.png :alt: plot half window effects :srcset: /generated/examples/morphological/images/sphx_glr_plot_half_window_effects_005.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.243 seconds) .. _sphx_glr_download_generated_examples_morphological_plot_half_window_effects.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_half_window_effects.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_half_window_effects.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_half_window_effects.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_