.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_10_two_peaks_background.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_auto_examples_plot_10_two_peaks_background.py: Two peaks on a sloped background ================================ A spectrum with two overlapping emission lines on a linear background. Models are combined with ``+``. Parameter names that occur twice get automatic prefixes; to choose your own names, use :meth:`~fitwise.Model.with_prefix`. The wavelength is measured relative to the center of the spectrum (655 nm). The intercept of the background is then the background *in* the spectrum. With absolute wavelengths it would be the background at 0 nm, an extrapolation over 650 nm, and slope and intercept would be almost fully correlated; fitwise would warn about that. .. GENERATED FROM PYTHON SOURCE LINES 16-35 .. code-block:: Python import numpy as np import fitwise as fw from fitwise.models import gaussian, linear rng = np.random.default_rng(10) wavelength = np.linspace(640, 670, 300) # nm intensity = ( gaussian(wavelength, 820, 651.2, 1.1) + gaussian(wavelength, 310, 655.0, 1.4) + 2.5 * (wavelength - 640) + 40 ) intensity = rng.poisson(intensity).astype(float) shift = wavelength - 655.0 # nm relative to the center model = gaussian.with_prefix("a_") + gaussian.with_prefix("b_") + linear print(model.param_names) .. rst-class:: sphx-glr-script-out .. code-block:: none ('a_amp', 'a_cen', 'a_sigma', 'b_amp', 'b_cen', 'b_sigma', 'slope', 'intercept') .. GENERATED FROM PYTHON SOURCE LINES 36-38 The starting values come from a sequential estimate: background from the edges, then the strongest peak, then the second peak on what remains. .. GENERATED FROM PYTHON SOURCE LINES 38-41 .. code-block:: Python result = fw.fit(model, shift, intensity, yerr=np.sqrt(intensity)) print(result) .. rst-class:: sphx-glr-script-out .. code-block:: none Model: gaussian(x; a_amp, a_cen, a_sigma) + gaussian(x; b_amp, b_cen, b_sigma) + linear(x; slope, intercept) Method: Trust Region Reflective (scipy least_squares) (auto: nonlinear) Converged: yes (31 evals) Data: 300 points, yerr given (absolute) Start: a_amp, a_cen, a_sigma, b_amp, b_cen, b_sigma: peak heuristic · slope, intercept: edge baseline Parameter Value ± Std.Err Rel. Bounds a_amp 821.0 7.8 0.9 % – a_cen -3.805 0.015 0.4 % – a_sigma 1.081 0.011 1.0 % – b_amp 310.4 4.6 1.5 % – b_cen -0.057 0.039 67.4 % – b_sigma 1.441 0.033 2.3 % – slope 2.513 0.058 2.3 % – intercept 76.50 0.62 0.8 % – χ²/ndf = 0.916 (292 dof, p = 0.85) R² = 0.9907 AIC = -18.4 Correlations > 0.5: a_cen–b_cen (+0.71), b_cen–b_sigma (-0.70), a_cen–b_sigma (-0.66), a_sigma–b_cen (+0.64), a_cen–a_sigma (+0.60), a_sigma–b_sigma (-0.53) Warnings: none .. GENERATED FROM PYTHON SOURCE LINES 42-52 .. code-block:: Python fig, axes = result.plot() x = np.linspace(-15, 15, 600) p = result.params.values_dict() background = linear.eval(x, p) for prefix, color in [("a_", "C2"), ("b_", "C3")]: peak = gaussian.with_prefix(prefix).eval(x, p) axes[0].fill_between(x, background, background + peak, color=color, alpha=0.3, lw=0) axes[0].set_ylabel("counts") axes[1].set_xlabel("wavelength − 655 nm") .. image-sg:: /auto_examples/images/sphx_glr_plot_10_two_peaks_background_001.png :alt: plot 10 two peaks background :srcset: /auto_examples/images/sphx_glr_plot_10_two_peaks_background_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 18.503588541666666, 'wavelength − 655 nm') .. GENERATED FROM PYTHON SOURCE LINES 53-54 Line positions in absolute wavelength: .. GENERATED FROM PYTHON SOURCE LINES 54-58 .. code-block:: Python for prefix in ("a_", "b_"): cen = result.params[prefix + "cen"] print(f"line {prefix[0]}: {655 + cen.value:.3f} ± {cen.stderr:.3f} nm") .. rst-class:: sphx-glr-script-out .. code-block:: none line a: 651.195 ± 0.015 nm line b: 654.943 ± 0.039 nm .. GENERATED FROM PYTHON SOURCE LINES 59-61 The line ratio with its uncertainty. ``as_ufloat()`` takes the correlation of the two amplitudes into account automatically: .. GENERATED FROM PYTHON SOURCE LINES 61-64 .. code-block:: Python u = result.params.as_ufloat() ratio = u["b_amp"] / u["a_amp"] print(f"I_b / I_a = {ratio:.2uS}") .. rst-class:: sphx-glr-script-out .. code-block:: none I_b / I_a = 0.3781(62) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.141 seconds) .. _sphx_glr_download_auto_examples_plot_10_two_peaks_background.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_10_two_peaks_background.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_10_two_peaks_background.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_10_two_peaks_background.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_