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
74 InterpolatingReader(FILE *f) : f(f), p(0.0), in_pos(-1) {}
76 double read_sample(double speed)
81 while ((int)(ceil(p)) > in_pos) {
83 if (fread(&sample, sizeof(short), 1, f) != 1) {
89 // linear interpolation (works OK since delta_p varies so slowly)
91 return prev_sample * (1.0 - t) + sample * t;
95 double filter(double x)
98 static bool init = false;
101 l = 0.05 * x + 0.95 * l;
109 int main(int argc, char **argv)
111 FILE *in1, *in2, *skew;
113 // open input1 (reference)
114 if (strcmp(argv[1], "-") == 0) {
117 in1 = fopen(argv[1], "rb");
125 if (strcmp(argv[2], "-") == 0) {
128 in2 = fopen(argv[2], "rb");
135 // open (estimated) skew
136 if (strcmp(argv[3], "-") == 0) {
139 skew = fopen(argv[3], "wb");
146 FlankDetector f1, f2;
148 // initial sync, reference
151 if (fread(&s, sizeof(short), 1, in1) != 1) {
156 } while (!f1.check_flag() || !f1.get_status());
158 // initial sync, input
161 if (fread(&s, sizeof(short), 1, in2) != 1) {
166 } while (!f2.check_flag() || !f2.get_status());
168 InterpolatingReader intp(in2);
169 double speed = 1.000;
171 while (!feof(in1) && !feof(in2)) {
174 // read reference until the next triggering
175 unsigned num_ref = 0;
176 while (!feof(in1) && !f1.check_flag()) {
177 if (fread(&refs, sizeof(short), 1, in1) != 1) {
185 unsigned num_inp = 0;
186 while (!feof(in2) && !f2.check_flag()) {
187 double s = intp.read_sample(speed);
192 double ns = filter(speed * double(num_inp) / double(num_ref));
193 printf("%u vs. %u -- ratio %.3f, speed %.3f, filtered speed %.3f\n", num_inp, num_ref,
194 double(num_inp) / double(num_ref), speed * double(num_inp) / double(num_ref), ns);