{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from scipy.signal import find_peaks\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "spettro = np.genfromtxt('HCl.csv', delimiter=',',skip_header=1,names=True)\n",
    "\n",
    "sp_x=spettro['cm1'][::-1] # invertiamo l'ordine del vettore con la notazione [::-1]\n",
    "sp_y=spettro['A'][::-1]\n",
    "\n",
    "plt.plot(sp_x,sp_y)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b1c=(sp_x>2700) & (sp_x<3050)\n",
    "b1x=sp_x[b1c]\n",
    "b1y=sp_y[b1c]\n",
    "plt.plot(b1x, b1y)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#find_peaks?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b1p,_=find_peaks(b1y, threshold=0.002)\n",
    "plt.plot(b1x,b1y)\n",
    "plt.plot(b1x[b1p],b1y[b1p],'*')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b1p35=b1p[1::2]\n",
    "b1x35=b1x[b1p35]\n",
    "b1y35=b1y[b1p35]\n",
    "plt.plot(b1x,b1y)\n",
    "plt.plot(b1x35, b1y35,'*')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def trova_00(x):\n",
    "    d=x[1]-x[0]\n",
    "    for i in range(len(x)-1):\n",
    "        di=x[i+1]-x[i]\n",
    "        if di>1.5*d:\n",
    "            return 0.5*(x[i+1]+x[i])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "c35=trova_00(b1x35)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b1p35=b1p[1::2]\n",
    "b1x35=b1x[b1p35]\n",
    "b1y35=b1y[b1p35]\n",
    "plt.plot(b1x,b1y)\n",
    "plt.plot(b1x35, b1y35,'*')\n",
    "plt.vlines([c35],ymin=0, ymax=2.0)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "md=np.arange(1, sum(b1x35 > c35)+1)\n",
    "ms=np.arange(1, sum(b1x35 < c35)+1)*(-1)\n",
    "m=np.append(ms[::-1], md)\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.plot(m, b1x35, '*')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "coeff = np.polyfit(m, b1x35, 3)\n",
    "polinomio=np.poly1d(coeff)\n",
    "valori_fit=polinomio(m)\n",
    "print(polinomio)\n",
    "plt.plot(m, b1x35, '+')\n",
    "plt.plot(m,valori_fit)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.11.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
