
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/plot_sin.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_auto_examples_plot_sin.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_plot_sin.py:


Approximating a Noisy Signal
----------------------------

In this example, we add noise to a sinusoidal signal and then approximate it using an exponential spline.

.. GENERATED FROM PYTHON SOURCE LINES 7-12

.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np
    import splinebox








.. GENERATED FROM PYTHON SOURCE LINES 13-16

We generate example data with 100 points by evaluating a sine function
at equidistant points over the interval from 0 to 15.
To simulate real-world data, we add Gaussian noise with a standard deviation of 0.3.

.. GENERATED FROM PYTHON SOURCE LINES 16-21

.. code-block:: Python


    N = 100
    x = np.linspace(0, 15, N)
    values = np.sin(x) + np.random.normal(0, 0.3, N)








.. GENERATED FROM PYTHON SOURCE LINES 22-25

To approximate this data, we'll use an exponential spline, which can effectively model sinusoidal functions.
We aim to use the smallest number of knots that still allows the spline to accurately approximate the signal.
Empirical testing indicates that 8 knots provide a good balance between simplicity and accuracy in this case.

.. GENERATED FROM PYTHON SOURCE LINES 25-32

.. code-block:: Python


    M = 8
    basis_function = splinebox.Exponential(M)
    spline = splinebox.Spline(M, basis_function, closed=False)

    spline.fit(values)








.. GENERATED FROM PYTHON SOURCE LINES 33-34

To plot the spline we evaluate it at finely spaced parameter values.

.. GENERATED FROM PYTHON SOURCE LINES 34-44

.. code-block:: Python


    ts = np.linspace(0, M - 1, 1000)
    spline_y = spline.eval(ts)
    spline_x = np.linspace(x.min(), x.max(), len(ts))

    plt.scatter(x, values, label="data")
    plt.plot(spline_x, spline_y, label="spline")
    plt.scatter(np.linspace(x.min(), x.max(), M), spline.knots, label="knots", marker="x", sizes=np.ones(M) * 150)
    plt.legend()
    plt.show()



.. image-sg:: /auto_examples/images/sphx_glr_plot_sin_001.png
   :alt: plot sin
   :srcset: /auto_examples/images/sphx_glr_plot_sin_001.png
   :class: sphx-glr-single-img






.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 9.039 seconds)


.. _sphx_glr_download_auto_examples_plot_sin.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: plot_sin.ipynb <plot_sin.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: plot_sin.py <plot_sin.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: plot_sin.zip <plot_sin.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
