{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## This notebook provides the decoder function to convert the photocurrent to morse code and then convert the morse code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import seaborn as sns\n",
    "from itertools import groupby"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## data pre-processing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "metadata": {},
   "outputs": [],
   "source": [
    "df=pd.read_excel(\"./raw_data.xlsx\",header=None,names=['time','value'])\n",
    "\n",
    "stand_value=df[df['time']<75]\n",
    "\n",
    "None_scale=max(np.abs(stand_value[stand_value['value']>stand_value['value'].mean()]['value']-None_mean))*10\n",
    "\n",
    "table_0_1=stand_value[stand_value['value']<stand_value['value'].mean()]\n",
    "\n",
    "mean_0_1=stand_value[stand_value['value']<stand_value['value'].mean()]['value'].mean() # 0-1 mean\n",
    "\n",
    "zero_mean=table_0_1[table_0_1['value']>mean_0_1]['value'].mean()\n",
    "\n",
    "zero_scale=max(np.abs(table_0_1[table_0_1['value']>mean_0_1]['value']-zero_mean))*3\n",
    "\n",
    "one_mean=table_0_1[table_0_1['value']<mean_0_1]['value'].mean()\n",
    "\n",
    "one_scale=max(np.abs(table_0_1[table_0_1['value']<mean_0_1]['value']-one_mean))*3\n",
    "\n",
    "data=df[df['time']>=75]\n",
    "\n",
    "data.insert(loc=2, column='state', value=None)\n",
    "\n",
    "data.loc[(data['value']<zero_mean+zero_scale) & (data['value']>zero_mean-zero_scale),'state']=0\n",
    "\n",
    "data.loc[(data['value']<one_mean+one_scale) & (data['value']>one_mean-one_scale),'state']=1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## parser function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "metadata": {},
   "outputs": [],
   "source": [
    "def parser(time,state): # detect it is point or a letter\n",
    "    label_sequences=[]\n",
    "    for i in range(1, len(state)):\n",
    "        if state[i] is None:\n",
    "            continue\n",
    "        else:\n",
    "            start_index=i+1\n",
    "            break\n",
    "    \n",
    "    for i in range(start_index,len(state)):\n",
    "        if state[i-1] is not None and state[i] is None:\n",
    "            start=time[i]\n",
    "            label_sequences.append(None)\n",
    "        elif state[i-1] is None and state[i] is not None:\n",
    "            end=time[i-1]\n",
    "            interval=end-start\n",
    "            if interval<10:\n",
    "                label_sequences.append(0) # delete\n",
    "            else:\n",
    "                label_sequences.append(1) # save\n",
    "        else:\n",
    "            continue\n",
    "    return np.array(label_sequences)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## morse code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "metadata": {},
   "outputs": [],
   "source": [
    "def morse(time, state):\n",
    "    result=[]\n",
    "    temp=np.delete(np.array([x[0] for x in groupby(state)][1:-1]), \\\n",
    "                   np.where(parser(time,state)==0)[0])\n",
    "    index=np.where(temp==None)[0]\n",
    "    for i in range(len(index)):\n",
    "        if i==0:\n",
    "            result.append(''.join(str(x) for x in temp[0:index[i]]))\n",
    "        elif i==len(index)-1:\n",
    "            result.append(''.join(str(x) for x in temp[index[i-1]+1:index[i]]))\n",
    "            result.append(''.join(str(x) for x in temp[index[i]+1:]))\n",
    "        else:\n",
    "            result.append(''.join(str(x) for x in temp[index[i-1]+1:index[i]]))\n",
    "    return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "metadata": {},
   "outputs": [],
   "source": [
    "code=morse(np.array(data['time']),np.array(data['state']))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## decode the morse code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "metadata": {},
   "outputs": [],
   "source": [
    "dic={'01':'A','1000':'B','1010':'C','100':'D','0':'E','0010':'F','110':'G','0000':'H','00':'I','0111':'J','101':'K','0100':'L','11':'M','10':'N',\\\n",
    "    '111':'O','0110':'P','1101':'Q','010':'R','000':'S','1':'T','001':'U','0001':'V','011':'W','1001':'X','1011':'Y','110':'Z'}\n",
    "\n",
    "def decode(code):\n",
    "    res=[]\n",
    "    for i in code:\n",
    "        res.append(dic[i])\n",
    "    return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['I', 'C', 'C', 'A', 'S']"
      ]
     },
     "execution_count": 142,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "decode(code)"
   ]
  },
  {
   "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.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
