{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "7681294e",
            "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": "ffe9b976",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy.datasets import electrocardiogram\necg = electrocardiogram()\necg",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([-0.245, -0.215, -0.185, ..., -0.405, -0.395, -0.385], shape=(108000,))"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "969609ce",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "ecg.shape, ecg.mean(), ecg.std()",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "((108000,), -0.16510875, 0.5992473991177294)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "25d14bd2",
            "cell_type": "markdown",
            "source": "As stated the signal features several areas with a different morphology.\nE.g., the first few seconds show the electrical activity of a heart in\nnormal sinus rhythm as seen below.\n",
            "metadata": {}
        },
        {
            "id": "faf02636",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nimport matplotlib.pyplot as plt\nfs = 360\ntime = np.arange(ecg.size) / fs\nplt.plot(time, ecg)\nplt.xlabel(\"time in s\")\nplt.ylabel(\"ECG in mV\")\nplt.xlim(9, 10.2)\nplt.ylim(-1, 1.5)\nplt.show()",
            "outputs": []
        },
        {
            "id": "8a73bf22",
            "cell_type": "markdown",
            "source": "After second 16, however, the first premature ventricular contractions,\nalso called extrasystoles, appear. These have a different morphology\ncompared to typical heartbeats. The difference can easily be observed\nin the following plot.\n",
            "metadata": {}
        },
        {
            "id": "534f112c",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "plt.plot(time, ecg)\nplt.xlabel(\"time in s\")\nplt.ylabel(\"ECG in mV\")\nplt.xlim(46.5, 50)\nplt.ylim(-2, 1.5)\nplt.show()",
            "outputs": []
        },
        {
            "id": "a8aa1ef3",
            "cell_type": "markdown",
            "source": "At several points large artifacts disturb the recording, e.g.:\n",
            "metadata": {}
        },
        {
            "id": "84571390",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "plt.plot(time, ecg)\nplt.xlabel(\"time in s\")\nplt.ylabel(\"ECG in mV\")\nplt.xlim(207, 215)\nplt.ylim(-2, 3.5)\nplt.show()",
            "outputs": []
        },
        {
            "id": "f566532e",
            "cell_type": "markdown",
            "source": "Finally, examining the power spectrum reveals that most of the biosignal is\nmade up of lower frequencies. At 60 Hz the noise induced by the mains\nelectricity can be clearly observed.\n",
            "metadata": {}
        },
        {
            "id": "0aa6cb7f",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy.signal import welch\nf, Pxx = welch(ecg, fs=fs, nperseg=2048, scaling=\"spectrum\")\nplt.semilogy(f, Pxx)\nplt.xlabel(\"Frequency in Hz\")\nplt.ylabel(\"Power spectrum of the ECG in mV**2\")\nplt.xlim(f[[0, -1]])\nplt.show()",
            "outputs": []
        }
    ]
}