A Less Complex Algorithmic Procedure for Computing Gray Codes

: The purpose of this paper is to present a new and faster algorithmic procedure for generating the n - bit Gray codes. Thereby, through this paper we have presented the derivation, design and implementation of a newly developed algorithm for the generation of an n-bit binary reflected Gray code sequences. The developed algorithm is stemmed from the fact of generating and properly placing the min-terms from the universal set of all the possible min-terms [m0 m1 m2 …. mN] of Boolean function of n variables, where, 0 < N < 2n-1. The resulting algorithm is in concise form and trivial to implement. Furthermore, the developed algorithm is equipped with added attributes of optimizing of time and space while executed.


Introduction
Although many codes for example Binary Coded Decimal (BCD), Excess-3 Code, Hamming Code, Cyclic Redundancy Code (CRC), Check Sum and many others exist and are in use.But the Gray codes which are named for Frank Gray who patented the use of them in shaft encoders (Gray, 1953) due to its attribute of single distance only, which avoids ambiguous switching situations, is particularly used to handle safely and conveniently the control problems.The term Gray code is sometimes used to refer to any single-distance code, that is, one in which adjacent code words differ by 1 in one digit position only, This property can be seen in Table 1 which shows the Gray codes for size n = 1 to 4 bits.
Gray codes were applied to mathematical puzzles before they became known to engineers.The Gray code arises naturally in many situations.Gray's interest in the code was related to what we would now call analog to digital conversion.The goal was to convert an integer value, represented as a voltage, into a series of pulses representing the same number in digital form.The technique, as described in Gray's patent, was to use the voltage being converted to displace vertically an electron beam that is being swept horizontally across the screen of a cathode ray tube.The screen has a mask etched on it that only allows the passage of the beam in certain places; a current is generated only when the beam passes through the mask.The passage of the beam will then give rise to a series of on/off conditions corresponding to the pattern of mask holes that it passes.Mechanical position sensors use Gray code to convert the angular position (angle-measuring devices) of a shaft to digital form.Gray codes were used in telegraphy.The Gray code also forms a Hamiltonian cycle on a hypercube, where each bit is seen as one dimension.In data transmission, Gray codes play an important role in error detection and correction.Solving puzzles such as the Tower of Hanoi and the Brain, the study of bell-ringing, analog-digital signal conversion, classifying of Venn diagrams, continuous space-filling curves, enhancing the resolution of spectrometer for a satellite application, labeling the axes of Karnaugh maps are the processes where Gray codes are used due to its uniqueness.Gray codes are also beneficial in Genetic Algorithms due to its incremental change property.Using Gray codes for addressing the memory results in saving of the power because a few address lines change as the program counter advances to the next location.Also, Gray codes are extensively used by digital system designers for passing multi-bit count information between synchronous logic that operates at different clock frequencies.In some numerical problems, Gray codes can be useful in situations of looping over many values of a bit.Furthermore, due to its attribute Gray code could be a good choice for the search of the optimal test-sequences in digital system testing.
Hence it can be said that the Gray codes which were originally designed to prevent spurious output from electromechanical switches.Today they are widely used to facilitate error correction in digital communications such as digital terrestrial television and some cable TV systems.
Since the Gray code has enormous applications as mentioned above has many facet researches as is evident in surveying the literature (Gray, 1953;Sundberg, 1975;Ludman, 1981;Lee, 1986;Conway, et al. 1989;Skiena, 1990;Press, et al. 1992;Etzion Peterson, 1992;Hiltgen, et al. 1996;Savage, 1997;Ruskey, 1997;Guan Dah-Jyu, 1998, Moshe andTuvi, 1999;Black Paul 2004;Alan and Alessandro Mei, 2004;Jywe-Fei and Lai, 2005;Bitner, et al. 1976;Er, 1984;Proskurowski and Ruskey, 1985;Ruskey, 1993;Dominique, 2000;Lassing, et al. 2003, Goddyn and Gvozdjak 2003and Vajnovszki and Walsh 2006).It is clear through the literature survey that there had been much discovered and written about the Gray code; it is associated with many elegant circuits and algorithms.However, the algorithms generating the Gray code was still done with the crude techniques (Ruskey, 1993;Dominique, 2000;Lassing, et al. 2003, Goddyn, Gvozdjak 2003and Vajnovszki and Walsh 2006).Researches are available in the literature only to script the faster codes but not much deviated from the existing crude algorithms for generating the Gray codes.This paper presents a new concept of generating the Gray code of n-bit size.The developed algorithm is stemmed from the fact of generating and properly placing the min-terms from the universal set of all the possible min-terms (m 0 m 1 m 2 …. m N ) of Boolean function of n variables, where, 0 < N < 2 n -1.The resulting algorithm is in concise form and trivial to implement.We designed an efficient algorithm to write the codes which reduces the processing time and memory space requirements.

Gray Code Conversion -Conventional Approaches
No doubt, a simple recursive equation in mod (2) operation can convert a simple binary -…-8-4-2-1 codes to the Gray one.The hardware is also simple which is based on the bank of Exclusive-OR gates.To make the content of this paper more readable to audience the description of the generation procedure of the Gray code is given below: To convert a binary number [b n-1 b n-2 …. b 1 b 0 ] to its corresponding Gray code (g n-1 g n-2 …. g 1 g 0 ), start at the left with the bit b n-1 (the n th , most significant bit) and use the following recursive equations.
Or, in general for i th bit where i varies as, 2 < i < n, the following Eq.( 3) can be used. (3) The above Eqs.(1) and ( 3) are illustrated for a 3-bit Gray code encoder in an example below.
The derivation of the generalized Eqs. ( 1) -( 3) can be computed as below.
From Table 1 above, a set of the following Boolean functions expressed in the form of min-terms Σ m i where i varies from 0 to 7, can be derived as: The minimized version of above Boolean functions using the K-maps and hence the implementations of those minimized functions are as given in Figs. 1 and 2 respectively.
Similarly, the reversal of Gray code bits [g n g n-1 … g 2 .7) -( 10) can be easily computed as: From Table 1 above, a set of the following Boolean functions expressed in the form of min-terms Σ m i where i varies from 0 to 7, can be derived as: The above Boolean functions can be minimized using the K-maps as demonstrated below (see Fig. 3).Whereas, the implementation of those minimized Boolean functions is shown in Fig. 4.
Since the Gray code encoder needs first to obtain the binary data (through a binary counter) before generating the Gray code.This process requires 2-stages as shown in Fig. 5. Thus, it is imperative to derive a mechanism to avoid this situation which needs much time and space to implement.The ensuing section is a consequence to it.

Computing Gray Codes -Proposed Methodology
If we look to the Table 2 and 3 which lists the Gray codes, respective min-terms and equivalent decimals for n = 1 to 6 forced us to derive the following conclusions.1. Gray code of size n has a specific pattern relationship between min-terms of the Gray code of its predeces-sor code of size n-1.2. The Gray code of size n can be directly scripted using the n-bit K-map where the min-terms cells are to be read clock-wise and down to the row as explained in the Fig. 6.The example of Fig. 6 is a 4-variable K-map used to

Proposed Algorithm
The following Eqs.( 14) to ( 18) describe the Gray codes for bit size 1 to 5 respectively.A specific relation between the min-terms patterns is visible and summarized in the form of a theorem below: (15) 2 G (n) is a matrix of order 2 n x n in binary format of nbit forming 2 n min-terms of n variables.By analyzing the patterns we reach to the conclusion that G(n) can be obtained first by writing the min-terms of G(n-1) then appending the min-terms by advancing each of the minterms starting from the last to the first by a value of 2 n-1 .

Proof:
Proof is as visible through the Eqs.from ( 14) to ( 18).An algorithm is designed on the basis of the study of Theorem 1 and is as given below.

Algorithm STEP 0:
START by inputting the bit size (n) of the Gray code to be generated;   To elaborate the computing of variables in the above mentioned steps a debug test of the above algorithm is carried out for n = 3 and is presented below in Table 4.

Implementation of Algorithm
The above designed algorithm is implemented using the MATLAB code.The out put of the m-file with the name "gray_generator_proposed" can be visualized as shown in Fig. 7.A sample output of the program shown in the figure is only for n = 4.This is provided just to make it more readable, otherwise the output for the higher values of n will require much space to present.Similarly, we also encoded the conventional method of Gray code conversion into MATLAB script.To compare the efficiency amongst these two approaches we run both of the pro-grams for n = 2 to 10 and some of the results are presented and made available through Figs. 8 to 9 in the ensuing section below.>>gray_generator_proposed Please enter a valid positive integer ( > 1) : 4 Gray code of n -bit size i.e. a matrix G (n) of size (N+1) by n;

Results
MATLAB 7.0 on a P 4 CPU 1.5 GHz, 512 MB of RAM is used as the bench marks for testing the codes for both of the computing methodologies.The processing time and memory space required to implement both of the programs (the conventional and the proposed one) are recorded while running the programs.
The results for a subset of the study, for n = 2 to 10 where the memory requirement comparison of the conventional approach of Gray code converter and the proposed approach is presented in Fig. 8.The memory requirement is mapped in bytes.Further, to judge the efficiency of the proposed algorithm, a relative error plot is shown in Fig. 9.With respect to the memory requirements, the behavior of the relative error shown in Fig. 9, demonstrates almost an exponential characteristic except for n = 2. And, this exception for (n = 2) is because of that the implementing the Gray code generator using conventional approach requires significant more memory space than the proposed approach of generating the Gray codes for n = 2.The run time requirements comparison is demonstrated through Fig. 10.

Conclusions
A time space optimal algorithmic procedure to generate Gray code-words of any bit length n is presented through this paper.The comparative study reveals that the proposed approach is not only faster but also, requires about 25% less memory space on average while compared with the conventional method of Gray code-word generation technique.Since Gray code is widely used for on line system monitoring hooked with sensors and with on board systems and hence these two parameters (space and time requirements) are very critical in these applications.Therefore this proposed algorithmic procedure is more advantageous.
g 1 ] again into -…-8-4-2-1 weighted binary code bits [b n b n-1 … b 2 .b 1 ] can be performed by using the equations as given below: general, to compute the binary code bits b n-1 , … , b 2 , and.b 1 the following recursion equation can be used where, i varies from 1 to n-1.Let a Gray code [g 3 g 2 g 1 ] = [1 1 0].The derivation of the generalized Eqs. (

Figure
Figure 1.K-maps and minimized Boolean functions for g 3 , g 2 and g 1