comparison util/Rice Wavelet Toolbox/midwt_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: MIDWT.c
3 Last Modification Date: 06/14/95 13:01:15
4 Current Version: MIDWT.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 decription of the matlab call:
52 %y = midwt(x,h,L);
53 %
54 % function computes the inverse discrete wavelet transform y for a 1D or 2D
55 % input signal x.
56 %
57 % Input:
58 % x : finite length 1D or 2D input signal (implicitely periodized)
59 % h : scaling filter
60 % L : number of levels. in case of a 1D signal length(x) must be
61 % divisible by 2^L; in case of a 2D signal the row and the
62 % column dimension must be divisible by 2^L.
63 %
64 % see also: mdwt, mrdwt, mirdwt
65
66 */
67
68 #include <math.h>
69 #include <stdio.h>
70 #include <inttypes.h>
71
72 #define max(A,B) (A > B ? A : B)
73 #define mat(a, i, j) (*(a + (m*(j)+i))) /* macro for matrix indices */
74
75
76 #ifdef __STDC__
77 MIDWT(double *x, uintptr_t m, uintptr_t n, double *h, uintptr_t lh, uintptr_t L, double *y)
78 #else
79 MIDWT(x, m, n, h, lh, L, y)
80 double *x, *h, *y;
81 uintptr_t m, n, lh, L;
82 #endif
83 {
84 double *g0, *g1, *ydummyl, *ydummyh, *xdummy;
85 uintptr_t i, j;
86 uintptr_t actual_L, actual_m, actual_n, r_o_a, c_o_a, ir, ic, lhm1, lhhm1, sample_f;
87 xdummy = (double *)(uintptr_t)mxCalloc(max(m,n),sizeof(double));
88 ydummyl = (double *)(uintptr_t)mxCalloc(max(m,n)+lh/2-1,sizeof(double));
89 ydummyh = (double *)(uintptr_t)mxCalloc(max(m,n)+lh/2-1,sizeof(double));
90 g0 = (double *)(uintptr_t)mxCalloc(lh,sizeof(double));
91 g1 = (double *)(uintptr_t)mxCalloc(lh,sizeof(double));
92
93 if (n==1){
94 n = m;
95 m = 1;
96 }
97 /* synthesis lowpass and highpass */
98 for (i=0; i<lh; i++){
99 g0[i] = h[i];
100 g1[i] = h[lh-i-1];
101 }
102 for (i=1; i<=lh; i+=2)
103 g1[i] = -g1[i];
104
105 lhm1 = lh - 1;
106 lhhm1 = lh/2 - 1;
107 /* 2^L */
108 sample_f = 1;
109 for (i=1; i<L; i++)
110 sample_f = sample_f*2;
111
112 if (m>1)
113 actual_m = m/sample_f;
114 else
115 actual_m = 1;
116 actual_n = n/sample_f;
117
118 for (i=0; i<(m*n); i++)
119 x[i] = y[i];
120
121 /* main loop */
122 for (actual_L=L; actual_L >= 1; actual_L--){
123 r_o_a = actual_m/2;
124 c_o_a = actual_n/2;
125
126 /* go by columns in case of a 2D signal*/
127 if (m>1){
128 for (ic=0; ic<actual_n; ic++){ /* loop over column */
129 /* store in dummy variables */
130 ir = r_o_a;
131 for (i=0; i<r_o_a; i++){
132 ydummyl[i+lhhm1] = mat(x, i, ic);
133 ydummyh[i+lhhm1] = mat(x, ir++, ic);
134 }
135 /* perform filtering lowpass and highpass*/
136 bpsconv(xdummy, r_o_a, g0, g1, lhm1, lhhm1, ydummyl, ydummyh);
137 /* restore dummy variables in matrix */
138 for (i=0; i<actual_m; i++)
139 mat(x, i, ic) = xdummy[i];
140 }
141 }
142 /* go by rows */
143 for (ir=0; ir<actual_m; ir++){ /* loop over rows */
144 /* store in dummy variable */
145 ic = c_o_a;
146 for (i=0; i<c_o_a; i++){
147 ydummyl[i+lhhm1] = mat(x, ir, i);
148 ydummyh[i+lhhm1] = mat(x, ir, ic++);
149 }
150 /* perform filtering lowpass and highpass*/
151 bpsconv(xdummy, c_o_a, g0, g1, lhm1, lhhm1, ydummyl, ydummyh);
152 /* restore dummy variables in matrices */
153 for (i=0; i<actual_n; i++)
154 mat(x, ir, i) = xdummy[i];
155 }
156 if (m==1)
157 actual_m = 1;
158 else
159 actual_m = actual_m*2;
160 actual_n = actual_n*2;
161 }
162 }
163
164 #ifdef __STDC__
165 bpsconv(double *x_out, uintptr_t lx, double *g0, double *g1, uintptr_t lhm1,
166 uintptr_t lhhm1, double *x_inl, double *x_inh)
167 #else
168 bpsconv(x_out, lx, g0, g1, lhm1, lhhm1, x_inl, x_inh)
169 double *x_inl, *x_inh, *g0, *g1, *x_out;
170 uintptr_t lx, lhm1, lhhm1;
171 #endif
172 {
173 uintptr_t i, j, ind, tj;
174 double x0, x1;
175
176 for (i=lhhm1-1; i > -1; i--){
177 x_inl[i] = x_inl[lx+i];
178 x_inh[i] = x_inh[lx+i];
179 }
180 ind = 0;
181 for (i=0; i<(lx); i++){
182 x0 = 0;
183 x1 = 0;
184 tj = -2;
185 for (j=0; j<=lhhm1; j++){
186 tj+=2;
187 x0 = x0 + x_inl[i+j]*g0[lhm1-1-tj] + x_inh[i+j]*g1[lhm1-1-tj] ;
188 x1 = x1 + x_inl[i+j]*g0[lhm1-tj] + x_inh[i+j]*g1[lhm1-tj] ;
189 }
190 x_out[ind++] = x0;
191 x_out[ind++] = x1;
192 }
193 }