Exemplo n.º 1
0
    res = Mat(({0, 1, 2, 3, 4, 5, 6},S.D[1]), {})
    for i in res.D[1]:
        e = find_error(S[i])  # S[i] returns i-th column of S
        for j in res.D[0]:
            res[j,i] = e[j]

    return res

## Task 8
s = "I'm trying to free your mind, Neo. But I can only show you the door. You're the one that has to walk through it."
P = bits2mat(str2bits(s))  # p[i] column is 4-bit nibble

## Ungraded task
# Use noise(A, s) to produce the transmitted message
# input: P - matrix being transmitted
print("Message w/o encoding and ecc:", bits2str(mat2bits(P + noise(P, 0.02))).encode("utf-8"))

## Task 9
C = G*P  # encode
bits_before = len(str2bits(s))  # bits before encoding
bits_after = bits_before//4*7   # bits after encoding
print(bits_before, bits_after)

## Ungraded Task
CTILDE = C + noise(C, 0.02)  # encoded message received
print("Message with encoding, but w/o ecc:", bits2str(mat2bits(R*CTILDE)).encode("utf-8"))


## Task 10
def correct(A):
    """
Exemplo n.º 2
0
    """
    return coldict2mat({c:find_error(v) for c, v in mat2coldict(S).items()})

## Task 8
s = "I'm trying to free your mind, Neo. But I can only show you the door. You're the one that has to walk through it."
P = bits2mat(str2bits(s))

#assert s == bits2str(str2bits(s))
#assert s == bits2str(mat2bits(bits2mat(str2bits(s))))

## Task 4.14.11
#print(bits2str(mat2bits(P + noise(P, 0.02))))

## Task 9
C = G*P
bits_before = len(mat2bits(P))
bits_after = len(mat2bits(C))

#print("Before: {}; After: {}. Rate: {}\n".format(
#    bits_before, bits_after, float(bits_after) / bits_before))


## Ungraded Task
CTILDE = C + noise(C, 0.02)
#print(bits2str(mat2bits(R*CTILDE)))


## Task 10
def correct(A):
    """
    Input: a matrix A each column of which differs from a codeword in at most one bit
Exemplo n.º 3
0
def error_test(s, prob):
    msg = bits2mat(str2bits(s))
    cw = G*msg
    rcvd = cw + noise(cw, prob)
    msg_rcvd = R*correct(rcvd)
    return s == bits2str(mat2bits(msg_rcvd))
Exemplo n.º 4
0
    Output: a matrix whose cth column is the error corresponding to the cth column of S.
    Example:
        >>> S = listlist2mat([[0,one,one,one],[0,one,0,0],[0,0,0,one]])
        >>> find_error_matrix(S) == Mat(({0, 1, 2, 3, 4, 5, 6}, {0, 1, 2, 3}), {(1, 3): 0, (3, 0): 0, (2, 1): 0, (6, 2): 0, (5, 1): one, (0, 3): 0, (4, 0): 0, (1, 2): 0, (3, 3): 0, (6, 3): 0, (5, 0): 0, (2, 2): 0, (4, 1): 0, (1, 1): 0, (3, 2): one, (0, 0): 0, (6, 0): 0, (2, 3): 0, (4, 2): 0, (1, 0): 0, (5, 3): 0, (0, 1): 0, (6, 1): 0, (3, 1): 0, (2, 0): 0, (4, 3): one, (5, 2): 0, (0, 2): 0})
        True
    """
    return coldict2mat(
        dict((k, find_error(c)) for (k, c) in mat2coldict(S).items()))


## Task 8
s = "I'm trying to free your mind, Neo. But I can only show you the door. You're the one that has to walk through it."
P = bits2mat(str2bits(s))

EP = P + noise(P, 0.02)
s_break = bits2str(mat2bits(EP))
# print(s_break)

## Task 9
C = G * P
bits_before = len(str2bits(s))
bits_after = len(mat2bits(C))
# print(bits_before, bits_after)

## Ungraded Task
CTILDE = C + noise(C, 0.04)

# print(bits2str(mat2bits(R * CTILDE)))


## Task 10
Exemplo n.º 5
0
                                  {(1, 3): 0, (3, 0): 0, (2, 1): 0, (6, 2): 0, (5, 1): one, (0, 3): 0, (4, 0): 0, (1, 2): 0, (3, 3): 0, (6, 3): 0, (5, 0): 0, (2, 2): 0, (4, 1): 0,
                                   (1, 1): 0, (3, 2): one, (0, 0): 0, (6, 0): 0, (2, 3): 0, (4, 2): 0, (1, 0): 0, (5, 3): 0, (0, 1): 0, (6, 1): 0, (3, 1): 0, (2, 0): 0,
                                   (4, 3): one, (5, 2): 0, (0, 2): 0}))

## Task 8
s = "I'm trying to free your mind, Neo. But I can only show you the door. You're the one that has to walk through it."

P = bits2mat(str2bits(s))
print(P)

## Task 9
C = G * P
print(C)
bits_before = len(str2bits(s))
print(bits_before)
bits_after = len(mat2bits(C))
print(bits_after
      )
## Ungraded Task
CTILDE = C + noise(C, 0.02)
print(CTILDE)


## Task 10
def correct(A):
    """
    Input: a matrix A each column of which differs from a codeword in at most one bit
    Output: a matrix whose columns are the corresponding valid codewords.
    Example:
        >>> A = Mat(({0,1,2,3,4,5,6}, {1,2,3}), {(0,3):one, (2, 1): one, (5, 2):one, (5,3):one, (0,2): one})
        >>> correct(A) == Mat(({0, 1, 2, 3, 4, 5, 6}, {1, 2, 3}), {(0, 1): 0, (1, 2): 0, (3, 2): 0, (1, 3): 0, (3, 3): 0, (5, 2): one, (6, 1): 0, (3, 1): 0, (2, 1): 0, (0, 2): one, (6, 3): one, (4, 2): 0, (6, 2): one, (2, 3): 0, (4, 3): 0, (2, 2): 0, (5, 1): 0, (0, 3): one, (4, 1): 0, (1, 1): 0, (5, 3): one})
Exemplo n.º 6
0
        >>> find_error_matrix(S) == Mat(({0, 1, 2, 3, 4, 5, 6}, {0, 1, 2, 3}), {(1, 3): 0, (3, 0): 0, (2, 1): 0, (6, 2): 0, (5, 1): one, (0, 3): 0, (4, 0): 0, (1, 2): 0, (3, 3): 0, (6, 3): 0, (5, 0): 0, (2, 2): 0, (4, 1): 0, (1, 1): 0, (3, 2): one, (0, 0): 0, (6, 0): 0, (2, 3): 0, (4, 2): 0, (1, 0): 0, (5, 3): 0, (0, 1): 0, (6, 1): 0, (3, 1): 0, (2, 0): 0, (4, 3): one, (5, 2): 0, (0, 2): 0})
        True
    """
    return coldict2mat(
        {k: find_error(mat2coldict(S)[k])
         for k in mat2coldict(S).keys()})


## Task 8
s = "I'm trying to free your mind, Neo. But I can only show you the door. You're the one that has to walk through it."
P = bits2mat(str2bits(s))

## Task 9
C = G * P
bits_before = len(str2bits(s))
bits_after = len(mat2bits(C))

## Ungraded Task
CTILDE = C + noise(C, 0.02)

#print(s)
#print(bits2str(mat2bits(CTILDE)))


## Task 10
def correct(A):
    """
    Input: a matrix A each column of which differs from a codeword in at most one bit
    Output: a matrix whose columns are the corresponding valid codewords.
    Example:
        >>> A = Mat(({0,1,2,3,4,5,6}, {1,2,3}), {(0,3):one, (2, 1): one, (5, 2):one, (5,3):one, (0,2): one})
Exemplo n.º 7
0
    Output: a matrix whose cth column is the error corresponding to the cth column of S.
    Example:
        >>> S = listlist2mat([[0,one,one,one],[0,one,0,0],[0,0,0,one]])
        >>> find_error_matrix(S) == Mat(({0, 1, 2, 3, 4, 5, 6}, {0, 1, 2, 3}), {(1, 3): 0, (3, 0): 0, (2, 1): 0, (6, 2): 0, (5, 1): one, (0, 3): 0, (4, 0): 0, (1, 2): 0, (3, 3): 0, (6, 3): 0, (5, 0): 0, (2, 2): 0, (4, 1): 0, (1, 1): 0, (3, 2): one, (0, 0): 0, (6, 0): 0, (2, 3): 0, (4, 2): 0, (1, 0): 0, (5, 3): 0, (0, 1): 0, (6, 1): 0, (3, 1): 0, (2, 0): 0, (4, 3): one, (5, 2): 0, (0, 2): 0})
        True
    """
    return coldict2mat({k: find_error(mat2coldict(S)[k]) for k in  mat2coldict(S).keys()}) 
    
## Task 8
s = "I'm trying to free your mind, Neo. But I can only show you the door. You're the one that has to walk through it."
P = bits2mat(str2bits(s)) 

## Task 9
C = G*P
bits_before =len(str2bits(s)) 
bits_after = len(mat2bits(C)) 


## Ungraded Task
CTILDE = C + noise(C, 0.02)
#print(s)
#print(bits2str(mat2bits(CTILDE)))


## Task 10
def correct(A):
    """
    Input: a matrix A each column of which differs from a codeword in at most one bit
    Output: a matrix whose columns are the corresponding valid codewords.
    Example:
        >>> A = Mat(({0,1,2,3,4,5,6}, {1,2,3}), {(0,3):one, (2, 1): one, (5, 2):one, (5,3):one, (0,2): one})
Exemplo n.º 8
0
    Output: a matrix whose cth column is the error corresponding to the cth column of S.
    Example:
        >>> S = listlist2mat([[0,one,one,one],[0,one,0,0],[0,0,0,one]])
        >>> find_error_matrix(S)
        Mat(({0, 1, 2, 3, 4, 5, 6}, {0, 1, 2, 3}), {(1, 2): 0, (3, 2): one, (0, 0): 0, (4, 3): one, (3, 0): 0, (6, 0): 0, (2, 1): 0, (6, 2): 0, (2, 3): 0, (5, 1): one, (4, 2): 0, (1, 0): 0, (0, 3): 0, (4, 0): 0, (0, 1): 0, (3, 3): 0, (4, 1): 0, (6, 1): 0, (3, 1): 0, (1, 1): 0, (6, 3): 0, (2, 0): 0, (5, 0): 0, (2, 2): 0, (1, 3): 0, (5, 3): 0, (5, 2): 0, (0, 2): 0})
    """
    return coldict2mat({i: find_error(he) for i, he in mat2coldict(S).items()})


## Task 6
s = "I'm trying to free your mind, Neo. But I can only show you the door. You’re the one that has to walk through it."
P = bits2mat(str2bits(s))

## Task 7
C = G * P
bits_before = len(mat2bits(P))
bits_after = len(mat2bits(C))

## Ungraded Task
CTILDE = C + noise(C, 0.02)


## Task 8
def correct(A):
    """
    Input: a matrix A each column of which differs from a codeword in at most one bit
    Output: a matrix whose columns are the corresponding valid codewords.
    Example:
        >>> A = Mat(({0,1,2,3,4,5,6}, {1,2,3}), {(0,3):one, (2, 1): one, (5, 2):one, (5,3):one, (0,2): one})
        >>> correct(A)
        Mat(({0, 1, 2, 3, 4, 5, 6}, {1, 2, 3}), {(0, 1): 0, (1, 2): 0, (3, 2): 0, (1, 3): 0, (3, 3): 0, (5, 2): one, (6, 1): 0, (3, 1): 0, (2, 1): 0, (0, 2): one, (6, 3): one, (4, 2): 0, (6, 2): one, (2, 3): 0, (4, 3): 0, (2, 2): 0, (5, 1): 0, (0, 3): one, (4, 1): 0, (1, 1): 0, (5, 3): one})
Exemplo n.º 9
0
    Input: a matrix S whose columns are error syndromes
    Output: a matrix whose cth column is the error corresponding to the cth column of S.
    Example:
        >>> S = listlist2mat([[0,one,one,one],[0,one,0,0],[0,0,0,one]])
        >>> find_error_matrix(S)
        Mat(({0, 1, 2, 3, 4, 5, 6}, {0, 1, 2, 3}), {(1, 2): 0, (3, 2): one, (0, 0): 0, (4, 3): one, (3, 0): 0, (6, 0): 0, (2, 1): 0, (6, 2): 0, (2, 3): 0, (5, 1): one, (4, 2): 0, (1, 0): 0, (0, 3): 0, (4, 0): 0, (0, 1): 0, (3, 3): 0, (4, 1): 0, (6, 1): 0, (3, 1): 0, (1, 1): 0, (6, 3): 0, (2, 0): 0, (5, 0): 0, (2, 2): 0, (1, 3): 0, (5, 3): 0, (5, 2): 0, (0, 2): 0})
    """
    return matutil.coldict2mat([find_error(v) for v in matutil.mat2coldict(S).values()])

## Task 6
s = "I'm trying to free your mind, Neo. But I can only show you the door. You’re the one that has to walk through it."
P = bitutil.bits2mat(bitutil.str2bits(s))

## Task 7
C = G*P
bits_before = len(bitutil.mat2bits(P))
bits_after = len(bitutil.mat2bits(C))


## Ungraded Task
CTILDE = None

## Task 8
def correct(A):
    """
    Input: a matrix A each column of which differs from a codeword in at most one bit
    Output: a matrix whose columns are the corresponding valid codewords.
    Example:
        >>> A = Mat(({0,1,2,3,4,5,6}, {1,2,3}), {(0,3):one, (2, 1): one, (5, 2):one, (5,3):one, (0,2): one})
        >>> correct(A)
        Mat(({0, 1, 2, 3, 4, 5, 6}, {1, 2, 3}), {(0, 1): 0, (1, 2): 0, (3, 2): 0, (1, 3): 0, (3, 3): 0, (5, 2): one, (6, 1): 0, (3, 1): 0, (2, 1): 0, (0, 2): one, (6, 3): one, (4, 2): 0, (6, 2): one, (2, 3): 0, (4, 3): 0, (2, 2): 0, (5, 1): 0, (0, 3): one, (4, 1): 0, (1, 1): 0, (5, 3): one})
Exemplo n.º 10
0
        e = find_error(S[i])  # S[i] returns i-th column of S
        for j in res.D[0]:
            res[j, i] = e[j]

    return res


## Task 8
s = "I'm trying to free your mind, Neo. But I can only show you the door. You're the one that has to walk through it."
P = bits2mat(str2bits(s))  # p[i] column is 4-bit nibble

## Ungraded task
# Use noise(A, s) to produce the transmitted message
# input: P - matrix being transmitted
print("Message w/o encoding and ecc:",
      bits2str(mat2bits(P + noise(P, 0.02))).encode("utf-8"))

## Task 9
C = G * P  # encode
bits_before = len(str2bits(s))  # bits before encoding
bits_after = bits_before // 4 * 7  # bits after encoding
print(bits_before, bits_after)

## Ungraded Task
CTILDE = C + noise(C, 0.02)  # encoded message received
print("Message with encoding, but w/o ecc:",
      bits2str(mat2bits(R * CTILDE)).encode("utf-8"))


## Task 10
def correct(A):
Exemplo n.º 11
0
    Example:
        >>> S = listlist2mat([[0,one,one,one],[0,one,0,0],[0,0,0,one]])
        >>> find_error_matrix(S)
        Mat(({0, 1, 2, 3, 4, 5, 6}, {0, 1, 2, 3}), {(1, 2): 0, (3, 2): one, (0, 0): 0, (4, 3): one, (3, 0): 0, (6, 0): 0, (2, 1): 0, (6, 2): 0, (2, 3): 0, (5, 1): one, (4, 2): 0, (1, 0): 0, (0, 3): 0, (4, 0): 0, (0, 1): 0, (3, 3): 0, (4, 1): 0, (6, 1): 0, (3, 1): 0, (1, 1): 0, (6, 3): 0, (2, 0): 0, (5, 0): 0, (2, 2): 0, (1, 3): 0, (5, 3): 0, (5, 2): 0, (0, 2): 0})
    """
    return coldict2mat ({key:find_error(val) for key, val in mat2coldict(S).items()})


## Task 6
s = "I'm trying to free your mind, Neo. But I can only show you the door. You’re the one that has to walk through it."
P = butl.bits2mat(butl.str2bits(s))

## Task 7
C = G*P
bits_before = len(butl.str2bits(s))
bits_after = len(butl.mat2bits(C))


## Ungraded Task
CTILDE = None

## Task 8
def correct(A):
    """
    Input: a matrix A each column of which differs from a codeword in at most one bit
    Output: a matrix whose columns are the corresponding valid codewords.
    Example:
        >>> A = Mat(({0,1,2,3,4,5,6}, {1,2,3}), {(0,3):one, (2, 1): one, (5, 2):one, (5,3):one, (0,2): one})
        >>> correct(A)
        Mat(({0, 1, 2, 3, 4, 5, 6}, {1, 2, 3}), {(0, 1): 0, (1, 2): 0, (3, 2): 0, (1, 3): 0, (3, 3): 0, (5, 2): one, (6, 1): 0, (3, 1): 0, (2, 1): 0, (0, 2): one, (6, 3): one, (4, 2): 0, (6, 2): one, (2, 3): 0, (4, 3): 0, (2, 2): 0, (5, 1): 0, (0, 3): one, (4, 1): 0, (1, 1): 0, (5, 3): one})
    """
Exemplo n.º 12
0
def decode_string_matrix(A):
    return bits2str(mat2bits(A))
Exemplo n.º 13
0
    Output: a matrix whose cth column is the error corresponding to the cth column of S.
    Example:
        >>> S = listlist2mat([[0,one,one,one],[0,one,0,0],[0,0,0,one]])
        >>> find_error_matrix(S)
        Mat(({0, 1, 2, 3, 4, 5, 6}, {0, 1, 2, 3}), {(1, 2): 0, (3, 2): one, (0, 0): 0, (4, 3): one, (3, 0): 0, (6, 0): 0, (2, 1): 0, (6, 2): 0, (2, 3): 0, (5, 1): one, (4, 2): 0, (1, 0): 0, (0, 3): 0, (4, 0): 0, (0, 1): 0, (3, 3): 0, (4, 1): 0, (6, 1): 0, (3, 1): 0, (1, 1): 0, (6, 3): 0, (2, 0): 0, (5, 0): 0, (2, 2): 0, (1, 3): 0, (5, 3): 0, (5, 2): 0, (0, 2): 0})
    """
    return coldict2mat({k: find_error(v) for k, v in mat2coldict(S).items()})


## Task 6
s = "I'm trying to free your mind, Neo. But I can only show you the door. You’re the one that has to walk through it."
P = bits2mat(str2bits(s))

## Task 7
C = G * P
bits_before = len(mat2bits(P))
bits_after = len(mat2bits(C))

## Ungraded Task
CTILDE = None


## Task 8
def correct(A):
    """
    Input: a matrix A each column of which differs from a codeword in at most one bit
    Output: a matrix whose columns are the corresponding valid codewords.
    Example:
        >>> A = Mat(({0,1,2,3,4,5,6}, {1,2,3}), {(0,3):one, (2, 1): one, (5, 2):one, (5,3):one, (0,2): one})
        >>> correct(A)
        Mat(({0, 1, 2, 3, 4, 5, 6}, {1, 2, 3}), {(0, 1): 0, (1, 2): 0, (3, 2): 0, (1, 3): 0, (3, 3): 0, (5, 2): one, (6, 1): 0, (3, 1): 0, (2, 1): 0, (0, 2): one, (6, 3): one, (4, 2): 0, (6, 2): one, (2, 3): 0, (4, 3): 0, (2, 2): 0, (5, 1): 0, (0, 3): one, (4, 1): 0, (1, 1): 0, (5, 3): one})
def find_error_matrix(s):
    return coldict2mat({pos: find_error(vec) for pos, vec in mat2coldict(s).items()})

test_matrix = listlist2mat([[One(), 0], [One(), 0], [One(), One()]])

# Task 4.14.8
s = ''.join([chr(i) for i in range(256)])
assert bits2str(str2bits(s)) == s

# Task 4.14.9 & 4.14.10 encode message string to matrix of nibbles
msg = "I'm trying to free your mind, Neo."
P = bits2mat(str2bits(msg))

# Task 4.14.11 add noise to message
E = noise(P, 0.02)
EP = E + P
perturbed_msg = bits2str(mat2bits(EP))

# Task 4.14.12
C = G*P

# Task 4.14.13
CE = noise(C, 0.02)
CTILDE = C + CE
perturbed_msg2 = bits2str(mat2bits(R*CTILDE))

# Task 4.13.13
def correct(A):
    return R * (find_error_matrix(H*A) + A)