NAG Library Manual, Mark 27.3
Interfaces:  FL   CL   CPP   AD 

NAG CL Interface Introduction
Example description
/* nag_tsa_multi_gain_bivar (g13cfc) Example Program.
 *
 * Copyright 2021 Numerical Algorithms Group.
 *
 * Mark 27.3, 2021.
 *
 */

#include <nag.h>
#include <stdio.h>

#define L 80
#define KC 8 * L
#define NGMAX KC
#define NXYMAX 300

int main(void) {

  Complex *xyg = 0;
  Integer exit_status = 0, i, is, j, kc = KC, l = L, mw, ng, nxy;
  NagError fail;
  double *gn = 0, *gnlw = 0, *gnup = 0, *ph = 0, *phlw = 0, *phup = 0, pw, pxy,
         *stats = 0;
  double *x = 0, *xg = 0, *y = 0, *yg = 0;

  INIT_FAIL(fail);

  printf("nag_tsa_multi_gain_bivar (g13cfc) Example Program Results\n");

  /* Skip heading in data file */
  scanf("%*[^\n] ");
  scanf("%" NAG_IFMT " ", &nxy);
  if (nxy > 0 && nxy <= NXYMAX) {
    if (!(stats = NAG_ALLOC(4, double)) || !(x = NAG_ALLOC(KC, double)) ||
        !(y = NAG_ALLOC(KC, double)) || !(gnlw = NAG_ALLOC(NGMAX, double)) ||
        !(gnup = NAG_ALLOC(NGMAX, double)) ||
        !(phlw = NAG_ALLOC(NGMAX, double)) ||
        !(phup = NAG_ALLOC(NGMAX, double)) ||
        !(gn = NAG_ALLOC(NGMAX, double)) || !(ph = 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_uni_spectrum_daniell (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_uni_spectrum_daniell (g13cbc).
     * Univariate time series, smoothed sample spectrum using
     * spectral smoothing by the trapezium frequency (Daniell)
     * window
     */
    nag_tsa_uni_spectrum_daniell(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_uni_spectrum_daniell (g13cbc).\n%s\n",
             fail.message);
      exit_status = 1;
      goto END;
    }

    /* nag_tsa_uni_spectrum_daniell (g13cbc), see above. */
    nag_tsa_uni_spectrum_daniell(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_uni_spectrum_daniell (g13cbc).\n%s\n",
             fail.message);
      exit_status = 1;
      goto END;
    }

    /* Obtain cross spectrum of the bivariate series */
    /* nag_tsa_multi_spectrum_daniell (g13cdc).
     * Multivariate time series, smoothed sample cross spectrum
     * using spectral smoothing by the trapezium frequency
     * (Daniell) window
     */
    nag_tsa_multi_spectrum_daniell(nxy, Nag_Mean, pxy, mw, is, pw, l, kc, x, y,
                                   &xyg, &ng, &fail);
    if (fail.code != NE_NOERROR) {
      printf("Error from nag_tsa_multi_spectrum_daniell (g13cdc).\n%s\n",
             fail.message);
      exit_status = 1;
      goto END;
    }

    /* nag_tsa_multi_gain_bivar (g13cfc).
     * Multivariate time series, gain, phase, bounds, univariate
     * and bivariate (cross) spectra
     */
    nag_tsa_multi_gain_bivar(xg, yg, xyg, ng, stats, gn, gnlw, gnup, ph, phlw,
                             phup, &fail);
    if (fail.code != NE_NOERROR) {
      printf("Error from nag_tsa_multi_gain_bivar (g13cfc).\n%s\n",
             fail.message);
      exit_status = 1;
      goto END;
    }

    printf("\n");
    printf("               The gain\n\n");
    printf("                       Lower     Upper\n");
    printf("           Value       bound     bound\n\n");
    for (j = 1; j <= ng; ++j)
      printf("%6" NAG_IFMT " %10.4f %10.4f %10.4f\n", j - 1, gn[j - 1],
             gnlw[j - 1], gnup[j - 1]);
    printf("\n              The phase\n\n");
    printf("                       Lower     Upper\n");
    printf("           Value       bound     bound\n\n");
    for (j = 1; j <= ng; ++j)
      printf("%6" NAG_IFMT " %10.4f %10.4f %10.4f\n", j - 1, ph[j - 1],
             phlw[j - 1], phup[j - 1]);
  }
  NAG_FREE(xg);
  NAG_FREE(yg);
  NAG_FREE(xyg);
END:
  NAG_FREE(stats);
  NAG_FREE(x);
  NAG_FREE(y);
  NAG_FREE(gnlw);
  NAG_FREE(gnup);
  NAG_FREE(phlw);
  NAG_FREE(phup);
  NAG_FREE(gn);
  NAG_FREE(ph);
  return exit_status;
}