{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# 1D Smoothing\n\nThis example shows how to use the :py:class:`pylops_distributed.Smoothing1D`\noperator to smooth an input signal along a given axis.\n\nA smoothing operator is a simple compact filter on lenght $n_{smooth}$\nand each elements is equal to $1/n_{smooth}$.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nimport dask.array as da\nimport matplotlib.pyplot as plt\n\nimport pylops_distributed\n\nplt.close('all')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Define the input parameters: number of samples of input signal (``N``) and\nlenght of the smoothing filter regression coefficients ($n_{smooth}$).\nIn this first case the input signal is one at the center and zero elsewhere.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "N = 31\nnchunks = 3\nnsmooth = 7\nx = np.zeros(N)\nx[int(N/2)] = 1\nx = da.from_array(x, chunks=N // nchunks + 1)\nprint('x:', x)\n\nSop = pylops_distributed.Smoothing1D(nsmooth=nsmooth, dims=[N],\n                                     dtype='float32')\n\ny = Sop * x\nxadj = Sop.H * y\n\nfig, ax = plt.subplots(1, 1, figsize=(10, 3))\nax.plot(x, 'k', lw=2, label=r'$x$')\nax.plot(y, 'r', lw=2, label=r'$y=Ax$')\nax.set_title('Smoothing in 1st direction', fontsize=14, fontweight='bold')\nax.legend()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Let's repeat the same exercise with a random signal as input. After applying\nsmoothing, we will also try to invert it.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "N = 120\nnchunks = 3\nnsmooth = 13\nx = np.random.normal(0, 1, N)\nx = da.from_array(x, chunks=N // nchunks + 1)\nprint('x:', x)\nSop = pylops_distributed.Smoothing1D(nsmooth=13, dims=(N), dtype='float32')\n\ny = Sop * x\nxest = Sop / y\n\nfig, ax = plt.subplots(1, 1, figsize=(10, 3))\nax.plot(x, 'k', lw=2, label=r'$x$')\nax.plot(y, 'r', lw=2, label=r'$y=Ax$')\nax.plot(xest, '--g', lw=2, label=r'$x_{ext}$')\nax.set_title('Smoothing in 1st direction',\n             fontsize=14, fontweight='bold')\nax.legend()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Finally we show that the same operator can be applied to multi-dimensional\ndata along a chosen axis.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "nx, ny = 21, 31\nnchunks = 2, 3\nA = np.zeros((nx, ny))\nA[5, 10] = 1\nA = da.from_array(A, chunks= (nx // nchunks[0] + 1, ny // nchunks[1] + 1))\nprint('A:', A)\n\nSop = pylops_distributed.Smoothing1D(nsmooth=5, dims=(nx, ny),\n                                     dir=0, dtype='float64')\nB = da.reshape(Sop * A.ravel(), (nx, ny))\n\nfig, axs = plt.subplots(1, 2, figsize=(10, 3))\nfig.suptitle('Smoothing in 1st direction for 2d data', fontsize=14,\n             fontweight='bold', y=0.95)\nim = axs[0].imshow(A, interpolation='nearest', vmin=0, vmax=1)\naxs[0].axis('tight')\naxs[0].set_title('Model')\nplt.colorbar(im, ax=axs[0])\nim = axs[1].imshow(B, interpolation='nearest', vmin=0, vmax=1)\naxs[1].axis('tight')\naxs[1].set_title('Data')\nplt.colorbar(im, ax=axs[1])\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.6.12"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}