{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "e6cff720-12a1-4ce3-b0c7-1b6790a4c740",
   "metadata": {},
   "outputs": [],
   "source": [
    "from matplotlib import pyplot as plt\n",
    "import numpy as np\n",
    "import scipy\n",
    "import scipy.signal as sig\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "40d5f9f9-376c-4835-9552-4cb4f8bd349a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x7f2838349c60>]"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 166456 characters]\n",
      "text/plain": [
       "<Figure size 2016x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "data = np.genfromtxt('barcode_test.csv', delimiter=',', skip_header=2, usecols=1)[::-1]\n",
    "fig, ax = plt.subplots(figsize=(28, 6))\n",
    "ax.plot(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "0be7c2c6-8937-4fd4-9a84-10098e951a42",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 140332 characters]\n",
      "text/plain": [
       "<Figure size 2016x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "sos = sig.butter(2, 0.5, btype='lowpass', output='sos')\n",
    "filtered = sig.sosfiltfilt(sos, data)\n",
    "filtered -= np.mean(filtered)\n",
    "filtered /= np.sqrt(np.mean(filtered**2))\n",
    "\n",
    "fig, ax = plt.subplots(figsize=(28, 6))\n",
    "ax.plot(filtered)\n",
    "ax.grid()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "e6cb6f5b-3611-4db5-a277-b9998cb7228c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 241664 characters]\n",
      "text/plain": [
       "<Figure size 2016x648 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "decimated = sig.decimate(filtered, 2)\n",
    "\n",
    "dy = decimated[1:] - decimated[:-1]\n",
    "\n",
    "fig, (ax1, ax2) = plt.subplots(2, figsize=(28, 9))\n",
    "ax1.plot(decimated)\n",
    "ax2.plot(dy)\n",
    "ax1.grid()\n",
    "ax2.grid()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "abda9fb4-4eba-4e98-ad95-5625e517282e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 194120 characters]\n",
      "text/plain": [
       "<Figure size 2016x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "#arr = np.zeros([5, len(dy)])\n",
    "#for i in range(arr.shape[0]):\n",
    "#    print(i, arr[i, i:].shape, np.abs(dy).shape)\n",
    "#    arr[i, i:] = np.abs(dy)[:len(dy)-i]\n",
    "#arr = arr.max(axis=0)\n",
    "\n",
    "fig, ax = plt.subplots(figsize=(28, 6))\n",
    "ax.plot(np.abs(dy))\n",
    "ax.grid()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.10.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
