/* nag_running_median_smoother (g10cac) Example Program. * * Copyright 1992 Numerical Algorithms Group. * * Mark 3, 1992. * Mark 8 revised, 2004. */ #include #include #include #include #include int main(int argc, char *argv[]) { FILE *fpin, *fpout; Integer exit_status = 0, i, isel, n; NagError fail; Nag_Smooth_Type smoother; double *rough = 0, *smooth = 0, *y = 0; INIT_FAIL(fail); /* Check for command-line IO options */ fpin = nag_example_file_io(argc, argv, "-data", NULL); fpout = nag_example_file_io(argc, argv, "-results", NULL); fprintf(fpout, "nag_running_median_smoother (g10cac) Example Program Results\n"); /* Skip heading in data file */ fscanf(fpin, "%*[^\n]"); fscanf(fpin, "%ld", &n); if (n >= 1) { if (!(rough = NAG_ALLOC(n, double)) || !(smooth = NAG_ALLOC(n, double)) || !(y = NAG_ALLOC(n, double))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } } else { fprintf(fpout, "Invalid n.\n"); exit_status = 1; return exit_status; } for (i = 0; i < n; ++i) fscanf(fpin, "%lf", &y[i]); for (isel = 1; isel <= 2; ++isel) { /* Select the smoothing method. */ if (isel == 1) smoother = Nag_4253H; else if (isel == 2) smoother = Nag_3RSSH; if (smoother == Nag_4253H) fprintf(fpout, "\nExample 1: Data smoothing using 4253H method.\n\n"); else if (smoother == Nag_3RSSH) fprintf(fpout, "\n\nExample 2: Data smoothing using 3RSSH method.\n\n"); /* nag_running_median_smoother (g10cac). * Compute smoothed data sequence using running median * smoothers */ nag_running_median_smoother(smoother, n, y, smooth, rough, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_running_median_smoother (g10cac).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, " index data smooth rough\n"); for (i = 0; i < n; ++i) fprintf(fpout, "%9ld%15.4f%15.4f%15.4f\n", i, y[i], smooth[i], rough[i]); } END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (rough) NAG_FREE(rough); if (smooth) NAG_FREE(smooth); if (y) NAG_FREE(y); return exit_status; }