What are Gold codes?

The key to how GPS works are “Gold codes” – a 1023-bit binary number that each Space Vehicle transmits once every millisecond to allow the presence and phase of the signal to be accurately detected.

Here is the Gold code for Space Vehicle 1:

110010000011100101001001111001010001001111101010110100010001010101
011001000111101001111110110111001101111100101010100001000000001110
101001000100110111100000111101011100110011110110000000101111001111
101010011000101101110001101111010100010101100000100000000100000011
000111011000000111000110111111111010011101001011011000010101011000
100111001011011101100011101110111100001101100001100100100100000110
110100101101111000101110000001010010011111100000101010111001111101
011111001100110001110001101101010101101100011011101110000000000010
110011011001110110100000101010111010111010010100011100111000100101
000101001011010000101011011010110110001110011110110010000111111001
011010001000011111010101110011001001001001011111111110000111110111
100011011100101100001110010101000010100101011111100011110110100111
011001111110111110100011000111110000000100101000101101000100010011
011000000111011010001101000100100011100010110011001001111001101111
110011001010011010011010111100110110101001110111100011010100010000
100010010011100001110010100010000

Testing the correlation of two binary numbers is pretty simple – XOR the two values together, and then count the numbers of 1s and 0s in the resulting string.

int correlate(unsigned char *a, unsigned char *b; int length) {
  for(i = 0; i < length; i++) {
    if(a[i] ^ b[i] == 0)
      match++
    else
      match--
  }
  return (match < 0) ? -match : match;
}

To prove how little they match, here are graphs of Space Vehicles 1’s Gold code correlated against itself and that of the Gold code for Space Vehicle 2:

correlation

The single peak can clearly be seen standing out from the noise, SV1’s code XORed with SV1’s code, with an phase offset of zero.

This special property of Gold codes allows a GPS receiver to check if a Space Vehicle’s signal is being received, and the relative phase to with sub-microsecond microsecond accuracy. However, this comes at a price. You can’t just ‘tune in’ to a Space Vehicle, you have to do a lot of work, deliberately looking for the correct single, at the correct frequency and with the correct phase.

Here is the C program used to produce the data for the graphs. You might find it useful:

/*****************************************************************************
* correlate.c - Testing Gold code correlation
*****************************************************************************/

unsigned char gold_sv1[1023] =
 "110010000011100101001001111001010001001111101010110100010001010101"
 "011001000111101001111110110111001101111100101010100001000000001110"
 "101001000100110111100000111101011100110011110110000000101111001111"
 "101010011000101101110001101111010100010101100000100000000100000011"
 "000111011000000111000110111111111010011101001011011000010101011000"
 "100111001011011101100011101110111100001101100001100100100100000110"
 "110100101101111000101110000001010010011111100000101010111001111101"
 "011111001100110001110001101101010101101100011011101110000000000010"
 "110011011001110110100000101010111010111010010100011100111000100101"
 "000101001011010000101011011010110110001110011110110010000111111001"
 "011010001000011111010101110011001001001001011111111110000111110111"
 "100011011100101100001110010101000010100101011111100011110110100111"
 "011001111110111110100011000111110000000100101000101101000100010011"
 "011000000111011010001101000100100011100010110011001001111001101111"
 "110011001010011010011010111100110110101001110111100011010100010000"
 "100010010011100001110010100010000";

unsigned char gold_sv2[1023] =
 "111001000011100000111110100110010110111111001011001011111111010010"
 "110000100010001011000111100011000001100111000010001110001000111010"
 "011110000010110100001101001011110000111000001111100011011100110001"
 "101011100000001111000110100010111001000110011001101101001100000101"
 "111000100100101010001100000010101010000010011110000010010111111111"
 "111011101010110101010000010101001010101010000100100011001000010100"
 "001001010011011100000011100101010100110111000001111011101100000010"
 "001000101101011100110101001001100100111011011111010110000010010111"
 "100111000010110000110100011010110000001001011101100010100101101110"
 "100101101100111110000010001000101011010010100011101001110110010010"
 "001111101011111011110110001110101110101001101111001101010010111010"
 "111100000111101111101101111010001111100110110111100010000101101011"
 "010000111100110010110011000001100001000011111101011001111101001100"
 "111010100010111101000011001111101000001001111010011001011111011100"
 "101101101001111011001110011110010110111001100110101111101111011010"
 "010100101101000000101000011001000";

/****************************************************************************/
int main(int argc, char *argv[]) {
 int offset;
 for(offset = 0; offset < 1023; offset++)
 {
  int i,match;

  match = 0;
  for(i = 0; i < 1023; i++) {
   if(gold_sv1[i%1023] == gold_sv2[(i+offset)%1023])
    match++;
   else
    match--;
  }
  printf("%4i: %i\n",offset,match);
 }
}
/*****************************************************************************
* End of file!
*****************************************************************************/

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s