comparison util/Rice Wavelet Toolbox/mirdwt_r.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: MIRDWT.c
3 Last Modification Date: 06/14/95 16:22:45
4 Current Version: MIRDWT.c 2.4
5 File Creation Date: Wed Oct 12 08:44:43 1994
6 Author: Markus Lang <lang@jazz.rice.edu>
7
8 Copyright (c) 2000 RICE UNIVERSITY. All rights reserved.
9 Created by Markus Lang, Department of ECE, Rice University.
10
11 This software is distributed and licensed to you on a non-exclusive
12 basis, free-of-charge. Redistribution and use in source and binary forms,
13 with or without modification, are permitted provided that the following
14 conditions are met:
15
16 1. Redistribution of source code must retain the above copyright notice,
17 this list of conditions and the following disclaimer.
18 2. Redistribution in binary form must reproduce the above copyright notice,
19 this list of conditions and the following disclaimer in the
20 documentation and/or other materials provided with the distribution.
21 3. All advertising materials mentioning features or use of this software
22 must display the following acknowledgment: This product includes
23 software developed by Rice University, Houston, Texas and its contributors.
24 4. Neither the name of the University nor the names of its contributors
25 may be used to endorse or promote products derived from this software
26 without specific prior written permission.
27
28 THIS SOFTWARE IS PROVIDED BY WILLIAM MARSH RICE UNIVERSITY, HOUSTON, TEXAS,
29 AND CONTRIBUTORS AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
30 BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
31 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RICE UNIVERSITY
32 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
33 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
34 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
35 OR BUSINESS INTERRUPTIONS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
36 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
37 OTHERWISE), PRODUCT LIABILITY, OR OTHERWISE ARISING IN ANY WAY OUT OF THE
38 USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39
40 For information on commercial licenses, contact Rice University's Office of
41 Technology Transfer at techtran@rice.edu or (713) 348-6173
42
43 Change History: Fixed the code such that 1D vectors passed to it can be in
44 either passed as a row or column vector. Also took care of
45 the code such that it will compile with both under standard
46 C compilers as well as for ANSI C compilers
47 Jan Erik Odegard <odegard@ece.rice.edu> Wed Jun 14 1995
48
49 Fix minor bug to allow maximum number of levels
50
51 MATLAB description:
52 %function x = mirdwt(yl,yh,h,L);
53 %
54 % function computes the inverse redundant discrete wavelet transform y for a
55 % 1D or 2D input signal. redundant means here that the subsampling after
56 % each stage of the forward transform has been omitted. yl contains the
57 % lowpass and yl the highpass components as computed, e.g., by mrdwt. In
58 % case of a 2D signal the ordering in yh is [lh hl hh lh hl ... ] (first
59 % letter refers to row, second to column filtering).
60 %
61 % Input:
62 % yl : lowpass component
63 % yh : highpass components
64 % h : scaling filter
65 % L : number of levels. in case of a 1D signal length(yl) must be
66 % divisible by 2^L; in case of a 2D signal the row and the
67 % column dimension must be divisible by 2^L.
68 %
69 % Output:
70 % x : finite length 1D or 2D signal
71 %
72 % see also: mdwt, midwt, mrdwt
73
74 */
75 #include <math.h>
76 #include <stdio.h>
77 #include <inttypes.h>
78
79 #define max(a, b) ((a) > (b) ? (a) : (b))
80 #define mat(a, i, j) (*(a + (m*(j)+i))) /* macro for matrix indices */
81
82 #ifdef __STDC__
83 MIRDWT(double *x, uintptr_t m, uintptr_t n, double *h, uintptr_t lh, uintptr_t L,
84 double *yl, double *yh)
85 #else
86 MIRDWT(x, m, n, h, lh, L, yl, yh)
87 double *x, *h, *yl, *yh;
88 uintptr_t m, n, lh, L;
89 #endif
90 {
91 double *g0, *g1, *ydummyll, *ydummylh, *ydummyhl;
92 double *ydummyhh, *xdummyl , *xdummyh, *xh;
93 long i, j;
94 uintptr_t actual_L, actual_m, actual_n, c_o_a, ir, n_c, n_cb, n_c_o, lhm1;
95 uintptr_t ic, n_r, n_rb, n_r_o, c_o_a_p2n, sample_f;
96 xh = (double *)(uintptr_t)mxCalloc(m*n,sizeof(double));
97 xdummyl = (double *)(uintptr_t)mxCalloc(max(m,n),sizeof(double));
98 xdummyh = (double *)(uintptr_t)mxCalloc(max(m,n),sizeof(double));
99 ydummyll = (double *)(uintptr_t)mxCalloc(max(m,n)+lh-1,sizeof(double));
100 ydummylh = (double *)(uintptr_t)mxCalloc(max(m,n)+lh-1,sizeof(double));
101 ydummyhl = (double *)(uintptr_t)mxCalloc(max(m,n)+lh-1,sizeof(double));
102 ydummyhh = (double *)(uintptr_t)mxCalloc(max(m,n)+lh-1,sizeof(double));
103 g0 = (double *)(uintptr_t)mxCalloc(lh,sizeof(double));
104 g1 = (double *)(uintptr_t)mxCalloc(lh,sizeof(double));
105
106 if (n==1){
107 n = m;
108 m = 1;
109 }
110 /* analysis lowpass and highpass */
111 for (i=0; i<lh; i++){
112 g0[i] = h[i]/2;
113 g1[i] = h[lh-i-1]/2;
114 }
115 for (i=1; i<=lh; i+=2)
116 g1[i] = -g1[i];
117
118 lhm1 = lh - 1;
119 /* 2^L */
120 sample_f = 1;
121 for (i=1; i<L; i++)
122 sample_f = sample_f*2;
123 actual_m = m/sample_f;
124 actual_n = n/sample_f;
125 /* restore yl in x */
126 for (i=0;i<m*n;i++)
127 x[i] = yl[i];
128
129 /* main loop */
130 for (actual_L=L; actual_L >= 1; actual_L--){
131 /* actual (level dependent) column offset */
132 if (m==1)
133 c_o_a = n*(actual_L-1);
134 else
135 c_o_a = 3*n*(actual_L-1);
136 c_o_a_p2n = c_o_a + 2*n;
137
138 /* go by columns in case of a 2D signal*/
139 if (m>1){
140 n_rb = m/actual_m; /* # of row blocks per column */
141 for (ic=0; ic<n; ic++){ /* loop over column */
142 for (n_r=0; n_r<n_rb; n_r++){ /* loop within one column */
143 /* store in dummy variables */
144 ir = -sample_f + n_r;
145 for (i=0; i<actual_m; i++){
146 ir = ir + sample_f;
147 ydummyll[i+lhm1] = mat(x, ir, ic);
148 ydummylh[i+lhm1] = mat(yh, ir, c_o_a+ic);
149 ydummyhl[i+lhm1] = mat(yh, ir,c_o_a+n+ic);
150 ydummyhh[i+lhm1] = mat(yh, ir, c_o_a_p2n+ic);
151 }
152 /* perform filtering and adding: first LL/LH, then HL/HH */
153 bpconv(xdummyl, actual_m, g0, g1, lh, ydummyll, ydummylh);
154 bpconv(xdummyh, actual_m, g0, g1, lh, ydummyhl, ydummyhh);
155 /* store dummy variables in matrices */
156 ir = -sample_f + n_r;
157 for (i=0; i<actual_m; i++){
158 ir = ir + sample_f;
159 mat(x, ir, ic) = xdummyl[i];
160 mat(xh, ir, ic) = xdummyh[i];
161 }
162 }
163 }
164 }
165
166 /* go by rows */
167 n_cb = n/actual_n; /* # of column blocks per row */
168 for (ir=0; ir<m; ir++){ /* loop over rows */
169 for (n_c=0; n_c<n_cb; n_c++){ /* loop within one row */
170 /* store in dummy variable */
171 ic = -sample_f + n_c;
172 for (i=0; i<actual_n; i++){
173 ic = ic + sample_f;
174 ydummyll[i+lhm1] = mat(x, ir, ic);
175 if (m>1)
176 ydummyhh[i+lhm1] = mat(xh, ir, ic);
177 else
178 ydummyhh[i+lhm1] = mat(yh, ir, c_o_a+ic);
179 }
180 /* perform filtering lowpass/highpass */
181 bpconv(xdummyl, actual_n, g0, g1, lh, ydummyll, ydummyhh);
182 /* restore dummy variables in matrices */
183 ic = -sample_f + n_c;
184 for (i=0; i<actual_n; i++){
185 ic = ic + sample_f;
186 mat(x, ir, ic) = xdummyl[i];
187 }
188 }
189 }
190 sample_f = sample_f/2;
191 actual_m = actual_m*2;
192 actual_n = actual_n*2;
193 }
194 }
195
196 #ifdef __STDC__
197 bpconv(double *x_out, uintptr_t lx, double *g0, double *g1, uintptr_t lh,
198 double *x_inl, double *x_inh)
199 #else
200 bpconv(x_out, lx, g0, g1, lh, x_inl, x_inh)
201 double *x_inl, *x_inh, *g0, *g1, *x_out;
202 uintptr_t lx, lh;
203 #endif
204 {
205 uintptr_t i, j;
206 double x0;
207
208 for (i=lh-2; i > -1; i--){
209 x_inl[i] = x_inl[lx+i];
210 x_inh[i] = x_inh[lx+i];
211 }
212 for (i=0; i<lx; i++){
213 x0 = 0;
214 for (j=0; j<lh; j++)
215 x0 = x0 + x_inl[j+i]*g0[lh-1-j] +
216 x_inh[j+i]*g1[lh-1-j];
217 x_out[i] = x0;
218 }
219 }