xue@1
|
1 //---------------------------------------------------------------------------
|
xue@1
|
2
|
xue@1
|
3 #include <math.h>
|
Chris@2
|
4 #include <string.h>
|
xue@1
|
5 #include "wavelet.h"
|
xue@1
|
6 #include "matrix.h"
|
xue@1
|
7
|
Chris@5
|
8 /** \file wavelet.h */
|
Chris@5
|
9
|
xue@1
|
10 //---------------------------------------------------------------------------
|
Chris@5
|
11 /**
|
xue@1
|
12 function csqrt: real implementation of complex square root z=sqrt(x)
|
xue@1
|
13
|
xue@1
|
14 In: xr and xi: real and imaginary parts of x
|
xue@1
|
15 Out: zr and zi: real and imaginary parts of z=sqrt(x)
|
xue@1
|
16
|
xue@1
|
17 No return value.
|
xue@1
|
18 */
|
xue@1
|
19 void csqrt(double& zr, double& zi, double xr, double xi)
|
xue@1
|
20 {
|
xue@1
|
21 if (xi==0)
|
xue@1
|
22 if (xr>=0) zr=sqrt(xr), zi=0;
|
xue@1
|
23 else zi=sqrt(-xr), zr=0;
|
xue@1
|
24 else
|
xue@1
|
25 {
|
xue@1
|
26 double xm=sqrt(xr*xr+xi*xi);
|
xue@1
|
27 double ri=sqrt((xm-xr)/2);
|
xue@1
|
28 zr=xi/2/ri;
|
xue@1
|
29 zi=ri;
|
xue@1
|
30 }
|
xue@1
|
31 }//csqrt
|
xue@1
|
32
|
Chris@5
|
33 /**
|
xue@1
|
34 function Daubechies: calculates the Daubechies filter of a given order p
|
xue@1
|
35
|
xue@1
|
36 In: filter order p
|
xue@1
|
37 Out: h[2p]: the 2p FIR coefficients
|
xue@1
|
38
|
xue@1
|
39 No reutrn value. The calculated filters are minimum phase, which means the energy concentrates at the
|
xue@1
|
40 beginning. This is usually used for reconstruction. On the contrary, for wavelet analysis the filter
|
xue@1
|
41 is mirrored.
|
xue@1
|
42 */
|
xue@1
|
43 void Daubechies(int p, double* h)
|
xue@1
|
44 {
|
xue@1
|
45 //initialize h(z)
|
xue@1
|
46 double r01=pow(2, -p-p+1.5);
|
xue@1
|
47
|
xue@1
|
48 h[0]=1;
|
xue@1
|
49 for (int i=1; i<=p; i++)
|
xue@1
|
50 {
|
xue@1
|
51 h[i]=h[i-1]*(p+1-i)/i;
|
xue@1
|
52 }
|
xue@1
|
53
|
xue@1
|
54 //construct polynomial p
|
xue@1
|
55 double *P=new double[p], *rp=new double[p], *ip=new double[p];
|
xue@1
|
56
|
xue@1
|
57 P[p-1]=1;
|
xue@1
|
58 double r02=1;
|
xue@1
|
59 for (int i=p-1; i>0; i--)
|
xue@1
|
60 {
|
xue@1
|
61 double rate=(i+1-1.0)/(p-2.0+i+1);
|
xue@1
|
62 P[i-1]=P[i]*rate;
|
xue@1
|
63 r02/=rate;
|
xue@1
|
64 }
|
xue@1
|
65 Roots(p-1, P, rp, ip);
|
xue@1
|
66 for (int i=0; i<p-1; i++)
|
xue@1
|
67 {
|
xue@1
|
68 //current length of h is p+1+i, h[0:p+i]
|
xue@1
|
69 if (i<p-2 && rp[i]==rp[i+1] && ip[i]==-ip[i+1])
|
xue@1
|
70 {
|
xue@1
|
71 double ar=rp[i], ai=ip[i];
|
xue@1
|
72 double bkr=-2*ar+1, bki=-2*ai, ckr=4*(ar*ar-ai*ai-ar), cki=4*(2*ar*ai-ai), dlr, dli;
|
xue@1
|
73 csqrt(dlr, dli, ckr, cki);
|
xue@1
|
74 double akr=bkr+dlr, aki=bki+dli;
|
xue@1
|
75 if (akr*akr+aki*aki>1) akr=bkr-dlr, aki=bki-dli;
|
xue@1
|
76 double ak1=-2*akr, ak2=akr*akr+aki*aki;
|
xue@1
|
77 h[p+2+i]=ak2*h[p+i];
|
xue@1
|
78 h[p+1+i]=ak2*h[p-1+i]+ak1*h[p+i];
|
xue@1
|
79 for (int j=p+i; j>1; j--) h[j]=h[j]+ak1*h[j-1]+ak2*h[j-2];
|
xue@1
|
80 h[1]=h[1]+ak1*h[0];
|
xue@1
|
81 r02/=ak2;
|
xue@1
|
82 i++;
|
xue@1
|
83 }
|
xue@1
|
84 else //real root of P
|
xue@1
|
85 {
|
xue@1
|
86 double ak, bk=-(2*rp[i]-1), delk=4*rp[i]*(rp[i]-1);
|
xue@1
|
87 if (bk>0) ak=bk-sqrt(delk);
|
xue@1
|
88 else ak=bk+sqrt(delk);
|
xue@1
|
89 r02/=ak;
|
xue@1
|
90 h[p+1+i]=-ak*h[p+i];
|
xue@1
|
91 for (int j=p+i; j>0; j--) h[j]=h[j]-ak*h[j-1];
|
xue@1
|
92 }
|
xue@1
|
93 }
|
xue@1
|
94 delete[] P; delete[] rp; delete[] ip;
|
xue@1
|
95 r01=r01*sqrt(r02);
|
xue@1
|
96 for (int i=0; i<p*2; i++) h[i]*=r01;
|
xue@1
|
97 }//Daubechies
|
xue@1
|
98
|
xue@1
|
99 /*
|
xue@1
|
100 Periodic wavelet decomposition and reconstruction routines
|
xue@1
|
101
|
xue@1
|
102 The wavelet transform of an N-point sequence is arranged in "interleaved" format
|
xue@1
|
103 as another N-point sequance. Level 1 details are found at N/2 points 1, 3, 5, ...,
|
xue@1
|
104 N-1; level 2 details are found at N/4 points 2, 6, ..., N-2; level 3 details are
|
xue@1
|
105 found at N/8 points 4, 12, ..., N-4; etc.
|
xue@1
|
106 */
|
xue@1
|
107
|
Chris@5
|
108 /**
|
xue@1
|
109 function dwtpqmf: in this implementation h and g are the same as reconstruction qmf filters. In fact
|
xue@1
|
110 the actual filters used are their mirrors and filter origin are aligned to the ends of the real
|
xue@1
|
111 filters, which turn out to be the starts of h and g.
|
xue@1
|
112
|
xue@1
|
113 The inverse transform is idwtp(), the same as inversing dwtp().
|
xue@1
|
114
|
xue@1
|
115 In: in[Count]: waveform data
|
xue@1
|
116 h[M], g[M]: quadratic mirror filter pair
|
xue@1
|
117 N: maximal time resolution
|
xue@1
|
118 Count=kN, N=2^lN being the reciprocal of the most detailed frequency scale, i.e.
|
xue@1
|
119 N=1 for no transforming at all, N=2 for dividing into approx. and detail,
|
xue@1
|
120 N=4 for dividing into approx+detail(approx+detial), etc.
|
xue@1
|
121 Count*2/N=2k gives the smallest length to be convolved with h and g.
|
xue@1
|
122 Out: out[N], the wavelet transform, arranged in interleaved format.
|
xue@1
|
123
|
xue@1
|
124 Returns maximal atom length (should equal N).
|
xue@1
|
125 */
|
xue@1
|
126 int dwtpqmf(double* in, int Count, int N, double* h, double* g, int M, double* out)
|
xue@1
|
127 {
|
xue@1
|
128 double* tmp=new double[Count];
|
xue@1
|
129 double *tmpa=tmp, *tmpla=in;
|
xue@1
|
130 int C=Count, L=0, n=1;
|
xue@1
|
131
|
xue@1
|
132 A:
|
xue@1
|
133 {
|
xue@1
|
134 //C: signal length at current layer
|
xue@1
|
135 //L: layer index, 0 being most detailed
|
xue@1
|
136 //n: atom length on current layer, equals 2^L.
|
xue@1
|
137 //C*n=(C<<L)=Count
|
xue@1
|
138 double* tmpd=&tmpa[C/2];
|
xue@1
|
139 for (int i=0; i<C; i+=2)
|
xue@1
|
140 {
|
xue@1
|
141 int i2=i/2;
|
xue@1
|
142 tmpa[i2]=tmpla[i]*h[0];
|
xue@1
|
143 tmpd[i2]=tmpla[i]*g[0];
|
xue@1
|
144 for (int j=1; j<M; j++)
|
xue@1
|
145 {
|
xue@1
|
146 if (i+j<C)
|
xue@1
|
147 {
|
xue@1
|
148 tmpa[i2]+=tmpla[i+j]*h[j];
|
xue@1
|
149 tmpd[i2]+=tmpla[i+j]*g[j];
|
xue@1
|
150 }
|
xue@1
|
151 else
|
xue@1
|
152 {
|
xue@1
|
153 tmpa[i2]+=tmpla[i+j-C]*h[j];
|
xue@1
|
154 tmpd[i2]+=tmpla[i+j-C]*g[j];
|
xue@1
|
155 }
|
xue@1
|
156 }
|
xue@1
|
157 }
|
xue@1
|
158 for (int i=0; i*2+1<C; i++) out[(2*i+1)<<L]=tmpd[i];
|
xue@1
|
159 for (int i=0; i*2<C; i++) out[i<<(L+1)]=tmpa[i];
|
xue@1
|
160 n*=2;
|
xue@1
|
161 if (n<N)
|
xue@1
|
162 {
|
xue@1
|
163 tmpla=tmpa;
|
xue@1
|
164 tmpa=tmpd;
|
xue@1
|
165 L++;
|
xue@1
|
166 C/=2;
|
xue@1
|
167 goto A;
|
xue@1
|
168 }
|
xue@1
|
169 }
|
xue@1
|
170 delete[] tmp;
|
xue@1
|
171 return n;
|
xue@1
|
172 }//dwtpqmf
|
xue@1
|
173
|
Chris@5
|
174 /**
|
xue@1
|
175 function dwtp: in this implementation h and g can be different from mirrored reconstruction filters,
|
xue@1
|
176 i.e. the biorthogonal transform. h[0] and g[0] are aligned at the ends of the filters, i.e. h[-M+1:0],
|
xue@1
|
177 g[-M+1:0].
|
xue@1
|
178
|
xue@1
|
179 In: in[Count]: waveform data
|
xue@1
|
180 h[-M+1:0], g[-M+1:0]: quadratic mirror filter pair
|
xue@1
|
181 N: maximal time resolution
|
xue@1
|
182 Out: out[N], the wavelet transform, arranged in interleaved format.
|
xue@1
|
183
|
xue@1
|
184 Returns maximal atom length (should equal N).
|
xue@1
|
185 */
|
xue@1
|
186 int dwtp(double* in, int Count, int N, double* h, double* g, int M, double* out)
|
xue@1
|
187 {
|
xue@1
|
188 double* tmp=new double[Count];
|
xue@1
|
189 double *tmpa=tmp, *tmpla=in;
|
xue@1
|
190 int C=Count, L=0, n=1;
|
xue@1
|
191
|
xue@1
|
192 A:
|
xue@1
|
193 {
|
xue@1
|
194 double* tmpd=&tmpa[C/2];
|
xue@1
|
195 for (int i=0; i<C; i+=2)
|
xue@1
|
196 {
|
xue@1
|
197 int i2=i/2;
|
xue@1
|
198 tmpa[i2]=tmpla[i]*h[0];
|
xue@1
|
199 tmpd[i2]=tmpla[i]*g[0];
|
xue@1
|
200 for (int j=-1; j>-M; j--)
|
xue@1
|
201 {
|
xue@1
|
202 if (i-j<C)
|
xue@1
|
203 {
|
xue@1
|
204 tmpa[i2]+=tmpla[i-j]*h[j];
|
xue@1
|
205 tmpd[i2]+=tmpla[i-j]*g[j];
|
xue@1
|
206 }
|
xue@1
|
207 else
|
xue@1
|
208 {
|
xue@1
|
209 tmpa[i2]+=tmpla[i-j-C]*h[j];
|
xue@1
|
210 tmpd[i2]+=tmpla[i-j-C]*g[j];
|
xue@1
|
211 }
|
xue@1
|
212 }
|
xue@1
|
213 }
|
xue@1
|
214 for (int i=0; i*2+1<C; i++) out[(2*i+1)<<L]=tmpd[i];
|
xue@1
|
215 for (int i=0; i*2<C; i++) out[i<<(L+1)]=tmpa[i];
|
xue@1
|
216 n*=2;
|
xue@1
|
217 if (n<N)
|
xue@1
|
218 {
|
xue@1
|
219 tmpla=tmpa;
|
xue@1
|
220 tmpa=tmpd;
|
xue@1
|
221 L++;
|
xue@1
|
222 C/=2;
|
xue@1
|
223 goto A;
|
xue@1
|
224 }
|
xue@1
|
225 }
|
xue@1
|
226 delete[] tmp;
|
xue@1
|
227 return n;
|
xue@1
|
228 }//dwtp
|
xue@1
|
229
|
Chris@5
|
230 /**
|
xue@1
|
231 function dwtp: in this implementation h and g can be different size. h[0] and g[0] are aligned at the
|
xue@1
|
232 ends of the filters, i.e. h[-Mh+1:0], g[-Mg+1:0].
|
xue@1
|
233
|
xue@1
|
234 In: in[Count]: waveform data
|
xue@1
|
235 h[-Mh+1:0], g[-Mg+1:0]: quadratic mirror filter pair
|
xue@1
|
236 N: maximal time resolution
|
xue@1
|
237 Out: out[N], the wavelet transform, arranged in interleaved format.
|
xue@1
|
238
|
xue@1
|
239 Returns maximal atom length (should equal N).
|
xue@1
|
240 */
|
xue@1
|
241 int dwtp(double* in, int Count, int N, double* h, int Mh, double* g, int Mg, double* out)
|
xue@1
|
242 {
|
xue@1
|
243 double* tmp=new double[Count];
|
xue@1
|
244 double *tmpa=tmp, *tmpla=in;
|
xue@1
|
245 int C=Count, L=0, n=1;
|
xue@1
|
246
|
xue@1
|
247 A:
|
xue@1
|
248 {
|
xue@1
|
249 double* tmpd=&tmpa[C/2];
|
xue@1
|
250 for (int i=0; i<C; i+=2)
|
xue@1
|
251 {
|
xue@1
|
252 int i2=i/2;
|
xue@1
|
253 tmpa[i2]=tmpla[i]*h[0];
|
xue@1
|
254 tmpd[i2]=tmpla[i]*g[0];
|
xue@1
|
255 for (int j=-1; j>-Mh; j--)
|
xue@1
|
256 {
|
xue@1
|
257 if (i-j<C)
|
xue@1
|
258 {
|
xue@1
|
259 tmpa[i2]+=tmpla[i-j]*h[j];
|
xue@1
|
260 }
|
xue@1
|
261 else
|
xue@1
|
262 {
|
xue@1
|
263 tmpa[i2]+=tmpla[i-j-C]*h[j];
|
xue@1
|
264 }
|
xue@1
|
265 }
|
xue@1
|
266 for (int j=-1; j>-Mg; j--)
|
xue@1
|
267 {
|
xue@1
|
268 if (i-j<C)
|
xue@1
|
269 {
|
xue@1
|
270 tmpd[i2]+=tmpla[i-j]*g[j];
|
xue@1
|
271 }
|
xue@1
|
272 else
|
xue@1
|
273 {
|
xue@1
|
274 tmpd[i2]+=tmpla[i-j-C]*g[j];
|
xue@1
|
275 }
|
xue@1
|
276 }
|
xue@1
|
277 }
|
xue@1
|
278 for (int i=0; i*2+1<C; i++) out[(2*i+1)<<L]=tmpd[i];
|
xue@1
|
279 for (int i=0; i*2<C; i++) out[i<<(L+1)]=tmpa[i];
|
xue@1
|
280 n*=2;
|
xue@1
|
281 if (n<N)
|
xue@1
|
282 {
|
xue@1
|
283 tmpla=tmpa;
|
xue@1
|
284 tmpa=tmpd;
|
xue@1
|
285 L++;
|
xue@1
|
286 C/=2;
|
xue@1
|
287 goto A;
|
xue@1
|
288 }
|
xue@1
|
289 }
|
xue@1
|
290 delete[] tmp;
|
xue@1
|
291 return n;
|
xue@1
|
292 }//dwtp
|
xue@1
|
293
|
Chris@5
|
294 /**
|
xue@1
|
295 function dwtp: in this implementation h and g can be arbitrarily located: h from $sh to $eh, g from
|
xue@1
|
296 $sg to $eg.
|
xue@1
|
297
|
xue@1
|
298 In: in[Count]: waveform data
|
xue@1
|
299 h[sh:eh-1], g[sg:eg-1]: quadratic mirror filter pair
|
xue@1
|
300 N: maximal time resolution
|
xue@1
|
301 Out: out[N], the wavelet transform, arranged in interleaved format.
|
xue@1
|
302
|
xue@1
|
303 Returns maximal atom length (should equal N).
|
xue@1
|
304 */
|
xue@1
|
305 int dwtp(double* in, int Count, int N, double* h, int sh, int eh, double* g, int sg, int eg, double* out)
|
xue@1
|
306 {
|
xue@1
|
307 double* tmp=new double[Count];
|
xue@1
|
308 double *tmpa=tmp, *tmpla=in;
|
xue@1
|
309 int C=Count, L=0, n=1;
|
xue@1
|
310
|
xue@1
|
311 A:
|
xue@1
|
312 {
|
xue@1
|
313 double* tmpd=&tmpa[C/2];
|
xue@1
|
314 for (int i=0; i<C; i+=2)
|
xue@1
|
315 {
|
xue@1
|
316 int i2=i/2;
|
xue@1
|
317 tmpa[i2]=0;//tmpla[i]*h[0];
|
xue@1
|
318 tmpd[i2]=0;//tmpla[i]*g[0];
|
xue@1
|
319 for (int j=sh; j<=eh; j++)
|
xue@1
|
320 {
|
xue@1
|
321 int ind=i-j;
|
xue@1
|
322 if (ind>=C) tmpa[i2]+=tmpla[ind-C]*h[j];
|
xue@1
|
323 else if (ind<0) tmpa[i2]+=tmpla[ind+C]*h[j];
|
xue@1
|
324 else tmpa[i2]+=tmpla[ind]*h[j];
|
xue@1
|
325 }
|
xue@1
|
326 for (int j=sg; j<=eg; j++)
|
xue@1
|
327 {
|
xue@1
|
328 int ind=i-j;
|
xue@1
|
329 if (ind>=C) tmpd[i2]+=tmpla[i-j-C]*g[j];
|
xue@1
|
330 else if (ind<0) tmpd[i2]+=tmpla[i-j+C]*g[j];
|
xue@1
|
331 else tmpd[i2]+=tmpla[i-j]*g[j];
|
xue@1
|
332 }
|
xue@1
|
333 }
|
xue@1
|
334 for (int i=0; i*2+1<C; i++) out[(2*i+1)<<L]=tmpd[i];
|
xue@1
|
335 for (int i=0; i*2<C; i++) out[i<<(L+1)]=tmpa[i];
|
xue@1
|
336 n*=2;
|
xue@1
|
337 if (n<N)
|
xue@1
|
338 {
|
xue@1
|
339 tmpla=tmpa;
|
xue@1
|
340 tmpa=tmpd;
|
xue@1
|
341 L++;
|
xue@1
|
342 C/=2;
|
xue@1
|
343 goto A;
|
xue@1
|
344 }
|
xue@1
|
345 }
|
xue@1
|
346 delete[] tmp;
|
xue@1
|
347 return n;
|
xue@1
|
348 }//dwtp
|
xue@1
|
349
|
Chris@5
|
350 /**
|
xue@1
|
351 function idwtp: periodic wavelet reconstruction by qmf
|
xue@1
|
352
|
xue@1
|
353 In: in[Count]: wavelet transform in interleaved format
|
xue@1
|
354 h[M], g[M]: quadratic mirror filter pair
|
xue@1
|
355 N: maximal time resolution
|
xue@1
|
356 Out: out[N], waveform data (detail level 0).
|
xue@1
|
357
|
xue@1
|
358 No return value.
|
xue@1
|
359 */
|
xue@1
|
360 void idwtp(double* in, int Count, int N, double* h, double* g, int M, double* out)
|
xue@1
|
361 {
|
xue@1
|
362 double* tmp=new double[Count];
|
xue@1
|
363 memcpy(out, in, sizeof(double)*Count);
|
xue@1
|
364 int n=N, C=Count/N, L=log2(N)-1;
|
xue@1
|
365 while (n>1)
|
xue@1
|
366 {
|
xue@1
|
367 memset(tmp, 0, sizeof(double)*C*2);
|
xue@1
|
368 //2k<<L being the approx, (2k+1)<<L being the detail
|
xue@1
|
369 for (int i=0; i<C; i++)
|
xue@1
|
370 {
|
xue@1
|
371 for (int j=0; j<M; j++)
|
xue@1
|
372 {
|
xue@1
|
373 if (i*2+j<C*2)
|
xue@1
|
374 {
|
xue@1
|
375 tmp[i*2+j]+=out[(2*i)<<L]*h[j]+out[(2*i+1)<<L]*g[j];
|
xue@1
|
376 }
|
xue@1
|
377 else
|
xue@1
|
378 {
|
xue@1
|
379 tmp[i*2+j-C*2]+=out[(2*i)<<L]*h[j]+out[(2*i+1)<<L]*g[j];
|
xue@1
|
380 }
|
xue@1
|
381 }
|
xue@1
|
382 }
|
xue@1
|
383 for (int i=0; i<C*2; i++) out[i<<L]=tmp[i];
|
xue@1
|
384 n/=2;
|
xue@1
|
385 C*=2;
|
xue@1
|
386 L--;
|
xue@1
|
387 }
|
xue@1
|
388 delete[] tmp;
|
xue@1
|
389 }//idwtp
|
xue@1
|
390
|
Chris@5
|
391 /**
|
xue@1
|
392 function idwtp: in which h and g can have different length.
|
xue@1
|
393
|
xue@1
|
394 In: in[Count]: wavelet transform in interleaved format
|
xue@1
|
395 h[Mh], g[Mg]: quadratic mirror filter pair
|
xue@1
|
396 N: maximal time resolution
|
xue@1
|
397 Out: out[N], waveform data (detail level 0).
|
xue@1
|
398
|
xue@1
|
399 No return value.
|
xue@1
|
400 */
|
xue@1
|
401 void idwtp(double* in, int Count, int N, double* h, int Mh, double* g, int Mg, double* out)
|
xue@1
|
402 {
|
xue@1
|
403 double* tmp=new double[Count];
|
xue@1
|
404 memcpy(out, in, sizeof(double)*Count);
|
xue@1
|
405 int n=N, C=Count/N, L=log2(N)-1;
|
xue@1
|
406 while (n>1)
|
xue@1
|
407 {
|
xue@1
|
408 memset(tmp, 0, sizeof(double)*C*2);
|
xue@1
|
409 //2k<<L being the approx, (2k+1)<<L being the detail
|
xue@1
|
410 for (int i=0; i<C; i++)
|
xue@1
|
411 {
|
xue@1
|
412 for (int j=0; j<Mh; j++)
|
xue@1
|
413 {
|
xue@1
|
414 int ind=i*2+j+(Mg-Mh)/2;
|
xue@1
|
415 if (ind>=C*2)
|
xue@1
|
416 {
|
xue@1
|
417 tmp[ind-C*2]+=out[(2*i)<<L]*h[j];
|
xue@1
|
418 }
|
xue@1
|
419 else if (ind<0)
|
xue@1
|
420 {
|
xue@1
|
421 tmp[ind+C*2]+=out[(2*i)<<L]*h[j];
|
xue@1
|
422 }
|
xue@1
|
423 else
|
xue@1
|
424 {
|
xue@1
|
425 tmp[ind]+=out[(2*i)<<L]*h[j];
|
xue@1
|
426 }
|
xue@1
|
427 }
|
xue@1
|
428 }
|
xue@1
|
429 for (int i=0; i<C; i++)
|
xue@1
|
430 {
|
xue@1
|
431 for (int j=0; j<Mg; j++)
|
xue@1
|
432 {
|
xue@1
|
433 int ind=i*2+j+(Mh-Mg)/2;
|
xue@1
|
434 if (ind>=C*2)
|
xue@1
|
435 {
|
xue@1
|
436 tmp[ind-C*2]+=out[(2*i+1)<<L]*g[j];
|
xue@1
|
437 }
|
xue@1
|
438 else if (ind<0)
|
xue@1
|
439 {
|
xue@1
|
440 tmp[ind+C*2]+=out[(2*i+1)<<L]*g[j];
|
xue@1
|
441 }
|
xue@1
|
442 else
|
xue@1
|
443 {
|
xue@1
|
444 tmp[ind]+=out[(2*i+1)<<L]*g[j];
|
xue@1
|
445 }
|
xue@1
|
446 }
|
xue@1
|
447 }
|
xue@1
|
448 for (int i=0; i<C*2; i++) out[i<<L]=tmp[i];
|
xue@1
|
449 n/=2;
|
xue@1
|
450 C*=2;
|
xue@1
|
451 L--;
|
xue@1
|
452 }
|
xue@1
|
453 delete[] tmp;
|
xue@1
|
454 }//idwtp
|
xue@1
|
455
|
Chris@5
|
456 /**
|
xue@1
|
457 function idwtp: in which h and g can be arbitrarily located: h from $sh to $eh, g from $sg to $eg
|
xue@1
|
458
|
xue@1
|
459 In: in[Count]: wavelet transform in interleaved format
|
xue@1
|
460 h[sh:eh-1], g[sg:eg-1]: quadratic mirror filter pair
|
xue@1
|
461 N: maximal time resolution
|
xue@1
|
462 Out: out[N], waveform data (detail level 0).
|
xue@1
|
463
|
xue@1
|
464 No return value.
|
xue@1
|
465 */
|
xue@1
|
466 void idwtp(double* in, int Count, int N, double* h, int sh, int eh, double* g, int sg, int eg, double* out)
|
xue@1
|
467 {
|
xue@1
|
468 double* tmp=new double[Count];
|
xue@1
|
469 memcpy(out, in, sizeof(double)*Count);
|
xue@1
|
470 int n=N, C=Count/N, L=log2(N)-1;
|
xue@1
|
471 while (n>1)
|
xue@1
|
472 {
|
xue@1
|
473 memset(tmp, 0, sizeof(double)*C*2);
|
xue@1
|
474 //2k<<L being the approx, (2k+1)<<L being the detail
|
xue@1
|
475 for (int i=0; i<C; i++)
|
xue@1
|
476 {
|
xue@1
|
477 for (int j=sh; j<=eh; j++)
|
xue@1
|
478 {
|
xue@1
|
479 int ind=i*2+j;
|
xue@1
|
480 if (ind>=C*2) tmp[ind-C*2]+=out[(2*i)<<L]*h[j];
|
xue@1
|
481 else if (ind<0) tmp[ind+C*2]+=out[(2*i)<<L]*h[j];
|
xue@1
|
482 else tmp[ind]+=out[(2*i)<<L]*h[j];
|
xue@1
|
483 }
|
xue@1
|
484 }
|
xue@1
|
485 for (int i=0; i<C; i++)
|
xue@1
|
486 {
|
xue@1
|
487 for (int j=sg; j<=eg; j++)
|
xue@1
|
488 {
|
xue@1
|
489 int ind=i*2+j;
|
xue@1
|
490 if (ind>=C*2) tmp[ind-C*2]+=out[(2*i+1)<<L]*g[j];
|
xue@1
|
491 else if (ind<0) tmp[ind+C*2]+=out[(2*i+1)<<L]*g[j];
|
xue@1
|
492 else tmp[ind]+=out[(2*i+1)<<L]*g[j];
|
xue@1
|
493 }
|
xue@1
|
494 }
|
xue@1
|
495 for (int i=0; i<C*2; i++) out[i<<L]=tmp[i];
|
xue@1
|
496 n/=2;
|
xue@1
|
497 C*=2;
|
xue@1
|
498 L--;
|
xue@1
|
499 }
|
xue@1
|
500 delete[] tmp;
|
xue@1
|
501 }//idwtp
|
xue@1
|
502
|
xue@1
|
503 //---------------------------------------------------------------------------
|
xue@1
|
504
|
xue@1
|
505 /*
|
xue@1
|
506 Spline biorthogonal wavelet routines.
|
xue@1
|
507
|
xue@1
|
508 Further reading: "Calculation of biorthogonal spline wavelets.pdf"
|
xue@1
|
509 */
|
xue@1
|
510
|
xue@1
|
511 //function Cmb: combination number C(n, k) (n>=k>=0)
|
xue@1
|
512 int Cmb(int n, int k)
|
xue@1
|
513 {
|
xue@1
|
514 if (k>n/2) k=n-k;
|
xue@1
|
515 int c=1;
|
xue@1
|
516 for (int i=1; i<=k; i++) c=c*(n+1-i)/i;
|
xue@1
|
517 return c;
|
xue@1
|
518 }
|
xue@1
|
519
|
Chris@5
|
520 /**
|
xue@1
|
521 function splinewl: computes spline biorthogonal wavelet filters. This version of splinewl calcualtes
|
xue@1
|
522 the positive-time half of h and hm coefficients only.
|
xue@1
|
523
|
xue@1
|
524 p1 and p2 must have the same parity. If p1 is even, p1 coefficients will be returned in h1; if p1 is
|
xue@1
|
525 odd, p1-1 coefficients will be returned in h1.
|
xue@1
|
526
|
xue@1
|
527 Actual length of h is p1+1, of hm is p1+2p2-1. only a half of each is kept.
|
xue@1
|
528 p even: h[0:p1/2] <- [p1/2:p1], hm[0:p1/2+p2-1] <- [p1/2+p2-1:p1+2p2-2]
|
xue@1
|
529 p odd: h[0:(p1-1)/2] <- [(p1+1)/2:p1], hm[0:(p1-3)/2+p2] <- [(p1-1)/2+p2:p1+2p2-2]
|
xue@1
|
530 i.e. h[0:hp1] <- [p1-hp1:p1], hm[0:hp1+p2-1] <- [p1-hp1-1+p2:p1+2p2-2]
|
xue@1
|
531
|
xue@1
|
532 In: p1, p2: specify vanishing moments of h and hm
|
xue@1
|
533 Out: h[] and hm[] as specified above.
|
xue@1
|
534
|
xue@1
|
535 No return value.
|
xue@1
|
536 */
|
xue@1
|
537 void splinewl(int p1, int p2, double* h, double* hm)
|
xue@1
|
538 {
|
xue@1
|
539 int hp1=p1/2, hp2=p2/2;
|
xue@1
|
540 int q=(p1+p2)/2;
|
xue@1
|
541 h[hp1]=sqrt(2.0)*pow(2, -p1);
|
xue@1
|
542 // h1[hp1]=1;
|
xue@1
|
543 for (int i=1, j=hp1-1; i<=hp1; i++, j--)
|
xue@1
|
544 {
|
xue@1
|
545 h[j]=h[j+1]*(p1+1-i)/i;
|
xue@1
|
546 }
|
xue@1
|
547
|
xue@1
|
548 double *_hh1=new double[p2+1], *_hh2=new double[2*q];
|
xue@1
|
549 double *hh1=&_hh1[p2-hp2], *hh2=&_hh2[q];
|
xue@1
|
550
|
xue@1
|
551 hh1[hp2]=sqrt(2.0)*pow(2, -p2);
|
xue@1
|
552 for (int i=1, j=hp2-1; i<=hp2; i++, j--)
|
xue@1
|
553 {
|
xue@1
|
554 hh1[j]=hh1[j+1]*(p2+1-i)/i;
|
xue@1
|
555 }
|
xue@1
|
556 if (p2%2) //p2 is odd
|
xue@1
|
557 {
|
xue@1
|
558 for (int i=0; i<=hp2; i++) hh1[-i-1]=hh1[i];
|
xue@1
|
559 }
|
xue@1
|
560 else //p2 even
|
xue@1
|
561 {
|
xue@1
|
562 for (int i=1; i<=hp2; i++) hh1[-i]=hh1[i];
|
xue@1
|
563 }
|
xue@1
|
564
|
xue@1
|
565 memset(_hh2, 0, sizeof(double)*2*q);
|
xue@1
|
566 for (int n=1-q; n<=q-1; n++)
|
xue@1
|
567 {
|
xue@1
|
568 int k=abs(n);
|
xue@1
|
569 int CC1=Cmb(q-1+k, k), CC2=Cmb(2*k, k-n); //CC=1.0*C(q-1+k, k)*C(2*k, k-n)
|
xue@1
|
570 for (; k<=q-1; k++)
|
xue@1
|
571 {
|
xue@1
|
572 hh2[n]=hh2[n]+1.0*CC1*CC2*pow(0.25, k);
|
xue@1
|
573 CC1=CC1*(q+k)/(k+1);
|
xue@1
|
574 CC2=CC2*(2*k+1)*(2*k+2)/((k+1-n)*(k+1+n));
|
xue@1
|
575 }
|
xue@1
|
576 hh2[n]*=pow(-1, n);
|
xue@1
|
577 }
|
xue@1
|
578
|
xue@1
|
579 //hh1[hp2-p2:hp2], hh2[1-q:q-1]
|
xue@1
|
580 //h2=conv(hh1, hh2), but the positive half only
|
xue@1
|
581 memset(hm, 0, sizeof(double)*(hp1+p2));
|
xue@1
|
582 for (int i=hp2-p2; i<=hp2; i++)
|
xue@1
|
583 for (int j=1-q; j<=q-1; j++)
|
xue@1
|
584 {
|
xue@1
|
585 if (i+j>=0 && i+j<hp1+p2)
|
xue@1
|
586 hm[i+j]+=hh1[i]*hh2[j];
|
xue@1
|
587 }
|
xue@1
|
588
|
xue@1
|
589 delete[] _hh1;
|
xue@1
|
590 delete[] _hh2;
|
xue@1
|
591 }//splinewl
|
xue@1
|
592
|
xue@1
|
593
|
Chris@5
|
594 /**
|
xue@1
|
595 function splinewl: calculates the analysis and reconstruction filter pairs of spline biorthogonal
|
xue@1
|
596 wavelet (h, g) and (hm, gm). h has the size p1+1, hm has the size p1+2p2-1.
|
xue@1
|
597
|
xue@1
|
598 If p1+1 is odd, then all four filters are symmetric; if not, then h and hm are symmetric, while g and
|
xue@1
|
599 gm are anti-symmetric.
|
xue@1
|
600
|
xue@1
|
601 The concatenation of filters h with hm (or g with gm) introduces a time shift of p1+p2-1, which is the
|
xue@1
|
602 return value multiplied by -1.
|
xue@1
|
603
|
xue@1
|
604 If normmode==1, the results are normalized so that ||h||^2=||g||^2=1;
|
xue@1
|
605 if normmode==2, the results are normalized so that ||hm||^2=||gm||^2=1,
|
xue@1
|
606 if normmode==3, the results are normalized so that ||h||^2==||g||^2=||hm||^2=||gm||^2.
|
xue@1
|
607
|
xue@1
|
608 If a *points* buffer is specified, the function returns the starting and ending
|
xue@1
|
609 positions (inclusive) of h, hm, g, and gm, in the order of (hs, he, hms, hme,
|
xue@1
|
610 gs, ge, gms, gme), as ps[0]~ps[7].
|
xue@1
|
611
|
xue@1
|
612 In: p1 and p2, specify vanishing moments of h and hm, respectively.
|
xue@1
|
613 normmode: mode for normalization
|
xue@1
|
614 Out: h[p1+1], g[p1+1], hm[p1+2p2-1], gm[p1+2p2-1], points[8] (optional)
|
xue@1
|
615
|
xue@1
|
616 Returns -p1-p2+1.
|
xue@1
|
617 */
|
xue@1
|
618 int splinewl(int p1, int p2, double* h, double* hm, double* g, double* gm, int normmode, int* points)
|
xue@1
|
619 {
|
xue@1
|
620 int lf=p1+1, lb=p1+2*p2-1;
|
xue@1
|
621 int hlf=lf/2, hlb=lb/2;
|
xue@1
|
622
|
xue@1
|
623 double *h1=&h[hlf], *h2=&hm[hlb];
|
xue@1
|
624 int hp1=p1/2, hp2=p2/2;
|
xue@1
|
625 int q=(p1+p2)/2;
|
xue@1
|
626 h1[hp1]=sqrt(2.0)*pow(2, -p1);
|
xue@1
|
627 // h1[hp1]=2*pow(2, -p1);
|
xue@1
|
628 for (int i=1, j=hp1-1; i<=hp1; i++, j--) h1[j]=h1[j+1]*(p1+1-i)/i;
|
xue@1
|
629
|
xue@1
|
630 double *_hh1=new double[p2+1+2*q];
|
xue@1
|
631 double *_hh2=&_hh1[p2+1];
|
xue@1
|
632 double *hh1=&_hh1[p2-hp2], *hh2=&_hh2[q];
|
xue@1
|
633 hh1[hp2]=sqrt(2.0)*pow(2, -p2);
|
xue@1
|
634 // hh1[hp2]=pow(2, -p2);
|
xue@1
|
635 for (int i=1, j=hp2-1; i<=hp2; i++, j--) hh1[j]=hh1[j+1]*(p2+1-i)/i;
|
xue@1
|
636 if (p2%2) for (int i=0; i<=hp2; i++) hh1[-i-1]=hh1[i];
|
xue@1
|
637 else for (int i=1; i<=hp2; i++) hh1[-i]=hh1[i];
|
xue@1
|
638 memset(_hh2, 0, sizeof(double)*2*q);
|
xue@1
|
639 for (int n=1-q; n<=q-1; n++)
|
xue@1
|
640 {
|
xue@1
|
641 int k=abs(n);
|
xue@1
|
642 int CC1=Cmb(q-1+k, k), CC2=Cmb(2*k, k-n); //CC=1.0*C(q-1+k, k)*C(2*k, k-n)
|
xue@1
|
643 for (int k=abs(n); k<=q-1; k++)
|
xue@1
|
644 {
|
xue@1
|
645 hh2[n]=hh2[n]+1.0*CC1*CC2*pow(0.25, k);
|
xue@1
|
646 CC1=CC1*(q+k)/(k+1);
|
xue@1
|
647 CC2=CC2*(2*k+1)*(2*k+2)/((k+1-n)*(k+1+n));
|
xue@1
|
648 }
|
xue@1
|
649 hh2[n]*=pow(-1, n);
|
xue@1
|
650 }
|
xue@1
|
651 //hh1[hp2-p2:hp2], hh2[1-q:q-1]
|
xue@1
|
652 //h2=conv(hh1, hh2), but the positive half only
|
xue@1
|
653 memset(h2, 0, sizeof(double)*(hp1+p2));
|
xue@1
|
654 for (int i=hp2-p2; i<=hp2; i++) for (int j=1-q; j<=q-1; j++)
|
xue@1
|
655 if (i+j>=0 && i+j<hp1+p2) h2[i+j]+=hh1[i]*hh2[j];
|
xue@1
|
656 delete[] _hh1;
|
xue@1
|
657
|
xue@1
|
658 int hs, he, hms, hme, gs, ge, gms, gme;
|
xue@1
|
659 if (lf%2)
|
xue@1
|
660 {
|
xue@1
|
661 hs=-hlf, he=hlf, hms=-hlb, hme=hlb;
|
xue@1
|
662 gs=-hlb+1, ge=hlb+1, gms=-hlf-1, gme=hlf-1;
|
xue@1
|
663 }
|
xue@1
|
664 else
|
xue@1
|
665 {
|
xue@1
|
666 hs=-hlf, he=hlf-1, hms=-hlb+1, hme=hlb;
|
xue@1
|
667 gs=-hlb, ge=hlb-1, gms=-hlf+1, gme=hlf;
|
xue@1
|
668 }
|
xue@1
|
669
|
xue@1
|
670 if (lf%2)
|
xue@1
|
671 {
|
xue@1
|
672 for (int i=1; i<=hlf; i++) h1[-i]=h1[i];
|
xue@1
|
673 for (int i=1; i<=hlb; i++) h2[-i]=h2[i];
|
xue@1
|
674 double* _g=&g[hlb-1], *_gm=&gm[hlf+1];
|
xue@1
|
675 for (int i=gs; i<=ge; i++) _g[i]=(i%2)?h2[1-i]:-h2[1-i];
|
xue@1
|
676 for (int i=gms; i<=gme; i++) _gm[i]=(i%2)?h1[-1-i]:-h1[-1-i];
|
xue@1
|
677 }
|
xue@1
|
678 else
|
xue@1
|
679 {
|
xue@1
|
680 for (int i=0; i<hlf; i++) h1[-i-1]=h1[i];
|
xue@1
|
681 for (int i=0; i<hlb; i++) h2[-i-1]=h2[i];
|
xue@1
|
682 h2=&h2[-1];
|
xue@1
|
683 double *_g=&g[hlb], *_gm=&gm[hlf-1];
|
xue@1
|
684 for (int i=gs; i<=ge; i++) _g[i]=(i%2)?-h2[-i]:h2[-i];
|
xue@1
|
685 for (int i=gms; i<=gme; i++) _gm[i]=(i%2)?-h1[-i]:h1[-i];
|
xue@1
|
686 }
|
xue@1
|
687
|
xue@1
|
688 if (normmode)
|
xue@1
|
689 {
|
xue@1
|
690 double sumh=0; for (int i=0; i<=he-hs; i++) sumh+=h[i]*h[i];
|
xue@1
|
691 double sumhm=0; for (int i=0; i<=hme-hms; i++) sumhm+=hm[i]*hm[i];
|
xue@1
|
692 if (normmode==1)
|
xue@1
|
693 {
|
xue@1
|
694 double rr=sqrt(sumh);
|
xue@1
|
695 for (int i=0; i<=hme-hms; i++) hm[i]*=rr;
|
xue@1
|
696 rr=1.0/rr;
|
xue@1
|
697 for (int i=0; i<=he-hs; i++) h[i]*=rr;
|
xue@1
|
698 rr=sqrt(sumhm);
|
xue@1
|
699 for (int i=0; i<=gme-gms; i++) gm[i]*=rr;
|
xue@1
|
700 rr=1.0/rr;
|
xue@1
|
701 for (int i=0; i<=ge-gs; i++) g[i]*=rr;
|
xue@1
|
702 }
|
xue@1
|
703 else if (normmode==2)
|
xue@1
|
704 {
|
xue@1
|
705 double rr=sqrt(sumh);
|
xue@1
|
706 for (int i=0; i<=ge-gs; i++) g[i]*=rr;
|
xue@1
|
707 rr=1.0/rr;
|
xue@1
|
708 for (int i=0; i<=gme-gms; i++) gm[i]*=rr;
|
xue@1
|
709 rr=sqrt(sumhm);
|
xue@1
|
710 for (int i=0; i<=he-hs; i++) h[i]*=rr;
|
xue@1
|
711 rr=1.0/rr;
|
xue@1
|
712 for (int i=0; i<=hme-hms; i++) hm[i]*=rr;
|
xue@1
|
713 }
|
xue@1
|
714 else if (normmode==3)
|
xue@1
|
715 {
|
xue@1
|
716 double rr=pow(sumh/sumhm, 0.25);
|
xue@1
|
717 for (int i=0; i<=hme-hms; i++) hm[i]*=rr;
|
xue@1
|
718 for (int i=0; i<=ge-gs; i++) g[i]*=rr;
|
xue@1
|
719 rr=1.0/rr;
|
xue@1
|
720 for (int i=0; i<=he-hs; i++) h[i]*=rr;
|
xue@1
|
721 for (int i=0; i<=gme-gms; i++) gm[i]*=rr;
|
xue@1
|
722 }
|
xue@1
|
723 }
|
xue@1
|
724
|
xue@1
|
725 if (points)
|
xue@1
|
726 {
|
xue@1
|
727 points[0]=hs, points[1]=he, points[2]=hms, points[3]=hme;
|
xue@1
|
728 points[4]=gs, points[5]=ge, points[6]=gms, points[7]=gme;
|
xue@1
|
729 }
|
xue@1
|
730 return -p1-p2+1;
|
xue@1
|
731 }//splinewl
|
xue@1
|
732
|
xue@1
|
733 //---------------------------------------------------------------------------
|
Chris@5
|
734 /**
|
xue@1
|
735 function wavpacqmf: calculate pseudo local cosine transforms using wavelet packet
|
xue@1
|
736
|
xue@1
|
737 In: data[Count], Count=fr*WID, waveform data
|
xue@1
|
738 WID: largest scale, equals 2^ORDER
|
xue@1
|
739 wid: smallest scale, euqals 2^order
|
xue@1
|
740 h[M], g[M]: quadratic mirror filter pair, fr>2*M
|
xue@1
|
741 Out: spec[0][fr][WID], spec[1][2*fr][WID/2], ..., spec[ORDER-order-1][FR][wid]
|
xue@1
|
742
|
xue@1
|
743 No return value.
|
xue@1
|
744 */
|
xue@1
|
745 void wavpacqmf(double*** spec, double* data, int Count, int WID, int wid, int M, double* h, double* g)
|
xue@1
|
746 {
|
xue@1
|
747 int fr=Count/WID, ORDER=log2(WID), order=log2(wid);
|
xue@1
|
748 double* _data1=new double[Count*2];
|
xue@1
|
749 double *data1=_data1, *data2=&_data1[Count];
|
xue@1
|
750 //the qmf always filters data1 and puts the results to data2
|
xue@1
|
751 memcpy(data1, data, sizeof(double)*Count);
|
xue@1
|
752 int l=0, C=fr*WID, FR=1;
|
xue@1
|
753 double *ldata, *ldataa, *ldatad;
|
xue@1
|
754 while (l<ORDER)
|
xue@1
|
755 {
|
xue@1
|
756 //qmf
|
xue@1
|
757 for (int f=0; f<FR; f++)
|
xue@1
|
758 {
|
xue@1
|
759 ldata=&data1[f*C];
|
xue@1
|
760 if (f%2==0)
|
xue@1
|
761 ldataa=&data2[f*C], ldatad=&data2[f*C+C/2];
|
xue@1
|
762 else
|
xue@1
|
763 ldatad=&data2[f*C], ldataa=&data2[f*C+C/2];
|
xue@1
|
764
|
xue@1
|
765 memset(&data2[f*C], 0, sizeof(double)*C);
|
xue@1
|
766 for (int i=0; i<C; i+=2)
|
xue@1
|
767 {
|
xue@1
|
768 int i2=i/2;
|
xue@1
|
769 ldataa[i2]=ldata[i]*h[0];
|
xue@1
|
770 ldatad[i2]=ldata[i]*g[0];
|
xue@1
|
771 for (int j=1; j<M; j++)
|
xue@1
|
772 {
|
xue@1
|
773 if (i+j<C)
|
xue@1
|
774 {
|
xue@1
|
775 ldataa[i2]+=ldata[i+j]*h[j];
|
xue@1
|
776 ldatad[i2]+=ldata[i+j]*g[j];
|
xue@1
|
777 }
|
xue@1
|
778 else
|
xue@1
|
779 {
|
xue@1
|
780 ldataa[i2]+=ldata[i+j-C]*h[j];
|
xue@1
|
781 ldatad[i2]+=ldata[i+j-C]*g[j];
|
xue@1
|
782 }
|
xue@1
|
783 }
|
xue@1
|
784 }
|
xue@1
|
785 }
|
xue@1
|
786 double *tmp=data2; data2=data1; data1=tmp;
|
xue@1
|
787 l++;
|
xue@1
|
788 C=(C>>1);
|
xue@1
|
789 FR=(FR<<1);
|
xue@1
|
790 if (l>=order)
|
xue@1
|
791 {
|
xue@1
|
792 for (int f=0; f<FR; f++)
|
xue@1
|
793 for(int i=0; i<C; i++)
|
xue@1
|
794 spec[ORDER-l][i][f]=data1[f*C+i];
|
xue@1
|
795 }
|
xue@1
|
796 }
|
xue@1
|
797
|
xue@1
|
798 delete[] _data1;
|
xue@1
|
799 }//wavpacqmf
|
xue@1
|
800
|
Chris@5
|
801 /**
|
xue@1
|
802 function iwavpacqmf: inverse transform of wavpacqmf
|
xue@1
|
803
|
xue@1
|
804 In: spec[Fr][Wid], Fr>M*2
|
xue@1
|
805 h[M], g[M], quadratic mirror filter pair
|
xue@1
|
806 Out: data[Fr*Wid]
|
xue@1
|
807
|
xue@1
|
808 No return value.
|
xue@1
|
809 */
|
xue@1
|
810 void iwavpacqmf(double* data, double** spec, int Fr, int Wid, int M, double* h, double* g)
|
xue@1
|
811 {
|
xue@1
|
812 int Count=Fr*Wid, Order=log2(Wid);
|
xue@1
|
813 double* _data1=new double[Count];
|
xue@1
|
814 double *data1, *data2, *ldata, *ldataa, *ldatad;
|
xue@1
|
815 if (Order%2) data1=_data1, data2=data;
|
xue@1
|
816 else data1=data, data2=_data1;
|
xue@1
|
817 //data pass to buffer
|
xue@1
|
818 for (int i=0, t=0; i<Wid; i++)
|
xue@1
|
819 for (int j=0; j<Fr; j++)
|
xue@1
|
820 data1[t++]=spec[j][i];
|
xue@1
|
821
|
xue@1
|
822 int l=Order;
|
xue@1
|
823 int C=Fr;
|
xue@1
|
824 int K=Wid/2;
|
xue@1
|
825 while (l>0)
|
xue@1
|
826 {
|
xue@1
|
827 memset(data2, 0, sizeof(double)*Count);
|
xue@1
|
828 for (int k=0; k<K; k++)
|
xue@1
|
829 {
|
xue@1
|
830 if (k%2==0) ldataa=&data1[2*k*C], ldatad=&data1[(2*k+1)*C];
|
xue@1
|
831 else ldatad=&data1[2*k*C], ldataa=&data1[(2*k+1)*C];
|
xue@1
|
832 ldata=&data2[2*k*C];
|
xue@1
|
833 //qmf
|
xue@1
|
834 for (int i=0; i<C; i++)
|
xue@1
|
835 {
|
xue@1
|
836 for (int j=0; j<M; j++)
|
xue@1
|
837 {
|
xue@1
|
838 if (i*2+j<C*2)
|
xue@1
|
839 {
|
xue@1
|
840 ldata[i*2+j]+=ldataa[i]*h[j]+ldatad[i]*g[j];
|
xue@1
|
841 }
|
xue@1
|
842 else
|
xue@1
|
843 {
|
xue@1
|
844 ldata[i*2+j-C*2]+=ldataa[i]*h[j]+ldatad[i]*g[j];
|
xue@1
|
845 }
|
xue@1
|
846 }
|
xue@1
|
847 }
|
xue@1
|
848 }
|
xue@1
|
849
|
xue@1
|
850 double *tmp=data2; data2=data1; data1=tmp;
|
xue@1
|
851 l--;
|
xue@1
|
852 C=(C<<1);
|
xue@1
|
853 K=(K>>1);
|
xue@1
|
854 }
|
xue@1
|
855 delete[] _data1;
|
xue@1
|
856 }//iwavpacqmf
|
xue@1
|
857
|
Chris@5
|
858 /**
|
xue@1
|
859 function wavpac: calculate pseudo local cosine transforms using wavelet packet,
|
xue@1
|
860
|
xue@1
|
861 In: data[Count], Count=fr*WID, waveform data
|
xue@1
|
862 WID: largest scale, equals 2^ORDER
|
xue@1
|
863 wid: smallest scale, euqals 2^order
|
xue@1
|
864 h[hs:he-1], g[gs:ge-1]: filter pair
|
xue@1
|
865 Out: spec[0][fr][WID], spec[1][2*fr][WID/2], ..., spec[ORDER-order-1][FR][wid]
|
xue@1
|
866
|
xue@1
|
867 No return value.
|
xue@1
|
868 */
|
xue@1
|
869 void wavpac(double*** spec, double* data, int Count, int WID, int wid, double* h, int hs, int he, double* g, int gs, int ge)
|
xue@1
|
870 {
|
xue@1
|
871 int fr=Count/WID, ORDER=log2(WID), order=log2(wid);
|
xue@1
|
872 double* _data1=new double[Count*2];
|
xue@1
|
873 double *data1=_data1, *data2=&_data1[Count];
|
xue@1
|
874 //the qmf always filters data1 and puts the results to data2
|
xue@1
|
875 memcpy(data1, data, sizeof(double)*Count);
|
xue@1
|
876 int l=0, C=fr*WID, FR=1;
|
xue@1
|
877 double *ldata, *ldataa, *ldatad;
|
xue@1
|
878 while (l<ORDER)
|
xue@1
|
879 {
|
xue@1
|
880 //qmf
|
xue@1
|
881 for (int f=0; f<FR; f++)
|
xue@1
|
882 {
|
xue@1
|
883 ldata=&data1[f*C];
|
xue@1
|
884 if (f%2==0)
|
xue@1
|
885 ldataa=&data2[f*C], ldatad=&data2[f*C+C/2];
|
xue@1
|
886 else
|
xue@1
|
887 ldatad=&data2[f*C], ldataa=&data2[f*C+C/2];
|
xue@1
|
888
|
xue@1
|
889 memset(&data2[f*C], 0, sizeof(double)*C);
|
xue@1
|
890 for (int i=0; i<C; i+=2)
|
xue@1
|
891 {
|
xue@1
|
892 int i2=i/2;
|
xue@1
|
893 ldataa[i2]=0;//ldata[i]*h[0];
|
xue@1
|
894 ldatad[i2]=0;//ldata[i]*g[0];
|
xue@1
|
895 for (int j=hs; j<=he; j++)
|
xue@1
|
896 {
|
xue@1
|
897 int ind=i-j;
|
xue@1
|
898 if (ind>=C)
|
xue@1
|
899 {
|
xue@1
|
900 ldataa[i2]+=ldata[ind-C]*h[j];
|
xue@1
|
901 }
|
xue@1
|
902 else if (ind<0)
|
xue@1
|
903 {
|
xue@1
|
904 ldataa[i2]+=ldata[ind+C]*h[j];
|
xue@1
|
905 }
|
xue@1
|
906 else
|
xue@1
|
907 {
|
xue@1
|
908 ldataa[i2]+=ldata[ind]*h[j];
|
xue@1
|
909 }
|
xue@1
|
910 }
|
xue@1
|
911 for (int j=gs; j<=ge; j++)
|
xue@1
|
912 {
|
xue@1
|
913 int ind=i-j;
|
xue@1
|
914 if (ind>=C)
|
xue@1
|
915 {
|
xue@1
|
916 ldatad[i2]+=ldata[ind-C]*g[j];
|
xue@1
|
917 }
|
xue@1
|
918 else if (ind<0)
|
xue@1
|
919 {
|
xue@1
|
920 ldatad[i2]+=ldata[ind+C]*g[j];
|
xue@1
|
921 }
|
xue@1
|
922 else
|
xue@1
|
923 {
|
xue@1
|
924 ldatad[i2]+=ldata[ind]*g[j];
|
xue@1
|
925 }
|
xue@1
|
926 }
|
xue@1
|
927 }
|
xue@1
|
928 }
|
xue@1
|
929 double *tmp=data2; data2=data1; data1=tmp;
|
xue@1
|
930 l++;
|
xue@1
|
931 C=(C>>1);
|
xue@1
|
932 FR=(FR<<1);
|
xue@1
|
933 if (l>=order)
|
xue@1
|
934 {
|
xue@1
|
935 for (int f=0; f<FR; f++)
|
xue@1
|
936 for(int i=0; i<C; i++)
|
xue@1
|
937 spec[ORDER-l][i][f]=data1[f*C+i];
|
xue@1
|
938 }
|
xue@1
|
939 }
|
xue@1
|
940
|
xue@1
|
941 delete[] _data1;
|
xue@1
|
942 }//wavpac
|
xue@1
|
943
|
Chris@5
|
944 /**
|
xue@1
|
945 function iwavpac: inverse transform of wavpac
|
xue@1
|
946
|
xue@1
|
947 In: spec[Fr][Wid]
|
xue@1
|
948 h[hs:he-1], g[gs:ge-1], reconstruction filter pair
|
xue@1
|
949 Out: data[Fr*Wid]
|
xue@1
|
950
|
xue@1
|
951 No return value.
|
xue@1
|
952 */
|
xue@1
|
953 void iwavpac(double* data, double** spec, int Fr, int Wid, double* h, int hs, int he, double* g, int gs, int ge)
|
xue@1
|
954 {
|
xue@1
|
955 int Count=Fr*Wid, Order=log2(Wid);
|
xue@1
|
956 double* _data1=new double[Count];
|
xue@1
|
957 double *data1, *data2, *ldata, *ldataa, *ldatad;
|
xue@1
|
958 if (Order%2) data1=_data1, data2=data;
|
xue@1
|
959 else data1=data, data2=_data1;
|
xue@1
|
960 //data pass to buffer
|
xue@1
|
961 for (int i=0, t=0; i<Wid; i++)
|
xue@1
|
962 for (int j=0; j<Fr; j++)
|
xue@1
|
963 data1[t++]=spec[j][i];
|
xue@1
|
964
|
xue@1
|
965 int l=Order;
|
xue@1
|
966 int C=Fr;
|
xue@1
|
967 int K=Wid/2;
|
xue@1
|
968 while (l>0)
|
xue@1
|
969 {
|
xue@1
|
970 memset(data2, 0, sizeof(double)*Count);
|
xue@1
|
971 for (int k=0; k<K; k++)
|
xue@1
|
972 {
|
xue@1
|
973 if (k%2==0) ldataa=&data1[2*k*C], ldatad=&data1[(2*k+1)*C];
|
xue@1
|
974 else ldatad=&data1[2*k*C], ldataa=&data1[(2*k+1)*C];
|
xue@1
|
975 ldata=&data2[2*k*C];
|
xue@1
|
976 //qmf
|
xue@1
|
977 for (int i=0; i<C; i++)
|
xue@1
|
978 {
|
xue@1
|
979 for (int j=hs; j<=he; j++)
|
xue@1
|
980 {
|
xue@1
|
981 int ind=i*2+j;
|
xue@1
|
982 if (ind>=C*2)
|
xue@1
|
983 {
|
xue@1
|
984 ldata[ind-C*2]+=ldataa[i]*h[j];
|
xue@1
|
985 }
|
xue@1
|
986 else if (ind<0)
|
xue@1
|
987 {
|
xue@1
|
988 ldata[ind+C*2]+=ldataa[i]*h[j];
|
xue@1
|
989 }
|
xue@1
|
990 else
|
xue@1
|
991 {
|
xue@1
|
992 ldata[ind]+=ldataa[i]*h[j];
|
xue@1
|
993 }
|
xue@1
|
994 }
|
xue@1
|
995 for (int j=gs; j<=ge; j++)
|
xue@1
|
996 {
|
xue@1
|
997 int ind=i*2+j;
|
xue@1
|
998 if (ind>=C*2)
|
xue@1
|
999 {
|
xue@1
|
1000 ldata[ind-C*2]+=ldatad[i]*g[j];
|
xue@1
|
1001 }
|
xue@1
|
1002 else if (ind<0)
|
xue@1
|
1003 {
|
xue@1
|
1004 ldata[ind+C*2]+=ldatad[i]*g[j];
|
xue@1
|
1005 }
|
xue@1
|
1006 else
|
xue@1
|
1007 {
|
xue@1
|
1008 ldata[ind]+=ldatad[i]*g[j];
|
xue@1
|
1009 }
|
xue@1
|
1010 }
|
xue@1
|
1011 }
|
xue@1
|
1012 }
|
xue@1
|
1013
|
xue@1
|
1014 double *tmp=data2; data2=data1; data1=tmp;
|
xue@1
|
1015 l--;
|
xue@1
|
1016 C=(C<<1);
|
xue@1
|
1017 K=(K>>1);
|
xue@1
|
1018 }
|
xue@1
|
1019 delete[] _data1;
|
xue@1
|
1020 }//iwavpac
|