{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "a89d8010",
            "cell_type": "markdown",
            "source": "<div class='alert alert-warning'>\n\nSciPy's interactive examples with Jupyterlite are experimental and may not always work as expected. Execution of cells containing imports may result in large downloads (up to 60MB of content for the first import from SciPy). Load times when importing from SciPy may take roughly 10-20 seconds. If you notice any problems, feel free to open an [issue](https://github.com/scipy/scipy/issues/new/choose).\n\n</div>",
            "metadata": {}
        },
        {
            "id": "49545d9d",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy.signal import chirp, find_peaks, peak_widths\nimport matplotlib.pyplot as plt",
            "outputs": []
        },
        {
            "id": "619c8edb",
            "cell_type": "markdown",
            "source": "Create a test signal with two overlaid harmonics\n",
            "metadata": {}
        },
        {
            "id": "3911b883",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "x = np.linspace(0, 6 * np.pi, 1000)\nx = np.sin(x) + 0.6 * np.sin(2.6 * x)",
            "outputs": []
        },
        {
            "id": "4966a1e5",
            "cell_type": "markdown",
            "source": "Find all peaks and calculate their widths at the relative height of 0.5\n(contour line at half the prominence height) and 1 (at the lowest contour\nline at full prominence height).\n",
            "metadata": {}
        },
        {
            "id": "5b1c145e",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "peaks, _ = find_peaks(x)\nresults_half = peak_widths(x, peaks, rel_height=0.5)\nresults_half[0]  # widths",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([ 64.25172825,  41.29465463,  35.46943289, 104.71586081,\n        35.46729324,  41.30429622, 181.93835853,  45.37078546])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "2377f58f",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "results_full = peak_widths(x, peaks, rel_height=1)\nresults_full[0]  # widths",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([181.9396084 ,  72.99284945,  61.28657872, 373.84622694,\n    61.78404617,  72.48822812, 253.09161876,  79.36860878])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "6b490502",
            "cell_type": "markdown",
            "source": "Plot signal, peaks and contour lines at which the widths where calculated\n",
            "metadata": {}
        },
        {
            "id": "bf1b879b",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "plt.plot(x)\nplt.plot(peaks, x[peaks], \"x\")\nplt.hlines(*results_half[1:], color=\"C2\")\nplt.hlines(*results_full[1:], color=\"C3\")\nplt.show()",
            "outputs": []
        }
    ]
}