annotate util/Rice Wavelet Toolbox/mrdwt.c @ 147:65fc57f3903c ivand_dev

Merge from the default branch
author Ivan Damnjanovic lnx <ivan.damnjanovic@eecs.qmul.ac.uk>
date Tue, 26 Jul 2011 15:55:14 +0100
parents f69ae88b8be5
children
rev   line source
ivan@78 1 /*
ivan@78 2 File Name: mrdwt.c
ivan@78 3 Last Modification Date: %G% %U%
ivan@78 4 Current Version: %M% %I%
ivan@78 5 File Creation Date: Wed Oct 12 08:44:43 1994
ivan@78 6 Author: Markus Lang <lang@jazz.rice.edu>
ivan@78 7
ivan@78 8 Copyright: All software, documentation, and related files in this distribution
ivan@78 9 are Copyright (c) 1994 Rice University
ivan@78 10
ivan@78 11 Permission is granted for use and non-profit distribution providing that this
ivan@78 12 notice be clearly maintained. The right to distribute any portion for profit
ivan@78 13 or as part of any commercial product is specifically reserved for the author.
ivan@78 14
ivan@78 15 Change History: Fixed code such that the result has the same dimension as the
ivan@78 16 input for 1D problems. Also, added some standard error checking.
ivan@78 17 Jan Erik Odegard <odegard@ece.rice.edu> Wed Jun 14 1995
ivan@78 18 */
ivan@78 19
ivan@78 20 #include <math.h>
ivan@78 21 /*#include <malloc.h>*/
ivan@78 22 #include <stdio.h>
ivan@78 23 #include "mex.h"
ivan@78 24 #include "matrix.h"
ivan@78 25 #if !defined(_WIN32) && !defined(_WIN64)
ivan@78 26 #include <inttypes.h>
ivan@78 27 #endif
ivan@78 28 #define max(A,B) (A > B ? A : B)
ivan@78 29 #define min(A,B) (A < B ? A : B)
ivan@78 30 #define even(x) ((x & 1) ? 0 : 1)
ivan@78 31 #define isint(x) ((x - floor(x)) > 0.0 ? 0 : 1)
ivan@78 32
ivan@78 33
ivan@78 34 void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
ivan@78 35
ivan@78 36 {
ivan@78 37 double *x, *h, *yl, *yh, *Lf, *Lr;
ivan@78 38 intptr_t m, n, h_col, h_row, lh, L, i, po2, j;
ivan@78 39 double mtest, ntest;
ivan@78 40
ivan@78 41 /* check for correct # of input variables */
ivan@78 42 if (nrhs>3){
ivan@78 43 mexErrMsgTxt("There are at most 3 input parameters allowed!");
ivan@78 44 return;
ivan@78 45 }
ivan@78 46 if (nrhs<2){
ivan@78 47 mexErrMsgTxt("There are at least 2 input parameters required!");
ivan@78 48 return;
ivan@78 49 }
ivan@78 50 x = mxGetPr(prhs[0]);
ivan@78 51 n = mxGetN(prhs[0]);
ivan@78 52 m = mxGetM(prhs[0]);
ivan@78 53 h = mxGetPr(prhs[1]);
ivan@78 54 h_col = mxGetN(prhs[1]);
ivan@78 55 h_row = mxGetM(prhs[1]);
ivan@78 56 if (h_col>h_row)
ivan@78 57 lh = h_col;
ivan@78 58 else
ivan@78 59 lh = h_row;
ivan@78 60 if (nrhs == 3){
ivan@78 61 L = (intptr_t) *mxGetPr(prhs[2]);
ivan@78 62 if (L < 0)
ivan@78 63 mexErrMsgTxt("The number of levels, L, must be a non-negative integer");
ivan@78 64 }
ivan@78 65 else /* Estimate L */ {
ivan@78 66 i=n;j=0;
ivan@78 67 while (even(i)){
ivan@78 68 i=(i>>1);
ivan@78 69 j++;
ivan@78 70 }
ivan@78 71 L=m;i=0;
ivan@78 72 while (even(L)){
ivan@78 73 L=(L>>1);
ivan@78 74 i++;
ivan@78 75 }
ivan@78 76 if(min(m,n) == 1)
ivan@78 77 L = max(i,j);
ivan@78 78 else
ivan@78 79 L = min(i,j);
ivan@78 80 if (L==0){
ivan@78 81 mexErrMsgTxt("Maximum number of levels is zero; no decomposition can be performed!");
ivan@78 82 return;
ivan@78 83 }
ivan@78 84 }
ivan@78 85 /* Check the ROW dimension of input */
ivan@78 86 if(m > 1){
ivan@78 87 mtest = (double) m/pow(2.0, (double) L);
ivan@78 88 if (!isint(mtest))
ivan@78 89 mexErrMsgTxt("The matrix row dimension must be of size m*2^(L)");
ivan@78 90 }
ivan@78 91 /* Check the COLUMN dimension of input */
ivan@78 92 if(n > 1){
ivan@78 93 ntest = (double) n/pow(2.0, (double) L);
ivan@78 94 if (!isint(ntest))
ivan@78 95 mexErrMsgTxt("The matrix column dimension must be of size n*2^(L)");
ivan@78 96 }
ivan@78 97 plhs[0] = mxCreateDoubleMatrix(m,n,mxREAL);
ivan@78 98 yl = mxGetPr(plhs[0]);
ivan@78 99 if (min(m,n) == 1)
ivan@78 100 plhs[1] = mxCreateDoubleMatrix(m,L*n,mxREAL);
ivan@78 101 else
ivan@78 102 plhs[1] = mxCreateDoubleMatrix(m,3*L*n,mxREAL);
ivan@78 103 yh = mxGetPr(plhs[1]);
ivan@78 104 plhs[2] = mxCreateDoubleMatrix(1,1,mxREAL);
ivan@78 105 Lr = mxGetPr(plhs[2]);
ivan@78 106 *Lr = L;
ivan@78 107 MRDWT(x, m, n, h, lh, L, yl, yh);
ivan@78 108 }
ivan@78 109 #define mat(a, i, j) (*(a + (m*(j)+i)))
ivan@78 110
ivan@78 111
ivan@78 112 #ifdef __STDC__
ivan@78 113 MRDWT(double *x, intptr_t m, intptr_t n, double *h, intptr_t lh, intptr_t L,
ivan@78 114 double *yl, double *yh)
ivan@78 115 #else
ivan@78 116 MRDWT(x, m, n, h, lh, L, yl, yh)
ivan@78 117 double *x, *h, *yl, *yh;
ivan@78 118 intptr_t m, n, lh, L;
ivan@78 119 #endif
ivan@78 120 {
ivan@78 121 double *tmp;
ivan@78 122 double *h0, *h1, *ydummyll, *ydummylh, *ydummyhl;
ivan@78 123 double *ydummyhh, *xdummyl , *xdummyh;
ivan@78 124 long i, j;
ivan@78 125 intptr_t actual_L, actual_m, actual_n, c_o_a, ir, n_c, n_cb, n_c_o;
ivan@78 126 intptr_t ic, n_r, n_rb, n_r_o, c_o_a_p2n, sample_f;
ivan@78 127 xdummyl = (double *)(intptr_t)mxCalloc(max(m,n)+lh-1,sizeof(double));
ivan@78 128 xdummyh = (double *)(intptr_t)mxCalloc(max(m,n)+lh-1,sizeof(double));
ivan@78 129 ydummyll = (double *)(intptr_t)mxCalloc(max(m,n),sizeof(double));
ivan@78 130 ydummylh = (double *)(intptr_t)mxCalloc(max(m,n),sizeof(double));
ivan@78 131 ydummyhl = (double *)(intptr_t)mxCalloc(max(m,n),sizeof(double));
ivan@78 132 ydummyhh = (double *)(intptr_t)mxCalloc(max(m,n),sizeof(double));
ivan@78 133 h0 = (double *)(intptr_t)mxCalloc(lh,sizeof(double));
ivan@78 134 h1 = (double *)(intptr_t)mxCalloc(lh,sizeof(double));
ivan@78 135
ivan@78 136 if (n==1){
ivan@78 137 n = m;
ivan@78 138 m = 1;
ivan@78 139 }
ivan@78 140 /* analysis lowpass and highpass */
ivan@78 141 for (i=0; i<lh; i++){
ivan@78 142 h0[i] = h[lh-i-1];
ivan@78 143 h1[i] =h[i];
ivan@78 144 }
ivan@78 145 for (i=0; i<lh; i+=2)
ivan@78 146 h1[i] = -h1[i];
ivan@78 147
ivan@78 148 actual_m = 2*m;
ivan@78 149 actual_n = 2*n;
ivan@78 150 for (i=0; i<m*n; i++)
ivan@78 151 yl[i] = x[i];
ivan@78 152
ivan@78 153 /* main loop */
ivan@78 154 sample_f = 1;
ivan@78 155 for (actual_L=1; actual_L <= L; actual_L++){
ivan@78 156 actual_m = actual_m/2;
ivan@78 157 actual_n = actual_n/2;
ivan@78 158 /* actual (level dependent) column offset */
ivan@78 159 if (m==1)
ivan@78 160 c_o_a = n*(actual_L-1);
ivan@78 161 else
ivan@78 162 c_o_a = 3*n*(actual_L-1);
ivan@78 163 c_o_a_p2n = c_o_a + 2*n;
ivan@78 164
ivan@78 165 /* go by rows */
ivan@78 166 n_cb = n/actual_n; /* # of column blocks per row */
ivan@78 167 for (ir=0; ir<m; ir++){ /* loop over rows */
ivan@78 168 for (n_c=0; n_c<n_cb; n_c++){ /* loop within one row */
ivan@78 169 /* store in dummy variable */
ivan@78 170 ic = -sample_f + n_c;
ivan@78 171 for (i=0; i<actual_n; i++){
ivan@78 172 ic = ic + sample_f;
ivan@78 173 xdummyl[i] = mat(yl, ir, ic);
ivan@78 174 }
ivan@78 175 /* perform filtering lowpass/highpass */
ivan@78 176 fpconv(xdummyl, actual_n, h0, h1, lh, ydummyll, ydummyhh);
ivan@78 177 /* restore dummy variables in matrices */
ivan@78 178 ic = -sample_f + n_c;
ivan@78 179 for (i=0; i<actual_n; i++){
ivan@78 180 ic = ic + sample_f;
ivan@78 181 mat(yl, ir, ic) = ydummyll[i];
ivan@78 182 mat(yh, ir, c_o_a+ic) = ydummyhh[i];
ivan@78 183 }
ivan@78 184 }
ivan@78 185 }
ivan@78 186
ivan@78 187 /* go by columns in case of a 2D signal*/
ivan@78 188 if (m>1){
ivan@78 189 n_rb = m/actual_m; /* # of row blocks per column */
ivan@78 190 for (ic=0; ic<n; ic++){ /* loop over column */
ivan@78 191 for (n_r=0; n_r<n_rb; n_r++){ /* loop within one column */
ivan@78 192 /* store in dummy variables */
ivan@78 193 ir = -sample_f + n_r;
ivan@78 194 for (i=0; i<actual_m; i++){
ivan@78 195 ir = ir + sample_f;
ivan@78 196 xdummyl[i] = mat(yl, ir, ic);
ivan@78 197 xdummyh[i] = mat(yh, ir,c_o_a+ic);
ivan@78 198 }
ivan@78 199 /* perform filtering: first LL/LH, then HL/HH */
ivan@78 200 fpconv(xdummyl, actual_m, h0, h1, lh, ydummyll, ydummylh);
ivan@78 201 fpconv(xdummyh, actual_m, h0, h1, lh, ydummyhl, ydummyhh);
ivan@78 202 /* restore dummy variables in matrices */
ivan@78 203 ir = -sample_f + n_r;
ivan@78 204 for (i=0; i<actual_m; i++){
ivan@78 205 ir = ir + sample_f;
ivan@78 206 mat(yl, ir, ic) = ydummyll[i];
ivan@78 207 mat(yh, ir, c_o_a+ic) = ydummylh[i];
ivan@78 208 mat(yh, ir,c_o_a+n+ic) = ydummyhl[i];
ivan@78 209 mat(yh, ir, c_o_a_p2n+ic) = ydummyhh[i];
ivan@78 210 }
ivan@78 211 }
ivan@78 212 }
ivan@78 213 }
ivan@78 214 sample_f = sample_f*2;
ivan@78 215 }
ivan@78 216 }
ivan@78 217
ivan@78 218 #ifdef __STDC__
ivan@78 219 fpconv(double *x_in, intptr_t lx, double *h0, double *h1, intptr_t lh,
ivan@78 220 double *x_outl, double *x_outh)
ivan@78 221 #else
ivan@78 222 fpconv(x_in, lx, h0, h1, lh, x_outl, x_outh)
ivan@78 223 double *x_in, *h0, *h1, *x_outl, *x_outh;
ivan@78 224 intptr_t lx, lh;
ivan@78 225 #endif
ivan@78 226 {
ivan@78 227 intptr_t i, j;
ivan@78 228 double x0, x1;
ivan@78 229
ivan@78 230 for (i=lx; i < lx+lh-1; i++)
ivan@78 231 x_in[i] = x_in[i-lx];
ivan@78 232 for (i=0; i<lx; i++){
ivan@78 233 x0 = 0;
ivan@78 234 x1 = 0;
ivan@78 235 for (j=0; j<lh; j++){
ivan@78 236 x0 = x0 + x_in[j+i]*h0[lh-1-j];
ivan@78 237 x1 = x1 + x_in[j+i]*h1[lh-1-j];
ivan@78 238 }
ivan@78 239 x_outl[i] = x0;
ivan@78 240 x_outh[i] = x1;
ivan@78 241 }
ivan@78 242 }