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

Binary to Gray code conversion for PSK and PAM

Posted By Krishna Sankar On May 11, 2008 @ 1:12 pm In Modulation | 14 Comments

In this post, let us try to understand Gray codes and their usage in digital communication. Quoting from Wiki entry on Gray code [Gray-Wiki [1]],

The reflected binary code, also known as Gray code after Frank Gray [2], is a binary numeral system [3] where two successive values differ in only one digit.

In a digital communication system, if the constellation symbols are Gray encoded, then the bit pattern representing the adjacent constellation symbols differ by only one bit. We will show in another post that having this encoding structure gives a lesser probability of error than the ‘natural binary ordering’. However, in this post, let us try to figure out the conversion of natural binary representation to Gray code.

## Conversion from natural Binary to Gray code

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

1. For $j=n-1$,

$g[n-1] = b[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$,

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

## Simulation

Simple Matlab/Octave code for doing the binary to Gray code conversion

```clear; ip = [0:15]; % decimal equivalent of a four bit binary word op = bitxor(ip,floor(ip/2)); % decimal equivalent of the equivalent four bit gray word```

## Table : Natural Binary to Gray code

 Input, decimal Input, binary Gray, decimal Gray, Binary 0 0000 0 0000 1 0001 1 0001 2 0010 3 0011 3 0011 2 0010 4 0100 6 0110 5 0101 7 0111 6 0110 5 0101 7 0111 4 0100 8 1000 12 1100 9 1001 13 1101 10 1010 15 1111 11 1011 14 1110 12 1100 10 1010 13 1101 11 1011 14 1110 9 1001 15 1111 8 1000

## Note

1. As can be seen from the Table above, each row differs from the row above and below by only one bit. Further, just to highlight that this behavior is indeed true for 16th row [1000] and the 1st row [0000] .

2. The conversion shown in the Table above can be used for general modulation schemes like M-PSK (Phase Shift Keying), M-PAM (Pulse Amplitude Modulation) etc.

3. However, for a general M-QAM modulation the binary to Gray code conversion is bit more complicated (and I need to figure that out). We will discuss the QAM case in a future post.

Thanks,
Krishna

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

URLs in this post:

[1] Gray-Wiki: http://en.wikipedia.org/wiki/Gray_code

[2] Frank Gray: http://en.wikipedia.org/wiki/Frank_Gray_%28researcher%29

[3] binary numeral system: http://en.wikipedia.org/wiki/Binary_numeral_system

[4] click here to SUBSCRIBE : http://www.feedburner.com/fb/a/emailverifySubmit?feedId=1348583&loc=en_US