- DSP log - http://www.dsplog.com -

Gray code to Binary conversion for PSK and PAM

Posted By Krishna Sankar On May 12, 2008 @ 5:47 am In Modulation | 18 Comments

Given that we have discussed Binary to Gray code conversion [1], let us discuss the Gray to BInary conversion.

## Conversion from Gray code to natural Binary

Let $g[n-1:0]$ be the equivalent Gray code for an $n$ bit binary number $b[n-1:0]$ with $j$ representing the index of the bit.

1. For $j=n-1$,

$b[n-1] = g[n-1]$ i.e, the most significant bit (MSB) of the Gray code is same as the MSB of original binary number.

2. For $j=n-2\mbox{ to }0$,

$b[j]=b[j+1]\oplus g[j]$ i.e, $j^{th}$ bit of the Binary number is the exclusive-OR (XOR) of ${j}^{th}$ of the bit of the Gray code and ${j+1}^{th}$ of the bit of the binary number.

## Simulation model

Look Up Table based Matlab/Octave mode for Gray to Binary conversion
``` clear % binary to gray code conversion ipBin = [0:15] ; % decimal equivalent for a 4-bit binary opGray = bitxor(ipBin,floor(ipBin/2)) % Gray to Binary conversion [tt ind] = sort(opGray); % sorting Gray code elements to form the lookup table opBin = ind(opGray+1)-1; % picking elements from the array```

The Trick from DSP-Guru to do Gray to Binary [2] conversion [2], thanks to Jerry Avins [3].
``` clear % binary to Gray code ipBin = [0:15] opGray = bitxor(ipBin,floor(ipBin/2)) % Gray code to binary opBin = bitxor(opGray,floor(opGray/2^8)) ; opBin = bitxor(opBin,floor(opBin/2^4)) ; opBin = bitxor(opBin,floor(opBin/2^2)) ; opBin = bitxor(opBin,floor(opBin/2^1)) ;```

## Concluding thoughts

1. As on now, I do not understand how the DSP Guru trick [2] for Gray code to Binary works. Of course, if I figure out, I will update this post.

2. Having discussed Binary to Gray code conversion and Gray to Binary conversion, we are now armed to discuss the bit-error rate probabilities for various modulation schemes (Recall: We have been discussing symbol error rate in Additive White Gaussian noise till date).

Thanks,
Krishna

URL to article: http://www.dsplog.com/2008/05/12/gray-code-to-binary-conversion-for-psk-pam/

URLs in this post:

[1] Binary to Gray code conversion: http://www.dsplog.com/2008/05/11/binary-to-gray-code-conversion-psk-pam/

[2] DSP-Guru to do Gray to Binary: http://www.dspguru.com/comp.dsp/tricks/alg/grayconv.htm