11 double filter(double x)
13 static double l = 0.0;
14 l = 0.001 * x + 0.999 * l;
18 int main(int argc, char **argv)
20 FILE *in1, *in2, *skew;
22 // open input1 (reference)
23 if (strcmp(argv[1], "-") == 0) {
26 in1 = fopen(argv[1], "rb");
34 if (strcmp(argv[2], "-") == 0) {
37 in2 = fopen(argv[2], "rb");
44 // open (estimated) skew
45 if (strcmp(argv[3], "-") == 0) {
48 skew = fopen(argv[3], "wb");
55 short prev_sample, sample = 0;
60 while (!feof(in1) && !feof(in2)) {
61 // read sample from reference
62 if (fread(&sample, sizeof(short), 1, in1) != 1) {
66 // read sample from in2 (at current speed)
68 while ((int)(ceil(p)) > in_pos) {
70 if (fread(&sample, sizeof(short), 1, in2) != 1) {
76 // linear interpolation (works well since delta_p varies so slowly)
78 short intp_sample = prev_sample * (1.0 - t) + sample * t;
80 // subtract the two samples and pull it through a filter. we assume
81 // the sines are normalized for now (and that there's no dc skew)
82 double offset = -0.001 * filter(intp_sample - sample);
84 fwrite(&speed, sizeof(double), 1, skew);
86 printf("%f (offset=%f)\n", speed, offset);