{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Restriction and Interpolation\n\nThis example shows how to use the :py:class:`pylops.Restriction` operator\nto sample a certain input vector at desired locations ``iava``. Moreover,\nwe go one step further and use the :py:class:`pylops.signalprocessing.Interp`\noperator to show how we can also sample values at locations that are not\nexactly on the grid of the input vector.\n\nAs explained in the `sphx_glr_tutorials_solvers.py` tutorial, such\noperators can be used as forward model in an inverse problem aimed at\ninterpolate irregularly sampled 1d or 2d signals onto a regular grid.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport numpy as np\n\nimport pylops\n\nplt.close(\"all\")\nnp.random.seed(10)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Let's create a signal of size ``nt`` and sampling ``dt`` that is composed\nof three sinusoids at frequencies ``freqs``.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "nt = 200\ndt = 0.004\n\nfreqs = [5.0, 3.0, 8.0]\n\nt = np.arange(nt) * dt\nx = np.zeros(nt)\n\nfor freq in freqs:\n    x = x + np.sin(2 * np.pi * freq * t)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "First of all, we subsample the signal at random locations and we retain 40%\nof the initial samples.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "perc_subsampling = 0.4\nntsub = int(np.round(nt * perc_subsampling))\n\nisample = np.arange(nt)\niava = np.sort(np.random.permutation(np.arange(nt))[:ntsub])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We then create the restriction and interpolation operators and display\nthe original signal as well as the subsampled signal.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "Rop = pylops.Restriction(nt, iava, dtype=\"float64\")\nNNop, iavann = pylops.signalprocessing.Interp(\n    nt, iava + 0.4, kind=\"nearest\", dtype=\"float64\"\n)\nLIop, iavali = pylops.signalprocessing.Interp(\n    nt, iava + 0.4, kind=\"linear\", dtype=\"float64\"\n)\nSIop, iavasi = pylops.signalprocessing.Interp(\n    nt, iava + 0.4, kind=\"sinc\", dtype=\"float64\"\n)\ny = Rop * x\nynn = NNop * x\nyli = LIop * x\nysi = SIop * x\nymask = Rop.mask(x)\n\n# Visualize data\nfig = plt.figure(figsize=(15, 5))\nplt.plot(isample, x, \".-k\", lw=3, ms=10, label=\"all samples\")\nplt.plot(isample, ymask, \".g\", ms=35, label=\"available samples\")\nplt.plot(iavann, ynn, \".r\", ms=25, label=\"NN interp samples\")\nplt.plot(iavali, yli, \".m\", ms=20, label=\"Linear interp samples\")\nplt.plot(iavasi, ysi, \".y\", ms=15, label=\"Sinc interp samples\")\nplt.legend(loc=\"right\")\nplt.title(\"Data restriction\")\n\nsubax = fig.add_axes([0.2, 0.2, 0.15, 0.6])\nsubax.plot(isample, x, \".-k\", lw=3, ms=10)\nsubax.plot(isample, ymask, \".g\", ms=35)\nsubax.plot(iavann, ynn, \".r\", ms=25)\nsubax.plot(iavali, yli, \".m\", ms=20)\nsubax.plot(iavasi, ysi, \".y\", ms=15)\nsubax.set_xlim([120, 127])\nsubax.set_ylim([-0.5, 0.5])\nplt.tight_layout()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Finally we show how the :py:class:`pylops.Restriction` is not limited to\none dimensional signals but can be applied to sample locations of a specific\naxis of a multi-dimensional array.\nsubsampling locations\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "nx, nt = 100, 50\n\nx = np.random.normal(0, 1, (nx, nt))\n\nperc_subsampling = 0.4\nnxsub = int(np.round(nx * perc_subsampling))\niava = np.sort(np.random.permutation(np.arange(nx))[:nxsub])\n\nRop = pylops.Restriction((nx, nt), iava, axis=0, dtype=\"float64\")\ny = Rop * x\nymask = Rop.mask(x)\n\nfig, axs = plt.subplots(1, 3, figsize=(10, 5), sharey=True)\naxs[0].imshow(x.T, cmap=\"gray\")\naxs[0].set_title(\"Model\")\naxs[0].axis(\"tight\")\naxs[1].imshow(y.T, cmap=\"gray\")\naxs[1].set_title(\"Data\")\naxs[1].axis(\"tight\")\naxs[2].imshow(ymask.T, cmap=\"gray\")\naxs[2].set_title(\"Masked model\")\naxs[2].axis(\"tight\")\nplt.tight_layout()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.9.15"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}