/* nag_tsa_auto_corr (g13abc) Example Program. * * Copyright 1991 Numerical Algorithms Group. * * Mark 2, 1991. * Mark 8 revised, 2004. * */ #include #include #include #include #include int main(int argc, char *argv[]) { FILE *fpin, *fpout; Integer exit_status = 0, i, nk, nx; NagError fail; double mean, *r = 0, stat, *x = 0, xv; 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_tsa_auto_corr (g13abc) Example Program Results\n"); /* Skip heading in data file */ fscanf(fpin, "%*[^\n]"); fscanf(fpin, "%ld %ld", &nx, &nk); if (nk > 0 && nx > 1 && nk < nx) { if (!(r = NAG_ALLOC(nk, double)) || !(x = NAG_ALLOC(nx, double))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } } else { fprintf(fpout, "Invalid nx or nk.\n"); exit_status = 1; return exit_status; } for (i = 0; i < nx; ++i) fscanf(fpin, "%lf", &x[i]); fprintf(fpout, "\nThe first %2ld coefficients are required\n", nk); /* nag_tsa_auto_corr (g13abc). * Sample autocorrelation function */ nag_tsa_auto_corr(x, nx, nk, &mean, &xv, r, &stat, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_tsa_auto_corr (g13abc).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, "The input array has sample mean %12.4f\n", mean); fprintf(fpout, "The input array has sample variance %12.4f\n", xv); fprintf(fpout, "The sample autocorrelation coefficients are\n\n"); fprintf(fpout, " Lag Coeff\n"); for (i = 0; i < 10; ++i) fprintf(fpout, "%6ld%10.4f\n", i+1, r[i]); fprintf(fpout, "\nThe value of stat is %12.4f\n", stat); END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (r) NAG_FREE(r); if (x) NAG_FREE(x); return exit_status; }