Mercurial > hg > smallbox
comparison util/Rice Wavelet Toolbox/mdwt.c @ 78:f69ae88b8be5
added Rice Wavelet Toolbox with my modification, so it can be compiled on newer systems.
author | Ivan Damnjanovic lnx <ivan.damnjanovic@eecs.qmul.ac.uk> |
---|---|
date | Fri, 25 Mar 2011 15:27:33 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
76:d052ec5b742f | 78:f69ae88b8be5 |
---|---|
1 /* | |
2 File Name: mdwt.c | |
3 Last Modification Date: 06/14/95 12:56:43 | |
4 Current Version: mdwt.c 1.5 | |
5 File Creation Date: Wed Oct 12 08:44:43 1994 | |
6 Author: Markus Lang <lang@jazz.rice.edu> | |
7 | |
8 Copyright: All software, documentation, and related files in this distribution | |
9 are Copyright (c) 1994 Rice University | |
10 | |
11 Permission is granted for use and non-profit distribution providing that this | |
12 notice be clearly maintained. The right to distribute any portion for profit | |
13 or as part of any commercial product is specifically reserved for the author. | |
14 | |
15 Change History: Fixed code such that the result has the same dimension as the | |
16 input for 1D problems. Also, added some standard error checking. | |
17 Jan Erik Odegard <odegard@ece.rice.edu> Wed Jun 14 1995 | |
18 | |
19 MATLAB gateway for MDWT.c, discrete wavelet transform | |
20 */ | |
21 #include <math.h> | |
22 /*#include <malloc.h>*/ | |
23 #include <stdio.h> | |
24 #include "mex.h" | |
25 #include "matrix.h" | |
26 #if !defined(_WIN32) && !defined(_WIN64) | |
27 #include <inttypes.h> | |
28 #endif | |
29 #define max(A,B) (A > B ? A : B) | |
30 #define min(A,B) (A < B ? A : B) | |
31 #define even(x) ((x & 1) ? 0 : 1) | |
32 #define isint(x) ((x - floor(x)) > 0.0 ? 0 : 1) | |
33 #define mat(a, i, j) (*(a + (m*(j)+i))) /* macro for matrix indices */ | |
34 | |
35 void mexFunction(const int nlhs,mxArray *plhs[],const int nrhs,const mxArray *prhs[]) | |
36 | |
37 { | |
38 double *x, *h, *y, *Lf, *Lr; | |
39 intptr_t m, n, h_col, h_row, lh, L, i, po2, j; | |
40 double mtest, ntest; | |
41 | |
42 /* check for correct # of input variables */ | |
43 if (nrhs>3){ | |
44 mexErrMsgTxt("There are at most 3 input parameters allowed!"); | |
45 return; | |
46 } | |
47 if (nrhs<2){ | |
48 mexErrMsgTxt("There are at least 2 input parameters required!"); | |
49 return; | |
50 } | |
51 x = mxGetPr(prhs[0]); | |
52 n = mxGetN(prhs[0]); | |
53 m = mxGetM(prhs[0]); | |
54 h = mxGetPr(prhs[1]); | |
55 h_col = mxGetN(prhs[1]); | |
56 h_row = mxGetM(prhs[1]); | |
57 if (h_col>h_row) | |
58 lh = h_col; | |
59 else | |
60 lh = h_row; | |
61 if (nrhs == 3){ | |
62 L = (intptr_t) *mxGetPr(prhs[2]); | |
63 if (L < 0) | |
64 mexErrMsgTxt("The number of levels, L, must be a non-negative integer"); | |
65 } | |
66 else /* Estimate L */ { | |
67 i=n;j=0; | |
68 while (even(i)){ | |
69 i=(i>>1); | |
70 j++; | |
71 } | |
72 L=m;i=0; | |
73 while (even(L)){ | |
74 L=(L>>1); | |
75 i++; | |
76 } | |
77 if(min(m,n) == 1) | |
78 L = max(i,j); | |
79 else | |
80 L = min(i,j); | |
81 if (L==0){ | |
82 mexErrMsgTxt("Maximum number of levels is zero; no decomposition can be performed!"); | |
83 return; | |
84 } | |
85 } | |
86 /* Check the ROW dimension of input */ | |
87 if(m > 1){ | |
88 mtest = (double) m/pow(2.0, (double) L); | |
89 if (!isint(mtest)) | |
90 mexErrMsgTxt("The matrix row dimension must be of size m*2^(L)"); | |
91 } | |
92 /* Check the COLUMN dimension of input */ | |
93 if(n > 1){ | |
94 ntest = (double) n/pow(2.0, (double) L); | |
95 if (!isint(ntest)) | |
96 mexErrMsgTxt("The matrix column dimension must be of size n*2^(L)"); | |
97 } | |
98 plhs[0] = mxCreateDoubleMatrix(m,n,mxREAL); | |
99 y = mxGetPr(plhs[0]); | |
100 plhs[1] = mxCreateDoubleMatrix(1,1,mxREAL); | |
101 Lr = mxGetPr(plhs[1]); | |
102 *Lr = L; | |
103 MDWT(x, m, n, h, lh, L, y); | |
104 } | |
105 #ifdef __STDC__ | |
106 MDWT(double *x, intptr_t m, intptr_t n, double *h, intptr_t lh, intptr_t L, double *y) | |
107 #else | |
108 MDWT(x, m, n, h, lh, L, y) | |
109 double *x, *h, *y; | |
110 intptr_t m, n, lh, L; | |
111 #endif | |
112 { | |
113 double *h0, *h1, *ydummyl, *ydummyh, *xdummy; | |
114 int *prob; | |
115 intptr_t i, j; | |
116 intptr_t actual_L, actual_m, actual_n, r_o_a, c_o_a, ir, ic, lhm1; | |
117 | |
118 xdummy = (double *)mxCalloc(max(m,n)+lh-1,sizeof(double)); | |
119 ydummyl =(double *) (intptr_t)mxCalloc(max(m,n),sizeof(double)); | |
120 ydummyh = (double *)(intptr_t)mxCalloc(max(m,n),sizeof(double)); | |
121 h0 =(double *)(intptr_t)mxCalloc(lh,sizeof(double)); | |
122 h1 = (double *)(intptr_t)mxCalloc(lh,sizeof(double)); | |
123 | |
124 | |
125 /* analysis lowpass and highpass */ | |
126 if (n==1){ | |
127 n = m; | |
128 m = 1; | |
129 } | |
130 for (i=0; i<lh; i++){ | |
131 h0[i] = h[lh-i-1]; | |
132 h1[i] =h[i]; | |
133 } | |
134 for (i=0; i<lh; i+=2) | |
135 h1[i] = -h1[i]; | |
136 | |
137 lhm1 = lh - 1; | |
138 actual_m = 2*m; | |
139 actual_n = 2*n; | |
140 | |
141 /* main loop */ | |
142 for (actual_L=1; actual_L <= L; actual_L++){ | |
143 if (m==1) | |
144 actual_m = 1; | |
145 else{ | |
146 actual_m = actual_m/2; | |
147 r_o_a = actual_m/2; | |
148 } | |
149 actual_n = actual_n/2; | |
150 c_o_a = actual_n/2; | |
151 | |
152 /* go by rows */ | |
153 for (ir=0; ir<actual_m; ir++){ /* loop over rows */ | |
154 /* store in dummy variable */ | |
155 for (i=0; i<actual_n; i++) | |
156 if (actual_L==1) | |
157 xdummy[i] = mat(x, ir, i); | |
158 else | |
159 xdummy[i] = mat(y, ir, i); | |
160 /* perform filtering lowpass and highpass*/ | |
161 fpsconv(xdummy, actual_n, h0, h1, lhm1, ydummyl, ydummyh); | |
162 /* restore dummy variables in matrices */ | |
163 ic = c_o_a; | |
164 for (i=0; i<c_o_a; i++){ | |
165 mat(y, ir, i) = ydummyl[i]; | |
166 mat(y, ir, ic++) = ydummyh[i]; | |
167 } | |
168 } | |
169 | |
170 /* go by columns in case of a 2D signal*/ | |
171 if (m>1){ | |
172 for (ic=0; ic<actual_n; ic++){ /* loop over column */ | |
173 /* store in dummy variables */ | |
174 for (i=0; i<actual_m; i++) | |
175 xdummy[i] = mat(y, i, ic); | |
176 /* perform filtering lowpass and highpass*/ | |
177 fpsconv(xdummy, actual_m, h0, h1, lhm1, ydummyl, ydummyh); | |
178 /* restore dummy variables in matrix */ | |
179 ir = r_o_a; | |
180 for (i=0; i<r_o_a; i++){ | |
181 mat(y, i, ic) = ydummyl[i]; | |
182 mat(y, ir++, ic) = ydummyh[i]; | |
183 } | |
184 } | |
185 } | |
186 } | |
187 } | |
188 | |
189 #ifdef __STDC__ | |
190 fpsconv(double *x_in, intptr_t lx, double *h0, double *h1, intptr_t lhm1, | |
191 double *x_outl, double *x_outh) | |
192 #else | |
193 fpsconv(x_in, lx, h0, h1, lhm1, x_outl, x_outh) | |
194 double *x_in, *h0, *h1, *x_outl, *x_outh; | |
195 intptr_t lx, lhm1; | |
196 #endif | |
197 | |
198 { | |
199 intptr_t i, j, ind; | |
200 double x0, x1; | |
201 | |
202 for (i=lx; i < lx+lhm1; i++) | |
203 x_in[i] = *(x_in+(i-lx)); | |
204 ind = 0; | |
205 for (i=0; i<(lx); i+=2){ | |
206 x0 = 0; | |
207 x1 = 0; | |
208 for (j=0; j<=lhm1; j++){ | |
209 x0 = x0 + x_in[i+j]*h0[lhm1-j]; | |
210 x1 = x1 + x_in[i+j]*h1[lhm1-j]; | |
211 } | |
212 x_outl[ind] = x0; | |
213 x_outh[ind++] = x1; | |
214 } | |
215 } |