/* nag_tsa_cross_spectrum_bivar (g13cec) Example Program. * * Copyright 1996 Numerical Algorithms Group. * * Mark 4, 1996. * Mark 8 revised, 2004. * */ #include #include #include #include #include #define L 80 #define KC 8*L #define NGMAX KC #define NXYMAX 300 int main(void) { Complex *xyg; Integer exit_status = 0, i, is, j, kc = KC, l = L, mw, ng, nxy; NagError fail; double *ca = 0, *calw = 0, *caup = 0, pw, pxy, *sc = 0, *sclw = 0; double *scup = 0, stats[4], t, *x = 0, *xg, *y = 0, *yg; INIT_FAIL(fail); printf( "nag_tsa_cross_spectrum_bivar (g13cec) Example Program Results\n"); /* Skip heading in data file */ scanf("%*[^\n] "); scanf("%ld ", &nxy); if (nxy > 0 && nxy <= NXYMAX) { if (!(x = NAG_ALLOC(KC, double)) || !(y = NAG_ALLOC(KC, double)) || !(ca = NAG_ALLOC(NGMAX, double)) || !(calw = NAG_ALLOC(NGMAX, double)) || !(caup = NAG_ALLOC(NGMAX, double)) || !(sc = NAG_ALLOC(NGMAX, double)) || !(sclw = NAG_ALLOC(NGMAX, double)) || !(scup = NAG_ALLOC(NGMAX, double))) { printf("Allocation failure\n"); exit_status = -1; goto END; } for (i = 1; i <= nxy; ++i) scanf("%lf ", &x[i - 1]); for (i = 1; i <= nxy; ++i) scanf("%lf ", &y[i - 1]); /* Set parameters for call to nag_tsa_spectrum_univar (g13cbc) and g13cdc * with mean correction and 10 percent taper */ pxy = 0.1; /* Window shape parameter and zero covariance at lag 16 */ pw = 0.5; mw = 16; /* Alignment shift of 3 */ is = 3; /* Obtain univariate spectrum for the x and the y series */ /* nag_tsa_spectrum_univar (g13cbc). * Univariate time series, smoothed sample spectrum using * spectral smoothing by the trapezium frequency (Daniell) * window */ nag_tsa_spectrum_univar(nxy, Nag_Mean, pxy, mw, pw, l, kc, Nag_Unlogged, x, &xg, &ng, stats, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_tsa_spectrum_univar (g13cbc).\n%s\n", fail.message); exit_status = 1; goto END; } /* nag_tsa_spectrum_univar (g13cbc), see above. */ nag_tsa_spectrum_univar(nxy, Nag_Mean, pxy, mw, pw, l, kc, Nag_Unlogged, y, &yg, &ng, stats, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_tsa_spectrum_univar (g13cbc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Obtain cross spectrum of the bivariate series */ /* nag_tsa_spectrum_bivar (g13cdc). * Multivariate time series, smoothed sample cross spectrum * using spectral smoothing by the trapezium frequency * (Daniell) window */ nag_tsa_spectrum_bivar(nxy, Nag_Mean, pxy, mw, is, pw, l, kc, x, y, &xyg, &ng, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_tsa_spectrum_bivar (g13cdc).\n%s\n", fail.message); exit_status = 1; goto END; } /* nag_tsa_cross_spectrum_bivar (g13cec). * Multivariate time series, cross amplitude spectrum, * squared coherency, bounds, univariate and bivariate * (cross) spectra */ nag_tsa_cross_spectrum_bivar(xg, yg, xyg, ng, stats, ca, calw, caup, &t, sc, sclw, scup, &fail); if (fail.code != NE_NOERROR) { printf( "Error from nag_tsa_cross_spectrum_bivar (g13cec).\n%s\n", fail.message); exit_status = 1; goto END; } printf("\n"); printf(" Cross amplitude spectrum\n\n"); printf(" Lower Upper\n"); printf(" Value bound bound\n\n"); for (j = 1; j <= ng; ++j) printf(" %5ld%10.4f%10.4f%10.4f\n", j - 1, ca[j - 1], calw[j - 1], caup[j - 1]); printf("\n"); printf(" Squared coherency test statistic =%12.4f\n\n", t); printf(" Squared coherency\n\n"); printf(" Lower Upper\n"); printf(" Value bound bound\n\n"); for (j = 1; j <= ng; ++j) printf(" %5ld%10.4f%10.4f%10.4f\n", j - 1, sc[j - 1], sclw[j - 1], scup[j - 1]); } NAG_FREE(xg); NAG_FREE(yg); NAG_FREE(xyg); END: if (x) NAG_FREE(x); if (y) NAG_FREE(y); if (ca) NAG_FREE(ca); if (calw) NAG_FREE(calw); if (caup) NAG_FREE(caup); if (sc) NAG_FREE(sc); if (sclw) NAG_FREE(sclw); if (scup) NAG_FREE(scup); return exit_status; }