{
  "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.Smoothing1D` operator\nto smooth an input signal along a given axis.\n\nDerivative (or roughening) operators are generally used *regularization*\nin inverse problems. Smoothing has the opposite effect of roughening and\nit can be employed as *preconditioning* in inverse problems.\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 matplotlib.pyplot as plt\n\nimport pylops\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\nnsmooth = 7\nx = np.zeros(N)\nx[int(N/2)] = 1\n\nSop = pylops.Smoothing1D(nsmooth=nsmooth, dims=[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 smoothing,\nwe will also try to invert it.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "N = 120\nnsmooth = 13\nx = np.random.normal(0, 1, N)\nSop = pylops.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": [
        "A = np.zeros((11, 21))\nA[5, 10] = 1\n\nSop = pylops.Smoothing1D(nsmooth=5, dims=(11, 21), dir=0, dtype='float64')\nB = np.reshape(Sop*np.ndarray.flatten(A), (11, 21))\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
}