.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "generated/examples/whittaker/plot_lam_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_whittaker_plot_lam_effects.py: `lam` effects ------------- This example shows the influence of the `lam` parameter that is used when fitting any algorithm that is based on Whittaker-smoothing. Note that the exact `lam` values used in this example are unimportant, just the changes in their scale. For this example, the :meth:`~.Baseline.arpls` algorithm will be used, which performs well in the presence of noise. .. GENERATED FROM PYTHON SOURCE LINES 15-39 .. 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, 1000) signal = ( gaussian(x, 9, 100, 12) + gaussian(x, 6, 180, 5) + gaussian(x, 8, 350, 11) + gaussian(x, 15, 400, 18) + gaussian(x, 6, 550, 6) + gaussian(x, 13, 700, 8) + gaussian(x, 9, 800, 9) + gaussian(x, 9, 880, 7) ) baseline = 5 + 10 * np.exp(-x / 800) noise = np.random.default_rng(0).normal(0, 0.2, len(x)) y = signal + baseline + noise baseline_fitter = Baseline(x_data=x) .. GENERATED FROM PYTHON SOURCE LINES 41-44 For extremely small `lam` values, the algorithm's output is more comparable with smoothing than baseline correction (which would be the desired result if using pure Whittaker smoothing). .. GENERATED FROM PYTHON SOURCE LINES 44-49 .. code-block:: Python plt.plot(y, label='data') lam = 1 plt.plot(baseline_fitter.arpls(y, lam=lam)[0], label=f'lam=10$^{np.log10(lam):.0f}$') plt.legend() .. image-sg:: /generated/examples/whittaker/images/sphx_glr_plot_lam_effects_001.png :alt: plot lam effects :srcset: /generated/examples/whittaker/images/sphx_glr_plot_lam_effects_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 50-52 Increasing the `lam` value produces results that more resemble the baseline. However, the baseline is still too flexible and cuts into the larger peaks. .. GENERATED FROM PYTHON SOURCE LINES 52-59 .. code-block:: Python plt.figure() plt.plot(y, label='data') lam = 1e3 plt.plot(baseline_fitter.arpls(y, lam=lam)[0], label=f'lam=10$^{np.log10(lam):.0f}$') plt.legend() .. image-sg:: /generated/examples/whittaker/images/sphx_glr_plot_lam_effects_002.png :alt: plot lam effects :srcset: /generated/examples/whittaker/images/sphx_glr_plot_lam_effects_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 60-62 Increasing the `lam` value further produces the desired result and follows the expected baseline without reducing the peak area. .. GENERATED FROM PYTHON SOURCE LINES 62-69 .. code-block:: Python plt.figure() plt.plot(y, label='data') lam = 1e6 plt.plot(baseline_fitter.arpls(y, lam=lam)[0], label=f'lam=10$^{np.log10(lam):.0f}$') plt.legend() .. image-sg:: /generated/examples/whittaker/images/sphx_glr_plot_lam_effects_003.png :alt: plot lam effects :srcset: /generated/examples/whittaker/images/sphx_glr_plot_lam_effects_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 70-72 Further increasing the `lam` value then makes the baseline too stiff and unable to follow the curve of the data. .. GENERATED FROM PYTHON SOURCE LINES 72-78 .. code-block:: Python plt.figure() plt.plot(y, label='data') lam = 1e10 plt.plot(baseline_fitter.arpls(y, lam=lam)[0], label=f'lam=10$^{{{np.log10(lam):.0f}}}$') plt.legend() .. image-sg:: /generated/examples/whittaker/images/sphx_glr_plot_lam_effects_004.png :alt: plot lam effects :srcset: /generated/examples/whittaker/images/sphx_glr_plot_lam_effects_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 79-81 Finally, put together all the results to show the change in the baseline as `lam` increases. .. GENERATED FROM PYTHON SOURCE LINES 81-88 .. code-block:: Python plt.figure() plt.plot(y, label='data') for lam in (1, 1e3, 1e6, 1e10): plt.plot(baseline_fitter.arpls(y, lam=lam)[0], label=f'lam=10$^{{{np.log10(lam):.0f}}}$') plt.legend() plt.show() .. image-sg:: /generated/examples/whittaker/images/sphx_glr_plot_lam_effects_005.png :alt: plot lam effects :srcset: /generated/examples/whittaker/images/sphx_glr_plot_lam_effects_005.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.352 seconds) .. _sphx_glr_download_generated_examples_whittaker_plot_lam_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_lam_effects.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_lam_effects.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_lam_effects.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_