0

I would like to know if I can speedup this code using numpy...

The code is actually running but I know it's possible to do better with np.where, which I've tried but without success :)

For each syn position I want to compare the string on first positions ('000','001'...) with the variable syndrome (casted to string) and get the int on the second position when match

Like if I have a syndrome '100' I will get the 4 so I know I've to flip the 4th bit in a 8 bit codeword

def recover_data(noisy_data):

syn=[['000','none'],['001',6],['010',5],['011',3],['100',4],['101',0],['110',1],['111',2]]

for ix in range(noisy_data.shape[0]):
    unflip=0    #index that will be flipped

    for jx in range(len(syn)):
        if(syn[jx][0] == ''.join(syndrome.astype('str'))):
            unflip = syn[jx][1]
    if(str(unflip)!='none'):
        noisy_data[ix,unflip]=1-noisy_data[ix,unflip]
  • Could you explain `syndrome` and why `join` it in every iteration instead of doing so once? Since you need to lookup the first filed of `syn`, would you conside using a `dict`? – Blownhither Ma Dec 23 '17 at 06:34
  • A `pandas` dataframe could also give you fast joins if your two sources of data are separated. – adrpino Dec 23 '17 at 11:46
  • @BlownhitherMa This is a simulation of block correction code, specifically in this case Hamming 7,4. You have 4 bits codewords that you want to send in a noisy channel. You add 3 bits redundancy calculated from the 4 original bits then each word will be sent as a 7 bits codeword. Then to simulate noisy channel I just flip a random bit, and in the receptor calculate the syndrome for each 7 bits which tells me the bit that got flipped so I have to unflip it. As simple as that, all code is working but I was trying to find a way to do a faster search the syndrome on a structure! Thank you! – Ricardo Moita Dec 26 '17 at 18:21
  • @adrpino what do you mean with the two sources separated? – Ricardo Moita Dec 26 '17 at 18:27

1 Answers1

0

looks like a dictionary would help

syn=dict([['000','none'],['001',6],['010',5],['011',3],['100',4],['101',0],['110',1],['111',2]])

syn

{'000': 'none',
 '001': 6,
 '010': 5,
 '011': 3,
 '100': 4,
 '101': 0,
 '110': 1,
 '111': 2}

syn.get('011')  # .get(key) will return None if the key isn't in the dict

3
f5r5e5d
  • 3,656
  • 3
  • 14
  • 18