-
Notifications
You must be signed in to change notification settings - Fork 0
/
GoldCodelib.c
82 lines (71 loc) · 2.87 KB
/
GoldCodelib.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <stdio.h>
#include <stdint.h>
#include <malloc.h>
#include <stdlib.h>
// GPS Shift Register function: the function "shift" emulates a Linear Feedback Shift Register
int shift(int *reg, int *feedback, int feedback_len, int *output, int output_len) {
int out = 0;
for (int i = 0; i < output_len; i++) {
out += reg[output[i] - 1];
}
out %= 2;
int fb = 0;
for (int i = 0; i < feedback_len; i++) {
fb += reg[feedback[i] - 1];
}
fb %= 2;
for (int i = 9; i > 0; i--) {
reg[i] = reg[i - 1];
}
reg[0] = fb;
return out;
}
// Build the CA code (PRN) for a given satellite ID
int PRN(int sv, int *ca) {
int G1[10] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
int G2[10] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
int feedback1[2] = {3, 10};
int output1[1] = {10};
int feedback2[6] = {2, 3, 6, 8, 9, 10};
int output2[2];
switch (sv) {
case 1: output2[0] = 2; output2[1] = 6; break;
case 2: output2[0] = 3; output2[1] = 7; break;
case 3: output2[0] = 4; output2[1] = 8; break;
case 4: output2[0] = 5; output2[1] = 9; break;
case 5: output2[0] = 1; output2[1] = 9; break;
case 6: output2[0] = 2; output2[1] = 10; break;
case 7: output2[0] = 1; output2[1] = 8; break;
case 8: output2[0] = 2; output2[1] = 9; break;
case 9: output2[0] = 3; output2[1] = 10; break;
case 10: output2[0] = 2; output2[1] = 3; break;
case 11: output2[0] = 3; output2[1] = 4; break;
case 12: output2[0] = 5; output2[1] = 6; break;
case 13: output2[0] = 6; output2[1] = 7; break;
case 14: output2[0] = 7; output2[1] = 8; break;
case 15: output2[0] = 8; output2[1] = 9; break;
case 16: output2[0] = 9; output2[1] = 10; break;
case 17: output2[0] = 1; output2[1] = 4; break;
case 18: output2[0] = 2; output2[1] = 5; break;
case 19: output2[0] = 3; output2[1] = 6; break;
case 20: output2[0] = 4; output2[1] = 7; break;
case 21: output2[0] = 5; output2[1] = 8; break;
case 22: output2[0] = 6; output2[1] = 9; break;
case 23: output2[0] = 1; output2[1] = 3; break;
case 24: output2[0] = 4; output2[1] = 6; break;
case 25: output2[0] = 5; output2[1] = 7; break;
case 26: output2[0] = 6; output2[1] = 8; break;
case 27: output2[0] = 7; output2[1] = 9; break;
case 28: output2[0] = 8; output2[1] = 10; break;
case 29: output2[0] = 1; output2[1] = 6; break;
case 30: output2[0] = 2; output2[1] = 7; break;
case 31: output2[0] = 3; output2[1] = 8; break;
default: output2[0] = 4; output2[1] = 9; break; // Default to satellite 32
}
for (int i = 0; i < 1023; i++) {
int g1 = shift(G1, feedback1, 2, output1, 1);
int g2 = shift(G2, feedback2, 6, output2, 2);
ca[i] = (g1 + g2) % 2;
}
return 0;
}