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