{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sympy as sp\n",
    "import matplotlib.pyplot as plt\n",
    "from sympy.plotting import plot\n",
    "from sympy.polys.polyfuncs import horner\n",
    "\n",
    "sp.init_printing()\n",
    "\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "x, s = sp.symbols(\"x, sigma\")\n",
    "s = sp.sympify(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "full = sp.exp(- (x/s)**2)\n",
    "approx = full.series(x, x0=0, n=5).removeO()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_vals = (-2 *s, -1.6*s, 0, 1.6*s, 2 * s)\n",
    "y_vals = list(full.subs(x, e) for e in x_vals)\n",
    "y_vals[0] = 0\n",
    "y_vals[-1] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 17520 characters]\n",
      "text/latex": [
       "$\\displaystyle \\begin{cases} - 0.27606958941084 x^{3} - 0.80213917882168 x^{2} + 6.93889390390723 \\cdot 10^{-18} x + 1.0 & \\text{for}\\: x \\geq -2 \\wedge x \\leq 0 \\\\0.27606958941084 x^{3} - 0.80213917882168 x^{2} + 6.93889390390723 \\cdot 10^{-18} x + 1.0 & \\text{for}\\: x \\geq 0 \\wedge x \\leq 2 \\end{cases}$"
      ],
      "text/plain": [
       "⎧                    3                     2                                  \n",
       "⎪- 0.27606958941084⋅x  - 0.80213917882168⋅x  + 6.93889390390723e-18⋅x + 1.0  f\n",
       "⎨                                                                             \n",
       "⎪                   3                     2                                   \n",
       "⎩ 0.27606958941084⋅x  - 0.80213917882168⋅x  + 6.93889390390723e-18⋅x + 1.0   f\n",
       "\n",
       "                 \n",
       "or x ≥ -2 ∧ x ≤ 0\n",
       "                 \n",
       "                 \n",
       "or x ≥ 0 ∧ x ≤ 2 "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "spline = sp.interpolating_spline(3, x, x_vals, y_vals)\n",
    "spline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 6564 characters]\n",
      "text/latex": [
       "$\\displaystyle - 0.27606958941084 x^{3} - 0.80213917882168 x^{2} + 6.93889390390723 \\cdot 10^{-18} x + 1.0$"
      ],
      "text/plain": [
       "                    3                     2                               \n",
       "- 0.27606958941084⋅x  - 0.80213917882168⋅x  + 6.93889390390723e-18⋅x + 1.0"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "spline.args[0][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 17348 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "p1 = plot(full, show=False, xlim=(0, 3), ylim=(-0.1, 1.1))\n",
    "p2 = plot(spline, show=False, line_color='r')\n",
    "p1.append(p2[0])\n",
    "p1.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAKUAAAAPCAYAAACFrA9SAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAFa0lEQVRoBe2a3XUVNxCAjQ8FOKYCoAMMHTgdhFABoYPk1W8c0gFQQYAOAhUQ6ABSQcztwPk+WaPs7tWuZO6amwfmHN2RRqP502hWu/aNi4uLg+/wPQK7RuDs7Owe7eNQDuMjxsfgz0N6q3+zxdA7j+Jnmfcf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vYzeVBn7KDgCL8rq5asqbxCRbZymn67iA3l6UYNHKfLNgX7sFfRjbTuR9ydOuW9zeRS5kHzXBjqzsdz1TqkST34t2FEpuyoDhlpx7oJLhaQfcsUPGPtYH4JVy1Mo3YQ1OE0++Mt9D/5dQP1vkReJOBeL0LF4qILpG+C17fQJUHtqphwgPuVlZ8G3Ess1ktKEiIox1Omp8YNqbNhwbtSHR4NMpmmymKgvoLuZW8kN/Yt0cEnkK/DB2gfI1w4/+t6mn/wBHzH2jjmsDl4vpgcHUuKxQm354OQeYG073aPp3umW8Rn5DF8zloe7BgQlXurPs7Ikjr4b9jMtXWolSqNd0LxfFWBsFXQjnffPeKVB84VlKanVY2tBi+9WFpBOdkWYNk4rgTZrX6kC9H2z9JCUQ0pf3aNYMJ5CS3/wr8L3lXYu6U57FkaK0RF/cRsWDKeasbyxxn8J5cC7ERuaLzYPaE+hlw1jrKGfQG5auU9kmobWwEp7Mp2A5kl3jSdRMBneQx+9rbf4mI+qphyTR3vTNYC50Rs040g0+YTZb4+ZN/hM9K1YKAC+Lv1r86lbQK4+Ldp5Bd3uR1RLffYQ++fiDXgEWa+00D2K5b8z6e0QbaTizAAAAABJRU5ErkJggg==\n",
      "text/latex": [
       "$\\displaystyle -0.2441961116159$"
      ],
      "text/plain": [
       "-0.244196111615900"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "spline.subs(x, 1.99).evalf() * 255"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "def bellCurveApproximation(x, inverseWidth):\n",
    "    if x < 0:\n",
    "        x = -x\n",
    "\n",
    "    nx = x * inverseWidth * 4\n",
    "    if nx > 2:\n",
    "        return 0\n",
    "\n",
    "    x2 = nx * nx\n",
    "    x3 = x2 * nx\n",
    "    res = 0.27606958941084 * x3 - 0.80213917882168 * x2 + 1\n",
    "    \n",
    "    return  0 if res < 0 else res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x7f4a503fd190>]"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 8568 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot([bellCurveApproximation(i, 1 / (51 / 2)) for i in range(51)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "def extract_blue(v):\n",
    "    return (v >> 16) & 255"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAL8AAAAPCAYAAAC1IB5zAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAHuklEQVRoBd2a7XEUORCGFxcBGC4CTAZ8ZAAZwBGBIQMo/vGPggyACPjIAIiAjwzgIgCcge95tGpZM6OZ0WLsqruu0kpqtbpb6lct7doXjo+PN+dJT548uUb5Wtukv0//MvX3mv9/abOuA9Zyi/KG9tF5ruusbKP3Px/Hi3UgWNCz3P9BfZXyDF4XIPMmP8jzBbMBd/6HzIvqIzzH4wDYlq5vq+1n1vco825Q/6Q8gh/z8tBmA+9F6XCIaB/CWwUZMteQfUAdfldqkl7H31KuL+nLepbk1KOPL5ClmtAR/Es1l37EItiv4U3WHoPWjLfWs5Ptyu4aBnrjaHwfV37af4udAS7od8mpB1mx1YWNLN/cywJ+FH5B8Cn1uzxBZ77Qv01ZPACMKyswC4ho34H3nvouJelULySIJYOiXse0e0SdiLaL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LGeWfkwxzkGTGGG7V64bGxpD7+xe/gsCwZXfYN0PQwAAAABJRU5ErkJggg==\n",
      "text/latex": [
       "$\\displaystyle 0.35294117647058826$"
      ],
      "text/plain": [
       "0.35294117647058826"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v = [extract_blue(e) for e in data[0]]\n",
    "v.count(0) / len(v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x7f4a4fe84340>]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 11664 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot([extract_blue(e) for e in data[0]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
