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