// // ti2favg -- Convert time interval to smoothed frequency average. // // - Frequency is usually calculated from phase using non-overlapping // averaging intervals. The longer the averaging interval the fewer // the number of frequency points and the more sparse the plot. // // - With this tool we use overlapping averaging intervals which makes // frequency plots as dense as the corresponding raw phase plots. // // - Input is phase (time interval), output is smoothed frequency. // // 20-Oct-2008 /tvb // #include #include #define MAX_HIST (1 << 17) double data[MAX_HIST]; #define WRAP(n) ( (n) % MAX_HIST ) int main (int argc, char *argv[]) { char line[1000]; unsigned long count, window, slide; double scale, favg; if (argc == 1) { fprintf(stderr, "%s [window] [slide] [scale]\n", argv[0]); exit(1); } // Get averaging window. window = (argc > 1) ? (long)atof(argv[1]) : 1; if (window < 1 || window >= MAX_HIST) { fprintf(stderr, "bad window %ld\n", window); exit(1); } fprintf(stderr, "window: %ld\n", window); // Get sliding window increment. // NOTE: slide = 1 for fully overlapped, running average // slide = window for back-to-back, conventional average slide = (argc > 2) ? atof(argv[2]) : window; if (slide < 1 || slide > window) { fprintf(stderr, "bad slide %lg\n", slide); exit(1); } fprintf(stderr, "slide: %ld\n", slide); // Get (optional) data scaling factor. scale = (argc > 3) ? atof(argv[3]) : 1.0; if (scale == 0.0) { fprintf(stderr, "bad scale %lg\n", scale); exit(1); } fprintf(stderr, "scale: %.3lg\n", scale); for (count = 0; NULL != fgets(line, sizeof line, stdin); count += 1) { data[WRAP(count)] = atof(line) * scale; if (count >= window && (count % slide) == 0) { favg = (data[WRAP(count)] - data[WRAP(count - window)]) / window; printf("%ld %.16le\n", count, favg); } } fprintf(stderr, "count: %ld\n", count); }