Esempio n. 1
0
def rect_parity(codeword,nrows,ncols):
    # construct rectangle
    rectangle = []
    for row in range(nrows):
        rectangle.append(codeword[row*ncols:row*ncols+ncols])
    # add row parity bits
    row = 0
    for row_parity_bit in codeword[nrows*ncols:nrows*ncols+nrows]:
        rectangle[row].append(row_parity_bit)
        row += 1
    # add column parity bits
    rectangle.append(codeword[nrows*ncols+nrows:])
    
    # calculate parity
    rows_with_error = []
    cols_with_error = []
    # row parity
    for row in range(nrows):
        if not PS2_tests.even_parity(rectangle[row][:ncols+1]):
            rows_with_error.append(row)
    # column parity
    for col in range(ncols):
        if not PS2_tests.even_parity([rectangle[row][col] for row in range(nrows+1)]):
            cols_with_error.append(col)
    
    # correct errors
    if len(rows_with_error) == 1 and len(cols_with_error) == 1:
        row = rows_with_error[0]
        col = cols_with_error[0]
        rectangle[row][col] = int(not rectangle[row][col]) # flip bit
    
    # return the corrected data
    message_sequence = []
    for row in range(nrows):
        for col in range(ncols):
            message_sequence.append(rectangle[row][col])
    return message_sequence
Esempio n. 2
0
    for j in xrange(nrows):
        parity_bit = get_parity_bit(codeword, 'row', j)
        row = get_row(codeword, j)
        if is_error(row, parity_bit):
            corrected_row = []
            for i in xrange(ncols):
                col = get_col(codeword, i)
                parity_bit = get_parity_bit(codeword, 'col', i)
                if is_error(col, parity_bit):
                    if corrected: return codeword[:nrows * ncols];
                    corrected_row.append(int(not row[i]))
                    corrected = True
                else:
                    corrected_row.append(row[i])
            corrected_code.extend(corrected_row)
        else:
            corrected_code.extend(row)
#    for i in xrange(ncols):
#        col = get_col(codeword, i)
#        parity_bit = get_parity_bit(codeword, 'col', i)
#        if is_error(col, parity_bit):
#            corrected_col = []
#            for j in xrange(nrows):
#                row = get_row(codeword, j)
#                parity_bit = get_parity_bit(codeword, 'row', j)
    return corrected_code


if __name__ == '__main__':
    PS2_tests.test_correct_errors(rect_parity)
Esempio n. 3
0
        return codeword[:k]


def compute_all_syndrome_results(h_matrix, n):
    results = []
    for i in xrange(n):
        new_array = [0 for j in xrange(n)]
        new_array[i] = 1
        error_vector = array(new_array)
        error_vector.shape = n,1
        results.append(mod2(h_matrix*error_vector))
    return results

if __name__ == '__main__':
    # (7,4,3) Hamming code
    G1 = matrix('1 0 0 0 1 1 0; 0 1 0 0 1 0 1; 0 0 1 0 0 1 1; 0 0 0 1 1 1 1', 
                dtype=int)
    PS2_tests.test_linear_sec(syndrome_decode, 7, 4, G1)

    # (8,4,3) rectangular parity code
    G2 = matrix('1 0 0 0 1 0 1 0; 0 1 0 0 1 0 0 1; 0 0 1 0 0 1 1 0; 0 0 0 1 0 1 0 1', dtype=int)
    PS2_tests.test_linear_sec(syndrome_decode, 8, 4, G2)

     # (6,3,3) pairwise parity code
    G3 = matrix('1 0 0 1 1 0; 0 1 0 0 1 1; 0 0 1 1 0 1', dtype=int)
    PS2_tests.test_linear_sec(syndrome_decode, 6, 3, G3)
    
    # (15,11,3) Hamming code
    G4 = matrix('1 0 0 0 0 0 0 0 0 0 0 0 1 1 1; 0 1 0 0 0 0 0 0 0 0 0 1 0 1 1; 0 0 1 0 0 0 0 0 0 0 0 1 1 0 1; 0 0 0 1 0 0 0 0 0 0 0 1 1 1 0; 0 0 0 0 1 0 0 0 0 0 0 1 1 1 1; 0 0 0 0 0 1 0 0 0 0 0 1 1 0 0; 0 0 0 0 0 0 1 0 0 0 0 1 0 1 0; 0 0 0 0 0 0 0 1 0 0 0 1 0 0 1; 0 0 0 0 0 0 0 0 1 0 0 0 1 1 0; 0 0 0 0 0 0 0 0 0 1 0 0 1 0 1; 0 0 0 0 0 0 0 0 0 0 1 0 0 1 1')
    PS2_tests.test_linear_sec(syndrome_decode, 15, 11, G4)
Esempio n. 4
0
# template for 6.02 rectangular parity decoding using error triangulation
import PS2_tests
import numpy

def rect_parity(codeword,nrows,ncols): 
    counter = 0
    row_sums = [0 for i in xrange(nrows)]
    column_sums = [0 for i in xrange(ncols)]
    for i in xrange(nrows):
        for j in xrange(ncols):
            row_sums[i] += codeword[counter]
            column_sums[j] += codeword[counter]
            counter += 1
    row_errors = []
    column_errors = []
    broken_bits = 0
    for i in xrange(nrows):
        if (row_sums[i] % 2) != codeword[nrows*ncols+i]:
            row_errors.append(i)
    for j in xrange(ncols):
        if (column_sums[j] % 2) != codeword[nrows*ncols+nrows+j]:
            column_errors.append(j)
    result = [codeword[k] for k in xrange(len(codeword)) if k <= (nrows*ncols-1)]
    if len(row_errors) == 1 and len(column_errors) == 1:
        result[row_errors[0]*ncols+column_errors[0]] = (result[row_errors[0]*ncols+column_errors[0]] + 1) % 2
    return result

if __name__ == '__main__':
    PS2_tests.test_correct_errors(rect_parity)
Esempio n. 5
0
    c = mod2(H * matrix(codeword).transpose())
    if not equal(c, zeros((n - k, 1), int)):
        for i in range(k):
            if equal(c, syndromes[i]):
                codeword[i] = int(not codeword[i])  # flip bit
                break

    return codeword[:k]


if __name__ == '__main__':
    # (7,4,3) Hamming code
    G1 = matrix('1 0 0 0 1 1 0; 0 1 0 0 1 0 1; 0 0 1 0 0 1 1; 0 0 0 1 1 1 1',
                dtype=int)
    PS2_tests.test_linear_sec(syndrome_decode, 7, 4, G1)

    # (8,4,3) rectangular parity code
    G2 = matrix(
        '1 0 0 0 1 0 1 0; 0 1 0 0 1 0 0 1; 0 0 1 0 0 1 1 0; 0 0 0 1 0 1 0 1',
        dtype=int)
    PS2_tests.test_linear_sec(syndrome_decode, 8, 4, G2)

    # (6,3,3) pairwise parity code
    G3 = matrix('1 0 0 1 1 0; 0 1 0 0 1 1; 0 0 1 1 0 1', dtype=int)
    PS2_tests.test_linear_sec(syndrome_decode, 6, 3, G3)

    # (15,11,3) Hamming code
    G4 = matrix(
        '1 0 0 0 0 0 0 0 0 0 0 0 1 1 1; 0 1 0 0 0 0 0 0 0 0 0 1 0 1 1; 0 0 1 0 0 0 0 0 0 0 0 1 1 0 1; 0 0 0 1 0 0 0 0 0 0 0 1 1 1 0; 0 0 0 0 1 0 0 0 0 0 0 1 1 1 1; 0 0 0 0 0 1 0 0 0 0 0 1 1 0 0; 0 0 0 0 0 0 1 0 0 0 0 1 0 1 0; 0 0 0 0 0 0 0 1 0 0 0 1 0 0 1; 0 0 0 0 0 0 0 0 1 0 0 0 1 1 0; 0 0 0 0 0 0 0 0 0 1 0 0 1 0 1; 0 0 0 0 0 0 0 0 0 0 1 0 0 1 1'
    )