{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Bilinear Interpolation\nThis example shows how to use the :py:class:`pylops.signalprocessing.Bilinar`\noperator to perform bilinear interpolation to a 2-dimensional input vector.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport numpy as np\nfrom scipy import misc\n\nimport pylops\n\nplt.close(\"all\")\nnp.random.seed(0)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "First of all, we create a 2-dimensional input vector containing an image\nfrom the ``scipy.misc`` family.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "x = misc.face()[::5, ::5, 0]\nnz, nx = x.shape"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We can now define a set of available samples in the\nfirst and second direction of the array and apply bilinear interpolation.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "nsamples = 2000\niava = np.vstack(\n    (np.random.uniform(0, nz - 1, nsamples), np.random.uniform(0, nx - 1, nsamples))\n)\n\nBop = pylops.signalprocessing.Bilinear(iava, (nz, nx))\ny = Bop * x"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "At this point we try to reconstruct the input signal imposing a smooth\nsolution by means of a regularization term that minimizes the Laplacian of\nthe solution.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "D2op = pylops.Laplacian((nz, nx), weights=(1, 1), dtype=\"float64\")\n\nxadj = Bop.H * y\nxinv = pylops.optimization.leastsquares.normal_equations_inversion(\n    Bop, y, [D2op], epsRs=[np.sqrt(0.1)], **dict(maxiter=100)\n)[0]\nxadj = xadj.reshape(nz, nx)\nxinv = xinv.reshape(nz, nx)\n\nfig, axs = plt.subplots(1, 3, figsize=(10, 4))\nfig.suptitle(\"Bilinear interpolation\", fontsize=14, fontweight=\"bold\", y=0.95)\naxs[0].imshow(x, cmap=\"gray_r\", vmin=0, vmax=250)\naxs[0].axis(\"tight\")\naxs[0].set_title(\"Original\")\naxs[1].imshow(xadj, cmap=\"gray_r\", vmin=0, vmax=250)\naxs[1].axis(\"tight\")\naxs[1].set_title(\"Sampled\")\naxs[2].imshow(xinv, cmap=\"gray_r\", vmin=0, vmax=250)\naxs[2].axis(\"tight\")\naxs[2].set_title(\"2D Regularization\")\nplt.tight_layout()\nplt.subplots_adjust(top=0.8)"
      ]
    }
  ],
  "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
}