mirdwt.c
上传用户:speoil
上传日期:2022-06-23
资源大小:224k
文件大小:3k
源码类别:

波变换

开发平台:

Matlab

  1. /*
  2. File Name: mirdwt.c
  3. Last Modification Date: %G% %U%
  4. Current Version: %M% %I%
  5. File Creation Date: Wed Oct 12 08:44:43 1994
  6. Author: Markus Lang  <lang@jazz.rice.edu>
  7. Copyright: All software, documentation, and related files in this distribution
  8.            are Copyright (c) 1994  Rice University
  9. Permission is granted for use and non-profit distribution providing that this
  10. notice be clearly maintained. The right to distribute any portion for profit
  11. or as part of any commercial product is specifically reserved for the author.
  12. Change History: Fixed code such that the result has the same dimension as the 
  13.                 input for 1D problems. Also, added some standard error checking.
  14. Jan Erik Odegard <odegard@ece.rice.edu> Wed Jun 14 1995
  15. */
  16. #include <math.h>
  17. //#include <malloc.h>
  18. #include <stdio.h>
  19. #include "mex.h"
  20. #include "matrix.h"
  21. #define max(A,B) (A > B ? A : B)
  22. #define min(A,B) (A < B ? A : B)
  23. #define even(x)  ((x & 1) ? 0 : 1)
  24. #define isint(x) ((x - floor(x)) > 0.0 ? 0 : 1)
  25. void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
  26. {
  27.   double *x, *h,  *yl, *yh, *Lf, *Lr;
  28.   int m, n, mh, nh, h_col, h_row, lh, L, i, po2, j;
  29.   double mtest, ntest;
  30.   /* check for correct # of input variables */
  31.   if (nrhs>4){
  32.     mexErrMsgTxt("There are at most 4 input parameters allowed!");
  33.     return;
  34.   }
  35.   if (nrhs<3){
  36.     mexErrMsgTxt("There are at least 3 input parameters required!");
  37.     return;
  38.   }
  39.   yl = mxGetPr(prhs[0]);
  40.   n = mxGetN(prhs[0]); 
  41.   m = mxGetM(prhs[0]); 
  42.   yh = mxGetPr(prhs[1]);
  43.   nh = mxGetN(prhs[1]); 
  44.   mh = mxGetM(prhs[1]); 
  45.   h = mxGetPr(prhs[2]);
  46.   h_col = mxGetN(prhs[2]); 
  47.   h_row = mxGetM(prhs[2]); 
  48.   if (h_col>h_row)
  49.     lh = h_col;
  50.   else  
  51.     lh = h_row;
  52.   if (nrhs == 4){
  53.     L = (int) *mxGetPr(prhs[3]);
  54.     if (L < 0)
  55.       mexErrMsgTxt("The number of levels, L, must be a non-negative integer");
  56.   }
  57.   else /* Estimate L */ {
  58.     i=n;j=0;
  59.     while (even(i)){
  60.       i=(i>>1);
  61.       j++;
  62.     }
  63.     L=m;i=0;
  64.     while (even(L)){
  65.       L=(L>>1);
  66.       i++;
  67.     }
  68.     if(min(m,n) == 1)
  69.       L = max(i,j);
  70.     else
  71.       L = min(i,j);
  72.     if (L==0){
  73.       mexErrMsgTxt("Maximum number of levels is zero; no decomposition can be performed!");
  74.       return;
  75.     }
  76.   }
  77.   /* check for consistency of rows and columns of yl, yh */
  78.   if (min(m,n) > 1){
  79.     if((m != mh) | (3*n*L != nh)){
  80.       mexErrMsgTxt("Dimensions of first two input matrices not consistent!");
  81.       return;
  82.     }
  83.   }
  84.   else{
  85.     if((m != mh) | (n*L != nh)){
  86.       mexErrMsgTxt("Dimensions of first two input vectors not consistent!");{
  87. return;
  88.       }
  89.     }
  90.   }
  91.   /* Check the ROW dimension of input */
  92.   if(m > 1){
  93.     mtest = (double) m/pow(2.0, (double) L);
  94.     if (!isint(mtest))
  95.       mexErrMsgTxt("The matrix row dimension must be of size m*2^(L)");
  96.   }
  97.   /* Check the COLUMN dimension of input */
  98.   if(n > 1){
  99.     ntest = (double) n/pow(2.0, (double) L);
  100.     if (!isint(ntest))
  101.       mexErrMsgTxt("The matrix column dimension must be of size n*2^(L)");
  102.   }
  103.   plhs[0] = mxCreateDoubleMatrix(m,n,mxREAL);
  104.   x = mxGetPr(plhs[0]);
  105.   plhs[1] = mxCreateDoubleMatrix(1,1,mxREAL);
  106.   Lr = mxGetPr(plhs[1]);
  107.   *Lr = L;
  108.   MIRDWT(x, m, n, h, lh, L, yl, yh);
  109. }