def test_error_correction(arrEncoded, nErrorBits): # data1 arrEncoded_1 = arrEncoded arrEncoded_bin_1 = ct.toBinaryArray(arrEncoded_1) # data2 arrEncoded_bin_2 = np.copy(arrEncoded_bin_1) # introduce some errors arrErrorPos = np.random.choice(range(len(arrEncoded_bin_2)), nErrorBits, replace=False) for i in arrErrorPos: arrEncoded_bin_2[i] = (arrEncoded_bin_2[i] + 1)%2 # error correction n = 7 k = 4 lsh = LSHash(k, n) arrDecoded_bin_1 = lshDecode(ct.zeroPadding(arrEncoded_bin_1, n)[0], lsh, n, k) arrDecoded_bin_2 = lshDecode(ct.zeroPadding(arrEncoded_bin_2, n)[0], lsh, n, k) # BER nErrorBits, dBER = ct.computeBER(arrEncoded_bin_1, arrEncoded_bin_2) nErrorBits_ec, dBER_ec = ct.computeBER(arrDecoded_bin_1, arrDecoded_bin_2) return nErrorBits, dBER, nErrorBits_ec, dBER_ec
def test_error_correction(eng, arrEncoded, nErrorBits, n=7, k=4, strCoder="hamming/binary"): # data1 arrEncoded_1 = arrEncoded arrEncoded_bin_1 = ct.toBinaryArray(arrEncoded_1, nBitBlock=n) print "data1(%d): " % len(arrEncoded_bin_1), arrEncoded_bin_1 # data2 arrEncoded_bin_2 = np.copy(arrEncoded_bin_1) # introduce some errors arrErrorPos = np.random.choice(range(len(arrEncoded_bin_2)), nErrorBits, replace=False) for i in arrErrorPos: arrEncoded_bin_2[i] = (arrEncoded_bin_2[i] + 1) % 2 print "data2:(%d) " % len(arrEncoded_bin_1), arrEncoded_bin_2 # decode print ("coder: %s" % strCoder) arrDecoded_bin_1 = decode(eng, arrEncoded_bin_1, n, k, strCoder) arrDecoded_bin_2 = decode(eng, arrEncoded_bin_2, n, k, strCoder) print "decoded1(%d): " % len(arrDecoded_bin_1), arrDecoded_bin_1 print "decoded2(%d): " % len(arrDecoded_bin_2), arrDecoded_bin_2 # BER nCount = 0 for i in xrange(len(arrDecoded_bin_1)): if arrDecoded_bin_1[i] != arrDecoded_bin_2[i]: nCount += 1 dBER = nCount * 1.0 / len(arrDecoded_bin_1) print "BER: ", dBER
def test(eng, arrMsg, nErrorBits, n=7, k=4, strCoder="hamming/binary"): arrMsg_bin = ct.toBinaryArray(arrMsg, nBitBlock=n) # encode arrEncoded_bin = encode(eng, arrMsg_bin, n, k, strCoder) # introduce some errors arrErrorPos = np.random.choice(range(len(arrEncoded_bin)), nErrorBits, replace=False) for i in arrErrorPos: arrEncoded_bin[i] = (arrEncoded_bin[i] + 1) % 2 # decode arrDecoded_bin = decode(eng, arrEncoded_bin, n, k, strCoder) print "message(%d): " % len(arrMsg_bin), arrMsg_bin print "encoded(%d): " % len(arrEncoded_bin), arrEncoded_bin print "decoded(%d): " % len(arrDecoded_bin), arrDecoded_bin # BER nCount = 0 for i in xrange(len(arrMsg_bin)): if arrDecoded_bin[i] != arrMsg_bin[i]: nCount += 1 dBER = nCount * 1.0 / len(arrMsg_bin) print "BER: ", dBER
def test_random_secret(matlabEng, n, k, nErrorBits=1): # data 1 arrData_1 = np.random.randint(0, 2, n) arrData_bin_1 = ct.toBinaryArray(arrData_1) print "d1 (%d): "%len(arrData_bin_1), arrData_bin_1 # data2 arrData_bin_2 = np.copy(arrData_bin_1) # introduce some errors arrErrorPos = np.random.choice(range(len(arrData_bin_2)), nErrorBits, replace=False) for i in arrErrorPos: arrData_bin_2[i] = (arrData_bin_2[i] + 1)%2 print "d2 (%d): "%len(arrData_bin_1), arrData_bin_2 # create secret at random arrSecret = np.random.randint(0, 2, k) arrSecret_bin = ct.toBinaryArray(arrSecret) arrCodeword_bin = fec.encode(matlabEng, arrSecret_bin, n, k, 'hamming/binary') # compute the difference arrDelta = np.bitwise_xor(arrData_bin_1, arrCodeword_bin) # deduce securet arrCodeword_bin_2 = np.bitwise_xor(arrData_bin_2, arrDelta) arrSecret_bin_2 = fec.decode(matlabEng, arrCodeword_bin_2, n, k, 'hamming/binary') # compute the BER nMismatchedBits = np.size(arrSecret_bin) - \ np.sum(arrSecret_bin==arrSecret_bin_2) print "error bit=", nMismatchedBits, ", BER=",\ nMismatchedBits*1.0/np.size(arrSecret_bin)
def test_reconciliation(nTrials, matlabEng, nKeySize, nErrorBits=1, n=7, k=4, m=3, strCoder='hamming/binary'): """ test the reconciliation method mentioned in 'proximate, mobisys'11' """ lsStat = [] for i in xrange(nTrials): # data 1 arrData_1 = np.random.randint(0, 2, nKeySize) arrData_bin_1 = ct.toBinaryArray(arrData_1) # data2 arrData_bin_2 = np.copy(arrData_bin_1) # introduce some errors arrErrorPos = np.random.choice(range(len(arrData_bin_2)), nErrorBits, replace=False) for i in arrErrorPos: arrData_bin_2[i] = (arrData_bin_2[i] + 1)%2 # padding nPaddingSize = m*n if strCoder == CODER_RS else n arrData_bin_1 = ct.zeroPadding(arrData_bin_1, nPaddingSize)[0] arrData_bin_2 = ct.zeroPadding(arrData_bin_2, nPaddingSize)[0] # compute the difference arrDelta = computeDelta(matlabEng, arrData_bin_1, n, k, m, strCoder) # reconciliation arrDeducedData_bin_1 = reconciliate(matlabEng, arrDelta, arrData_bin_2, n, k, m, strCoder) # compute the BER nMismatchedBits = np.size(arrData_bin_1) - \ np.sum(arrData_bin_1==arrDeducedData_bin_1) dBER = nMismatchedBits*1.0/np.size(arrData_bin_1) lsStat.append({'err_bits': nMismatchedBits, 'BER': dBER}) if (nMismatchedBits != 0): print nMismatchedBits, arrErrorPos # print "error bit=", nMismatchedBits, ", BER=", dBER dAvgErrBits = np.mean([i['err_bits'] for i in lsStat ]) dAvgBER = np.mean([i['BER'] for i in lsStat ]) print "----\n AvgErrorBits=%.3f, BER=%.3f" % (dAvgErrBits, dAvgBER)
def sourceEncode(lsData_rectified, nSCWndSize, nNeighborWnd=3): """ encoding data Parameters: ---------- lsData_rectified: list of rectified EMG data nSCWndSize: source coding window size (in points) nNeighborWnd: searching area of nearby neighbors Returns: ------- lsSourceCode: list of source code lists lsSourceCode_bin: list of source codes in binary lsSourceShape: list of source approximated shape arrays """ lsSourceCode = [] lsSourceCode_bin = [] lsSourceShape = [] for i, arrData in enumerate(lsData_rectified): lsDataSrcCode, arrDataShape = encoder.shapeEncoding(arrData, nSCWndSize, nNeighborWnd) arrDataSrcCode = np.array(lsDataSrcCode) lsSourceCode.append(arrDataSrcCode) arrDataSrcCode_bin = ct.toBinaryArray(arrDataSrcCode, 2) lsSourceCode_bin.append(arrDataSrcCode_bin) lsSourceShape.append(arrDataShape) return lsSourceCode, lsSourceCode_bin, lsSourceShape