/* nag_dwt (c09cac) 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, 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); /* 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_dwt (c09cac) Example Program Results\n\n"); /* Skip heading in data file*/ fscanf(fpin, "%*[^\n] "); /* Read n*/ 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 wavnam, 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, "DWT :: \n"); fprintf(fpout, " Wavelet :%16s\n", wavnam); fprintf(fpout, " End mode :%16s\n", mode); fprintf(fpout, " N :%16ld\n\n", n); /* Read array*/ fprintf(fpout, "%s\n", "Input Data X :"); for (i = 0; i < n; i++) { fscanf(fpin, "%lf ", &x[i]); fprintf(fpout, "%8.4f%s", x[i], (i+1)%8?" ":"\n"); } fprintf(fpout, "\n"); /* * nag_wfilt (c09aac) * Wavelet filter query */ nag_wfilt(wavnamenum, Nag_Singlelevel, 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 (!(ca = NAG_ALLOC(nwc, double)) || !(cd = NAG_ALLOC(nwc, double))) { fprintf(fpout, "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) { fprintf(fpout, "Error from nag_dwt (c09cac).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, "Approximation coefficients CA : \n"); for (i = 0; i < nwc; i++) fprintf(fpout, "%8.4f%s", ca[i]*sqrt(2.00e0), (i+1)%8?" ":"\n"); fprintf(fpout, "\n"); fprintf(fpout, "Detail coefficients CD : \n"); for (i = 0; i < nwc; i++) fprintf(fpout, "%8.4f%s", cd[i]*sqrt(2.00e0), (i+1)%8?" ":"\n"); fprintf(fpout, "\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) { fprintf(fpout, "Error from nag_idwt (c09cbc).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, "Reconstruction Y : \n"); for (i = 0; i < ny; i++) fprintf(fpout, "%8.4f%s", y[i], (i+1)%8?" ":"\n"); } END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (ca) NAG_FREE(ca); if (cd) NAG_FREE(cd); if (x) NAG_FREE(x); if (y) NAG_FREE(y); if (icomm) NAG_FREE(icomm); return exit_status; }