/* nag_dwt (c09cac) Example Program.
 *
 * Copyright 2017 Numerical Algorithms Group.
 *
 * Mark 26.1, 2017.
 */
/* Pre-processor includes */
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagc09.h>

int main(void)
{
  /* Constants */
  Integer licomm = 100;
  /*Integer scalar and array declarations */
  Integer exit_status = 0;
  Integer i, n, nf, nwc, nwl, ny;
  Integer *icomm = 0;
  NagError fail;
  Nag_Wavelet wavnamenum;
  Nag_WaveletMode modenum;
  /*Double scalar and array declarations */
  double *ca = 0, *cd = 0, *x = 0, *y = 0;
  /*Character scalar and array declarations */
  char mode[24], wavnam[20];

  INIT_FAIL(fail);

  printf("nag_dwt (c09cac) Example Program Results\n\n");
  fflush(stdout);

  /*     Skip heading in data file */
  scanf("%*[^\n] ");
  /*     Read n */
  scanf("%" NAG_IFMT "%*[^\n] ", &n);
  if (!(x = NAG_ALLOC(n, double)) ||
      !(y = NAG_ALLOC(n, double)) || !(icomm = NAG_ALLOC(licomm, Integer)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  /*     Read wavnam, mode */
  scanf("%19s%23s%*[^\n] ", wavnam, mode);
  /*
   * nag_enum_name_to_value (x04nac).
   * Converts NAG enum member name to value
   */
  wavnamenum = (Nag_Wavelet) nag_enum_name_to_value(wavnam);
  modenum = (Nag_WaveletMode) nag_enum_name_to_value(mode);
  if (n >= 2) {
    printf("DWT :: \n");
    printf("     Wavelet  :%16s\n", wavnam);
    printf("     End mode :%16s\n", mode);
    printf("     N        :%16" NAG_IFMT "\n\n", n);
    /*        Read array */
    printf("%s\n", "Input Data                  X :");
    for (i = 0; i < n; i++) {
      scanf("%lf ", &x[i]);
      printf("%8.4f%s", x[i], (i + 1) % 8 ? " " : "\n");
    }
    printf("\n");
    /*
     * nag_wfilt (c09aac)
     * Wavelet filter query
     */
    nag_wfilt(wavnamenum, Nag_SingleLevel, modenum, n, &nwl, &nf, &nwc,
              icomm, &fail);
    if (fail.code != NE_NOERROR) {
      printf("Error from nag_wfilt (c09aac).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }
    if (!(ca = NAG_ALLOC(nwc, double)) || !(cd = NAG_ALLOC(nwc, double)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
    /*
     * nag_dwt (c09cac)
     * one-dimensional discrete wavelet transform (dwt)
     */
    nag_dwt(n, x, nwc, ca, cd, icomm, &fail);
    if (fail.code != NE_NOERROR) {
      printf("Error from nag_dwt (c09cac).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }
    printf("Approximation coefficients CA : \n");
    for (i = 0; i < nwc; i++)
      printf("%8.4f%s", ca[i] * sqrt(2.00e0), (i + 1) % 8 ? " " : "\n");
    printf("\n");
    printf("Detail coefficients        CD : \n");
    for (i = 0; i < nwc; i++)
      printf("%8.4f%s", cd[i] * sqrt(2.00e0), (i + 1) % 8 ? " " : "\n");
    printf("\n\n");
    if (modenum == Nag_Periodic) {
      ny = 2 * nwc;
    }
    else {
      ny = n;
    }
    /*
     * nag_idwt (c09cbc)
     * one-dimensional inverse discrete wavelet transform (IDWT)
     */
    nag_idwt(nwc, ca, cd, n, y, icomm, &fail);
    if (fail.code != NE_NOERROR) {
      printf("Error from nag_idwt (c09cbc).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }
    printf("Reconstruction              Y : \n");
    for (i = 0; i < ny; i++)
      printf("%8.4f%s", y[i], (i + 1) % 8 ? " " : "\n");
  }

END:
  NAG_FREE(ca);
  NAG_FREE(cd);
  NAG_FREE(x);
  NAG_FREE(y);
  NAG_FREE(icomm);

  return exit_status;
}