1 /*
2  * -------------------------------------------------------------------------
3  * cwt.c -- continuous wavelet transform
4  * SWT - Scilab wavelet toolbox
5  * Copyright (C) 2005-2006  Roger Liu
6  * Copyright (C) 20010-2012  Holger Nahrstaedt
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  * -------------------------------------------------------------------------
22  */
23 #include "swtlib.h"
24 #include "kiss_fft.h"
25 
26 int fftshift (double *vector_in, double *vector_out, int vector_length);
27 int ifft (int Signal_Length, int Nfft, double *sig_real, double *sig_imag);
28 
29 
30 /*void haar(double *x, int sigInLength, double *psi, int sigOutLength, double ys)
31 {
32 	int count;
33 
34 	for(count=0;count<sigInLength;count++)
35 	{
36 		if (x[count]<0)
37 			psi[count] = -1/ys;
38 		else
39 			psi[count] = 1/ys;
40 	}
41 	return;
42 }*/
43 
44 double
powof(double x,double alpha)45 powof (double x, double alpha)
46 {
47   double         resu;
48 
49 
50   if (x >= 0) /* in this case, no problem */
51     {
52       if (x == 0)
53 	resu = 0.0;
54       else
55 	resu = exp (alpha * log (x));
56     }
57   else /* there may be problems */
58     {
59       if (alpha == (int)(alpha)) /* if alpha is an integer */
60 	{
61 	  /* then x^alpha is real-valued */
62 	  resu = powof ( -x, alpha);
63 	  /* and the sign of the results depends on
64 	     wether alpha is ODD or EVEN */
65 	  if (ISODD(alpha) == 1)
66 	    {
67 	      /* alpha is ODD */
68 	      resu = -resu;
69 	    }
70 	}
71       else
72 	{
73 	  //Scierror (999,"Attempt to compute x^alpha with x<0 : complex valued result\n");
74 	  fprintf(stderr, "Error: Attempt to compute x^alpha with x<0 : complex valued result\n");
75 	  return 0;
76 	}
77     }
78   return resu;
79 }
80 
81 /*-------------------------------------------
82  * Sinus Scale Filter Generation
83  *-----------------------------------------*/
84 
sinus(double * x,int sigInLength,double * psi,int sigOutLength,double ys)85 void sinus(double *x, int sigInLength, double *psi, int sigOutLength, double ys)
86 {
87 	int count;
88 
89 	for(count=0;count<sigInLength;count++)
90 		psi[count] = -1*sin(2*PI*x[count])/ys;
91 	return;
92 }
93 
94 /*-------------------------------------------
95  * Poisson Scale Filter Generation
96  *-----------------------------------------*/
97 
poisson(double * x,int sigInLength,double * psi,int sigOutLength,double ys)98 void poisson(double *x, int sigInLength, double *psi, int sigOutLength, double ys)
99 {
100 	int count;
101 	double x2;
102 
103     for(count=0;count<sigInLength;count++)
104 	{
105 		x2 = x[count] * x[count] * 4.0 / 9.0;
106 		psi[count] = (1-x2)/(PI*(1+x2)*ys);
107 	}
108 	return;
109 }
110 
111 /*-------------------------------------------
112  * Mexican Hat Scale Filter Generation
113  *-----------------------------------------*/
114 
mexihat(double * x,int sigInLength,double * psi,int sigOutLength,double ys)115 void mexihat(double *x, int sigInLength, double *psi, int sigOutLength, double ys)
116 {
117 	int count;
118 	double x2, con;
119 
120     //con = 2*sqrt(sqrt(PI))/sqrt(3);
121     con = 2/(sqrt(3)*sqrt(sqrt(PI)));
122 	for(count=0;count<sigInLength;count++)
123 	{
124 		x2 = x[count] * x[count];
125 		psi[count] = (1-x2)*exp(-x2/2)*con/ys;
126 	}
127 	return;
128 }
129 
130 /*-------------------------------------------
131  * Morlet Scale Filter Generation
132  *-----------------------------------------*/
133 
morlet(double * x,int sigInLength,double * psi,int sigOutLength,double ys)134 void morlet(double *x, int sigInLength, double *psi, int sigOutLength, double ys)
135 {
136 	int count;
137 	double x2;
138 
139     for(count=0;count<sigInLength;count++)
140 	{
141 		x2 = x[count] * x[count];
142 		psi[count] = cos(5*x[count])*exp(-x2/2)/ys;
143 	}
144 	return;
145 }
146 
147 /*-------------------------------------------
148  * DoG Scale Filter Generation
149  *-----------------------------------------*/
150 
DOGauss(double * x,int sigInLength,double * psi,int sigOutLength,double ys)151 void DOGauss(double *x, int sigInLength, double *psi, int sigOutLength, double ys)
152 {
153 	int count;
154 	double x2;
155 
156     for(count=0;count<sigInLength;count++)
157 	{
158 		x2 = x[count] * x[count];
159 		psi[count] = exp(-x2/2)/ys-exp(-x2/8)/(2*ys);
160 	}
161 	return;
162 }
163 
164 /*-------------------------------------------
165  * Gauss Scale Filter Generation
166  *-----------------------------------------*/
167 
Gauss(double * x,int sigInLength,double * psi,int sigOutLength,int n,double ys)168 void Gauss(double *x, int sigInLength, double *psi, int sigOutLength, int n, double ys)
169 {
170 	switch (n) {
171 		case 1:
172 			Gaus1(x, sigInLength, psi, sigOutLength, ys);
173 			break;
174 		case 2:
175 			Gaus2(x, sigInLength, psi, sigOutLength, ys);
176 			break;
177 		case 3:
178 			Gaus3(x, sigInLength, psi, sigOutLength, ys);
179 			break;
180 		case 4:
181 			Gaus4(x, sigInLength, psi, sigOutLength, ys);
182 			break;
183 		case 5:
184 			Gaus5(x, sigInLength, psi, sigOutLength, ys);
185 			break;
186 		case 6:
187 			Gaus6(x, sigInLength, psi, sigOutLength, ys);
188 			break;
189 		case 7:
190 			Gaus7(x, sigInLength, psi, sigOutLength, ys);
191 			break;
192 		case 8:
193 			Gaus8(x, sigInLength, psi, sigOutLength, ys);
194 			break;
195 		default:
196 			break;
197 	}
198 
199 
200 	return;
201 }
202 
Gaus1(double * x,int sigInLength,double * psi,int sigOutLength,double ys)203 void Gaus1(double *x, int sigInLength, double *psi, int sigOutLength, double ys)
204 {
205 	int count;
206 	double x2;
207 
208 	for(count=0;count<sigInLength;count++)
209 	{
210 		x2 = x[count] * x[count];
211 		psi[count] = -2*x[count]*exp(-x2)/sqrt(sqrt(PI/2));
212 	}
213 	return;
214 }
215 
216 
Gaus2(double * x,int sigInLength,double * psi,int sigOutLength,double ys)217 void Gaus2(double *x, int sigInLength, double *psi, int sigOutLength, double ys)
218 {
219 	int count;
220 	double x2;
221 
222 	for(count=0;count<sigInLength;count++)
223 	{
224 		x2 = x[count] * x[count];
225 		psi[count] = 2*(2*x2-1)*exp(-x2)/sqrt(3*sqrt(PI/2));
226 	}
227 	return;
228 }
229 
230 
Gaus3(double * x,int sigInLength,double * psi,int sigOutLength,double ys)231 void Gaus3(double *x, int sigInLength, double *psi, int sigOutLength, double ys)
232 {
233 	int count;
234 	double x2,x3;
235 
236 	for(count=0;count<sigInLength;count++)
237 	{
238 		x2 = x[count] * x[count];
239 		x3 = x2 * x[count];
240 		psi[count] = -4*(2*x3-3*x[count])*exp(-x2)/sqrt(15*sqrt(PI/2));
241 	}
242 	return;
243 }
244 
245 
Gaus4(double * x,int sigInLength,double * psi,int sigOutLength,double ys)246 void Gaus4(double *x, int sigInLength, double *psi, int sigOutLength, double ys)
247 {
248 	int count;
249 	double x2,x4;
250 
251 	for(count=0;count<sigInLength;count++)
252 	{
253 	 	x2 = x[count] * x[count];
254 		x4 = x2 * x2;
255 		psi[count] = 4*(-12*x2+4*x4+3)*exp(-x2)/sqrt(105*sqrt(PI/2));
256 	}
257 	return;
258 }
259 
260 
Gaus5(double * x,int sigInLength,double * psi,int sigOutLength,double ys)261 void Gaus5(double *x, int sigInLength, double *psi, int sigOutLength, double ys)
262 {
263 	int count;
264 	double x2,x3,x5;
265 
266 	for(count=0;count<sigInLength;count++)
267 	{
268          x2 = x[count] * x[count];
269 	 	 x3 = x2* x[count];
270 		 x5 = x3 * x2;
271 		psi[count] = 8*(-4*x5+20*x3-15*x[count])*exp(-x2)/sqrt(105*9*sqrt(PI/2));
272 	}
273 	return;
274 }
275 
276 
Gaus6(double * x,int sigInLength,double * psi,int sigOutLength,double ys)277 void Gaus6(double *x, int sigInLength, double *psi, int sigOutLength, double ys)
278 {
279 	int count;
280 	double x2,x4,x6;
281 
282 	for(count=0;count<sigInLength;count++)
283 	{
284         x2 = x[count] * x[count];
285 		x4 = x2 * x2;
286 		x6 = x4 * x2;
287 		psi[count] = 8*(8*x6-60*x4+90*x2-15)*exp(-x2)/sqrt(105*9*11*sqrt(PI/2));
288 	}
289 	return;
290 }
291 
292 
Gaus7(double * x,int sigInLength,double * psi,int sigOutLength,double ys)293 void Gaus7(double *x, int sigInLength, double *psi, int sigOutLength, double ys)
294 {
295 	int count;
296 	double x2,x3,x5,x7;
297 
298 	for(count=0;count<sigInLength;count++)
299 	{
300         x2 = x[count] * x[count];
301 	    x3 = x2 * x[count];
302 		x5 = x3 * x2;
303 		x7 = x5 * x2;
304 		psi[count] = 16*(-8*x7+84*x5-210*x3+105*x[count])*exp(-x2)/sqrt(105*9*11*13*sqrt(PI/2));
305 	}
306 	return;
307 }
308 
309 
Gaus8(double * x,int sigInLength,double * psi,int sigOutLength,double ys)310 void Gaus8(double *x, int sigInLength, double *psi, int sigOutLength, double ys)
311 {
312 	int count;
313 	double x2,x4,x6,x8;
314 
315 	for(count=0;count<sigInLength;count++)
316 	{
317         x2 = x[count] * x[count];
318     	x4 = x2 * x2;
319 	    x6 = x4 * x2;
320 		x8 = x6 * x2;
321 		psi[count] = 16*(16*x8-224*x6+840*x4-840*x2+105)*exp(-x2)/sqrt(105*9*11*13*15*sqrt(PI/2));
322 	}
323 	return;
324 }
325 
326 /*-------------------------------------------
327  * Complex Gauss Scale Filter Generation
328  *-----------------------------------------*/
cgauss(double * x,int sigInLength,int p,double * psir,double * psii,int sigOutLength,double ys)329 void cgauss(double *x, int sigInLength, int p, double *psir, double *psii, int sigOutLength, double ys)
330 {
331 
332     switch (p) {
333 		case 1:
334 			cgau1(x, sigInLength, psir, psii, sigOutLength, ys);
335 			break;
336 		case 2:
337 			cgau2(x, sigInLength, psir, psii, sigOutLength, ys);
338 			break;
339 		case 3:
340 			cgau3(x, sigInLength, psir, psii, sigOutLength, ys);
341 			break;
342 		case 4:
343 			cgau4(x, sigInLength, psir, psii, sigOutLength, ys);
344 			break;
345 		case 5:
346 			cgau5(x, sigInLength, psir, psii, sigOutLength, ys);
347 			break;
348 		case 6:
349 			cgau6(x, sigInLength, psir, psii, sigOutLength, ys);
350 			break;
351 		case 7:
352 			cgau7(x, sigInLength, psir, psii, sigOutLength, ys);
353 			break;
354 		case 8:
355 			cgau8(x, sigInLength, psir, psii, sigOutLength, ys);
356 			break;
357 		default:
358 			break;
359 	}
360 	return;
361 }
362 
cgau1(double * x,int sigInLength,double * psir,double * psii,int sigOutLength,double ys)363 void cgau1(double *x, int sigInLength,
364 			double *psir, double *psii,
365 			int sigOutLength, double ys)
366 {
367     int count;
368 	double x2,cosx,sinx;
369 
370 	for(count=0;count<sigInLength;count++)
371 	{
372        x2 = x[count] * x[count];
373 	   cosx = cos(x[count]);
374 	   sinx = sin(x[count]);
375 	   psir[count]=(-2*x[count]*cosx-sinx)*exp(-x2)/sqrt(2*sqrt(PI/2));
376 	   psii[count]=(2*x[count]*sinx-cosx)*exp(-x2)/sqrt(2*sqrt(PI/2));
377 	}
378 	return;
379 }
380 
cgau1_packet(double * x,int sigInLength,double * f,int sigOutLength,double ys)381 void cgau1_packet(double *x, int sigInLength,
382 			double *f, int sigOutLength, double ys)
383 {
384 	cgau1(x, sigInLength,f, f+sigInLength, sigOutLength,1);
385     return;
386 }
387 
cgau2(double * x,int sigInLength,double * psir,double * psii,int sigOutLength,double ys)388 void cgau2(double *x, int sigInLength,
389 			double *psir, double *psii,
390 			int sigOutLength, double ys)
391 {
392     int count;
393 	double x2,cosx,sinx;
394 
395 	for(count=0;count<sigInLength;count++)
396 	{
397        x2 = x[count] * x[count];
398 	   cosx = cos(x[count]);
399 	   sinx = sin(x[count]);
400 	   psir[count]=(4*x2*cosx+4*x[count]*sinx-3*cosx)*exp(-x2)/sqrt(10*sqrt(PI/2));
401 	   psii[count]=(-4*x2*sinx+4*x[count]*cosx+3*sinx)*exp(-x2)/sqrt(10*sqrt(PI/2));
402 	}
403 	return;
404 }
405 
cgau2_packet(double * x,int sigInLength,double * f,int sigOutLength,double ys)406 void cgau2_packet(double *x, int sigInLength,
407 			double *f, int sigOutLength, double ys)
408 {
409 	cgau2(x, sigInLength,f, f+sigInLength, sigOutLength,1);
410     return;
411 }
412 
cgau3(double * x,int sigInLength,double * psir,double * psii,int sigOutLength,double ys)413 void cgau3(double *x, int sigInLength,
414 			double *psir, double *psii,
415 			int sigOutLength, double ys)
416 {
417     int count;
418 	double x2,cosx,sinx,x3;
419 
420 	for(count=0;count<sigInLength;count++)
421 	{
422        x2 = x[count] * x[count];
423 	   x3 = x[count] * x2;
424 	   cosx = cos(x[count]);
425 	   sinx = sin(x[count]);
426 	   psir[count]=(-8*x3*cosx-12*x2*sinx+18*x[count]*cosx+7*sinx)*exp(-x2)/sqrt(76*sqrt(PI/2));
427 	   psii[count]=(8*x3*sinx-12*x2*cosx-18*x[count]*sinx+7*cosx)*exp(-x2)/sqrt(76*sqrt(PI/2));
428 	}
429 	return;
430 }
431 
cgau3_packet(double * x,int sigInLength,double * f,int sigOutLength,double ys)432 void cgau3_packet(double *x, int sigInLength,
433 			double *f, int sigOutLength, double ys)
434 {
435 	cgau3(x, sigInLength,f, f+sigInLength, sigOutLength,1);
436     return;
437 }
438 
cgau4(double * x,int sigInLength,double * psir,double * psii,int sigOutLength,double ys)439 void cgau4(double *x, int sigInLength,
440 			double *psir, double *psii,
441 			int sigOutLength, double ys)
442 {
443     int count;
444 	double x2,cosx,sinx,x3,x4;
445 
446 	for(count=0;count<sigInLength;count++)
447 	{
448        x2 = x[count] * x[count];
449 	   x3 = x[count] * x2;
450 	   x4 = x2 * x2;
451 	   cosx = cos(x[count]);
452 	   sinx = sin(x[count]);
453  	   psir[count]=(16*x4*cosx+32*x3*sinx-72*x2*cosx-56*x[count]*sinx+25*cosx)*exp(-x2)/sqrt(764*sqrt(PI/2));
454  	   psii[count]=(-16*x4*sinx+32*x3*cosx+72*x2*sinx-56*x[count]*cosx-25*sinx)*exp(-x2)/sqrt(764*sqrt(PI/2));
455 	}
456 	return;
457 }
458 
cgau4_packet(double * x,int sigInLength,double * f,int sigOutLength,double ys)459 void cgau4_packet(double *x, int sigInLength,
460 			double *f, int sigOutLength, double ys)
461 {
462 	cgau4(x, sigInLength,f, f+sigInLength, sigOutLength,1);
463     return;
464 }
465 
cgau5(double * x,int sigInLength,double * psir,double * psii,int sigOutLength,double ys)466 void cgau5(double *x, int sigInLength,
467 			double *psir, double *psii,
468 			int sigOutLength, double ys)
469 {
470     int count;
471 	double x2,cosx,sinx,x3,x4,x5;
472 
473 	for(count=0;count<sigInLength;count++)
474 	{
475        x2 = x[count] * x[count];
476 	   x3 = x[count] * x2;
477 	   x4 = x2 * x2;
478 	   x5 = x2 * x3;
479 	   cosx = cos(x[count]);
480 	   sinx = sin(x[count]);
481  	   psir[count]=(-32*x5*cosx-80*x4*sinx+240*x3*cosx+280*x2*sinx-250*x[count]*cosx-81*sinx)*exp(-x2)/sqrt(9496*sqrt(PI/2));
482  	   psii[count]=(32*x5*sinx-80*x4*cosx-240*x3*sinx+280*x2*cosx+250*x[count]*sinx-81*cosx)*exp(-x2)/sqrt(9496*sqrt(PI/2));
483 	}
484 	return;
485 }
486 
cgau5_packet(double * x,int sigInLength,double * f,int sigOutLength,double ys)487 void cgau5_packet(double *x, int sigInLength,
488 			double *f, int sigOutLength, double ys)
489 {
490 	cgau5(x, sigInLength,f, f+sigInLength, sigOutLength,1);
491     return;
492 }
493 
cgau6(double * x,int sigInLength,double * psir,double * psii,int sigOutLength,double ys)494 void cgau6(double *x, int sigInLength,
495 			double *psir, double *psii,
496 			int sigOutLength, double ys)
497 {
498     int count;
499 	double x2,cosx,sinx,x3,x4,x5, x6;
500 
501 	for(count=0;count<sigInLength;count++)
502 	{
503        x2 = x[count] * x[count];
504 	   x3 = x[count] * x2;
505 	   x4 = x2 * x2;
506 	   x5 = x2 * x3;
507 	   x6 = x3 * x3;
508 	   cosx = cos(x[count]);
509 	   sinx = sin(x[count]);
510 	   psir[count]=(64*x6*cosx+192*x5*sinx-720*x4*cosx-1120*x3*sinx+1500*x2*cosx+972*x[count]*sinx-331*cosx)*exp(-x2)/sqrt(140152*sqrt(PI/2));
511 	   psii[count]=(-64*x6*sinx+192*x5*cosx+720*x4*sinx-1120*x3*cosx-1500*x2*sinx+972*x[count]*cosx+331*sinx)*exp(-x2)/sqrt(140152*sqrt(PI/2));
512 	}
513 	return;
514 }
515 
cgau6_packet(double * x,int sigInLength,double * f,int sigOutLength,double ys)516 void cgau6_packet(double *x, int sigInLength,
517 			double *f, int sigOutLength, double ys)
518 {
519 	cgau6(x, sigInLength,f, f+sigInLength, sigOutLength,1);
520     return;
521 }
522 
cgau7(double * x,int sigInLength,double * psir,double * psii,int sigOutLength,double ys)523 void cgau7(double *x, int sigInLength,
524 			double *psir, double *psii,
525 			int sigOutLength, double ys)
526 {
527     int count;
528 	double x2,cosx,sinx,x3,x4,x5, x6, x7;
529 
530 	for(count=0;count<sigInLength;count++)
531 	{
532        x2 = x[count] * x[count];
533 	   x3 = x[count] * x2;
534 	   x4 = x2 * x2;
535 	   x5 = x2 * x3;
536 	   x6 = x3 * x3;
537 	   x7 = x4 * x3;
538 	   cosx = cos(x[count]);
539 	   sinx = sin(x[count]);
540 	   psir[count]=(-128*x7*cosx-448*x6*sinx+2016*x5*cosx+3920*x4*sinx-7000*x3*cosx-6804*x2*sinx+4634*x[count]*cosx+1303*sinx)*exp(-x2)/sqrt(2390480*sqrt(PI/2));
541 	   psii[count]=(128*x7*sinx-448*x6*cosx-2016*x5*sinx+3920*x4*cosx+7000*x3*sinx-6804*x2*cosx-4634*x[count]*sinx+1303*cosx)*exp(-x2)/sqrt(2390480*sqrt(PI/2));
542 	}
543 	return;
544 }
545 
cgau7_packet(double * x,int sigInLength,double * f,int sigOutLength,double ys)546 void cgau7_packet(double *x, int sigInLength,
547 			double *f, int sigOutLength, double ys)
548 {
549 	cgau7(x, sigInLength,f, f+sigInLength, sigOutLength,1);
550     return;
551 }
552 
cgau8(double * x,int sigInLength,double * psir,double * psii,int sigOutLength,double ys)553 void cgau8(double *x, int sigInLength,
554 			double *psir, double *psii,
555 			int sigOutLength, double ys)
556 {
557     int count;
558 	double x2,cosx,sinx,x3,x4,x5, x6, x7, x8;
559 
560 	for(count=0;count<sigInLength;count++)
561 	{
562        x2 = x[count] * x[count];
563 	   x3 = x[count] * x2;
564 	   x4 = x2 * x2;
565 	   x5 = x2 * x3;
566 	   x6 = x3 * x3;
567 	   x7 = x4 * x3;
568 	   x8 = x4 * x4;
569 	   cosx = cos(x[count]);
570 	   sinx = sin(x[count]);
571 	   psir[count]=(256*x8*cosx+1024*x7*sinx-5376*x6*cosx-12544*x5*sinx+28000*x4*cosx+36288*x3*sinx-37072*x2*cosx-20848*x[count]*sinx+5937*cosx)*exp(-x2)/sqrt(46206736*sqrt(PI/2));
572 	   psii[count]=(-256*x8*sinx+1024*x7*cosx+5376*x6*sinx-12544*x5*cosx-28000*x4*sinx+36288*x3*cosx+37072*x2*sinx-20848*x[count]*cosx-5937*sinx)*exp(-x2)/sqrt(46206736*sqrt(PI/2));
573 	}
574 	return;
575 }
576 
cgau8_packet(double * x,int sigInLength,double * f,int sigOutLength,double ys)577 void cgau8_packet(double *x, int sigInLength,
578 			double *f, int sigOutLength, double ys)
579 {
580 	cgau8(x, sigInLength,f, f+sigInLength, sigOutLength,1);
581     return;
582 }
583 
584 
585 /*-------------------------------------------
586  * Complex Morlet Scale Filter Generation
587  *-----------------------------------------*/
588 
cmorlet(double * x,int sigInLength,double fb,double fc,double * psir,double * psii,int sigOutLength,double ys)589 void cmorlet(double *x, int sigInLength,
590 			double fb, double fc, double *psir, double *psii,
591 			int sigOutLength, double ys)
592 {
593 	int count;
594 	double con, econ, x2;
595 
596 	con = 1/sqrt(PI*fb);
597 	for(count=0;count<sigInLength;count++)
598 	{
599 		x2=x[count]*x[count];
600 		econ = exp(-x2/fb);
601 		psir[count] = cos(2*PI*fc*x[count])*econ*con/ys;
602 		psii[count] = sin(2*PI*fc*x[count])*econ*con/ys;
603 	}
604 	return;
605 }
606 
cmorlet_packet(double * x,int sigInLength,double * f,int sigOutLength,double ys)607 void cmorlet_packet(double *x, int sigInLength,
608 			double *f, int sigOutLength, double ys)
609 {
610 	int count;
611 	double con, econ, x2;
612 
613 	con = 1/sqrt(PI);
614 	for(count=0;count<sigInLength;count++)
615 	{
616 		x2=x[count]*x[count];
617 		econ = exp(-x2);
618 		f[count] = cos(2*PI*x[count])*econ*con/ys;
619 		f[count+sigInLength] = sin(2*PI*x[count])*econ*con/ys;
620 	}
621 	return;
622 }
623 
624 /*-------------------------------------------
625  * Shannon Scale Filter Generation
626  *-----------------------------------------*/
627 
shanwavf(double * x,int sigInLength,double fb,double fc,double * psir,double * psii,int sigOutLength,double ys)628 void shanwavf(double *x, int sigInLength,
629 			double fb, double fc, double *psir, double *psii,
630 			int sigOutLength, double ys)
631 {
632 	int count;
633 	double con, econ;
634 
635 	con = sqrt(fb);
636 	for(count=0;count<sigInLength;count++)
637 	{
638 		if (x[count] != 0)
639 			econ = sin(x[count]*fb*PI)/(x[count]*fb*PI);
640 		else
641 			econ = 1;
642 		psir[count] = cos(2*PI*fc*x[count])*econ*con/ys;
643 		psii[count] = sin(2*PI*fc*x[count])*econ*con/ys;
644 	}
645 	return;
646 }
647 
shanwavf_packet(double * x,int sigInLength,double * f,int sigOutLength,double ys)648 void shanwavf_packet(double *x, int sigInLength,
649 			double *f, int sigOutLength, double ys)
650 {
651 	int count;
652 	double con, econ;
653 
654 	con = 1;
655 	for(count=0;count<sigInLength;count++)
656 	{
657 		if (x[count] != 0)
658 			econ = sin(x[count]*PI)/(x[count]*PI);
659 		else
660 			econ = 1;
661 		f[count] = cos(2*PI*x[count])*econ*con/ys;
662 		f[count+sigInLength] = sin(2*PI*x[count])*econ*con/ys;
663 	}
664 	return;
665 }
666 
667 /*-------------------------------------------
668  * Frequency B-Spline Scale Filter Generation
669  *-----------------------------------------*/
fbspwavf(double * x,int sigInLength,int m,double fb,double fc,double * psir,double * psii,int sigOutLength,double ys)670 void fbspwavf(double *x, int sigInLength,int m,
671 			double fb, double fc, double *psir, double *psii,
672 			int sigOutLength, double ys)
673 {
674 	int count, i;
675 	double con, econ;
676 
677 	con = sqrt(fb);
678 	for(count=0;count<sigInLength;count++)
679 	{
680 		if (x[count] != 0)
681 			econ = sin(x[count]*fb*PI/(double)m)/(x[count]*fb*PI/(double)m);
682 		else
683 			econ = 1;
684 //         for(i=0;i<m;i++)
685 // 			econ*=econ;
686 
687 		psir[count] = cos(2*PI*fc*x[count])*powof(econ,m)*con/ys;
688 		psii[count] = sin(2*PI*fc*x[count])*powof(econ,m)*con/ys;
689 	}
690 	return;
691 }
692 
fbspwavf_packet(double * x,int sigInLength,double * f,int sigOutLength,double ys)693 void fbspwavf_packet(double *x, int sigInLength,
694 			double *f, int sigOutLength, double ys)
695 {
696 	int count;
697 	double con, econ;
698 
699 	con = 1;
700 	for(count=0;count<sigInLength;count++)
701 	{
702 		if (x[count] != 0)
703 			econ = sin(x[count]*PI)/(x[count]*PI);
704 		else
705 			econ = 1;
706      	f[count] = cos(2*PI*x[count])*econ*con/ys;
707 		f[count+sigInLength] = sin(2*PI*x[count])*econ*con/ys;
708 	}
709 	return;
710 }
711 
712 /*-------------------------------------------
713  * Cauchy Scale Filter Generation
714  *-----------------------------------------*/
715 
cauchy(double * x,int sigInLength,double fb,double fc,double * psir,double * psii,int sigOutLength,double ys)716 void cauchy(double *x, int sigInLength,
717 			double fb, double fc, double *psir, double *psii,
718 			int sigOutLength, double ys)
719 {
720 	int count;
721 	double con;
722 
723 
724 	for(count=0;count<sigInLength;count++)
725 	{
726 		con=2*PI*x[count]*fc/fb;
727 		//econ = exp(-x2/fb);
728 		psir[count] = (1.0-con*con)/((1.0+con*con)*(1.0+con*con)*ys);
729 		psii[count] = -2.0*con/((1.0+con*con)*(1.0+con*con)*ys);
730 	}
731 	return;
732 }
733 
cauchy_neo(double * x,int sigInLength,double * psir,double * psii,int sigOutLength,double ys)734 void cauchy_neo(double *x, int sigInLength, double *psir, double *psii, int sigOutLength, double ys)
735 {
736     int count;
737 	double x2, con, econ;
738 
739 	for (count=0;count<sigInLength;count++)
740 	{
741 		x2 = x[count]*x[count];
742 		con = 1-x2;
743 		econ = (1-x2)*(1-x2)+4*x2;
744 		psir[count]=con/(2*PI*econ*ys);
745 		psii[count]=2*x2/(2*PI*econ*ys);
746 	}
747 	return;
748 }
749 
cauchy_packet(double * x,int sigInLength,double * f,int sigOutLength,double ys)750 void cauchy_packet(double *x, int sigInLength,
751 			double *f, int sigOutLength, double ys)
752 {
753 	//int count;
754 	//double con;
755 
756 	//con = 1/sqrt(PI);
757 	/*for(count=0;count<sigInLength;count++)
758 	{
759 		con=PI*x[count];
760 
761 		f[count] = (1.0-con*con)/((1.0+con*con)*(1.0+con*con)*ys);;
762 		f[count+sigInLength] = -2.0*con/((1.0+con*con)*(1.0+con*con)*ys);
763 	}*/
764     cauchy_neo(x, sigInLength, f, f+sigInLength, sigOutLength, ys);
765 	return;
766 }
767 
768 
769 /*-------------------------------------------
770  * Meyer Filter Generation
771  *-----------------------------------------*/
772 
773 
meyer_phi(double * x,int sigInLength,double lb,double ub,double * phir,double * phii,int sigOutLength,double ys)774 void meyer_phi(double *x, int sigInLength,
775 			double lb, double ub, double *phir, double *phii,
776 			int sigOutLength, double ys)
777 {
778 	int count;
779 	double con,delta,omega,xhat;
780 	double *xhat_r,*xhat_i;
781 	 xhat_r = (double *)malloc(sigInLength*sizeof(double));
782 	 xhat_i = (double *)malloc(sigInLength*sizeof(double));
783 	delta = (ub-lb)/sigInLength;
784 
785 	for(count=0;count<sigInLength;count++)
786 	{
787 		xhat_r[count]=0;
788 		xhat_i[count]=0;
789 		xhat=0;
790 	        if (abs(x[count]) <(2*PI/3))
791 		  xhat=1;
792 		if (abs(x[count]) >=(2*PI/3) && abs(x[count]) <(4*PI/3)){
793 		  meyeraux(3/2/PI*abs(x[count])-1,&con);
794 		  xhat=cos(PI/2*con);
795 		}
796 
797 		//tmp omega
798 		omega=(-sigInLength+2*count)/(2*sigInLength*delta);
799 		//xhat ohne fftshift
800 		xhat_r[count]=xhat*cos(2*PI*omega*lb)/delta;
801 		xhat_i[count]=xhat*sin(2*PI*omega*lb)/delta;
802 
803 	}
804  	fftshift(xhat_r,phir,sigInLength);
805  	fftshift(xhat_i,phii,sigInLength);
806 
807  	ifft (sigInLength, sigInLength, phir, phii);
808 	for(count=0;count<sigInLength;count++)
809 	{
810 	  phir[count]=phir[count]*ys;
811 	  phii[count]=phii[count]*ys;
812 	}
813 	free(xhat_r);
814 	free(xhat_i);
815 	return;
816 }
817 
meyeraux(double x,double * y)818 void meyeraux(double x, double *y)
819 {
820     double x4, x5, x6, x7;
821 
822 
823 	x4 = x*x*x*x;
824 	x5 = x4*x;
825 	x6 = x5*x;
826 	x7 = x6*x;
827 
828 	*y = 35*x4-84*x5+70*x6-20*x7;
829 	return;
830 }
831 
832 
833 /*-------------------------------------------
834  * CWT Utility
835  *-----------------------------------------*/
cwt_fun_parser(char * wname,int * ind)836 void cwt_fun_parser(char *wname, int *ind)
837 {
838     int count;
839 
840 	*ind = -1;
841 	for(count=0;count<cwtIdentityNum;count++)
842 	{
843 		if (strcmp(wname,ci[count].wname) == 0)
844 		{
845 			*ind = count;
846 			break;
847 		}
848 	}
849 	return;
850 }
851 
852 
full_range_scalef(char * wname,double * f,int sigOutLength)853 void full_range_scalef (char *wname, double *f, int sigOutLength)
854 {
855     int level, ind, family, member, count, s1, s2, l;
856 	double one, *lowfltr, *hifltr, *trange;
857 	char d[2]="d";
858     Func syn_fun, ana_fun;
859     swt_wavelet pWaveStruct;
860 
861     level = 10;
862 	one = 1.0;
863 	wavelet_fun_parser (wname, &ind);
864     if ((ind!=-1) && (wi[ind].rOrB==ORTH))
865     {
866       wavelet_parser(wname,&family,&member);
867 	  syn_fun = wi[ind].synthesis;
868       (*syn_fun)(member, &pWaveStruct);
869 	  upcoef_len_cal (1, pWaveStruct.length, level,
870 	       &s1, &s2);
871 	  l=1;
872 	  //l=(int)(floor((sigOutLength-s1)/2));
873 	  for(count=0;count<sigOutLength;count++)
874 		  f[count] = 0;
875 	  upcoef (&one, 1, pWaveStruct.pLowPass,
876 		  pWaveStruct.pHiPass, pWaveStruct.length, &(f[l]),
877 	      s1, s1, d, level);
878 	  if ((family==COIFLETS) || (family==SYMLETS) || (family==DMEY))
879 	  {
880 		  for(count=0;count<sigOutLength;count++)
881 		      f[count] = -1*f[count];
882 	  }
883 	  for(count=0;count<sigOutLength;count++)
884 		      f[count] = f[count]*pow(sqrt(2),level);
885 	  filter_clear();
886 	  return;
887 	 }
888 
889 	if ((ind!=-1) && (wi[ind].rOrB==BIORTH))
890     {
891       wavelet_parser(wname,&family,&member);
892 	  ana_fun = wi[ind].analysis;
893       (*ana_fun)(member, &pWaveStruct);
894 	  upcoef_len_cal (1, pWaveStruct.length, level,
895 	       &s1, &s2);
896 	  //l=(int)(floor((sigOutLength-s1)/2));
897 	  l=1;
898    	  for(count=0;count<sigOutLength;count++)
899 	       f[count]=0;
900 	  lowfltr = malloc(pWaveStruct.length*sizeof(double));
901 	  hifltr = malloc(pWaveStruct.length*sizeof(double));
902       wrev(pWaveStruct.pLowPass, pWaveStruct.length, lowfltr, pWaveStruct.length);
903 	  qmf_wrev(lowfltr,pWaveStruct.length,hifltr,pWaveStruct.length);
904       upcoef (&one, 1, lowfltr, hifltr, pWaveStruct.length, &(f[l]),
905 	      s1, s1, d, level);
906 	  free(lowfltr);
907 	  free(hifltr);
908 	  filter_clear();
909       for(count=0;count<sigOutLength;count++)
910 	  	   f[count] = -1*f[count]*pow(sqrt(2),level);
911 	  return;
912    }
913 
914 	cwt_fun_parser(wname, &ind);
915   if ((ind!=-1) && (ci[ind].realOrComplex==REAL))
916   {
917 	  trange = malloc(sigOutLength*sizeof(double));
918 	  linspace(ci[ind].lb, ci[ind].ub, sigOutLength, trange, sigOutLength);
919 	  (*(ci[ind].scalef))(trange,sigOutLength,f,sigOutLength,ci[ind].cpsi);
920 	  free(trange);
921 	  return;
922   }
923 
924   if ((ind!=-1) && (ci[ind].realOrComplex==COMPLEX))
925   {
926 	  trange = malloc(sigOutLength*sizeof(double)/2);
927 	  linspace(ci[ind].lb, ci[ind].ub, sigOutLength/2, trange, sigOutLength/2);
928 	  (*(ci[ind].scalef))(trange,sigOutLength/2,f,sigOutLength,ci[ind].cpsi);
929 	  free(trange);
930 	  return;
931   }
932 
933 	return;
934 }
935 
cwt_len_cal(int sigInLength,int scale,int * sigOutLength,double * delta)936 void cwt_len_cal (int sigInLength, int scale, int *sigOutLength, double *delta)
937 {
938     *sigOutLength = scale;
939 	*delta = (double)(sigInLength-1)/(double)(scale-1);
940 	return;
941 }
942 
scale_real(double * f,int sigInLength,double delta,double * fout,int sigOutLength)943 void scale_real (double *f, int sigInLength, double delta, double *fout, int sigOutLength)
944 {
945     int count;
946 	for(count=0;count<sigOutLength;count++)
947 	{
948 		fout[count] = f[(int)(floor(count*delta))];
949 		//sciprint("%d\n",count*delta+1);
950 	}
951 	return;
952 }
953 
954 /*void scale_complex (double *f, int sigInLength, int delta, double *fout, int sigOutLength)
955 {
956 
957 	return;
958 }*/
959 
cwt_conv_real(double * sigIn,int sigInLength,double * f,int filterLen,double * sigOut,int sigOutLength)960 void cwt_conv_real (double *sigIn, int sigInLength, double *f, int filterLen, double *sigOut, int sigOutLength)
961 {
962 	int len;
963     double *fTemp, *buf;
964 
965 	len = sigInLength+filterLen-1;
966 	buf = malloc(len*sizeof(double));
967     fTemp = malloc(filterLen*sizeof(double));
968 
969 	wrev(f, filterLen, fTemp, filterLen);
970 	conv(sigIn,sigInLength,buf,len,fTemp,filterLen);
971 	free(fTemp);
972     //for (i=1;i<len;i++)
973 	//	buf[i]=buf[i]-buf[i-1];
974 	wkeep_1D_center(buf,len,sigOut,sigOutLength);
975 	free(buf);
976 	return;
977 }
978 
cwt_iconv_real(double * sigIn,int sigInLength,double * f,int filterLen,double * sigOut,int sigOutLength)979 void cwt_iconv_real (double *sigIn, int sigInLength, double *f, int filterLen, double *sigOut, int sigOutLength)
980 {
981 	int len;
982     double *fTemp, *buf;
983 
984 	len = sigInLength+filterLen-1;
985 	buf = malloc(len*sizeof(double));
986     fTemp = malloc(filterLen*sizeof(double));
987 
988 	wrev(f, filterLen, fTemp, filterLen);
989 	//iconv(sigIn,sigInLength,buf,len,fTemp,filterLen);
990 	i_conv(sigIn,sigInLength,buf,len,fTemp,filterLen);
991 	free(fTemp);
992 	wkeep_1D_center(buf,len,sigOut,sigOutLength);
993 	free(buf);
994 	return;
995 }
996 
cwt_conv_complex(double * sigIn,int sigInLength,double * fr,double * fi,int filterLen,double * sigOutR,double * sigOutI,int sigOutLength)997 void cwt_conv_complex (double *sigIn, int sigInLength, double *fr, double *fi, int filterLen,
998 					   double *sigOutR, double *sigOutI, int sigOutLength)
999 {
1000     cwt_conv_real(sigIn,sigInLength,fr,filterLen,sigOutR,sigOutLength);
1001 	cwt_conv_real(sigIn,sigInLength,fi,filterLen,sigOutI,sigOutLength);
1002 	return;
1003 }
1004 
cwt_conv_complex_complex(double * a,double * b,int sigInLength,double * c,double * d,int filterLen,double * sigOutR,double * sigOutI,int sigOutLength)1005 void cwt_conv_complex_complex (double *a, double *b, int sigInLength,double *c, double *d,
1006 							   int filterLen, double *sigOutR, double *sigOutI, int sigOutLength)
1007 {
1008 	int count;
1009     double *ac, *bd, *bc, *ad;
1010 	ac = malloc(sigOutLength*sizeof(double));
1011 	bd = malloc(sigOutLength*sizeof(double));
1012 	bc = malloc(sigOutLength*sizeof(double));
1013 	ad = malloc(sigOutLength*sizeof(double));
1014 
1015 	cwt_conv_real(a,sigInLength,c,filterLen,ac,sigOutLength);
1016 	cwt_conv_real(b,sigInLength,d,filterLen,bd,sigOutLength);
1017 	cwt_conv_real(b,sigInLength,c,filterLen,bc,sigOutLength);
1018 	cwt_conv_real(a,sigInLength,d,filterLen,ad,sigOutLength);
1019 
1020     for(count=0;count<sigOutLength;count++)
1021 	{
1022 		sigOutR[count]=ac[count]-bd[count];
1023 		sigOutI[count]=bc[count]+ad[count];
1024 	}
1025 	free(ac);
1026 	free(bd);
1027 	free(bc);
1028 	free(ad);
1029 
1030 	return;
1031 }
1032 
1033 
1034 // void
1035 // cwt_upcoef_len_cal (int sigInLength, int filterLen, int stride,
1036 // 		int *sigOutLength, int *sigOutLengthDefault)
1037 // {
1038 //   int count;
1039 //   *sigOutLength = sigInLength;
1040 //   *sigOutLengthDefault = sigInLength;
1041 //       for(count=0;count<stride;count++)
1042 //       {
1043 // 	// original version
1044 // 	*sigOutLengthDefault = 2*(*sigOutLengthDefault) + filterLen - 1;
1045 // 	*sigOutLength = 2*(*sigOutLength) + filterLen - 2;
1046 //
1047 //       }
1048 //   return;
1049 // }
1050 
1051 // void
1052 // cwt_upcoef (double *sigIn, int sigInLength, double *lowRe,double *hiRe,
1053 // 	int filterLen, double *sigOut, int sigOutLength,
1054 // 	int defaultLength, char *coefType, int step)
1055 // {
1056 //   int count, sigInLengthTemp, leng;
1057 //   double *sigInTemp, *sigOutTemp;
1058 //
1059 //   // works with wavefun, cwt
1060 //   sigInLengthTemp = 2 * sigInLength + filterLen - 2;
1061 //
1062 //
1063 //
1064 //   //sigInLengthTemp = 2 * sigInLength + filterLen - 1;
1065 //   sigInTemp = (double *) malloc(defaultLength*sizeof(double));
1066 //
1067 //   if (strcmp(coefType,"a")==0)
1068 //   {
1069 // 	  //sciprint("recognized\n");
1070 // // 	  printf("sigInLength %d, filterLen%d, sigInLengthTemp %d\n",sigInLength,filterLen,sigInLengthTemp);
1071 // 	  idwt_approx_neo (sigIn, sigInLength, lowRe, filterLen,
1072 // 		 sigInTemp, sigInLengthTemp);
1073 // // 	  sciprint("recognized\n");
1074 //   }
1075 //   else
1076 //     idwt_detail_neo (sigIn, sigInLength, hiRe, filterLen,
1077 // 		 sigInTemp, sigInLengthTemp);
1078 //
1079 //   if (step > 1)
1080 //     {
1081 //       sigOutTemp = (double *) malloc(defaultLength*sizeof(double));
1082 //       for(count=0;count<defaultLength;count++)
1083 // 	sigOutTemp[count] = 0;
1084 //       leng = sigInLengthTemp;
1085 //       for(count=0;count<(step-1);count++) //for cwt
1086 // 	{
1087 // 	  //printf("leng %d, filterLen%d, leng*2-filterLen+2 %d\n",leng,filterLen,leng*2-filterLen+2);
1088 // 	  // original version
1089 // 	  idwt_approx_neo (sigInTemp, leng, lowRe, filterLen,
1090 // 	               sigOutTemp, leng*2+filterLen-2);
1091 // 	  leng = leng*2+filterLen-2;
1092 //
1093 // 	  verbatim_copy (sigOutTemp, leng, sigInTemp, leng);
1094 // 	}
1095 //       sigInLengthTemp = leng;
1096 //       free(sigOutTemp);
1097 //     }
1098 //
1099 //
1100 //   wkeep_1D_center (sigInTemp, sigInLengthTemp, sigOut, sigOutLength);
1101 //   free(sigInTemp);
1102 //   return;
1103 // }
1104 
1105 
1106 /*====================================================================*
1107  * Name of the function : fftshift                                    *
1108  * Date of creation     : 02 - 06 - 1999                              *
1109  *--------------------------------------------------------------------*
1110  * Action of the function                                             *
1111  * swaps the first and second halves of a vector. Example             *
1112  * [1 2 3 4 5 6 7 8 9 10 ] becomes [6 7 8 9 10 1 2 3 4 5]             *
1113  * The parameters to pass are :                          	      *
1114  *   - the input vector		                            	      *
1115  *   - the output vector	                            	      *
1116  *   - its length                                                     *
1117  * if the length is odd, example [1 2 3 4 5] becomes [4 5 1 2 3]      *
1118  *====================================================================*/
1119 int
fftshift(double * vector_in,double * vector_out,int vector_length)1120 fftshift (double *vector_in, double *vector_out, int vector_length)
1121 
1122 {
1123   double inter1, inter2;
1124   int i, half_length;
1125 
1126 
1127   /* computation of the half length in case of odd or even length */
1128   half_length = (int) (vector_length/2.0);
1129 
1130 
1131   /* case where the length is odd */
1132   if (ISODD(vector_length)==1)
1133     {
1134       inter2=vector_in[half_length];
1135       for (i=0; i<half_length; i++)
1136 	{
1137 	  inter1 = vector_in[i];
1138 	  vector_out[i] = vector_in[half_length+i+1];
1139 	  vector_out[half_length + i ] = inter1;
1140 	}
1141       vector_out[vector_length-1]=inter2;
1142     }
1143   /* case where the length is even */
1144   else
1145     {
1146       for (i=0; i<half_length; i++)
1147 	{
1148 	  inter1 = vector_in[half_length + i ];
1149 	  vector_out[half_length + i] = vector_in[i];
1150 	  vector_out[i] = inter1;
1151 	}
1152     }
1153   /* fftshifting of the vector */
1154 
1155 
1156 return 0;
1157 }
1158 
1159 
1160 
1161 
1162 
1163 int
ifft(int Signal_Length,int Nfft,double * sig_real,double * sig_imag)1164 ifft (int Signal_Length, int Nfft, double *sig_real, double *sig_imag)
1165 {
1166       kiss_fft_cpx * buf;
1167     kiss_fft_cpx * bufout;
1168   kiss_fft_cfg cfg = kiss_fft_alloc(Signal_Length , 1, 0, 0);
1169 //      fftw_complex * in=NULL;
1170 //     fftw_complex * out=NULL;
1171 //     fftw_plan p;
1172 
1173         int            i, j, k, n, n2;
1174       double         c, s, e, a, t1, t2;
1175   /*------------------------------------------------------------------*/
1176   /*          when the signal length is a power of two                */
1177   /*------------------------------------------------------------------*/
1178   if (Signal_Length == (int) powof (2, Nfft) + 1)
1179     {
1180 
1181 
1182       j = 0;			/* bit-reverse  */
1183       n2 = Signal_Length / 2;
1184       for (i = 1; i < Signal_Length - 1; i++)
1185 	{
1186 	  n = n2;
1187 	  while (j >= n)
1188 	    {
1189 	      j = j - n;
1190 	      n = n / 2;
1191 	    }
1192 	  j = j + n;
1193 
1194 	  if (i < j)
1195 	    {
1196 	      t1 = sig_real[i];
1197 	      sig_real[i] = sig_real[j];
1198 	      sig_real[j] = t1;
1199 	      t1 = sig_imag[i];
1200 	      sig_imag[i] = sig_imag[j];
1201 	      sig_imag[j] = t1;
1202 	    }
1203 	}
1204 
1205 
1206       n = 0;			/*IFFT  */
1207       n2 = 1;
1208 
1209       for (i = 0; i < Nfft; i++)
1210 	{
1211 	  n = n2;
1212 	  n2 = n2 + n2;
1213 	  e = 6.283185307179586 / n2;
1214 	  a = 0.0;
1215 
1216 	  for (j = 0; j < n; j++)
1217 	    {
1218 	      c = cos (a);
1219 	      s = sin (a);
1220 	      a = a + e;
1221 
1222 	      for (k = j; k < Signal_Length; k = k + n2)
1223 		{
1224 		  t1 = c * sig_real[k + n] - s * sig_imag[k + n];
1225 		  t2 = s * sig_real[k + n] + c * sig_imag[k + n];
1226 		  sig_real[k + n] = sig_real[k] - t1;
1227 		  sig_imag[k + n] = sig_imag[k] - t2;
1228 		  sig_real[k] = sig_real[k] + t1;
1229 		  sig_imag[k] = sig_imag[k] + t2;
1230 		}
1231 	    }
1232 	}
1233       /* divide by Signal_Length */
1234       for (k = 0; k < Signal_Length; k++)
1235 	{
1236 	  sig_real[k] = sig_real[k] / Signal_Length;
1237 	  sig_imag[k] = sig_imag[k] / Signal_Length;
1238 	}
1239   	  free(cfg);
1240     }
1241   /*------------------------------------------------------------------*/
1242   /*        when the signal length is NOT a power of two              */
1243   /*            Calls the matlab subroutine ifft                      */
1244   /*------------------------------------------------------------------*/
1245   else
1246     {
1247 //       cfg = kiss_fft_alloc(Signal_Length , 1, 0, 0);
1248 //       mxArray       *outputArray[1];
1249 //       mxArray       *inputArray[1];
1250 //       mxArray       *array_ptr;
1251 
1252 
1253 //       num_out = 1;
1254 //       num_in = 1;
1255 
1256       /* recopy the real and imag parts of the signal in matrices */
1257 //       array_ptr = mxCreateDoubleMatrix (1, Signal_Length, mxCOMPLEX);
1258 //       memcpy (mxGetPr (array_ptr), sig_real, Signal_Length * sizeof (double));
1259 //       memcpy (mxGetPi (array_ptr), sig_imag, Signal_Length * sizeof (double));
1260 //       inputArray[0] = array_ptr;
1261        buf	= (kiss_fft_cpx*)KISS_FFT_MALLOC(Signal_Length * sizeof(kiss_fft_cpx));
1262        bufout	= (kiss_fft_cpx*)KISS_FFT_MALLOC(Signal_Length * sizeof(kiss_fft_cpx));
1263       for (i=0;i<Signal_Length;i++){
1264 	buf[i].r=sig_real[i];
1265 	buf[i].i=sig_imag[i];
1266       }
1267 //       printf("sig in %f\n",buf[0].r);
1268   /*in=fftw_malloc(sizeof(fftw_complex) * Signal_Length);
1269     out=fftw_malloc(sizeof(fftw_complex) * Signal_Length);
1270     for (i=0;i<Signal_Length;++i ) {
1271         in[i][0] = sig_real[i];
1272         in[i][1] = sig_imag[i];
1273     }
1274       p = fftw_plan_dft_1d(Signal_Length, in, out, FFTW_BACKWARD, FFTW_ESTIMATE);*/
1275 //        for (i=0;i<Nfft;i++)
1276 //          fftw_execute(p);
1277 //
1278 /*  sig_copy_real= (double*)malloc(sizeof(double) * 1*Signal_Length);
1279   sig_copy_imag= (double*)malloc(sizeof(double) * 1*Signal_Length);
1280 
1281        memcpy (sig_copy_real, sig_real, Signal_Length * sizeof (double));
1282       memcpy ( sig_copy_imag, sig_imag, Signal_Length * sizeof (double));*/
1283 
1284 //        for (i=0;i<Signal_Length;i++){
1285 // 	sig_real[i]=(double)out[i][0];
1286 // 	sig_imag[i]=(double)out[i][1];
1287 //       }
1288       /* calls the MATLAB function */
1289       //mexCallMATLAB (num_out, outputArray, num_in, inputArray, "ifft");
1290 //        for (j = 0; j < Nfft; j++)
1291        kiss_fft(cfg, buf, bufout);
1292 //
1293       for (i=0;i<Signal_Length;i++){
1294 	sig_real[i]=(double)bufout[i].r;
1295 	sig_imag[i]=(double)bufout[i].i;
1296       }
1297 //        printf("sig out %f\n",bufout[0].r);
1298       /* recovers the output */
1299 
1300 //       if (mxIsComplex (outputArray[0]))
1301 // 	{
1302 // 	   for (i=0;i<Signal_Length;i++){
1303 // 	sig_copy_real[i]=(double)buf[i].r;
1304 // 	sig_copy_imag[i]=(double)buf[i].i;
1305 //       }
1306 //  	  memcpy (sig_imag, sig_copy_real, Signal_Length * sizeof (double));
1307 //       memcpy (sig_real, mxGetPr (outputArray[0]), Signal_Length * sizeof (double));
1308 //       if (mxIsComplex (outputArray[0]))
1309 // 	{
1310 //    memcpy (sig_imag, sig_copy_imag, Signal_Length * sizeof (double));
1311 // 	  memcpy (sig_imag, mxGetPi (outputArray[0]), Signal_Length * sizeof (double));
1312 // 	}
1313 //       else
1314 // 	{
1315 // 	  for (i = 0; i < Signal_Length; i++)
1316 // 	    sig_imag[i] = 0;
1317 // 	}
1318 
1319       /* free memory */
1320        free(cfg);free(buf);free(bufout);
1321 //         free(sig_copy_real);free(sig_copy_imag);
1322 //          fftw_destroy_plan(p);
1323 //           fftw_free(in); fftw_free(out);
1324 
1325       //mxDestroyArray (outputArray[0]);
1326       //mxDestroyArray (inputArray[0]);
1327     }
1328   return 0;
1329 }
1330 
1331 
1332 /*void real_scale (double lb, double ub, double scale, int length, double *f, double ys, RWScaleFunc w)
1333 {
1334     int ns, count;
1335 	double *x, *ft;
1336 
1337     ns = (int)(floor(ub*scale)-ceil(lb*scale));
1338 	x = malloc(ns*sizeof(double));
1339 	ft = malloc(ns*sizeof(double));
1340     for(count=0;count<ns;count++)
1341 		x[count]=ceil(lb*scale)+count;
1342 	(*w)(x, ns, ft, ns, ys);
1343 	free(x);
1344     wextend_1D_center (ft, ns, f, length, ZPD);
1345 	free(ft);
1346 	return;
1347 }*/
1348 
1349 
1350 //void complex_scale(int lb, int ub, double scale, int length, double *fr, double *fi, double ys, WScaleFunc w)
1351 //{
1352 //	int ns, count;
1353 //	double *x, *ftr, *fti;
1354 //
1355   //  ns = (int)(floor(ub*scale)-ceil(lb*scale));
1356 //	x = malloc(ns*sizeof(double));
1357 //	ftr = malloc(ns*sizeof(double));
1358 //	fti = malloc(ns*sizeof(double));
1359 //    for(count=0;count<ns;count++)
1360 //		x[count]=ceil(lb*scale)+count;
1361 //	(*w)(x, ns, ft, ns, ys);
1362 //	free(x);
1363 //    wextend_1D_center (ft, ns, fr, length, ZPD);
1364 //	free(ft);
1365 //	return;
1366 //}
1367 
1368 
1369 //void cwt (double *sigIn, int sigInLength,
1370 //		  double *matrixOut, int matrixOutRow,
1371 //		  int matrixCol, char *wname, int *scale)
1372 //{
1373 //	int count;
1374 //    double *scaleFunc;
1375 
1376 //	cwt_fun_parser(wname,&ind);
1377 
1378 //	scaleFunc = malloc(sigInLength*sizeof(double));
1379 //	if (ci[count].realOrComplex == 0)
1380 //	{
1381 //	    for(count=0:count<matrixOutRow;count++)
1382 //	    {
1383 //		    ci[count].scalef(ci[ind].lb, ci[ind],ub, scale[count], sigInLength, scaleFunc,sqrt(scale[count]));
1384 //		    iconv(sigIn,sigInLength,matrixOut+count*sigInLength,sigInLength,scaleFunc,sigInLength);
1385 //      }
1386 //	}
1387 //	else
1388 //	{
1389 //
1390 //	}
1391 //    free(scaleFunc);
1392 //
1393 //	return;
1394 //}
1395