comparison util/Rice Wavelet Toolbox/mrdwt_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: MRDWT.c
3 Last Modification Date: 09/21/95 15:42:59
4 Current Version: MRDWT.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 MATLAB description:
50 %[yl,yh] = mrdwt(x,h,L);
51 %
52 % function computes the redundant discrete wavelet transform y for a 1D or
53 % 2D input signal . redundant means here that the subsampling after each
54 % stage is omitted. yl contains the lowpass and yl the highpass
55 % components. In case of a 2D signal the ordering in yh is [lh hl hh lh hl
56 % ... ] (first letter refers to row, second to column filtering).
57 %
58 % Input:
59 % x : finite length 1D or 2D signal (implicitely periodized)
60 % h : scaling filter
61 % L : number of levels. in case of a 1D signal length(x) must be
62 % divisible by 2^L; in case of a 2D signal the row and the
63 % column dimension must be divisible by 2^L.
64 %
65 % Output:
66 % yl : lowpass component
67 % yh : highpass components
68 %
69 % see also: mdwt, midwt, mirdwt
70
71
72 */
73
74 #include <math.h>
75 #include <stdio.h>
76 #include <inttypes.h>
77
78 /*#define mat(a, i, j) (a[m*(j)+i]) */
79 #define max(a, b) ((a) > (b) ? (a) : (b))
80 #define mat(a, i, j) (*(a + (m*(j)+i)))
81
82
83 #ifdef __STDC__
84 MRDWT(double *x, uintptr_t m, uintptr_t n, double *h, uintptr_t lh, uintptr_t L,
85 double *yl, double *yh)
86 #else
87 MRDWT(x, m, n, h, lh, L, yl, yh)
88 double *x, *h, *yl, *yh;
89 uintptr_t m, n, lh, L;
90 #endif
91 {
92 double *tmp;
93 double *h0, *h1, *ydummyll, *ydummylh, *ydummyhl;
94 double *ydummyhh, *xdummyl , *xdummyh;
95 long i, j;
96 uintptr_t actual_L, actual_m, actual_n, c_o_a, ir, n_c, n_cb, n_c_o;
97 uintptr_t ic, n_r, n_rb, n_r_o, c_o_a_p2n, sample_f;
98 xdummyl = (double *)(uintptr_t)mxCalloc(max(m,n)+lh-1,sizeof(double));
99 xdummyh = (double *)(uintptr_t)mxCalloc(max(m,n)+lh-1,sizeof(double));
100 ydummyll = (double *)(uintptr_t)mxCalloc(max(m,n),sizeof(double));
101 ydummylh = (double *)(uintptr_t)mxCalloc(max(m,n),sizeof(double));
102 ydummyhl = (double *)(uintptr_t)mxCalloc(max(m,n),sizeof(double));
103 ydummyhh = (double *)(uintptr_t)mxCalloc(max(m,n),sizeof(double));
104 h0 = (double *)(uintptr_t)mxCalloc(lh,sizeof(double));
105 h1 = (double *)(uintptr_t)mxCalloc(lh,sizeof(double));
106
107 if (n==1){
108 n = m;
109 m = 1;
110 }
111 /* analysis lowpass and highpass */
112 for (i=0; i<lh; i++){
113 h0[i] = h[lh-i-1];
114 h1[i] =h[i];
115 }
116 for (i=0; i<lh; i+=2)
117 h1[i] = -h1[i];
118
119 actual_m = 2*m;
120 actual_n = 2*n;
121 for (i=0; i<m*n; i++)
122 yl[i] = x[i];
123
124 /* main loop */
125 sample_f = 1;
126 for (actual_L=1; actual_L <= L; actual_L++){
127 actual_m = actual_m/2;
128 actual_n = actual_n/2;
129 /* actual (level dependent) column offset */
130 if (m==1)
131 c_o_a = n*(actual_L-1);
132 else
133 c_o_a = 3*n*(actual_L-1);
134 c_o_a_p2n = c_o_a + 2*n;
135
136 /* go by rows */
137 n_cb = n/actual_n; /* # of column blocks per row */
138 for (ir=0; ir<m; ir++){ /* loop over rows */
139 for (n_c=0; n_c<n_cb; n_c++){ /* loop within one row */
140 /* store in dummy variable */
141 ic = -sample_f + n_c;
142 for (i=0; i<actual_n; i++){
143 ic = ic + sample_f;
144 xdummyl[i] = mat(yl, ir, ic);
145 }
146 /* perform filtering lowpass/highpass */
147 fpconv(xdummyl, actual_n, h0, h1, lh, ydummyll, ydummyhh);
148 /* restore dummy variables in matrices */
149 ic = -sample_f + n_c;
150 for (i=0; i<actual_n; i++){
151 ic = ic + sample_f;
152 mat(yl, ir, ic) = ydummyll[i];
153 mat(yh, ir, c_o_a+ic) = ydummyhh[i];
154 }
155 }
156 }
157
158 /* go by columns in case of a 2D signal*/
159 if (m>1){
160 n_rb = m/actual_m; /* # of row blocks per column */
161 for (ic=0; ic<n; ic++){ /* loop over column */
162 for (n_r=0; n_r<n_rb; n_r++){ /* loop within one column */
163 /* store in dummy variables */
164 ir = -sample_f + n_r;
165 for (i=0; i<actual_m; i++){
166 ir = ir + sample_f;
167 xdummyl[i] = mat(yl, ir, ic);
168 xdummyh[i] = mat(yh, ir,c_o_a+ic);
169 }
170 /* perform filtering: first LL/LH, then HL/HH */
171 fpconv(xdummyl, actual_m, h0, h1, lh, ydummyll, ydummylh);
172 fpconv(xdummyh, actual_m, h0, h1, lh, ydummyhl, ydummyhh);
173 /* restore dummy variables in matrices */
174 ir = -sample_f + n_r;
175 for (i=0; i<actual_m; i++){
176 ir = ir + sample_f;
177 mat(yl, ir, ic) = ydummyll[i];
178 mat(yh, ir, c_o_a+ic) = ydummylh[i];
179 mat(yh, ir,c_o_a+n+ic) = ydummyhl[i];
180 mat(yh, ir, c_o_a_p2n+ic) = ydummyhh[i];
181 }
182 }
183 }
184 }
185 sample_f = sample_f*2;
186 }
187 }
188
189 #ifdef __STDC__
190 fpconv(double *x_in, uintptr_t lx, double *h0, double *h1, uintptr_t lh,
191 double *x_outl, double *x_outh)
192 #else
193 fpconv(x_in, lx, h0, h1, lh, x_outl, x_outh)
194 double *x_in, *h0, *h1, *x_outl, *x_outh;
195 uintptr_t lx, lh;
196 #endif
197 {
198 uintptr_t i, j;
199 double x0, x1;
200
201 for (i=lx; i < lx+lh-1; i++)
202 x_in[i] = x_in[i-lx];
203 for (i=0; i<lx; i++){
204 x0 = 0;
205 x1 = 0;
206 for (j=0; j<lh; j++){
207 x0 = x0 + x_in[j+i]*h0[lh-1-j];
208 x1 = x1 + x_in[j+i]*h1[lh-1-j];
209 }
210 x_outl[i] = x0;
211 x_outh[i] = x1;
212 }
213 }