// // unavg -- Unwind running average to get raw, pre-averaged values. // #include #include void main (int argc, char *argv[]) { long count; int n_avg; double temp, new_run_avg, old_run_avg; n_avg = (argc > 1) ? atoi(argv[1]) : 10; // // A running average, of say 10 temperature samples, is computed with: // new_run_avg = (9 * old_run_avg + temp) / 10 // // Now if all you have are the running averages and want to extract // the original raw data you need to unwind the average, which is to // reverse the above equation; i.e., to solve for temp given a series // of new_run_avg: // // Start with: new_run_avg = (9 * old_run_avg + temp) / 10 // then: 10 * new_run_avg = (9 * old_run_avg + temp) // and: 10 * new_run_avg - 9 * old_run_avg = temp // So: temp = 10 * new_run_avg - 9 * old_run_avg // for (count = 0; 1 == fscanf(stdin, "%lf", &new_run_avg); count += 1) { if (count > 0) { temp = n_avg * new_run_avg - (n_avg - 1) * old_run_avg; printf("%.6lf %.6lf\n", new_run_avg, temp); } old_run_avg = new_run_avg; } }