comparison util/Rice Wavelet Toolbox/midwt.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: midwt.c
3 Last Modification Date: 06/14/95 12:55:58
4 Current Version: midwt.c 1.4
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 */
20 #include <math.h>
21 /*#include <malloc.h>*/
22 #include <stdio.h>
23 #include "mex.h"
24 #include "matrix.h"
25 #if !defined(_WIN32) && !defined(_WIN64)
26 #include <inttypes.h>
27 #endif
28 #define max(A,B) (A > B ? A : B)
29 #define min(A,B) (A < B ? A : B)
30 #define even(x) ((x & 1) ? 0 : 1)
31 #define isint(x) ((x - floor(x)) > 0.0 ? 0 : 1)
32
33
34
35 void mexFunction(int nlhs,mxArray *plhs[],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 y = 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 x = mxGetPr(plhs[0]);
100 plhs[1] = mxCreateDoubleMatrix(1,1,mxREAL);
101 Lr = mxGetPr(plhs[1]);
102 *Lr = L;
103 MIDWT(x, m, n, h, lh, L, y);
104 }
105
106 #define mat(a, i, j) (*(a + (m*(j)+i))) /* macro for matrix indices */
107
108
109 #ifdef __STDC__
110 MIDWT(double *x, intptr_t m, intptr_t n, double *h, intptr_t lh, intptr_t L, double *y)
111 #else
112 MIDWT(x, m, n, h, lh, L, y)
113 double *x, *h, *y;
114 intptr_t m, n, lh, L;
115 #endif
116 {
117 double *g0, *g1, *ydummyl, *ydummyh, *xdummy;
118 intptr_t i, j;
119 intptr_t actual_L, actual_m, actual_n, r_o_a, c_o_a, ir, ic, lhm1, lhhm1, sample_f;
120 xdummy = (double *)mxCalloc(max(m,n),sizeof(double));
121 ydummyl = (double *)mxCalloc(max(m,n)+lh/2-1,sizeof(double));
122 ydummyh = (double *)(intptr_t)mxCalloc(max(m,n)+lh/2-1,sizeof(double));
123 g0 = (double *)(intptr_t)mxCalloc(lh,sizeof(double));
124 g1 = (double *)(intptr_t)mxCalloc(lh,sizeof(double));
125
126 if (n==1){
127 n = m;
128 m = 1;
129 }
130 /* synthesis lowpass and highpass */
131 for (i=0; i<lh; i++){
132 g0[i] = h[i];
133 g1[i] = h[lh-i-1];
134 }
135 for (i=1; i<=lh; i+=2)
136 g1[i] = -g1[i];
137
138 lhm1 = lh - 1;
139 lhhm1 = lh/2 - 1;
140 /* 2^L */
141 sample_f = 1;
142 for (i=1; i<L; i++)
143 sample_f = sample_f*2;
144
145 if (m>1)
146 actual_m = m/sample_f;
147 else
148 actual_m = 1;
149 actual_n = n/sample_f;
150
151 for (i=0; i<(m*n); i++)
152 x[i] = y[i];
153
154 /* main loop */
155 for (actual_L=L; actual_L >= 1; actual_L--){
156 r_o_a = actual_m/2;
157 c_o_a = actual_n/2;
158
159 /* go by columns in case of a 2D signal*/
160 if (m>1){
161 for (ic=0; ic<actual_n; ic++){ /* loop over column */
162 /* store in dummy variables */
163 ir = r_o_a;
164 for (i=0; i<r_o_a; i++){
165 ydummyl[i+lhhm1] = mat(x, i, ic);
166 ydummyh[i+lhhm1] = mat(x, ir++, ic);
167 }
168 /* perform filtering lowpass and highpass*/
169 bpsconv(xdummy, r_o_a, g0, g1, lhm1, lhhm1, ydummyl, ydummyh);
170 /* restore dummy variables in matrix */
171 for (i=0; i<actual_m; i++)
172 mat(x, i, ic) = xdummy[i];
173 }
174 }
175 /* go by rows */
176 for (ir=0; ir<actual_m; ir++){ /* loop over rows */
177 /* store in dummy variable */
178 ic = c_o_a;
179 for (i=0; i<c_o_a; i++){
180 ydummyl[i+lhhm1] = mat(x, ir, i);
181 ydummyh[i+lhhm1] = mat(x, ir, ic++);
182 }
183 /* perform filtering lowpass and highpass*/
184 bpsconv(xdummy, c_o_a, g0, g1, lhm1, lhhm1, ydummyl, ydummyh);
185 /* restore dummy variables in matrices */
186 for (i=0; i<actual_n; i++)
187 mat(x, ir, i) = xdummy[i];
188 }
189 if (m==1)
190 actual_m = 1;
191 else
192 actual_m = actual_m*2;
193 actual_n = actual_n*2;
194 }
195 }
196
197 #ifdef __STDC__
198 bpsconv(double *x_out, intptr_t lx, double *g0, double *g1, intptr_t lhm1,
199 intptr_t lhhm1, double *x_inl, double *x_inh)
200 #else
201 bpsconv(x_out, lx, g0, g1, lhm1, lhhm1, x_inl, x_inh)
202 double *x_inl, *x_inh, *g0, *g1, *x_out;
203 intptr_t lx, lhm1, lhhm1;
204 #endif
205 {
206 intptr_t i, j, ind, tj;
207 double x0, x1;
208
209 for (i=lhhm1-1; i > -1; i--){
210 x_inl[i] = x_inl[lx+i];
211 x_inh[i] = x_inh[lx+i];
212 }
213 ind = 0;
214 for (i=0; i<(lx); i++){
215 x0 = 0;
216 x1 = 0;
217 tj = -2;
218 for (j=0; j<=lhhm1; j++){
219 tj+=2;
220 x0 = x0 + x_inl[i+j]*g0[lhm1-1-tj] + x_inh[i+j]*g1[lhm1-1-tj] ;
221 x1 = x1 + x_inl[i+j]*g0[lhm1-tj] + x_inh[i+j]*g1[lhm1-tj] ;
222 }
223 x_out[ind++] = x0;
224 x_out[ind++] = x1;
225 }
226 }