Anda di halaman 1dari 4

7/25/2017 Gray Code - algorithm in Matlab bin/dec conversions

Home
Gray Code in Matlab from/to
Welcome binary and decimal
Matrixmania Blog The Gray code (also known as
reflected binary code), is a
Sitemap / Search binary numerical system where
two consecutive values differ in
Matlab Books only one bit.
Forums and Help The Gray code code was
originally designed to prevent
Contact undesired transient states or
outputs from electro-
Basics
mechanical switches. Today,
Quick Matlab Guide this code is used to facilitate
error correction in digital
Matlab Tutorial communications and digital-to-
analog converters.
Matlab Examples ANNUAL PRIME MEMBERSHIP 499/ YEAR T&C Apply

Matlab Flow In this article, were going to develop a simple Matlab algorithm to make
conversions between (from/to) binary and gray codes.
Boolean Logic

Plots and GUI


Convert a binary number to a Gray number
Matlab 2D Plots
Lets understand the algorithm to go from binary to Gray. See the conversion
Matlab 3D Plots from 11101 binary to its equivalent in Gray code.

Matlab GUI

Applications
Calculus

Linear Algebra

Matlab Cookbook I

Matlab Cookbook II

Electrical Calc

Probab and Stats The most significant bit (MSB) in Gray is taken directly from the MSB in binary.
The rest of the Gray bits comes from a xor operation between the precedent
Finance Apps binary bit(b(i-1)) and the current binary bit (b(i)). In the case shown in the
figure above:
Other
Online Calculators g(1) = b(1)
g(2) = b(1) xor b(2)
Relevant Links
g(3) = b(2) xor b(3)
g(4) = b(3) xor b(4)
Notes on Comp
g(5) = b(4) xor b(5)
Fun!

Scilab The xor operation produces a 1 if the bits are different, and produces a 0 if
the bits are equal. So, a binary 11101 becomes a 10011 in Gray.
Your own Website?
Lets propose a code in Matlab to do it.
Terms/Policies
function g = bin2gray(b)
g(1) = b(1);
for i = 2 : length(b);
x = xor(str2num(b(i-1)), str2num(b(i)));
g(i) = num2str(x);
end

http://www.matrixlab-examples.com/gray-code.html 1/4
7/25/2017 Gray Code - algorithm in Matlab bin/dec conversions

The input parameter to this function is a binary number (expressed in a


string), the output is the equivalent Gray number (also expressed as a string).

g = bin2gray('11101')
g = bin2gray('10010')
g = bin2gray('11110')
g = bin2gray('10000')

We get these string-results from Matlab:

g = 10011
g = 11011
g = 10001
g = 11000

We also can test a full sequence. We go from decimal-to-binary (using built-in


function dec2bin) and then to Gray (using our developed function), for
example:

for d = 0 : 15
b = dec2bin(d);
g = bin2gray(b);
disp({d b g})
end

The results are (decimal, binary, gray):

[0] '0' '0'


[1] '1' '1'
[2] '10' '11'
[3] '11' '10'
[4] '100' '110'
[5] '101' '111'
[6] '110' '101'
[7] '111' '100'
[8] '1000' '1100'
[9] '1001' '1101'
[10] '1010' '1111'
[11] '1011' '1110'
[12] '1100' '1010'
[13] '1101' '1011'
[14] '1110' '1001'
[15] '1111' '1000'

Convert a Gray number to a binary number

Now lets understand the algorithm to go from Gray to binary. See the
conversion from 10011 Gray to its binary equivalent.

The most significant bit (MSB) in binary is taken directly from the MSB in Gray.
The rest of the binary bits comes from a xor operation between the precedent
binary bit (b(i-1)) and the current Gray bit (g(i)). In the case shown in the
figure above:

b(1) = g(1)
b(2) = b(1) xor g(2)
b(3) = b(2) xor g(3)
b(4) = b(3) xor g(4)
b(5) = b(4) xor g(5)

http://www.matrixlab-examples.com/gray-code.html 2/4
7/25/2017 Gray Code - algorithm in Matlab bin/dec conversions

Our proposed Matlab function to achieve the conversion is:

function b = gray2bin(g)
b(1) = g(1);
for i = 2 : length(g);
x = xor(str2num(b(i-1)), str2num(g(i)));
b(i) = num2str(x);
end

Lets test our function:

b = gray2bin('10011')
b = gray2bin('11011')
b = gray2bin('10001')
b = gray2bin('11000')

We get these string-results:

b = 11101
b = 10010
b = 11110
b = 10000

And now we test a full sequence, going decimal-to-binary, binary-to-gray, and


gray-to-binary:

for d = 0 : 15
g = bin2gray(dec2bin(d));
b = gray2bin(g);
disp({d g b})
end

We get (decimal, gray, binary):

[0] '0' '0'


[1] '1' '1'
[2] '11' '10'
[3] '10' '11'
[4] '110' '100'
[5] '111' '101'
[6] '101' '110'
[7] '100' '111'
[8] '1100' '1000'
[9] '1101' '1001'
[10] '1111' '1010'
[11] '1110' '1011'
[12] '1010' '1100'
[13] '1011' '1101'
[14] '1001' '1110'
[15] '1000' '1111'

From 'Gray Code' to home

From 'Gray Code' to 'Matlab Cookbook II'

http://www.matrixlab-examples.com/gray-code.html 3/4
7/25/2017 Gray Code - algorithm in Matlab bin/dec conversions

Top

Binary-to-dec

Decimal-to-bin

Bin-to-hexadecimal

Hex-to-bin

Share this page: Facebook Twitter Google


Whats this?
Pinterest Tumblr Reddit

Enjoy this page? Please pay it forward. Here's how...

http://www.matrixlab-examples.com/gray-code.html 4/4

Anda mungkin juga menyukai