// // Convert frequency to Time interval error. // // - Input is a file of frequency readings. // - User gives nominal frequency. // - User gives sample period. // - Output is accumulated time error. // // 14-Mar-2009 tvb // #include #include void main (int argc, char *argv[]) { double Count, f0, t0, f, t; char Format[1000], Line[1000], TimeStamp[1000]; if (argc < 2) { fprintf(stderr, "Usage: %s [f0] [tau] [format]\n", argv[0]); exit(0); } f0 = (argc > 1) ? atof(argv[1]) : 1.0; fprintf(stderr, "f0 %lg\n", f0); t0 = (argc > 2) ? atof(argv[2]) : 1.0; fprintf(stderr, "t0 %lg\n", t0); sprintf(Format, "%%%s\n", (argc > 3) ? argv[3] : ".12lg"); fprintf(stderr, "format %s", Format); t = 0.0; for (Count = 0; fgets(Line, sizeof(Line), stdin) != NULL; Count += 1) { // TimeStamp is optional if (2 == sscanf(Line, "%s %lf", TimeStamp, &f)) { printf("%s ", TimeStamp); } else { sscanf(Line, "%lf", &f); } t += ((f - f0) / f0) * t0; printf(Format, t); } } #if 0 Sample input file: C:\tvb\cl> field 2 < \tmp\log23475.dat | head -5 60.0065776684 60.0080932691 60.0061470697 60.0023879296 60.0023691993 These are 100 second average 60 Hz mains frequency measurements. Sampe usage: C:\tvb\cl> field 2 < \tmp\log23475.dat | fr2ti 60 100 | head -5 0.0109627806666642 0.0244515625000001 0.0346966786666651 0.0386765613333277 0.0426252268333253 or, keeping time stamps: C:\tvb\cl> fr2ti 60 100 .6le < \tmp\log23475.dat | head -5 f0 60 t0 100 format %.6le 53750.320438 1.096278e-002 53750.321597 2.445156e-002 53750.322756 3.469668e-002 53750.323915 3.867656e-002 53750.325074 4.262523e-002 #endif