[PYTHON] Hamming code with numpy

Looking at the numpy reference, I found a useful function for bit processing, so I also implemented a Hamming code for testing. For the time being, only the Hamming code (4,3) is supported. I wish I could support other than uint8.

hamming.py


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import numpy as np
import random

if __name__ == '__main__':
    k = 4
    m = 3

    #Create a list of non-powers of 2
    column_values = np.array([ [x] for x in range( 2 ** m ) if ( x & ( x - 1 ) ) != 0 ], dtype=np.uint8 )
    A = np.reshape( np.unpackbits( column_values ), ( k, 8) )[:, -3:].T

    #Create generator and check matrices
    H = np.concatenate( ( A, np.eye( m, dtype=np.uint8 ) ), axis=1 )
    G = np.concatenate( ( np.eye( k, dtype = np.uint8 ), A.T ), axis = 1 )

    #Appropriately set the value to be encoded
    random.seed()
    code = random.randint( 0, 2 ** k - 1 )
    bit_code = np.unpackbits( np.array( code, dtype = np.uint8 ) )[-k:]

    #Generate a Hamming code to invert a bit
    error_bit_offset = random.randint( 0, ( k + m ) - 1 )
    hamming_code = np.dot( bit_code, G ) % 2
    hamming_code[ error_bit_offset ] = ( hamming_code[ error_bit_offset ] + 1 ) % 2

    #Verification

    check_result = np.dot( H, hamming_code ) % 2

    print code
    print hamming_code
    print H
    print error_bit_offset
    print check_result

Execution result

% ./hamming.py 
12
[1 0 0 0 1 1 0]
[[0 1 1 1 1 0 0]
 [1 0 1 1 0 1 0]
 [1 1 0 1 0 0 1]]
1
[1 0 1]

Recommended Posts

Hamming code with numpy
Strengthen with code test ⑨
Strengthen with code test ③
Moving average with numpy
Strengthen with code test ⑤
Strengthen with code test ④
Debug with VS Code using boost python numpy
Strengthen with code test ②
Getting Started with Numpy
Learn with Cheminformatics NumPy
Matrix concatenation with Numpy
Strengthen with code test ①
Regression analysis with NumPy
Extend NumPy with Rust
Strengthen with code test ⑧
Strengthen with code test ⑨
Kernel regression with Numpy only
Get country code with python
Python with VS Code (Windows 10)
I wrote GP with numpy
CNN implementation with just numpy
Artificial data generation with numpy
[Python] Calculation method with numpy
Debt repayment simulation with numpy
Implemented SMO with Python + NumPy
Stick strings together with Numpy
Debug Python with VS Code
Handle numpy arrays with f2py
Check the code with flake8
Use OpenBLAS with numpy, scipy
Python3 | Getting Started with numpy
Document Python code with Doxygen
Implementing logistic regression with NumPy
Decrypt the QR code with CNN
Perform least squares fitting with numpy.
Java with Visual Studio Code (Part 2)
Classify "Wine" with TensorFlow MLP code
Draw a beautiful circle with numpy
Implement Keras LSTM feedforward with numpy
Try running Jupyter with VS Code
Extract multiple elements with Numpy array
It was with Nintendo's Code Puzzle
Install python with mac vs code
Read and write csv files with numpy
Display Japanese graphs with VS Code + matplotlib
Graph trigonometric functions with numpy and matplotlib
I made a life game with Numpy
Speeding up numerical calculations with NumPy: Basics
Handle numpy with Cython (method by memoryview)
Use multithreaded BLAS / LAPACK with numpy / scipy