{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "fd8984aa",
            "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": "00576ed1",
            "cell_type": "markdown",
            "source": "MLS uses binary convention:\n",
            "metadata": {}
        },
        {
            "id": "efda1f2d",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy.signal import max_len_seq\nmax_len_seq(4)[0]",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0], dtype=int8)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "5a5ebc48",
            "cell_type": "markdown",
            "source": "MLS has a white spectrum (except for DC):\n",
            "metadata": {}
        },
        {
            "id": "f869e7a8",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nimport matplotlib.pyplot as plt\nfrom numpy.fft import fft, ifft, fftshift, fftfreq\nseq = max_len_seq(6)[0]*2-1  # +1 and -1\nspec = fft(seq)\nN = len(seq)\nplt.plot(fftshift(fftfreq(N)), fftshift(np.abs(spec)), '.-')\nplt.margins(0.1, 0.1)\nplt.grid(True)\nplt.show()",
            "outputs": []
        },
        {
            "id": "f77ed30b",
            "cell_type": "markdown",
            "source": "Circular autocorrelation of MLS is an impulse:\n",
            "metadata": {}
        },
        {
            "id": "ca125911",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "acorrcirc = ifft(spec * np.conj(spec)).real\nplt.figure()\nplt.plot(np.arange(-N/2+1, N/2+1), fftshift(acorrcirc), '.-')\nplt.margins(0.1, 0.1)\nplt.grid(True)\nplt.show()",
            "outputs": []
        },
        {
            "id": "13f3f416",
            "cell_type": "markdown",
            "source": "Linear autocorrelation of MLS is approximately an impulse:\n",
            "metadata": {}
        },
        {
            "id": "fb81e144",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "acorr = np.correlate(seq, seq, 'full')\nplt.figure()\nplt.plot(np.arange(-N+1, N), acorr, '.-')\nplt.margins(0.1, 0.1)\nplt.grid(True)\nplt.show()",
            "outputs": []
        }
    ]
}