{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "b02af9ba",
            "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": "3be5c67d",
            "cell_type": "markdown",
            "source": "Let's show that a Bartlett window with 75% overlap fulfills the COLA condition:\n",
            "metadata": {}
        },
        {
            "id": "2f7b5757",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import matplotlib.pyplot as plt\nimport numpy as np\nfrom scipy.signal import closest_STFT_dual_window, windows\n\nm = 24\nwin, w_rect = windows.bartlett(m, sym=False), np.ones(m)\nd_win, alpha = closest_STFT_dual_window(win, m//4, w_rect, scaled=True)\nprint(f\"COLA: {np.allclose(d_win, w_rect*alpha)}, {alpha = :g}\")",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "COLA: True, alpha = 0.5"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "234ebb27",
            "cell_type": "markdown",
            "source": "We can also determine for which hop intervals the COLA condition is fulfilled:\n",
            "metadata": {}
        },
        {
            "id": "6a5152a1",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "hops, deviations, alphas = np.arange(1, 16, dtype=int), [], []\nfor h_ in hops:\n    w_cola, alpha = closest_STFT_dual_window(w_rect, h_, win, scaled=True)\n    deviations.append(np.linalg.norm(w_cola - win*alpha))\n    alphas.append(alpha)\n\nfg0, (ax0, ax1) = plt.subplots(2, 1, sharex='all', tight_layout=True)\nax0.set_title(f\"COLA Window closest to a {m}-Sample Bartlett Window\")\nax0.set(ylabel=r\"$||w_\\text{cola}-\\alpha w||$\", xlim=(0, hops[-1]-.5))\nax1.set(xlabel=\"Hop Interval\", ylabel=r\"Scaling factor $\\alpha$\",\n        ylim=(0, 1.25))\nax0.plot(hops, deviations, 'C0.-')\nax1.plot(hops, alphas, 'C1.-')\nfor ax_ in (ax0, ax1):\n    ax_.grid()\nplt.show()",
            "outputs": []
        },
        {
            "id": "8231895b",
            "cell_type": "markdown",
            "source": "The lower plot shows the calculated scaling factor $\\alpha$ for different\n`hops` whereas the upper displays the  $L^2$-norm of the difference\nbetween the scaled Bartlett window and the calculated window. Since for `hops`\n1 to 4 as well as for 6 and 12 the $L^2$-norm of the difference is\npractically zero, the COLA condition is fulfilled for those.\n",
            "metadata": {}
        }
    ]
}