.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "generated/examples/general/plot_noisy_data.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_general_plot_noisy_data.py: Fitting noisy data ------------------ Many baseline correction algorithms were created without considering noise in the experimental data, which can lead to an underestimation of the baseline. This example will show how to reduce this issue by simply smoothing the data before performing baseline correction. Two algorithms will be compared: :meth:`~.Baseline.modpoly`, which is not suited for noisy data, and :meth:`~.Baseline.imodpoly`, which is a modification of the modpoly algorithm created specifically to address noise. .. GENERATED FROM PYTHON SOURCE LINES 16-41 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from scipy.ndimage import uniform_filter1d 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 / 600) noise = np.random.default_rng(0).normal(0, 0.6, len(x)) y = signal + baseline + noise baseline_fitter = Baseline(x_data=x) .. GENERATED FROM PYTHON SOURCE LINES 43-46 Smoothing will be performed using a simple 11-point moving average. Other types of smoothing include Savitzky-Golay smoothing, Gaussian smoothing, Whittaker smoothing, or wavelet-based smoothing. .. GENERATED FROM PYTHON SOURCE LINES 46-53 .. code-block:: Python smooth_y = uniform_filter1d(y, 11) plt.figure() plt.plot(y, label='original data') plt.plot(smooth_y, label='smoothed data') plt.legend() .. image-sg:: /generated/examples/general/images/sphx_glr_plot_noisy_data_001.png :alt: plot noisy data :srcset: /generated/examples/general/images/sphx_glr_plot_noisy_data_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 54-58 The modpoly fits of both the original data and the smoothed data are shown below. It is clear that the fit for the original data underestimates the baseline due to noise, while the modpoly fit of the smoothed data is much closer to the true baseline. .. GENERATED FROM PYTHON SOURCE LINES 58-67 .. code-block:: Python regular_modpoly = baseline_fitter.modpoly(y, poly_order=3)[0] smoothed_modpoly = baseline_fitter.modpoly(smooth_y, poly_order=3)[0] plt.figure() plt.plot(y) plt.plot(regular_modpoly, label='modpoly') plt.plot(smoothed_modpoly, '--', label='smoothed modpoly') plt.plot(baseline, ':', label='true baseline') plt.legend() .. image-sg:: /generated/examples/general/images/sphx_glr_plot_noisy_data_002.png :alt: plot noisy data :srcset: /generated/examples/general/images/sphx_glr_plot_noisy_data_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 68-71 Unlike the modpoly, the imodpoly function fits the noisy data quite well. Smoothing the input has little effect, with both the original and smoothed data producing similar results. .. GENERATED FROM PYTHON SOURCE LINES 71-82 .. code-block:: Python regular_imodpoly = baseline_fitter.imodpoly(y, poly_order=3, num_std=0.7)[0] smoothed_imodpoly = baseline_fitter.imodpoly(smooth_y, poly_order=3, num_std=0.7)[0] plt.figure() plt.plot(y) plt.plot(regular_imodpoly, label='imodpoly') plt.plot(smoothed_imodpoly, '--', label='smoothed imodpoly') plt.plot(baseline, ':', label='true baseline') plt.legend() plt.show() .. image-sg:: /generated/examples/general/images/sphx_glr_plot_noisy_data_003.png :alt: plot noisy data :srcset: /generated/examples/general/images/sphx_glr_plot_noisy_data_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.178 seconds) .. _sphx_glr_download_generated_examples_general_plot_noisy_data.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_noisy_data.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_noisy_data.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_noisy_data.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_