.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/06_denoise/06_04_recursive_smoothing_spline.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. or to run this example in your browser via JupyterLite or Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_06_denoise_06_04_recursive_smoothing_spline.py: Recursive Smoothing Spline ========================== We plot a recursive smoothing spline with different parameters. .. GENERATED FROM PYTHON SOURCE LINES 13-15 Imports ------- .. GENERATED FROM PYTHON SOURCE LINES 15-22 .. code-block:: Python import math import numpy as np import matplotlib.pyplot as plt from splineops.denoise.smoothing_spline import smoothing_spline from splineops.denoise.smoothing_spline import recursive_smoothing_spline .. GENERATED FROM PYTHON SOURCE LINES 23-25 Recursive Smoothing Spline -------------------------- .. GENERATED FROM PYTHON SOURCE LINES 25-67 .. code-block:: Python # Example signal: A noisy sine wave x = np.linspace(0, np.pi, 100) signal = np.sin(x) + 0.1 * np.random.normal(size=x.shape) # Different values for the smoothing parameter in recursive smoothing spline lam_values = [0.005, 0.05, 0.1] # You can try smaller or larger values # Apply fractional smoothing spline as a baseline for comparison lambda_ = 0.1 # Regularization parameter for fractional method m = 1 # No upsampling gamma = 0.6 # Spline order parameter _, smoothed_fractional = smoothing_spline(signal, lambda_, m, gamma) # Compute MSE values for different recursive smoothing spline parameters mse_values = [] # Plot results plt.figure(figsize=(12, 8)) plt.plot(x, signal, label="Noisy Signal", linestyle="--", color="gray") plt.plot(x, smoothed_fractional, label="Fractional Smoothing Spline", color="red") # Apply and plot recursive smoothing spline for each lambda value for lam_recursive in lam_values: smoothed_recursive = recursive_smoothing_spline(signal, lamb=lam_recursive) # Compute MSE mse = np.mean((smoothed_recursive - smoothed_fractional) ** 2) mse_values.append(mse) plt.plot(x, smoothed_recursive, label=f"Recursive Smoothing (λ={lam_recursive})") # Print MSE values print("\nMean Squared Error (MSE) between Recursive and Fractional Smoothing Spline:") for lam, mse in zip(lam_values, mse_values): print(f"λ={lam:.3f}: MSE = {mse:.6f}") plt.legend() plt.xlabel("x") plt.ylabel("Signal Value") plt.title("Comparison of Recursive Smoothing with Different λ Values") plt.show() .. image-sg:: /auto_examples/06_denoise/images/sphx_glr_06_04_recursive_smoothing_spline_001.png :alt: Comparison of Recursive Smoothing with Different λ Values :srcset: /auto_examples/06_denoise/images/sphx_glr_06_04_recursive_smoothing_spline_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Mean Squared Error (MSE) between Recursive and Fractional Smoothing Spline: λ=0.005: MSE = 0.000035 λ=0.050: MSE = 0.001070 λ=0.100: MSE = 0.003683 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.114 seconds) .. _sphx_glr_download_auto_examples_06_denoise_06_04_recursive_smoothing_spline.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/splineops/splineops.github.io/main?urlpath=lab/tree/notebooks_binder/auto_examples/06_denoise/06_04_recursive_smoothing_spline.ipynb :alt: Launch binder :width: 150 px .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../lite/lab/index.html?path=auto_examples/06_denoise/06_04_recursive_smoothing_spline.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 06_04_recursive_smoothing_spline.ipynb <06_04_recursive_smoothing_spline.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 06_04_recursive_smoothing_spline.py <06_04_recursive_smoothing_spline.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 06_04_recursive_smoothing_spline.zip <06_04_recursive_smoothing_spline.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_