.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "generated/examples/general/plot_padding.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_padding.py: Padding data ------------ Several baseline algorithms, notably :doc:`smoothing <../../../algorithms/smooth>` and :doc:`morphological <../../../algorithms/morphological>` algorithms, pad the input data in order to reduce edge effects from calculations. Padding is performed by :func:`~.pad_edges`, which is a thin wrapper around :func:`numpy.pad` that also allows linear extrapolation. .. GENERATED FROM PYTHON SOURCE LINES 12-38 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from scipy.ndimage import uniform_filter1d from pybaselines.utils import pad_edges half_window = 80 # for 161 point moving average window num_points = 1000 x = np.linspace(0, 1000, num_points) # use an exponentially decaying line to get a large edge effect one # one side and a small edge effect on the other line = 10 * np.exp(-x / 150) noise = np.random.default_rng(0).normal(0, 0.5, num_points) y = line + noise pad_len = 2 * half_window + 1 plt.plot(pad_edges(y, pad_len, mode='constant', constant_values=np.nan), label='original') for i, pad_mode in enumerate(('reflect', 'edge', 'extrapolate'), 1): padded_y = pad_edges(y, pad_len, pad_mode, extrapolate_window=int(0.1 * num_points)) # offset each plot to show differences plt.plot(padded_y + 5 * i, label=pad_mode) plt.legend() .. image-sg:: /generated/examples/general/images/sphx_glr_plot_padding_001.png :alt: plot padding :srcset: /generated/examples/general/images/sphx_glr_plot_padding_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 39-41 To show the effects that padding can have, the data is smoothed with a moving average. Poor padding produces more severe edge effects from the smoothing. .. GENERATED FROM PYTHON SOURCE LINES 41-52 .. code-block:: Python plt.figure() plt.plot(y, label='original') for i, pad_mode in enumerate(('reflect', 'edge', 'extrapolate'), 1): padded_y = pad_edges(y, pad_len, pad_mode, extrapolate_window=int(0.1 * num_points)) plt.plot( uniform_filter1d(padded_y, 2 * half_window + 1)[pad_len:num_points + pad_len], label=pad_mode ) plt.legend() plt.show() .. image-sg:: /generated/examples/general/images/sphx_glr_plot_padding_002.png :alt: plot padding :srcset: /generated/examples/general/images/sphx_glr_plot_padding_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.135 seconds) .. _sphx_glr_download_generated_examples_general_plot_padding.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_padding.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_padding.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_padding.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_