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
71 short prev_sample, sample;
74 InterpolatingReader(FILE *f) : f(f), p(0.0), in_pos(-1) {}
76 double read_sample(double speed)
79 while ((int)(ceil(p)) > in_pos) {
81 if (fread(&sample, sizeof(short), 1, f) != 1) {
87 // linear interpolation (works OK since delta_p varies so slowly)
89 return prev_sample * (1.0 - t) + sample * t;
93 double filter(double x)
96 static bool init = false;
99 l = 0.05 * x + 0.95 * l;
107 int main(int argc, char **argv)
109 FILE *in1, *in2, *skew;
111 // open input1 (reference)
112 if (strcmp(argv[1], "-") == 0) {
115 in1 = fopen(argv[1], "rb");
123 if (strcmp(argv[2], "-") == 0) {
126 in2 = fopen(argv[2], "rb");
133 // open (estimated) skew
134 if (strcmp(argv[3], "-") == 0) {
137 skew = fopen(argv[3], "wb");
144 FlankDetector f1, f2;
145 InterpolatingReader intp(in2);
146 double speed = 1.000;
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
160 short s = (short)intp.read_sample(speed);
163 fwrite(&speed, sizeof(double), 1, skew);
164 } while (!f2.check_flag() || !f2.get_status());
166 while (!feof(in1) && !feof(in2)) {
169 // read reference until the next triggering
170 unsigned num_ref = 0;
171 while (!feof(in1) && !f1.check_flag()) {
172 if (fread(&refs, sizeof(short), 1, in1) != 1) {
180 unsigned num_inp = 0;
181 while (!feof(in2) && !f2.check_flag()) {
182 double s = intp.read_sample(speed);
186 fwrite(&speed, sizeof(double), 1, skew);
189 double ns = filter(speed * double(num_inp) / double(num_ref));
190 fprintf(stderr, "%u vs. %u -- ratio %.3f, speed %.3f, filtered speed %.3f\n", num_inp, num_ref,
191 double(num_inp) / double(num_ref), speed * double(num_inp) / double(num_ref), ns);