Last active
May 6, 2019 15:09
-
-
Save wakusei-meron-/1f10d6846032a283705b19f3c6e760c8 to your computer and use it in GitHub Desktop.
Desktop/PPP/python/audio/HammingCode-Copy1.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "class PyHammingCode84:\n _GEN_MAT = np.array([\n [1,1,0,1],\n [1,0,1,1],\n [1,0,0,0],\n [0,1,1,1],\n [0,1,0,0],\n [0,0,1,0],\n [0,0,0,1],\n [1,1,1,0]\n ])\n _CHECK_MAT = np.array([\n [1,0,1,0,1,0,1,0],\n [0,1,1,0,0,1,1,0],\n [0,0,0,1,1,1,1,0],\n [1,1,1,1,1,1,1,1]\n ])\n _EXTRACT_MAT = np.array([\n [0,0,1,0,0,0,0,0],\n [0,0,0,0,1,0,0,0],\n [0,0,0,0,0,1,0,0],\n [0,0,0,0,0,0,1,0],\n ])\n _SYNDROME_TO_INDEX_MAT = np.array([1,2,4,0])\n \n def calc_parity(self, data):\n return np.dot(self._GEN_MAT, data) % 2\n \n def correct(self, data):\n _d = np.copy(data)\n s = np.dot(self._CHECK_MAT, _d) % 2\n print(s)\n if s[-1] == 0 and np.any(s[:-1]):\n raise Exception(\"detected double error\")\n if np.any(s):\n i = np.dot(self._SYNDROME_TO_INDEX_MAT, s.T) - 1 # -1で0から始まるインデックスにする\n _d[i] = _d[i] ^ 1# 誤り符号の訂正\n return np.dot(self._EXTRACT_MAT, _d)\n", | |
"execution_count": 4, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# データの初期化\ndata = np.array([0,1,0,1])\nprint(\"送信データ: {}\".format(data))\n\n# ハミング符号の計算\nh = PyHammingCode84()\nprint(\"ハミング符号: {}\".format(h.calc_parity(data)))\n\n# ハミング符号の復号\ndata = np.array([0,1,0,0,1,0,1,1])\nprint(\"受信データ:{}\".format(h.correct(data)))\n\n# 1bit誤りのあるデータの復号\ndata = np.array([1,1,0,0,1,0,1,1])\nprint(\"受信データ:{}\".format(h.correct(data)))\n\n# 2bit誤りのあるデータの検知\ndata = np.array([1,0,0,0,1,0,1,1])\nprint(\"受信データ:{}\".format(h.correct(data)))", | |
"execution_count": 5, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "送信データ: [0 1 0 1]\nハミング符号: [0 1 0 0 1 0 1 1]\n[0 0 0 0]\n受信データ:[0 1 0 1]\n[1 0 0 1]\n受信データ:[0 1 0 1]\n[1 1 0 0]\n", | |
"name": "stdout" | |
}, | |
{ | |
"output_type": "error", | |
"ename": "Exception", | |
"evalue": "detected double error", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[0;31mException\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-5-0214be42abb6>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 17\u001b[0m \u001b[0;31m# 2bit誤りのあるデータの検知\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 18\u001b[0m \u001b[0mdata\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 19\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"受信データ:{}\"\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mh\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcorrect\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;32m<ipython-input-4-e9c43ce28067>\u001b[0m in \u001b[0;36mcorrect\u001b[0;34m(self, data)\u001b[0m\n\u001b[1;32m 32\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 33\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m0\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0many\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 34\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mException\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"detected double error\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 35\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0many\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 36\u001b[0m \u001b[0mi\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_SYNDROME_TO_INDEX_MAT\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mT\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;36m1\u001b[0m \u001b[0;31m# -1で0から始まるインデックスにする\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;31mException\u001b[0m: detected double error" | |
] | |
} | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3", | |
"language": "python" | |
}, | |
"language_info": { | |
"name": "python", | |
"version": "3.6.3", | |
"mimetype": "text/x-python", | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"pygments_lexer": "ipython3", | |
"nbconvert_exporter": "python", | |
"file_extension": ".py" | |
}, | |
"gist": { | |
"id": "1f10d6846032a283705b19f3c6e760c8", | |
"data": { | |
"description": "Desktop/PPP/python/audio/HammingCode-Copy1.ipynb", | |
"public": true | |
} | |
}, | |
"_draft": { | |
"nbviewer_url": "https://gist.github.com/1f10d6846032a283705b19f3c6e760c8" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment