14 static const unsigned NUM_SAMPLES = 10;
15 static const int MIN_LEVEL = 8192;
17 bool status, flag; // high/low
21 FlankDetector() : status(false), flag(false), counter(0) {}
24 if (abs(x) < MIN_LEVEL) {
38 if (x <= -MIN_LEVEL) {
45 if (counter >= NUM_SAMPLES) {
59 bool get_status() const
65 class InterpolatingReader
75 InterpolatingReader(FILE *f) : f(f), speed(1.0), p(0.0), in_pos(-1) {}
77 void update_speed(double speed)
87 while ((int)(ceil(p)) > in_pos) {
89 if (fread(&sample, sizeof(short), 1, f) != 1) {
95 // linear interpolation (works OK since delta_p varies so slowly)
97 return prev_sample * (1.0 - t) + sample * t;
101 double filter(double x)
103 static double l = 1.0;
104 l = 0.01 * x + 0.99 * l;
108 int main(int argc, char **argv)
110 FILE *in1, *in2, *skew;
112 // open input1 (reference)
113 if (strcmp(argv[1], "-") == 0) {
116 in1 = fopen(argv[1], "rb");
124 if (strcmp(argv[2], "-") == 0) {
127 in2 = fopen(argv[2], "rb");
134 // open (estimated) skew
135 if (strcmp(argv[3], "-") == 0) {
138 skew = fopen(argv[3], "wb");
145 FlankDetector f1, f2;
147 // initial sync, reference
150 if (fread(&s, sizeof(short), 1, in1) != 1) {
155 } while (!f1.check_flag() || !f1.get_status());
157 // initial sync, input
160 if (fread(&s, sizeof(short), 1, in2) != 1) {
165 } while (!f2.check_flag() || !f2.get_status());
167 double speed = 1.000;
169 InterpolatingReader intp(in2);
170 intp.update_speed(speed); // test to see if we can resync OK
172 while (!feof(in1) && !feof(in2)) {
175 // read reference until the next triggering
176 unsigned num_ref = 0;
177 while (!feof(in1) && !f1.check_flag()) {
178 if (fread(&refs, sizeof(short), 1, in1) != 1) {
186 unsigned num_inp = 0;
187 while (!feof(in2) && !f2.check_flag()) {
188 double s = intp.read_sample();
193 double ns = filter(speed * double(num_inp) / double(num_ref));
194 printf("%u vs. %u -- ratio %f, speed %f, filtered speed %f\n", num_inp, num_ref,
195 double(num_inp) / double(num_ref), speed * double(num_inp) / double(num_ref), ns);
197 intp.update_speed(ns);