diff --git a/src/ldpc/bp_decoder.pxd b/src/ldpc/bp_decoder.pxd index e676035..a3c5397 100755 --- a/src/ldpc/bp_decoder.pxd +++ b/src/ldpc/bp_decoder.pxd @@ -29,7 +29,7 @@ cdef class bp_decoder: cdef int MEM_ALLOCATED cdef int input_vector_type - cpdef np.ndarray[np.int_t, ndim=1] decode(self, input_vector) + cpdef np.ndarray[np.uint8_t, ndim=1] decode(self, input_vector) cdef char* bp_decode_cy(self) diff --git a/src/ldpc/bp_decoder.pyx b/src/ldpc/bp_decoder.pyx index cdb9553..dbee686 100755 --- a/src/ldpc/bp_decoder.pyx +++ b/src/ldpc/bp_decoder.pyx @@ -119,7 +119,7 @@ cdef class bp_decoder: for j in range(self.n): self.channel_probs[j]=error_rate self.error_rate=error_rate - cpdef np.ndarray[np.int_t, ndim=1] decode(self, input_vector): + cpdef np.ndarray[np.uint8_t, ndim=1] decode(self, input_vector): """ Runs the BP decoder for a given input_vector. diff --git a/src/ldpc/c_util.pxd b/src/ldpc/c_util.pxd index f9a12d0..ed59b73 100755 --- a/src/ldpc/c_util.pxd +++ b/src/ldpc/c_util.pxd @@ -4,12 +4,12 @@ import numpy as np cimport numpy as np cimport cython -# cdef char* numpy2char(np.ndarray[np.int_t, ndim=1] np_array,char* char_array) +# cdef char* numpy2char(np.ndarray[np.uint8_t, ndim=1] np_array,char* char_array) cdef char* numpy2char(np_array,char* char_array) cdef char* spmatrix2char(matrix,char* char_array) -cdef double* numpy2double(np.ndarray[np.float_t, ndim=1] np_array,double* double_array) -cdef np.ndarray[np.int_t, ndim=1] char2numpy(char* char_array, int n) -cdef np.ndarray[np.float_t, ndim=1] double2numpy(double* char_array, int n) +cdef double* numpy2double(np.ndarray[dtype = np.float_t, ndim=1] np_array,double* double_array) +cdef np.ndarray[dtype = np.uint8_t, ndim=1] char2numpy(char* char_array, int n) +cdef np.ndarray[dtype = np.float_t, ndim=1] double2numpy(double* char_array, int n) cdef extern from "binary_char.h": diff --git a/src/ldpc/c_util.pyx b/src/ldpc/c_util.pyx index 82f5cf9..e807859 100755 --- a/src/ldpc/c_util.pyx +++ b/src/ldpc/c_util.pyx @@ -3,7 +3,7 @@ import numpy as np from scipy.sparse import spmatrix -# cdef char* numpy2char(np.ndarray[np.int_t, ndim=1] np_array,char* char_array): +# cdef char* numpy2char(np.ndarray[np.uint8_t, ndim=1] np_array,char* char_array): # cdef int n = np_array.shape[0] # for i in range(n): char_array[i]=np_array[i] @@ -30,15 +30,15 @@ cdef double* numpy2double(np.ndarray[np.float_t, ndim=1] np_array,double* double for i in range(n): double_array[i]=np_array[i] return double_array -cdef np.ndarray[np.int_t, ndim=1] char2numpy(char* char_array, int n): +cdef np.ndarray[np.uint8_t, ndim=1] char2numpy(char* char_array, int n): - cdef np.ndarray[np.int_t, ndim=1] np_array=np.zeros(n).astype(int) + cdef np.ndarray[np.uint8_t, ndim=1] np_array=np.zeros(n).astype(np.uint8) for i in range(n):np_array[i]=char_array[i] return np_array cdef np.ndarray[np.float_t, ndim=1] double2numpy(double* char_array, int n): - cdef np.ndarray[np.float_t, ndim=1] np_array=np.zeros(n) + cdef np.ndarray[np.float_t, ndim=1] np_array=np.zeros(n, dtype=np.float64) for i in range(n):np_array[i]=char_array[i] return np_array diff --git a/src/ldpc/experimental/bp_decoder2.pxd b/src/ldpc/experimental/bp_decoder2.pxd index c34caad..c2a1b16 100755 --- a/src/ldpc/experimental/bp_decoder2.pxd +++ b/src/ldpc/experimental/bp_decoder2.pxd @@ -27,7 +27,7 @@ cdef class bp_decoder: cdef double ms_scaling_factor cdef int MEM_ALLOCATED - cpdef np.ndarray[np.int_t, ndim=1] bp_decode(self, np.ndarray[np.int_t, ndim=1] syndrome) + cpdef np.ndarray[np.uint8_t, ndim=1] bp_decode(self, np.ndarray[np.uint8_t, ndim=1] syndrome) cdef char* bp_decode_cy(self) diff --git a/src/ldpc/experimental/bp_decoder2.pyx b/src/ldpc/experimental/bp_decoder2.pyx index abe2e5c..b517392 100755 --- a/src/ldpc/experimental/bp_decoder2.pyx +++ b/src/ldpc/experimental/bp_decoder2.pyx @@ -83,7 +83,7 @@ cdef class bp_decoder: else: print("Decoder not called") - cpdef np.ndarray[np.int_t, ndim=1] bp_decode(self, np.ndarray[np.int_t, ndim=1] syndrome): + cpdef np.ndarray[np.uint8_t, ndim=1] bp_decode(self, np.ndarray[np.uint8_t, ndim=1] syndrome): self.synd=numpy2char(syndrome,self.synd) self.bp_decode_cy() return char2numpy(self.bp_decoding,self.n) diff --git a/src/ldpc/experimental/pymod2sparse.pxd b/src/ldpc/experimental/pymod2sparse.pxd index 293f7ba..cdd18bf 100644 --- a/src/ldpc/experimental/pymod2sparse.pxd +++ b/src/ldpc/experimental/pymod2sparse.pxd @@ -9,5 +9,5 @@ cdef class pymod2sparse(): cpdef iter_row(self,int row_index,int reverse_iterate=False) cpdef iter_col(self,int col_index,int reverse_iterate=False) - cpdef np.ndarray[np.int_t, ndim=1] mul(self, np.ndarray[np.int_t, ndim=1] vector) + cpdef np.ndarray[np.uint8_t, ndim=1] mul(self, np.ndarray[np.uint8_t, ndim=1] vector) diff --git a/src/ldpc/experimental/pymod2sparse.pyx b/src/ldpc/experimental/pymod2sparse.pyx index 25e1425..eb6e720 100644 --- a/src/ldpc/experimental/pymod2sparse.pyx +++ b/src/ldpc/experimental/pymod2sparse.pyx @@ -87,7 +87,7 @@ cdef class pymod2sparse(): return self - cpdef np.ndarray[np.int_t, ndim=1] mul(self, np.ndarray[np.int_t, ndim=1] vector): + cpdef np.ndarray[np.uint8_t, ndim=1] mul(self, np.ndarray[np.uint8_t, ndim=1] vector): if len(vector)!=self.n: raise ValueError(f'Dimension mismatch. The supplied vector of length {len(vector)} cannot be multiplied by a matrix with dimesnions ({self.m},{self.n})!') diff --git a/src/ldpc/mod2sparse.pxd b/src/ldpc/mod2sparse.pxd index df0f239..b6272dd 100755 --- a/src/ldpc/mod2sparse.pxd +++ b/src/ldpc/mod2sparse.pxd @@ -77,6 +77,6 @@ cdef class pymod2sparse(): cpdef iter_row(self,int row_index,int reverse_iterate) cpdef iter_col(self,int col_index,int reverse_iterate) - cpdef np.ndarray[np.int_t, ndim=1] mul(self, np.ndarray[np.int_t, ndim=1] vector) + cpdef np.ndarray[np.uint8_t, ndim=1] mul(self, np.ndarray[np.uint8_t, ndim=1] vector) diff --git a/src/ldpc/mod2sparse.pyx b/src/ldpc/mod2sparse.pyx index 1eb7383..7691a14 100755 --- a/src/ldpc/mod2sparse.pyx +++ b/src/ldpc/mod2sparse.pyx @@ -173,7 +173,7 @@ cdef class pymod2sparse(): return self - cpdef np.ndarray[np.int_t, ndim=1] mul(self, np.ndarray[np.int_t, ndim=1] vector): + cpdef np.ndarray[np.uint8_t, ndim=1] mul(self, np.ndarray[np.uint8_t, ndim=1] vector): if len(vector)!=self.n: raise ValueError(f'Dimension mismatch. The supplied vector of length {len(vector)} cannot be multiplied by a matrix with dimesnions ({self.m},{self.n})!') diff --git a/src/ldpc/osd.pyx b/src/ldpc/osd.pyx index 271933e..1e588ab 100755 --- a/src/ldpc/osd.pyx +++ b/src/ldpc/osd.pyx @@ -154,7 +154,7 @@ cdef class bposd_decoder(bp_decoder): return self.osdw_decoding - cpdef np.ndarray[np.int_t, ndim=1] decode(self, input_vector): + cpdef np.ndarray[np.uint8_t, ndim=1] decode(self, input_vector): """ Runs the BP+OSD decoder for a given syndrome. diff --git a/test.py b/test.py new file mode 100644 index 0000000..0fb702a --- /dev/null +++ b/test.py @@ -0,0 +1,15 @@ +import numpy as np +from ldpc.codes import rep_code +from ldpc.bp_decoder import bp_decoder + +H = rep_code(3) +print(H) + +bpd = bp_decoder(H, error_rate = 0.1, max_iter=0, bp_method='ps', ms_scaling_factor=0.625) + +syndrome = np.array([1,0]) + +decoding = bpd.decode(syndrome) +print(decoding) +print(bpd.bp_decoding) +print(bpd.log_prob_ratios)