4 Replies - 1286 Views - Last Post: 30 November 2012 - 04:40 PM Rate Topic: -----

#1 fledgeling  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 10-October 12

Hamming coding

Posted 30 November 2012 - 12:16 PM

So what i wanna do here is to take as an input a phrase like 0101 and use hamming code in it
what i have done so far is supposed to difine the parity bits positions but its not working well :/

can you help me fix it and give me a guidline of what to do next ?
here is the code

def hamming(k):
    
    lk=len(k)
    i=0
    
    while True :
        if lk>2**i:
            
            
                k=k[:2**i-1]+'p'+k[2**i:]
                lk=lk+1
                i=i+1
                
        else:
            break
    

    return k




Is This A Good Question/Topic? 0
  • +

Replies To: Hamming coding

#2 Hqtitan  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 51
  • Joined: 28-August 10

Re: Hamming coding

Posted 30 November 2012 - 02:24 PM

I'm not sure what you're trying to do, but your while loop could be made a bit more efficient.

The way your while loop is set up, you've created an infinite loop. Inside of this loop, you then check whether a statement is true, and then run some code if it is. If the statement is false, you force the loop to break.

Since the statement that you're checking with the if will always evaluate to True or False, wouldn't it be better to check that as the condition to enter the loop? This way, instead of checking whether a statement is true or false within the loop and then breaking out of the loop if it is false, you tell the while loop that if the statement is false, it doesn't need to run another time.

The code for the loopwould look something like this:

while(lk>2**i):
    k=k[:2**i-1]+'p'+k[2**i:]
    lk=lk+1
    i=i+1


With this, as soon as the condition lk>2**i becomes false, you're out of the loop.
Was This Post Helpful? 0
  • +
  • -

#3 fledgeling  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 10-October 12

Re: Hamming coding

Posted 30 November 2012 - 03:25 PM

View PostHqtitan, on 30 November 2012 - 02:24 PM, said:

I'm not sure what you're trying to do, but your while loop could be made a bit more efficient.

The way your while loop is set up, you've created an infinite loop. Inside of this loop, you then check whether a statement is true, and then run some code if it is. If the statement is false, you force the loop to break.

Since the statement that you're checking with the if will always evaluate to True or False, wouldn't it be better to check that as the condition to enter the loop? This way, instead of checking whether a statement is true or false within the loop and then breaking out of the loop if it is false, you tell the while loop that if the statement is false, it doesn't need to run another time.

The code for the loopwould look something like this:

while(lk>2**i):
    k=k[:2**i-1]+'p'+k[2**i:]
    lk=lk+1
    i=i+1


With this, as soon as the condition lk>2**i becomes false, you're out of the loop.

thanks but it still doest put the parities in the right place
Was This Post Helpful? 0
  • +
  • -

#4 Tayacan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 275
  • Joined: 18-January 11

Re: Hamming coding

Posted 30 November 2012 - 03:58 PM

Right, so I'll assume that you're talking about Hamming(7,4) (encoding four bits of data into seven bits). Right now, you're doing tricky math in a while loop, which is hard to read. You can do it much simpler:
  • Split the string into chars, so you have each individual bit.
  • Figure out the parity bits (p1,p2,p3)
  • Put it together into a string and return it.


So, how do we split a string into chars? Well, in this case, you know that you have exactly four chars, so you can do it with a tuple pattern:
d1,d2,d3,d4 = "0101"


That will make d1 = '0', d2 = '1' and so on.

How do we figure out the parity bits? Hopefully you know this part, since you're trying to implement it - for the rest of the world:

We have three parity bits, p1, p2 and p3. They each cover three of the original bits, so there's some overlap. For example, p1 covers d1, d2 and d4. That means that p1 = (d1 + d2 + d4) % 2. If you want to know more, go read about it, it's pretty interesting (I did this for a high school math project once).

Since the bits are represented as strings, you will have to convert back and forth with int() and str(), but that shouldn't be a problem.

So, now we know our three parity bits, and we do of course know the original four bits, so all that's left is to put them together. If you've been clever and converted everything into strings, you can of course simply use + to concatenate them together.
p1+p2+d1+p3+d2+d3+d4

Was This Post Helpful? 2
  • +
  • -

#5 fledgeling  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 10-October 12

Re: Hamming coding

Posted 30 November 2012 - 04:40 PM

View PostTayacan, on 30 November 2012 - 03:58 PM, said:

Right, so I'll assume that you're talking about Hamming(7,4) (encoding four bits of data into seven bits). Right now, you're doing tricky math in a while loop, which is hard to read. You can do it much simpler:
  • Split the string into chars, so you have each individual bit.
  • Figure out the parity bits (p1,p2,p3)
  • Put it together into a string and return it.


So, how do we split a string into chars? Well, in this case, you know that you have exactly four chars, so you can do it with a tuple pattern:
d1,d2,d3,d4 = "0101"


That will make d1 = '0', d2 = '1' and so on.

How do we figure out the parity bits? Hopefully you know this part, since you're trying to implement it - for the rest of the world:

We have three parity bits, p1, p2 and p3. They each cover three of the original bits, so there's some overlap. For example, p1 covers d1, d2 and d4. That means that p1 = (d1 + d2 + d4) % 2. If you want to know more, go read about it, it's pretty interesting (I did this for a high school math project once).

Since the bits are represented as strings, you will have to convert back and forth with int() and str(), but that shouldn't be a problem.

So, now we know our three parity bits, and we do of course know the original four bits, so all that's left is to put them together. If you've been clever and converted everything into strings, you can of course simply use + to concatenate them together.
p1+p2+d1+p3+d2+d3+d4



I love the way you explain things ^^ Anyway i want this function to be able to turn into hamming a phrase of no matter how many bits for example to make this in hamming (01010101) and so on ... Can you help me ?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1