{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "c78c7674",
            "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": "9622cdc0",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nrng = np.random.default_rng()",
            "outputs": []
        },
        {
            "id": "ad89e759",
            "cell_type": "markdown",
            "source": "First define some input parameters for the signal:\n",
            "metadata": {}
        },
        {
            "id": "4f789704",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "A = 2.  # amplitude\nc = 2.  # offset\nw0 = 1.  # rad/sec\nnin = 150\nnout = 1002",
            "outputs": []
        },
        {
            "id": "69d1b8a5",
            "cell_type": "markdown",
            "source": "Randomly generate sample times:\n",
            "metadata": {}
        },
        {
            "id": "a4fd281a",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "x = rng.uniform(0, 10*np.pi, nin)",
            "outputs": []
        },
        {
            "id": "cc3c94d1",
            "cell_type": "markdown",
            "source": "Plot a sine wave for the selected times:\n",
            "metadata": {}
        },
        {
            "id": "ac9c2056",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "y = A * np.cos(w0*x) + c",
            "outputs": []
        },
        {
            "id": "f47c231f",
            "cell_type": "markdown",
            "source": "Define the array of frequencies for which to compute the periodogram:\n",
            "metadata": {}
        },
        {
            "id": "a5e8c42b",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "w = np.linspace(0.25, 10, nout)",
            "outputs": []
        },
        {
            "id": "5b24b9d9",
            "cell_type": "markdown",
            "source": "Calculate Lomb-Scargle periodogram for each of the normalize options:\n",
            "metadata": {}
        },
        {
            "id": "9629213c",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy.signal import lombscargle\npgram_power = lombscargle(x, y, w, normalize=False)\npgram_norm = lombscargle(x, y, w, normalize=True)\npgram_amp = lombscargle(x, y, w, normalize='amplitude')\n\npgram_power_f = lombscargle(x, y, w, normalize=False, floating_mean=True)\npgram_norm_f = lombscargle(x, y, w, normalize=True, floating_mean=True)\npgram_amp_f = lombscargle(x, y, w, normalize='amplitude', floating_mean=True)",
            "outputs": []
        },
        {
            "id": "e1b0b40f",
            "cell_type": "markdown",
            "source": "Now make a plot of the input data:\n",
            "metadata": {}
        },
        {
            "id": "65cd0ea2",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import matplotlib.pyplot as plt\nfig, (ax_t, ax_p, ax_n, ax_a) = plt.subplots(4, 1, figsize=(5, 6))\nax_t.plot(x, y, 'b+')\nax_t.set_xlabel('Time [s]')\nax_t.set_ylabel('Amplitude')",
            "outputs": []
        },
        {
            "id": "8d33af37",
            "cell_type": "markdown",
            "source": "Then plot the periodogram for each of the normalize options, as well as with and\nwithout floating_mean=True:\n",
            "metadata": {}
        },
        {
            "id": "45fc4b87",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "ax_p.plot(w, pgram_power, label='default')\nax_p.plot(w, pgram_power_f, label='floating_mean=True')\nax_p.set_xlabel('Angular frequency [rad/s]')\nax_p.set_ylabel('Power')\nax_p.legend(prop={'size': 7})\n\nax_n.plot(w, pgram_norm, label='default')\nax_n.plot(w, pgram_norm_f, label='floating_mean=True')\nax_n.set_xlabel('Angular frequency [rad/s]')\nax_n.set_ylabel('Normalized')\nax_n.legend(prop={'size': 7})\n\nax_a.plot(w, np.abs(pgram_amp), label='default')\nax_a.plot(w, np.abs(pgram_amp_f), label='floating_mean=True')\nax_a.set_xlabel('Angular frequency [rad/s]')\nax_a.set_ylabel('Amplitude')\nax_a.legend(prop={'size': 7})\n\nplt.tight_layout()\nplt.show()",
            "outputs": []
        }
    ]
}