{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "66d4bdd5",
            "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": "fab67700",
            "cell_type": "markdown",
            "source": "In this example we use the Hilbert transform to determine the amplitude\nenvelope and instantaneous frequency of an amplitude-modulated signal.\n\nLet's create a chirp of which the frequency increases from 20 Hz to 100 Hz and\napply an amplitude modulation:\n",
            "metadata": {}
        },
        {
            "id": "1cea4054",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nimport matplotlib.pyplot as plt\nfrom scipy.signal import hilbert, chirp\n\nduration, fs = 1, 400  # 1 s signal with sampling frequency of 400 Hz\nt = np.arange(int(fs*duration)) / fs  # timestamps of samples\nsignal = chirp(t, 20.0, t[-1], 100.0)\nsignal *= (1.0 + 0.5 * np.sin(2.0*np.pi*3.0*t) )",
            "outputs": []
        },
        {
            "id": "f42aa493",
            "cell_type": "markdown",
            "source": "The amplitude envelope is given by the magnitude of the analytic signal. The\ninstantaneous frequency can be obtained by differentiating the\ninstantaneous phase in respect to time. The instantaneous phase corresponds\nto the phase angle of the analytic signal.\n",
            "metadata": {}
        },
        {
            "id": "481a1b4c",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "analytic_signal = hilbert(signal)\namplitude_envelope = np.abs(analytic_signal)\ninstantaneous_phase = np.unwrap(np.angle(analytic_signal))\ninstantaneous_frequency = np.diff(instantaneous_phase) / (2.0*np.pi) * fs\n\nfig, (ax0, ax1) = plt.subplots(nrows=2, sharex='all', tight_layout=True)\nax0.set_title(\"Amplitude-modulated Chirp Signal\")\nax0.set_ylabel(\"Amplitude\")\nax0.plot(t, signal, label='Signal')\nax0.plot(t, amplitude_envelope, label='Envelope')\nax0.legend()\nax1.set(xlabel=\"Time in seconds\", ylabel=\"Phase in rad\", ylim=(0, 120))\nax1.plot(t[1:], instantaneous_frequency, 'C2-', label='Instantaneous Phase')\nax1.legend()\nplt.show()",
            "outputs": []
        }
    ]
}