     (6 votes, average: 3.17 out of 5) Loading ...

# Gray code to Binary conversion for PSK and PAM

by on May 12, 2008

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

## Conversion from Gray code to natural Binary

Let be the equivalent Gray code for an bit binary number with representing the index of the bit.

1. For ,

i.e, the most significant bit (MSB) of the Gray code is same as the MSB of original binary number.

2. For ,

i.e, bit of the Binary number is the exclusive-OR (XOR) of of the bit of the Gray code and 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 conversion, thanks to Jerry Avins.
``` 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 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

D id you like this article? Make sure that you do not miss a new article by subscribing to RSS feed OR subscribing to e-mail newsletter. Note: Subscribing via e-mail entitles you to download the free e-Book on BER of BPSK/QPSK/16QAM/16PSK in AWGN. M.Ramya April 12, 2012 at 7:42 pm

how can we convert decimal to 2421 code Krishna Sankar April 16, 2012 at 5:23 am

@M.Ramya: I have not tried it, but can the following link be of help to you?
http://mathforum.org/library/drmath/view/67281.html shanu May 19, 2011 at 11:17 am

i want a c program for converting a digital signal into binary format??????? pls help me Krishna Sankar May 23, 2011 at 2:34 am

@shanu: you might want to check dec2bin() function in matlab sneha September 1, 2010 at 10:44 am

i want conversion of gray code to binary code using kmaps???????if this is thereplz tel me???not there den tel me d reason Krishna Sankar September 5, 2010 at 6:26 am

@sneha: What is kmaps? Bob July 30, 2010 at 2:12 am

Since you ask how it works, here is an explanation:

Notation: A single letter represents an 8 bit value
A single letter followed by a digit represents one bit in that 8 bit value.
Bits are numbered from right to left, so bit n has the value 2^n.

b=b7;b6;b5;b4;b3;b2;b1;b0

Compute its gray code, g = b ^ ( b >> 1 )
g=g7;g6;g5;g4;g3;g2;g1;g0
 g7=b7; g6=b7^b6; g5=b6^b5; g4=b5^b4; g3=b4^b3; g2=b3^b2; g1=b2^b1; g0=b1^b0

Now recover the original binary value from g in three steps
 x = g ^ (g >> 4)
x=x7;x6;x5;x4;x3;x2;x1;x0
x7=g7; x6=g6; x5=g5; x4=g4 x3=g3^g7 x2=g2^g6 x1=g1^g5; x0=g0^g4

 y = x ^ (x >> 2)
y=y7;y6;y5;y4;y3;y2;y1;y0
y7=x7; y6=x6; y5=x5^x7; y4=x4^x6; y3=x3^x5; y2=x2^x4; y1=x1^x3; y0=x0^x2

substitute eqn  into eqn 
[2a] y7=g7; y6=g6; y5=g5^g7; y4=g4^g6; y3=g3^g7^g5; y2=g2^g6^g4; y1=g1^g5^g3^g7; y0=g0^g4^g2^g6

 z = y ^ (y >> 1)
z7=y7; z6=y6^y7; z5=y5^y6; z4=y4^y5; z3=y3^y4; z2=y2^y3; z1=y1^y2; z0=y0^y1

Substitute eqn [2a] into eqn 
[3a] z7=g7; z6=g6^g7; z5=g5^g7^g6; z4=g4^g6^g5^g7; z3=g3^g7^g5^g4^g6; z2=g2^g6^g4^g3^g7^g5; z1=g1^g5^g3^g7^g2^g6^g4; z0=g0^g4^g2^g6^g1^g5^g3^g7

Substitute eqn  into eqn [3a]
[3b] z7=b7;
z6=b7^b6^b7;
z5=b6^b5^b7^b7^b6;
z4=b5^b4^b7^b6^b6^b5^b7;
z3=b4^b3^b7^b6^b5^b5^b4^b7^b6;
z2=b3^b2^b7^b6^b5^b4^b4^b3^b7^b6^b5;
z1=b2^b1^b6^b5^b4^b3^b7^b3^b2^b7^b6^b5^b4;
z0=b1^b0^b5^b4^b3^b2^b7^b6^b2^b1^b6^b5^b4^b3^b7

Rearrange the bits on the right
[3c] z7=b7;
z6=b6^b7^b7;
z5=b5^b6^b6^b7^b7;
z4=b4^b5^b5^b6^b6^b7^b7;
z3=b3^b4^b4^b5^b5^b6^b6^b7^b7;
z2=b2^b3^b3^b4^b4^b5^b5^b6^b6^b7^b7;
z1=b1^b3^b3^b2^b2^b4^b4^b5^b5^b6^b6^b7^b7;
z0=b0^b1^b1^b2^b2^b3^b3^b4^b4^b5^b5^b6^b6^b7^b7

Now, because x^x=0 and y^0=y, we can just remove each identical pair
[3c] z7=b7;
z6=b6;
z5=b5;
z4=b4;
z3=b3;
z2=b2;
z1=b1;
z0=b0;

And that’s how it works. Krishna Sankar July 30, 2010 at 6:36 am

@Bob: Thanks much. I will try to digest and get back, for any clarifications… rayan2 January 29, 2010 at 11:55 pm

use this formula
G=(N XOR 2*N)/2 Krishna Sankar April 4, 2010 at 4:43 am

@rayan2: It does the same thing? How does that work? jerraz September 7, 2009 at 2:22 pm

Hello!
I need convert 10-bits number from gray to binary. Could anybody help me? Krishna Sankar September 9, 2009 at 5:48 am

@jerraz: Hope the Lookup Table based Matlab code provided in the post helps. khalid_hassan September 3, 2009 at 1:53 pm

iam a student of heritage of 2nd yera I.T R.Ramya March 29, 2009 at 9:41 am

I cant understand this line particualr line:

[tt ind] = sort(opGray); % sorting Gray code elements to form the lookup table

here ind refers to ??? Krishna Sankar April 4, 2009 at 8:49 am

@R.Ramya:
The variable ind refers to the index of the elements prior to sorting.
as a quick example,
a = [3 1 4];
[tt ind] = sort(a);
tt =
1 3 4
ind =
2 1 3

Hope this helps. khushi August 24, 2012 at 3:51 pm

thanks a lot sir:) R.Ramya March 29, 2009 at 7:51 am

hi krishna,
there is a typo in the article
it shuld be written as
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.

cheers. Krishna Sankar April 4, 2009 at 8:43 am
@R.Ramya: Thanks for noticing the typo. I corrected. 