Last active
May 5, 2019 13:25
-
-
Save wakusei-meron-/0502c3c6fcd78485611afed5904b76f3 to your computer and use it in GitHub Desktop.
Desktop/PPP/python/audio/HammingCode.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 PyHammingCode74:\n # ハミング符号生成行列\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 ])\n # パリティ検査行列\n _CHECK_MAT = np.array([\n [1,0,1,0,1,0,1],\n [0,1,1,0,0,1,1],\n [0,0,0,1,1,1,1]\n ])\n # 復号データ抽出行列\n _EXTRACT_MAT = np.array([\n [0,0,1,0,0,0,0],\n [0,0,0,0,1,0,0],\n [0,0,0,0,0,1,0],\n [0,0,0,0,0,0,1],\n ])\n # シンドロームから誤りビットの位置を計算する行列\n _SYNDROME_TO_INDEX_MAT = np.array([1,2,4])\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 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": 178, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "[0 1 0 0 1 0 1]\n[0 1 0 1]\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# データの初期化\ndata = np.array([0,1,0,1])\nprint(\"送信データ: {}\".format(data))\n\n# ハミング符号の計算\nh = PyHammingCode74()\nprint(\"ハミング符号: {}\".format(h.calc_parity(data)))\n\n# ハミング符号の復号\ndata = np.array([0,1,0,0,1,0,1])\nprint(\"受信データ:{}\".format(h.correct(data)))\n\n# 1bit誤りのあるデータの復号\ndata = np.array([1,1,0,0,1,0,1])\nprint(\"受信データ:{}\".format(h.correct(data)))", | |
"execution_count": 181, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "送信データ: [0 1 0 1]\nハミング符号: [0 1 0 0 1 0 1]\n受信データ:[0 1 0 1]\n受信データ:[0 1 0 1]\n", | |
"name": "stdout" | |
} | |
] | |
} | |
], | |
"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": "", | |
"data": { | |
"description": "Desktop/PPP/python/audio/HammingCode.ipynb", | |
"public": true | |
} | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment