```/* nag_mlmodwt (c09dcc) 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, na, nf, nwc, nwcmax, nwlmax, nwl, nwlinv;
Integer *icomm = 0;
NagError fail;
Nag_Wavelet wavnamenum;
Nag_WaveletCoefficients keepnum;
Nag_WaveletMode modenum;
/*Double scalar and array declarations */
double *c = 0, *x = 0, *y = 0;
/*Character scalar and array declarations */
char keep[15], mode[24], wavnam[20];

INIT_FAIL(fail);

printf("nag_mlmodwt (c09dcc) Example Program Results\n\n");
fflush(stdout);

/*     Skip heading in data file */
scanf("%*[^\n] ");
/*     Read n - length of input data sequence */
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 Wavelet name (wavnam) and end mode (mode) */
scanf("%19s%23s%14s%*[^\n] ", wavnam, mode, keep);
/*
* 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);
keepnum = (Nag_WaveletCoefficients) nag_enum_name_to_value(keep);
if (n >= 2) {
printf("MLMODWT :: \n");
printf("     Wavelet            :%16s\n", wavnam);
printf("     End mode           :%16s\n", mode);
printf("     Store coefficients :%16s\n", keep);
printf("     N                  :%16" NAG_IFMT "\n\n", n);
/*        Read data array and write it out */
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_MODWTMulti, modenum, n, &nwlmax, &nf, &nwcmax,
icomm, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_wfilt (c09aac).\n%s\n", fail.message);
exit_status = 1;
goto END;
}
/* Choose to decompose over two levels */
nwl = 2;
/* Set size of array c according to number of coefficients stored */
if (keepnum == Nag_StoreFinal)
nwc = nwcmax - (nwlmax - nwl) * n;
else
nwc = nwcmax + (nwlmax - 1) * n - (nwlmax - nwl) * 2 * n;

if (!(c = NAG_ALLOC(nwc, double)))
{
printf("Allocation failure\n");
exit_status = -1;
goto END;
}
/*        Perform Maximal Overlap Discrete Wavelet transform */
/*
* nag_mlmodwt (c09dcc)
* one-dimensional multi-level maximal overlap discrete wavelet
* transform (mlmodwt)
*/
nag_mlmodwt(n, x, keepnum, nwc, c, nwl, &na, icomm, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_mlmodwt (c09dcc).\n%s\n", fail.message);
exit_status = 1;
goto END;
}
printf("   Number of Levels : %20" NAG_IFMT "\n", nwl);
printf("   Number of coefficients in each level : %20" NAG_IFMT "\n", na);
printf("   Wavelet coefficients C : \n");
for (i = 0; i < nwc; i++)
printf("%8.4f%s", c[i], (i + 1) % 8 ? " " : "\n");
printf("\n\n");
/*           Reconstruct original data */
nwlinv = nwl;
/*
* nag_imlmodwt (c09ddc)
* one-dimensional inverse multi-level discrete wavelet transform
* (imlmodwt)
*/
nag_imlmodwt(nwlinv, keepnum, nwc, c, n, y, icomm, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_imlmodwt (c09ddc).\n%s\n", fail.message);
exit_status = 1;
goto END;
}
printf("   Reconstruction              Y : \n");
for (i = 0; i < n; i++)
printf("%8.4f%s", y[i], (i + 1) % 8 ? " " : "\n");
printf("\n");
}

END:
NAG_FREE(c);
NAG_FREE(x);
NAG_FREE(y);
NAG_FREE(icomm);

return exit_status;
}
```