Example #1
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))
Example #2
0
def find_error_matrix(S):
    """
    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, 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
    """
    coldict_s = mat2coldict(S)
    return coldict2mat({col: find_error(coldict_s[col]) for col in coldict_s})


## 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(P.D[0]) * len(P.D[1])
bits_after = len(C.D[0]) * len(C.D[1])

## Ungraded Task
CTILDE = None


## 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 #3
0
        True
    """

    return coldict2mat({k: find_error(mat2coldict(S)[k]) for k in mat2coldict(S).keys()})


S = listlist2mat([[0, one, one, one], [0, one, 0, 0], [0, 0, 0, one]])
print(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}))

## 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)

Example #4
0
## Task 7
def find_error_matrix(S):
    """
    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, 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({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))
Example #5
0
## Task 7
def find_error_matrix(S):
    """
    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, 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):
Example #6
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})
    """
    sCols = mat2coldict(S)
    return coldict2mat({k: find_error(sCols[k]) for k in sCols})


## print(find_error_matrix(listlist2mat([[0,one,one,one],[0,one,0,0],[0,0,0,one]])))

## 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))

##print(P)

n = bitutil.noise(P, 0.03)

##print(bitutil.bits2str(bitutil.mat2bits(n+P)))

## Task 7
C = G * P
bits_before = len(P.D[0]) * len(P.D[1])
bits_after = len(C.D[0]) * len(C.D[1])

##print(C)

## Ungraded Task
Example #7
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, 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({
        col_label: find_error(col_vec)
        for col_label, col_vec 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))

## Task 9
C = G * P
bits_before = len(str2bits(s))
bits_after = len(str2bits(s)) * 7 / 4

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


## 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 #8
0
def string_matrix(words, nvectors):
    return bits2mat(str2bits(words), nvectors)
Example #9
0
def str2matrix(text):
    """
    Computes a matrix over GF2 which represents the string passed in.
    """
    return bitutil.bits2matrix(bitutil.str2bits(text))
Example #10
0
        >>> 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
    """
    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
Example #11
0
## Task 7
def find_error_matrix(S):
    """
    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, 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({x:find_error(col) for x,col 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))


## Task 9
C = G*P
bits_before = len(str2bits(s))
bits_after = len(str2bits(s))*7/4


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


## Task 10
def correct(A):
    """
Example #12
0
## Task 5
def find_error_matrix(S):
    """
    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 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 #13
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})
    """
    cols={col:Vec(S.D[0], {row:S[row,col] for row in S.D[0]}) for col in S.D[1]}
    return coldict2mat({x:find_error(y) for (x,y) in cols.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."

c=bitutil.str2bits(s)
#print(bitutil.bits2str(c))
P = bitutil.bits2mat(c,4,False)
E=noise(P,0.02)


## Task 7
C = G*P

bits_before = 896
bits_after = 1568


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

#print(find_error_matrix(H))
#print(find_error_matrix(listlist2mat([[one, zero], [one, zero], [one, one]])))

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

#Task 4.14.9
#print(mat2bits(bits2mat(str2bits(s))) == str2bits(s)) #True

#Task 4.14.10
neo_str = "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."
#neo_str = 'a'
P = bits2mat(str2bits(neo_str))

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

#Task 4.14.12
C = G * P
CTILDE = C + noise(C, 0.02)

#Task 4.14.13
#print(bits2str(mat2bits(G_R * CTILDE)))


#Task 4.14.14
def correct(A):
    return A + find_error_matrix(A)
Example #15
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})
    """
    sCols = mat2coldict(S)
    return coldict2mat({k:find_error(sCols[k]) for k in sCols})

## print(find_error_matrix(listlist2mat([[0,one,one,one],[0,one,0,0],[0,0,0,one]])))

## 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))

##print(P)

n = bitutil.noise(P, 0.03)

##print(bitutil.bits2str(bitutil.mat2bits(n+P)))

## Task 7
C = G*P
bits_before = len(P.D[0])*len(P.D[1])
bits_after = len(C.D[0])*len(C.D[1])

##print(C)

## Ungraded Task
Example #16
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, 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
    """
    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 4.14.6 Derive original message from [1, 0, 1, 1, 0, 1, 1]
non_c = list2vec([One(), 0, One(), One(), 0, One(), One()])
syndrome = H*non_c
e = find_error(syndrome)
codeword = e + non_c
message = R * codeword

# Task 4.14.7
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)