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)) {
63 // read sample from reference
64 if (fread(&refs, sizeof(short), 1, in1) != 1) {
68 // read sample from in2 (at current speed)
70 while ((int)(ceil(p)) > in_pos) {
72 if (fread(&sample, sizeof(short), 1, in2) != 1) {
78 // linear interpolation (works well since delta_p varies so slowly)
80 double intp_sample = prev_sample * (1.0 - t) + sample * t;
82 // subtract the two samples and pull it through a filter. we assume
83 // the sines are normalized for now (and that there's no dc skew)
84 double offset = filter(intp_sample - refs);
85 speed += 1e-8 * offset;
86 fwrite(&speed, sizeof(double), 1, skew);
88 printf("%f (offset=%f / filt=%f)\n", speed, intp_sample - refs, offset);