/* nag_wfilt (c09aac) Example Program. * * Copyright 2008, Numerical Algorithms Group. * * Mark 9, 2009. */ /* Pre-processor includes */ #include #include #include #include #include #include #include int main(int argc, char *argv[]) { FILE *fpin, *fpout; /* Constants */ Integer licomm = 100; /*Integer scalar and array declarations */ Integer exit_status = 0; Integer i, n, nf, nnz, nwc, nwl, ny; Integer *dwtlev = 0, *icomm = 0; NagError fail; Nag_Wavelet wavnamenum; Nag_WaveletMode modenum; /*Double scalar and array declarations */ double *c = 0, *x = 0, *y = 0; /*Character scalar and array declarations */ char mode[24], wavnam[20]; 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_wfilt (c09aac) Example Program Results\n\n"); /* Skip heading in data file*/ fscanf(fpin, "%*[^\n] "); /* Read n - length of input data sequence*/ fscanf(fpin, "%ld%*[^\n] ", &n); if (!(x = NAG_ALLOC(n, double)) || !(y = NAG_ALLOC(n, double)) || !(icomm = NAG_ALLOC(licomm, Integer))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } /* Read Wavelet name (wavnam) and end mode (mode)*/ fscanf(fpin, "%s%s%*[^\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) { fprintf(fpout, " Parameters read from file :: \n"); fprintf(fpout, " Wavelet :%15s\n", wavnam); fprintf(fpout, " End mode :%15s\n", mode); fprintf(fpout, " N :%15ld\n\n", n); /* Read data array and write it out*/ fprintf(fpout, "%s\n", " Input Data X :"); for (i = 0; i < n; i++) { fscanf(fpin, "%lf ", &x[i]); fprintf(fpout, "%8.3f%s", x[i], (i+1)%8?" ":"\n"); } fscanf(fpin, "%*[^\n] "); fprintf(fpout, "\n"); /* * nag_wfilt (c09aac) * Wavelet filter query */ nag_wfilt(wavnamenum, Nag_Multilevel, modenum, n, &nwl, &nf, &nwc, icomm, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_wfilt (c09aac).\n%s\n", fail.message); exit_status = 1; goto END; } if (!(c = NAG_ALLOC(nwc, double)) || !(dwtlev = NAG_ALLOC(nwl+1, Integer))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } /* Perform Discrete Wavelet transform*/ /* * nag_mldwt (c09ccc) * one-dimensional multi-level discrete wavelet transform (mldwt) */ nag_mldwt(n, x, nwc, c, nwl, dwtlev, icomm, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_mldwt (c09ccc).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, " Length of wavelet filter : %10ld\n", nf); fprintf(fpout, " Number of Levels : %10ld\n", nwl); fprintf(fpout, " Number of coefficients in each level : \n"); for (i = 0; i < nwl+1; i++) fprintf(fpout, " %8ld%s", dwtlev[i], (i+1)%8?" ":"\n"); fprintf(fpout, "\n"); fprintf(fpout, " Total number of wavelet coefficients :%10ld\n", nwc); nnz = 0; for (i = 0; i < nwl+1; i++) nnz = nnz+dwtlev[i]; fprintf(fpout, "\n"); fprintf(fpout, " Wavelet coefficients C : \n"); for (i = 0; i < nnz; i++) fprintf(fpout, "%8.3f%s", c[i], (i+1)%8?" ":"\n"); fprintf(fpout, "\n"); /* Reconstruct original data*/ ny = n; /* * nag_imldwt (c09cdc) * one-dimensional inverse multi-level discrete wavelet transform * (imldwt) */ nag_imldwt(nwl, nwc, c, n, y, icomm, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_imldwt (c09cdc).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, "\n"); fprintf(fpout, " Reconstruction Y : \n"); for (i = 0; i < ny; i++) fprintf(fpout, "%8.3f%s", y[i], (i+1)%8?" ":"\n"); } END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (c) NAG_FREE(c); if (dwtlev) NAG_FREE(dwtlev); if (x) NAG_FREE(x); if (y) NAG_FREE(y); if (icomm) NAG_FREE(icomm); return exit_status; }