FazBrowse Wikipedia Viewer | Main Page |
Search Wikipedia:
|
Viewing: https://en.wikipedia.org/wiki/Gray_code

Gray code

Contents
    Communication between clock domains
    Cycling through states with minimal effort
        Gray code counters and arithmetic
        Gray code addressing
        Evenness and oddness of Gray codes
    n-ary Gray code
    Beckett–Gray code

0
001
31101
60110
90110
121011
150001" and "100

The problem with natural binary codes is that physical switches are not ideal: it is very unlikely that physical switches will change states exactly in synchrony. In the transition between the two states shown above, all three switches change state. In the brief period while all are changing, the switches will read some spurious position. Even without keybounce, the transition might look like 101 — 00000001 200110110 501100100 810011111 1111001011 141111

  • Codes 4-PSK
    Codes 4-PSK
  • Codes 8-PSK
    Codes 8-PSK
  • Codes 16-QAM
    Codes 16-QAM
  • Communication between clock domains

    edit

    Digital logic designers use Gray codes extensively for passing multi-bit count information between synchronous logic that operates at different clock frequencies. The logic is considered operating in different "clock domains". It is fundamental to the design of large chips that operate with many different clocking frequencies.

    Cycling through states with minimal effort

    edit

    If a system has to cycle sequentially through all possible combinations of on-off states of some set of controls, and the changes of the controls require non-trivial expense (e.g. time, wear, human work), a Gray code minimizes the number of setting changes to just one change for each combination of states. An example would be testing a piping system for all combinations of settings of its manually operated valves.

    A balanced Gray code can be constructed, that flips every bit equally often. Since bit-flips are evenly distributed, this is optimal in the following way: balanced Gray codes minimize the maximal count of bit-flips for each digit.

    Gray code counters and arithmetic

    edit

    George R. Stibitz utilized a reflected binary code in a binary pulse counting device in 1941.

    A typical use of Gray code counters is building a FIFO (first-in, first-out) data buffer that has read and write ports that exist in different clock domains. The input and output counters inside such a dual-port FIFO are often stored using Gray code to prevent invalid transient states from being captured when the count crosses clock domains. The updated read and write pointers need to be passed between clock domains when they change, to be able to track FIFO empty and full status in each domain. Each bit of the pointers is sampled non-deterministically for this clock domain transfer. So for each bit, either the old value or the new value is propagated. Therefore, if more than one bit in the multi-bit pointer is changing at the sampling point, a "wrong" binary value (neither new nor old) can be propagated. By guaranteeing only one bit can be changing, Gray codes guarantee that the only possible sampled values are the new or old multi-bit value. Typically Gray codes of power-of-two length are used.

    Sometimes digital buses in electronic systems are used to convey quantities that can only increase or decrease by one at a time, for example the output of an event counter which is being passed between clock domains or to a digital-to-analog converter. The advantage of Gray codes in these applications is that differences in the propagation delays of the many wires that represent the bits of the code cannot cause the received value to go through states that are out of the Gray code sequence. This is similar to the advantage of Gray codes in the construction of mechanical encoders, however the source of the Gray code is an electronic counter in this case. The counter itself must count in Gray code, or if the counter runs in binary then the output value from the counter must be reclocked after it has been converted to Gray code, because when a value is converted from binary to Gray code, it is possible that differences in the arrival times of the binary data bits into the binary-to-Gray conversion circuit will mean that the code could go briefly through states that are wildly out of sequence. Adding a clocked register after the circuit that converts the count value to Gray code may introduce a clock cycle of latency, so counting directly in Gray code may be advantageous.

    To produce the next count value in a Gray-code counter, it is necessary to have some combinational logic that will increment the current count value that is stored. One way to increment a Gray code number is to convert it into ordinary binary code, add one to it with a standard binary adder, and then convert the result back to Gray code. Other methods of counting in Gray code are discussed in a report by Robert W. Doran, including taking the output from the first latches of the master-slave flip flops in a binary ripple counter.

    Gray code addressing

    edit

    As the execution of executable code typically causes an instruction memory access pattern of locally consecutive addresses, bus encodings using Gray code addressing instead of binary addressing can reduce the number of state changes of the address bits significantly, thereby reducing the CPU power consumption in some low-power designs.

    Evenness and oddness of Gray codes

    edit

    In the natural binary code system, the least significant bit indicates if the number is even (0) or odd (1), a property absent in the Gray code. Because just one bit is changed in consecutive Gray codes, the number of 1 bits will alternate between even and odd, so, to check a Gray code's evenness, it's necessary to count them, that is, an even number of 1s means the Gray code is even:

    DecimalBinaryEvennessBinary bit countGrayGray bit count
    00001odd100112
    30100even101113
    60111odd311002
    91010even211103
    121101odd310012
    1500, 10
    Reflected: 01, 000, 010,
    Prefix new entries with 111, 000, 010, 101, n⊕⌊n2⌋. Prepending a i of codewords are inverted, the order of neighbouring blocks of
    000,001,010,011,100,101,110,111 → 010,011,000,001,110,111,100,101  (invert bit 1)

    If bit 2 is inverted, blocks of 4 codewords reverse order:

    bi at position i+1 leaves the order of codewords intact if bi+1=𝟷. Now, this is exactly the same operation as the reflect-and-prefix method to generate the Gray code.

    A similar method can be used to perform the reverse translation, but the computation of each bit depends on the computed value of the next higher bit so it cannot be performed in parallel. Assuming g0 being the most significant bit), and b0 being the most-significant bit), the reverse translation can be given recursively: code0=𝟶, and at step 0"}}'>i>0 find the bit position of the least significant codei−1 to get the next code 2n that include zero and use the minimum number of bits.

    n-ary Gray code

    edit
    Ternary number → ternary Gray code
    \n1&nbsp;→&nbsp;001<br />\n2&nbsp;→&nbsp;002<br />\n10&nbsp;→&nbsp;012<br />\n11&nbsp;→&nbsp;011<br />\n12&nbsp;→&nbsp;010<br />\n20&nbsp;→&nbsp;020<br />\n21&nbsp;→&nbsp;021<br />\n22&nbsp;→&nbsp;022<br />\n100&nbsp;→&nbsp;122<br />\n101&nbsp;→&nbsp;121<br />\n102&nbsp;→&nbsp;120<br />\n110&nbsp;→&nbsp;110<br />\n111&nbsp;→&nbsp;111<br />\n112&nbsp;→&nbsp;112<br />\n120&nbsp;→&nbsp;102<br />\n121&nbsp;→&nbsp;101<br />\n122&nbsp;→&nbsp;100<br />\n200&nbsp;→&nbsp;200<br />\n201&nbsp;→&nbsp;201<br />\n202&nbsp;→&nbsp;202<br />\n210&nbsp;→&nbsp;212<br />\n211&nbsp;→&nbsp;211<br />\n212&nbsp;→&nbsp;210<br />\n220&nbsp;→&nbsp;220<br />\n221&nbsp;→&nbsp;221<br />\n222&nbsp;→&nbsp;222"}},"i":0}}]}'>00,01,02,12,11,10,20,21,22. The (n, k)-Gray code may be constructed recursively, as the BRGC, or may be constructed iteratively. An algorithm to iteratively generate the (N, k)-Gray code is presented (in C):

    λk=|{j∈ℤRn:δj=k}|, for k∈ℤn

    A Gray code is uniform or uniformly balanced if its transition counts are all equal, in which case we have 2⌊2n2n⌋ or

    G=g0,…,g2n−1 into an even number L of non-empty blocks of the form

    kL−1=−2, and mi=|{j:δkj=i,1≤j≤L}|

    to be the number of times the digit in position i changes between consecutive blocks in a partition, then for the (n + 2)-digit Gray code induced by this partition the transition spectrum λ'i, so one may for example designate the first R≡0(mod4) and Vn(i)={v∈Vn:v has weight i}

    for Qn(i) be the subgraph of En(i) be the edges in δ1∈En(i) comes before Pn,j of length P1,0=(𝟶,𝟷), j≥n, and

    Pπ refers to the path P with its coordinates permuted by Gn(2) given by

    πn=E−1(πn−12). The first few values of 0, 1

    n = 2 000, 0010000, 00011110, 1111

    These monotonic Gray codes can be efficiently implemented in such a way that each subsequent element can be generated in O(n) time. The algorithm is most easily described using coroutines.

    Monotonic codes have an interesting connection to the Lovász conjecture, which states that every connected vertex-transitive graph contains a Hamiltonian path. The "middle-level" subgraph ‍N, where N is the number of vertices in the middle-level subgraph.

    Beckett–Gray code

    edit

    Another type of Gray code, the Beckett–Gray code, is named for Irish playwright Samuel Beckett, who was interested in symmetry. His play "Quad" features four actors and is divided into sixteen time periods. Each period ends with one of the four actors entering or leaving the stage. The play begins and ends with an empty stage, and Beckett wanted each subset of actors to appear on stage exactly once. Clearly the set of actors currently on stage can be represented by a 4-bit binary Gray code. Beckett, however, placed an additional restriction on the script: he wished the actors to enter and exit so that the actor who had been on stage the longest would always be the one to exit. The actors could then be represented by a FIFO queue, so that (of the actors onstage) the actor being dequeued is always the one who was enqueued first. Beckett was unable to find a Beckett–Gray code for his play, and indeed, an exhaustive listing of all possible sequences reveals that no such code exists for n = 4. It is known today that such codes do exist for n = 2, 5, 6, 7, and 8, and do not exist for n = 3 or 4. An example of an 8-bit Beckett–Gray code can be found in Donald Knuth's Art of Computer Programming. According to Sawada and Wong, the search space for n = 6 can be explored in 15 hours, and more than 0 or 01000

    144°00001
    12°00101228°1110096°10011312°01111180°11101
    48°10110264°11000132°00011348°
    View original on Wikipedia | Back | FazBrowse Home