# Gray code to Binary conversion for PSK and PAM

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

## 18 thoughts on “Gray code to Binary conversion for PSK and PAM”

1. M.Ramya says:

how can we convert decimal to 2421 code

2. shanu says:

i want a c program for converting a digital signal into binary format??????? pls help me

1. Krishna Sankar says:

@shanu: you might want to check dec2bin() function in matlab

3. sneha says:

i want conversion of gray code to binary code using kmaps???????if this is thereplz tel me???not there den tel me d reason

1. Krishna Sankar says:

@sneha: What is kmaps?

4. Bob says:

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.

1. Krishna Sankar says:

@Bob: Thanks much. 🙂
I will try to digest and get back, for any clarifications…

5. rayan2 says:

use this formula
G=(N XOR 2*N)/2

1. Krishna Sankar says:

@rayan2: It does the same thing? How does that work?

6. jerraz says:

Hello!
I need convert 10-bits number from gray to binary. Could anybody help me?

1. Krishna Sankar says:

@jerraz: Hope the Lookup Table based Matlab code provided in the post helps.

7. khalid_hassan says:

iam a student of heritage of 2nd yera I.T

8. R.Ramya says:

I cant understand this line particualr line:

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

here ind refers to ???

1. Krishna Pillai says:

@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.

1. khushi says:

thanks a lot sir:)

9. R.Ramya says:

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];

1. Krishna Pillai says: