1: /*
2: ***********************************************************************
3: * Program to simulate NTP phase-lock loop *
4: ***********************************************************************
5: */
6: #include <stdio.h>
7: #include <ctype.h>
8: #include <math.h>
9: /*
10: Parameters
11: */
12: #define FMAX 8 /* max filter size */
13: #define DAY 86400 /* one day of seconds */
14: #define DMAX .128 /* max clock aperture */
15: #define PI 3.1415926535 /* the real thing */
16: /*
17: Function declarations
18: */
19: extern double pow(double, double);
20: /*
21: Global declarations
22: */
23: double Kf = 1024; /* frequency weight */
24: double Kg = 256; /* phase weight */
25: double Kh = 256; /* compliance weight */
26: double S = 16; /* compliance maximum */
27: double T = 262144; /* compliance factor */
28: double U = 4; /* adjustment interval */
29: double poll = 64; /* poll interval */
30: ;
31: double f = 0; /* frequency error */
32: double g = 0; /* phase error */
33: double h = 0; /* compliance */
34: double c = 0; /* clock error */
35: double d; /* current filter output */
36: double filt[FMAX]; /* clock filter */
37: ;
38: double offset = .01; /* initial phase offset */
39: double freq = 10e-6; /* initial frequency offset */
40: ;
41: double t; /* current time */
42: double q, a, x, z; /* temporaries */
43: int ns = 0; /* reset counter */
44: int n,i; /* integer temporary */
45: /*
46: Main program
47: */
48: main() {
49: n = poll/U; q = 1-1/Kg; q = ((pow(q,n)-1)/(q-1))/Kg;
50: ns = 0; x = 0; z = 8;
51: for (t = 0; t < 2*DAY; t += poll) {
52: for (i = FMAX-1; i > 0; i--) filt[i] = filt[i-1];
53: filt[0] = (offset-c-freq*t); d = filt[FMAX-1];
54: if (fabs(d) >= DMAX) {
55: ns++; c = c+d;
56: for (i = 0; i < FMAX; i++) filt[i] = 0;
57: printf("Reset %5.0f\n", d*1000);
58: }
59: a = S-T*fabs(h); if (a<1) a=1;
60: h = h+(d-h)/Kh;
61: f = f+d/(a*poll);
62: g = d*q; c = c+g+(poll/(U*Kf))*f;
63:
64: printf("%8.2f%8.2f%8.2f%8.2f%8.2f %8.2e",
65: t/60, offset*1000, d*1000, (f/(U*Kf)+freq)*1e6,
66: h*1000, a);
67: printf("\n");
68: }
69: return (0);
70: }
Defined functions
main
defined in line
48;
never used
Defined variables
Kf
defined in line
23; used 2 times
Kg
defined in line
24; used 2 times
Kh
defined in line
25; used 1 times
S
defined in line
26; used 1 times
T
defined in line
27; used 1 times
U
defined in line
28; used 3 times
a
defined in line
42; used 5 times
c
defined in line
34; used 5 times
d
defined in line
35; used 8 times
f
defined in line
31; used 4 times
filt
defined in line
36; used 5 times
freq
defined in line
39; used 2 times
g
defined in line
32; used 2 times
h
defined in line
33; used 5 times
i
defined in line
44; used 9 times
n
defined in line
44; used 2 times
ns
defined in line
43; used 2 times
poll
defined in line
29; used 4 times
q
defined in line
42; used 5 times
t
defined in line
41; used 5 times
x
defined in line
42; used 1 times
z
defined in line
42; used 1 times
Defined macros
DAY
defined in line
13; used 1 times
DMAX
defined in line
14; used 1 times
FMAX
defined in line
12; used 4 times
PI
defined in line
15;
never used