1 /*
2  * -------------------------------------------------------------------------
3  * utility.c -- utility function
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 
24 #include "swtlib.h"
25 
26 /*-------------------------------------------
27  * Matrix Transposition Operation
28  *-----------------------------------------*/
29 
30 void
matrix_tran(double * matrixIn,int matrixInRow,int matrixInCol,double * matrixOut,int matrixOutRow,int matrixOutCol)31 matrix_tran (double *matrixIn, int matrixInRow, int matrixInCol,
32 	     double *matrixOut, int matrixOutRow, int matrixOutCol)
33 {
34   int row, col;
35 
36   for (col = 0; col < matrixInCol; col++)
37     {
38       for (row = 0; row < matrixInRow; row++)
39 	{
40 	  matrixOut[row + col * matrixInRow] =
41 	    matrixIn[col + row * matrixInCol];
42 	}
43     }
44   return;
45 }
46 
47 /*-------------------------------------------
48  * Flipping Operation
49  *-----------------------------------------*/
50 
51 void
wrev(const double * sigIn,int sigInLength,double * sigOut,int sigOutLength)52 wrev (const double *sigIn, int sigInLength,
53       double *sigOut, int sigOutLength)
54 {
55   int count = 0;
56   for (count = 0; count < sigInLength; count++)
57     sigOut[count] = sigIn[sigInLength - count - 1];
58   return;
59 }
60 
61 /*-------------------------------------------
62  * Quadrature Mirror Filtering Operation
63  *-----------------------------------------*/
64 
65 void
qmf_odd(double * sigIn,int sigInLength,double * sigOut,int sigOutLength)66 qmf_odd (double *sigIn, int sigInLength,
67      double *sigOut, int sigOutLength)
68 {
69   int count = 0;
70   for (count = 0; count < sigInLength; count++)
71     {
72       sigOut[count] = sigIn[sigInLength - count - 1];
73       if (count % 2 == 0)
74 	  {
75 	    sigOut[count] = -1 * sigOut[count];
76 	  }
77     }
78     return;
79 }
80 
81 void
qmf_even(const double * sigIn,int sigInLength,double * sigOut,int sigOutLength)82 qmf_even (const double *sigIn, int sigInLength,
83      double *sigOut, int sigOutLength)
84 {
85   int count = 0;
86   for (count = 0; count < sigInLength; count++)
87     {
88       sigOut[count] = sigIn[sigInLength - count - 1];
89       if (count % 2 != 0)
90 	  {
91 	    sigOut[count] = -1 * sigOut[count];
92 	  }
93     }
94   return;
95 }
96 
97 /*-------------------------------------------
98  * Flipping and QMF at the same time
99  *-----------------------------------------*/
100 void
qmf_wrev(const double * sigIn,int sigInLength,double * sigOut,int sigOutLength)101 qmf_wrev (const double *sigIn, int sigInLength,
102 	  double *sigOut, int sigOutLength)
103 {
104   int count = 0;
105   double *sigOutTemp;
106   sigOutTemp = malloc(sigInLength*sizeof(double));
107   for (count = 0; count < sigInLength; count++)
108     {
109       sigOutTemp[count] = sigIn[sigInLength - count - 1];
110       if (count % 2 != 0)
111 	{
112 	  sigOutTemp[count] = -1 * sigOutTemp[count];
113 	}
114     }
115 
116   for (count = 0; count < sigInLength; count++)
117     sigOut[count] = sigOutTemp[sigInLength - count - 1];
118   free(sigOutTemp);
119   return;
120 }
121 
122 /*-------------------------------------------
123  * Verbatim Copying
124  *-----------------------------------------*/
125 
126 void
verbatim_copy(const double * sigIn,int sigInLength,double * sigOut,int sigOutLength)127 verbatim_copy (const double *sigIn, int sigInLength,
128 	       double *sigOut, int sigOutLength)
129 {
130   int count = 0;
131   for (count = 0; count < sigInLength; count++)
132     sigOut[count] = sigIn[count];
133 }
134 
135 /*-------------------------------------------
136  * Dyaddown
137  *-----------------------------------------*/
138 
139 void
dyaddown_1D_keep_odd(double * sigIn,int sigInLength,double * sigOut,int sigOutLength)140 dyaddown_1D_keep_odd (double *sigIn, int sigInLength,
141 		   double *sigOut, int sigOutLength)
142 {
143   int count = 0;
144   for (count = 0; count < sigOutLength; count++)
145     sigOut[count] = sigIn[count * 2];
146   return;
147 }
148 
149 void
dyaddown_1D_keep_even(double * sigIn,int sigInLength,double * sigOut,int sigOutLength)150 dyaddown_1D_keep_even (double *sigIn, int sigInLength,
151 		    double *sigOut, int sigOutLength)
152 {
153   int count = 0;
154   for (count = 0; count < sigOutLength; count++)
155     sigOut[count] = sigIn[count * 2 + 1];
156   return;
157 }
158 
159 void
dyaddown_2D_keep_odd_row(double * matrixIn,int matrixInRow,int matrixInCol,double * matrixOut,int matrixOutRow,int matrixOutCol)160 dyaddown_2D_keep_odd_row (double *matrixIn, int matrixInRow,
161 			  int matrixInCol, double *matrixOut,
162 			  int matrixOutRow, int matrixOutCol)
163 {
164   int row, col;
165   double *matrixInTemp, *matrixOutPre;
166 
167   matrixInTemp = malloc (matrixInRow * matrixInCol * sizeof (double));
168   matrix_tran (matrixIn, matrixInCol, matrixInRow,
169 	       matrixInTemp, matrixOutCol, matrixInRow);
170 
171   matrixOutPre = malloc (matrixOutRow * matrixInCol * sizeof (double));
172   for(row=0;row<matrixOutRow;row++)
173     {
174       for(col=0;col<matrixInCol;col++)
175 	matrixOutPre[col+row*matrixInCol]=
176 	  matrixInTemp[col+row*matrixInCol*2];
177     }
178   free (matrixInTemp);
179   matrix_tran (matrixOutPre, matrixOutRow, matrixInCol,
180 	       matrixOut, matrixInRow, matrixOutCol);
181   free (matrixOutPre);
182   return;
183 }
184 
185 void
dyaddown_2D_keep_odd_col(double * matrixIn,int matrixInRow,int matrixInCol,double * matrixOut,int matrixOutRow,int matrixOutCol)186 dyaddown_2D_keep_odd_col (double *matrixIn, int matrixInRow,
187 			  int matrixInCol, double *matrixOut,
188 			  int matrixOutRow, int matrixOutCol)
189 {
190   int row, col;
191   for(col=0;col<matrixOutCol;col++)
192     {
193       for(row=0;row<matrixInRow;row++)
194 	matrixOut[row+col*matrixInRow]=
195 	  matrixIn[row+col*matrixInRow*2];
196     }
197   return;
198 }
199 
200 void
dyaddown_2D_keep_even_row(double * matrixIn,int matrixInRow,int matrixInCol,double * matrixOut,int matrixOutRow,int matrixOutCol)201 dyaddown_2D_keep_even_row (double *matrixIn, int matrixInRow,
202 			  int matrixInCol, double *matrixOut,
203 			  int matrixOutRow, int matrixOutCol)
204 {
205   int row, col;
206   double *matrixInTemp, *matrixOutPre;
207 
208   matrixInTemp = malloc (matrixInRow * matrixInCol * sizeof (double));
209   matrix_tran (matrixIn, matrixInCol, matrixInRow,
210 	       matrixInTemp, matrixOutCol, matrixInRow);
211 
212   matrixOutPre = malloc (matrixOutRow * matrixInCol * sizeof (double));
213   for(row=0;row<matrixOutRow;row++)
214     {
215       for(col=0;col<matrixInCol;col++)
216 	matrixOutPre[col+row*matrixInCol]=
217 	  matrixInTemp[col+(2*row+1)*matrixInCol];
218     }
219   free (matrixInTemp);
220   matrix_tran (matrixOutPre, matrixOutRow, matrixInCol,
221 	       matrixOut, matrixInRow, matrixOutCol);
222   free (matrixOutPre);
223   return;
224 }
225 
226 void
dyaddown_2D_keep_even_col(double * matrixIn,int matrixInRow,int matrixInCol,double * matrixOut,int matrixOutRow,int matrixOutCol)227 dyaddown_2D_keep_even_col (double *matrixIn, int matrixInRow,
228 			  int matrixInCol, double *matrixOut,
229 			  int matrixOutRow, int matrixOutCol)
230 {
231   int row, col;
232   for(col=0;col<matrixOutCol;col++)
233     {
234       for(row=0;row<matrixInRow;row++)
235 	matrixOut[row+col*matrixInRow]=
236 	  matrixIn[row+(2*col+1)*matrixInRow];
237     }
238   return;
239 }
240 
241 
242 void
dyaddown_2D_keep_odd(double * matrixIn,int matrixInRow,int matrixInCol,double * matrixOut,int matrixOutRow,int matrixOutCol)243 dyaddown_2D_keep_odd (double *matrixIn, int matrixInRow,
244 		      int matrixInCol, double *matrixOut,
245 		      int matrixOutRow, int matrixOutCol)
246 {
247   int row, col;
248   double *matrixInTemp, *matrixOutPre, *matrixOutTemp;
249 
250   matrixInTemp = malloc (matrixInRow * matrixInCol * sizeof (double));
251   matrix_tran (matrixIn, matrixInCol, matrixInRow,
252 	       matrixInTemp, matrixOutCol, matrixInRow);
253 
254   matrixOutPre = malloc (matrixOutRow * matrixInCol * sizeof (double));
255   for(row=0;row<matrixOutRow;row++)
256     {
257       for(col=0;col<matrixInCol;col++)
258 	matrixOutPre[col+row*matrixInCol]=
259 	  matrixInTemp[col+2*row*matrixInCol];
260     }
261   free (matrixInTemp);
262 
263   matrixOutTemp = malloc (matrixOutRow * matrixInCol * sizeof (double));
264   matrix_tran (matrixOutPre, matrixOutRow, matrixInCol,
265 	       matrixOutTemp, matrixInRow, matrixOutCol);
266   free (matrixOutPre);
267   for(col=0;col<matrixOutCol;col++)
268     {
269       for(row=0;row<matrixOutRow;row++)
270 	matrixOut[row+col*matrixOutRow]=
271 	  matrixOutTemp[row+2*col*matrixOutRow];
272     }
273   free(matrixOutTemp);
274   return;
275 }
276 
277 void
dyaddown_2D_keep_even(double * matrixIn,int matrixInRow,int matrixInCol,double * matrixOut,int matrixOutRow,int matrixOutCol)278 dyaddown_2D_keep_even (double *matrixIn, int matrixInRow,
279 		       int matrixInCol, double *matrixOut,
280 		       int matrixOutRow, int matrixOutCol)
281 {
282   int row, col;
283   double *matrixInTemp, *matrixOutPre, *matrixOutTemp;
284 
285   matrixInTemp = malloc (matrixInRow * matrixInCol * sizeof (double));
286   matrix_tran (matrixIn, matrixInCol, matrixInRow,
287 	       matrixInTemp, matrixOutCol, matrixInRow);
288 
289   matrixOutPre = malloc (matrixOutRow * matrixInCol * sizeof (double));
290   for(row=0;row<matrixOutRow;row++)
291     {
292       for(col=0;col<matrixInCol;col++)
293 	matrixOutPre[col+row*matrixInCol]=
294 	  matrixInTemp[col+(2*row+1)*matrixInCol];
295     }
296   free (matrixInTemp);
297 
298   matrixOutTemp = malloc (matrixOutRow * matrixInCol * sizeof (double));
299   matrix_tran (matrixOutPre, matrixOutRow, matrixInCol,
300 	       matrixOutTemp, matrixInRow, matrixOutCol);
301   free (matrixOutPre);
302   for(col=0;col<matrixOutCol;col++)
303     {
304       for(row=0;row<matrixOutRow;row++)
305 	matrixOut[row+col*matrixOutRow]=
306 	  matrixOutTemp[row+(2*col+1)*matrixOutRow];
307     }
308   free(matrixOutTemp);
309   return;
310 }
311 
312 /*-------------------------------------------
313  * Dyadup
314  *-----------------------------------------*/
315 
316 void
dyadup_1D_feed_odd(double * sigIn,int sigInLength,double * sigOut,int sigOutLength)317 dyadup_1D_feed_odd (double *sigIn, int sigInLength,
318 		 double *sigOut, int sigOutLength)
319 {
320   int count = 0;
321   for (count = 0; count < sigInLength - 1; count++)
322     {
323       sigOut[count * 2 + 1] = 0;
324       sigOut[count * 2] = sigIn[count];
325     }
326   sigOut[sigOutLength-1] = sigIn[sigInLength-1];
327   return;
328 }
329 
330 void
dyadup_1D_feed_even(double * sigIn,int sigInLength,double * sigOut,int sigOutLength)331 dyadup_1D_feed_even (double *sigIn, int sigInLength,
332 		  double *sigOut, int sigOutLength)
333 {
334   int count = 0;
335   for (count = 0; count < sigInLength; count++)
336     {
337       //printf("count %d\n",count);
338       sigOut[count * 2] = 0;
339       sigOut[count * 2 + 1] = sigIn[count];
340     }
341   sigOut[sigOutLength-1] = 0;
342   return;
343 }
344 
345 void
dyadup_2D_feed_odd_row(double * matrixIn,int matrixInRow,int matrixInCol,double * matrixOut,int matrixOutRow,int matrixOutCol)346 dyadup_2D_feed_odd_row (double *matrixIn, int matrixInRow,
347 			int matrixInCol, double *matrixOut,
348 			int matrixOutRow, int matrixOutCol)
349 {
350   int row, col;
351   double *matrixInTemp, *matrixOutPre;
352 
353   matrixInTemp = malloc (matrixInRow * matrixInCol * sizeof (double));
354   matrix_tran (matrixIn, matrixInCol, matrixInRow,
355 	       matrixInTemp, matrixOutCol, matrixInRow);
356 
357   matrixOutPre = malloc (matrixOutRow * matrixInCol * sizeof (double));
358   for(row=0;row<matrixInRow-1;row++)
359     {
360       for(col=0;col<matrixInCol;col++)
361 	{
362 	  matrixOutPre[col+2*row*matrixInCol]=
363 	    matrixInTemp[col+row*matrixInCol];
364 	  matrixOutPre[col+(2*row+1)*matrixInCol] = 0;
365 	}
366     }
367   for(col=0;col<matrixInCol;col++)
368     matrixOutPre[col+(matrixOutRow-1)*matrixInCol] =
369       matrixInTemp[col+(matrixInRow-1)*matrixInCol];
370   free (matrixInTemp);
371   matrix_tran (matrixOutPre, matrixOutRow, matrixInCol,
372 	       matrixOut, matrixInRow, matrixOutCol);
373   free (matrixOutPre);
374   return;
375 }
376 
377 void
dyadup_2D_feed_odd_col(double * matrixIn,int matrixInRow,int matrixInCol,double * matrixOut,int matrixOutRow,int matrixOutCol)378 dyadup_2D_feed_odd_col (double *matrixIn, int matrixInRow,
379 			int matrixInCol, double *matrixOut,
380 			int matrixOutRow, int matrixOutCol)
381 {
382   int row, col;
383   for(col=0;col<matrixInCol-1;col++)
384     {
385       for(row=0;row<matrixInRow;row++)
386 	{
387 	  matrixOut[row+2*col*matrixInRow]=
388 	    matrixIn[row+col*matrixInRow];
389 	  matrixOut[row+(2*col+1)*matrixInRow] = 0;
390 	}
391     }
392   for(row=0;row<matrixInRow;row++)
393     matrixOut[row+(matrixOutCol-1)*matrixInRow] =
394       matrixIn[row+(matrixInCol-1)*matrixInRow];
395   return;
396 }
397 
398 void
dyadup_2D_feed_even_row(double * matrixIn,int matrixInRow,int matrixInCol,double * matrixOut,int matrixOutRow,int matrixOutCol)399 dyadup_2D_feed_even_row (double *matrixIn, int matrixInRow,
400 			int matrixInCol, double *matrixOut,
401 			int matrixOutRow, int matrixOutCol)
402 {
403   int row, col;
404   double *matrixInTemp, *matrixOutPre;
405 
406   matrixInTemp = malloc (matrixInRow * matrixInCol * sizeof (double));
407   matrix_tran (matrixIn, matrixInCol, matrixInRow,
408 	       matrixInTemp, matrixOutCol, matrixInRow);
409 
410   matrixOutPre = malloc (matrixOutRow * matrixInCol * sizeof (double));
411   for(row=0;row<matrixInRow;row++)
412     {
413       for(col=0;col<matrixInCol;col++)
414 	{
415 	  matrixOutPre[col+2*row*matrixInCol]= 0;
416 	  matrixOutPre[col+(2*row+1)*matrixInCol]
417 	    = matrixInTemp[col+row*matrixInCol];
418 	}
419     }
420   for(col=0;col<matrixInCol;col++)
421     matrixOutPre[col+(matrixOutRow-1)*matrixInCol] = 0;
422   free (matrixInTemp);
423   matrix_tran (matrixOutPre, matrixOutRow, matrixInCol,
424 	       matrixOut, matrixInRow, matrixOutCol);
425   free (matrixOutPre);
426   return;
427 }
428 
429 void
dyadup_2D_feed_even_col(double * matrixIn,int matrixInRow,int matrixInCol,double * matrixOut,int matrixOutRow,int matrixOutCol)430 dyadup_2D_feed_even_col (double *matrixIn, int matrixInRow,
431 			int matrixInCol, double *matrixOut,
432 			int matrixOutRow, int matrixOutCol)
433 {
434   int row, col;
435   for(col=0;col<matrixInCol;col++)
436     {
437       for(row=0;row<matrixInRow;row++)
438 	{
439 	  matrixOut[row+2*col*matrixInRow] = 0;
440 	  matrixOut[row+(2*col+1)*matrixInRow] =
441 	    matrixIn[row+col*matrixInRow];
442 	}
443     }
444   for(row=0;row<matrixOutRow;row++)
445     matrixOut[row+(matrixOutCol-1)*matrixOutRow]=0;
446   return;
447 }
448 
449 
450 void
dyadup_2D_feed_odd(double * matrixIn,int matrixInRow,int matrixInCol,double * matrixOut,int matrixOutRow,int matrixOutCol)451 dyadup_2D_feed_odd (double *matrixIn, int matrixInRow,
452 		    int matrixInCol, double *matrixOut,
453 		    int matrixOutRow, int matrixOutCol)
454 {
455   int row, col;
456   double *matrixInTemp, *matrixOutPre, *matrixOutTemp;
457 
458   matrixInTemp = malloc (matrixInRow * matrixInCol * sizeof (double));
459   matrix_tran (matrixIn, matrixInCol, matrixInRow,
460 	       matrixInTemp, matrixOutCol, matrixInRow);
461 
462   matrixOutPre = malloc (matrixOutRow * matrixInCol * sizeof (double));
463   for(row=0;row<matrixInRow-1;row++)
464     {
465       for(col=0;col<matrixInCol;col++)
466 	{
467 	  matrixOutPre[col+2*row*matrixInCol] =
468 	    matrixInTemp[col+row*matrixInCol];
469 	  matrixOutPre[col+(2*row+1)*matrixInCol] = 0;
470 	}
471     }
472   for(col=0;col<matrixInCol;col++)
473     matrixOutPre[col+(matrixOutRow-1)*matrixInCol] =
474       matrixInTemp[col+(matrixInRow-1)*matrixInCol];
475   free (matrixInTemp);
476 
477   matrixOutTemp = malloc (matrixOutRow * matrixInCol * sizeof (double));
478   matrix_tran (matrixOutPre, matrixOutRow, matrixInCol,
479 	       matrixOutTemp, matrixInRow, matrixOutCol);
480   free (matrixOutPre);
481   for(col=0;col<matrixInCol-1;col++)
482     {
483       for(row=0;row<matrixOutRow;row++)
484 	{
485 	  matrixOut[row+2*col*matrixOutRow]=
486 	    matrixOutTemp[row+col*matrixOutRow];
487 	  matrixOut[row+(2*col+1)*matrixOutRow] = 0;
488 	}
489     }
490   for(row=0;row<matrixOutRow;row++)
491     matrixOut[row+(matrixOutCol-1)*matrixOutRow] =
492       matrixOutTemp[row+(matrixInCol-1)*matrixOutRow];
493   free(matrixOutTemp);
494   return;
495 }
496 
497 void
dyadup_2D_feed_even(double * matrixIn,int matrixInRow,int matrixInCol,double * matrixOut,int matrixOutRow,int matrixOutCol)498 dyadup_2D_feed_even (double *matrixIn, int matrixInRow,
499 		     int matrixInCol, double *matrixOut,
500 		     int matrixOutRow, int matrixOutCol)
501 {
502   int row, col;
503   double *matrixInTemp, *matrixOutPre, *matrixOutTemp;
504 
505   matrixInTemp = malloc (matrixInRow * matrixInCol * sizeof (double));
506   matrix_tran (matrixIn, matrixInCol, matrixInRow,
507 	       matrixInTemp, matrixOutCol, matrixInRow);
508 
509   matrixOutPre = malloc (matrixOutRow * matrixInCol * sizeof (double));
510   for(row=0;row<matrixInRow;row++)
511     {
512       for(col=0;col<matrixInCol;col++)
513 	{
514 	  matrixOutPre[col+(2*row+1)*matrixInCol]=
515 	    matrixInTemp[col+row*matrixInCol];
516 	  matrixOutPre[col+2*row*matrixInCol] = 0;
517 	}
518     }
519   for(col=0;col<matrixInCol;col++)
520     matrixOutPre[col+(matrixOutRow-1)*matrixInCol] = 0;
521   free (matrixInTemp);
522 
523   matrixOutTemp = malloc (matrixOutRow * matrixInCol * sizeof (double));
524   matrix_tran (matrixOutPre, matrixOutRow, matrixInCol,
525 	       matrixOutTemp, matrixInRow, matrixOutCol);
526   free (matrixOutPre);
527   for(col=0;col<matrixInCol;col++)
528     {
529       for(row=0;row<matrixOutRow;row++)
530 	{
531 	  matrixOut[row+(2*col+1)*matrixOutRow]=
532 	    matrixOutTemp[row+col*matrixOutRow];
533 	  matrixOut[row+2*col*matrixOutRow] = 0;
534 	}
535     }
536   for(row=0;row<matrixOutRow;row++)
537     matrixOut[row+(matrixOutCol-1)*matrixOutRow]=0;
538   free(matrixOutTemp);
539   return;
540 }
541 
542 
543 /*-------------------------------------------
544  * Signal Extending
545  *-----------------------------------------*/
546 
547 void
extend_method_parse(char * mode,extend_method * extMethod)548 extend_method_parse (char *mode, extend_method *extMethod)
549 {
550   int count;
551 
552   for (count=0;count<extensionIdentityNum;count++)
553     {
554       if (strcmp(mode,ei[count].extMethodName) == 0)
555 	{
556 	  *extMethod = ei[count].extMethod;
557 	  break;
558 	}
559     }
560   return;
561 }
562 
563 
564 void
wextend_1D_center(double * sigIn,int sigInLength,double * sigOut,int sigOutLength,extend_method extMethod)565 wextend_1D_center (double *sigIn, int sigInLength,
566 	 double *sigOut, int sigOutLength,
567 	 extend_method extMethod)
568 {
569   int count = 0;
570   int addLength = 0;
571 
572   addLength = (sigOutLength - sigInLength) >> 1;
573   for (count = 0; count < addLength; count++)
574     {
575       sigOut[count] = 0;
576       sigOut[count + sigInLength + addLength] = 0;
577     }
578 
579   for (count = 0; count < sigInLength; count++)
580     {
581       sigOut[count + addLength] = sigIn[count];
582     }
583 
584   switch (extMethod) {
585   case ZPD: break;
586   case SYMH:
587     {
588       for (count = 0; count < addLength; count++)
589 	{
590 	  sigOut[count] = sigIn[addLength - count - 1];
591 	  sigOut[count + sigInLength + addLength] =
592 	    sigIn[sigInLength - count - 1];
593 	}
594       break;
595     }
596   case SYMW:
597     {
598       for (count = 0; count < addLength; count++)
599 	{
600 	  sigOut[count] = sigIn[addLength - count];
601 	  sigOut[count + sigInLength + addLength] =
602 	    sigIn[sigInLength - count - 2];
603 	}
604       break;
605     }
606   case ASYMH:
607     {
608       for (count = 0; count < addLength; count++)
609 	{
610 	  sigOut[count] = sigIn[addLength - count - 1] * (-1);
611 	  sigOut[count + sigInLength + addLength] =
612 	    sigIn[sigInLength - count - 1] * (-1);
613 	}
614       break;
615     }
616   case ASYMW:
617     {
618       for (count = 0; count < addLength; count++)
619 	{
620 	  sigOut[count] = sigIn[addLength - count] * (-1);
621 	  sigOut[count + sigInLength + addLength] =
622 	    sigIn[sigInLength - count - 2] * (-1);
623 	}
624       break;
625     }
626   case SP0:
627     {
628       for (count = 0; count < addLength; count++)
629 	{
630 	  sigOut[count] = sigIn[0];
631 	  sigOut[count + sigInLength + addLength] =
632 	    sigIn[sigInLength - 1];
633 	}
634       break;
635     }
636   case SP1:
637     {
638       for (count = (addLength - 1); count >= 0; count--)
639 	{
640 		sigOut[count] = sigIn[0]-(sigIn[1]-sigIn[0])*(addLength-count);
641 		sigOut[sigInLength + 2 * addLength - count - 1] =
642 			sigIn[sigInLength - 1] - (sigIn[sigInLength-2] - sigIn[sigInLength-1])*(addLength-count);
643 	 }
644       break;
645     }
646   case PPD:
647     {
648       for (count = 0; count < addLength; count++)
649 	{
650 	  sigOut[count] = sigIn[sigInLength - addLength + count];
651 	  sigOut[count + sigInLength + addLength] = sigIn[count];
652 	}
653       break;
654     }
655   case PER:
656     {
657       if (sigInLength%2 == 0)
658 	{
659 	  for (count = 0; count < addLength; count++)
660 	    {
661 	      sigOut[count] = sigIn[sigInLength - addLength + count];
662 	      sigOut[count + sigInLength + addLength] = sigIn[count];
663 	    }
664 	}
665       else
666 	{
667 	  sigOut[addLength + sigInLength] = sigIn[sigInLength - 1];
668 	  for (count = 0; count < addLength; count++)
669 	    {
670 	      sigOut[count] =
671 		     sigOut[sigInLength + 1 + count];
672 		  sigOut[count + sigInLength + addLength + 1] =
673 		     sigIn[count];
674 	    }
675 	}
676       break;
677     }
678   default: break;
679   }
680   return;
681 }
682 
683 
684 void
wextend_1D_left(double * sigIn,int sigInLength,double * sigOut,int sigOutLength,extend_method extMethod)685 wextend_1D_left (double *sigIn, int sigInLength,
686 	 double *sigOut, int sigOutLength,
687 	 extend_method extMethod)
688 {
689   int count = 0;
690   int addLength = 0;
691 
692   addLength = (sigOutLength - sigInLength);
693   for (count = 0; count < sigOutLength; count++)
694     sigOut[count] = 0;
695 
696   for (count = 0; count < sigInLength; count++)
697     {
698       sigOut[count + addLength] = sigIn[count];
699     }
700 
701   switch (extMethod) {
702   case ZPD: break;
703   case SYMH:
704     {
705       for (count = 0; count < addLength; count++)
706 	sigOut[count] = sigIn[addLength - count - 1];
707       break;
708     }
709   case SYMW:
710     {
711       for (count = 0; count < addLength; count++)
712 	sigOut[count] = sigIn[addLength - count];
713       break;
714     }
715   case ASYMH:
716     {
717       for (count = 0; count < addLength; count++)
718 	sigOut[count] = sigIn[addLength - count - 1] * (-1);
719       break;
720     }
721   case ASYMW:
722     {
723       for (count = 0; count < addLength; count++)
724 	sigOut[count] = sigIn[addLength - count] * (-1);
725       break;
726     }
727   case SP0:
728     {
729       for (count = 0; count < addLength; count++)
730 	sigOut[count] = sigIn[0];
731       break;
732     }
733   case SP1:
734     {
735       for (count = addLength - 1; count >= 0; count--)
736          sigOut[count] = sigIn[0]-(sigIn[1]-sigIn[0])*(addLength-count);
737 
738 
739       break;
740     }
741   case PPD:
742     {
743       for (count = 0; count < addLength; count++)
744 	sigOut[count] = sigIn[sigInLength - addLength + count];
745       break;
746     }
747   case PER:
748     {
749       if (sigInLength%2 == 0)
750 	{
751 	  for (count = 0; count < addLength; count++)
752 	    sigOut[count] = sigIn[sigInLength - addLength + count];
753 	}
754       else
755 	{
756 	  for (count = 0; count < sigInLength; count++)
757 	    {
758 	      sigOut[count + addLength - 1] = sigIn[count];
759 	    }
760 	  sigOut[sigOutLength-1] = sigOut[sigOutLength-2];
761 	  for (count = 0; count < (addLength - 1); count++)
762 	    sigOut[count] =
763 	      sigOut[sigInLength + count + 1];
764 	}
765       break;
766     }
767   default: break;
768   }
769   return;
770 }
771 
772 void
wextend_1D_right(double * sigIn,int sigInLength,double * sigOut,int sigOutLength,extend_method extMethod)773 wextend_1D_right (double *sigIn, int sigInLength,
774 	 double *sigOut, int sigOutLength,
775 	 extend_method extMethod)
776 {
777   int count = 0;
778   int addLength = 0;
779 
780   addLength = (sigOutLength - sigInLength);
781   for (count = 0; count < addLength; count++)
782     sigOut[count + sigInLength] = 0;
783 
784   for (count = 0; count < sigInLength; count++)
785     sigOut[count] = sigIn[count];
786 
787   switch (extMethod) {
788   case ZPD: break;
789   case SYMH:
790     {
791       for (count = 0; count < addLength; count++)
792 	sigOut[count + sigInLength] = sigIn[sigInLength - count - 1];
793       break;
794     }
795   case SYMW:
796     {
797       for (count = 0; count < addLength; count++)
798 	sigOut[count + sigInLength] = sigIn[sigInLength - count - 2];
799       break;
800     }
801   case ASYMH:
802     {
803       for (count = 0; count < addLength; count++)
804 	sigOut[count + sigInLength] =
805 	  sigIn[sigInLength - count - 1] * (-1);
806       break;
807     }
808   case ASYMW:
809     {
810       for (count = 0; count < addLength; count++)
811 	sigOut[count + sigInLength] =
812 	  sigIn[sigInLength - count - 2] * (-1);
813       break;
814     }
815   case SP0:
816     {
817       for (count = 0; count < addLength; count++)
818 	  sigOut[count + sigInLength] = sigIn[sigInLength - 1];
819       break;
820     }
821   case SP1:
822     {
823       for (count = 0; count < addLength; count++)
824 	  	sigOut[count+sigInLength] = sigIn[sigInLength-1] - (sigIn[sigInLength-2]-sigIn[sigInLength-1])*(count+1);
825 
826       break;
827     }
828   case PPD:
829     {
830       for (count = 0; count < addLength; count++)
831 	sigOut[count + sigInLength] = sigIn[count];
832       break;
833     }
834   case PER:
835     {
836       if (sigInLength%2 == 0)
837 	{
838 	  for (count = 0; count < addLength; count++)
839 	    sigOut[count + sigInLength] = sigIn[count];
840 	}
841       else
842 	{
843 	  sigOut[sigInLength] = sigOut[sigInLength - 1];
844 	  for(count = 0; count < (addLength - 1); count++)
845 	    {
846 	      sigOut[sigInLength + count + 1] = sigOut[count];
847 	    }
848 	}
849       break;
850     }
851   default: break;
852   }
853   return;
854 }
855 
856 void
wextend_2D(double * matrixIn,int matrixInRow,int matrixInCol,double * matrixOut,int matrixOutRow,int matrixOutCol,extend_method extMethod,char * rowOpt,char * colOpt)857 wextend_2D (double *matrixIn, int matrixInRow, int matrixInCol,
858 	    double *matrixOut, int matrixOutRow, int matrixOutCol,
859 	    extend_method extMethod, char *rowOpt, char *colOpt)
860 {
861   int row, col;
862   double *matrixInTemp, *matrixOutPre, *matrixOutTemp;
863 
864   matrixInTemp = malloc (matrixInRow * matrixInCol * sizeof (double));
865   matrix_tran (matrixIn, matrixInCol, matrixInRow,
866 	       matrixInTemp, matrixOutCol, matrixInRow);
867 
868   matrixOutPre = malloc (matrixInRow * matrixOutCol * sizeof (double));
869 
870   for (row = 0; row < matrixInRow; row++)
871     {
872       if (*rowOpt=='b')
873 	wextend_1D_center((matrixInTemp + row * matrixInCol),
874 			  matrixInCol,
875 			  (matrixOutPre + row * matrixOutCol),
876 			  matrixOutCol, extMethod);
877       if (*rowOpt=='l')
878 	wextend_1D_left((matrixInTemp + row * matrixInCol),
879 			matrixInCol,
880 			(matrixOutPre + row * matrixOutCol),
881 			matrixOutCol, extMethod);
882       if (*rowOpt=='r')
883 	wextend_1D_right((matrixInTemp + row * matrixInCol),
884 			 matrixInCol,
885 			 (matrixOutPre + row * matrixOutCol),
886 			 matrixOutCol, extMethod);
887     }
888   free (matrixInTemp);
889 
890   matrixOutTemp = malloc (matrixInRow * matrixOutCol * sizeof (double));
891   matrix_tran (matrixOutPre, matrixInRow, matrixOutCol,
892 	       matrixOutTemp, matrixInRow, matrixOutCol);
893   free (matrixOutPre);
894 
895   for (col = 0; col < matrixOutCol; col++)
896     {
897       if (*colOpt=='b')
898 	wextend_1D_center((matrixOutTemp + col * matrixInRow),
899 			  matrixInRow,
900 			  (matrixOut + col * matrixOutRow),
901 			  matrixOutRow, extMethod);
902       if (*colOpt=='l')
903 	wextend_1D_left((matrixOutTemp + col * matrixInRow),
904 			matrixInRow,
905 			(matrixOut + col * matrixOutRow),
906 			matrixOutRow, extMethod);
907       if (*colOpt=='r')
908 	wextend_1D_right((matrixOutTemp + col * matrixInRow),
909 			 matrixInRow,
910 			 (matrixOut + col * matrixOutRow),
911 			 matrixOutRow, extMethod);
912     }
913   free (matrixOutTemp);
914   return;
915 }
916 
917 void
wextend_2D_col(double * matrixIn,int matrixInRow,int matrixInCol,double * matrixOut,int matrixOutRow,int matrixOutCol,extend_method extMethod,char * Opt)918 wextend_2D_col (double *matrixIn, int matrixInRow, int matrixInCol,
919 		double *matrixOut, int matrixOutRow, int matrixOutCol,
920 		extend_method extMethod, char *Opt)
921 {
922   int row;//, col;
923   double *matrixInTemp, *matrixOutPre;
924 
925   matrixInTemp = malloc (matrixInRow * matrixInCol * sizeof (double));
926   matrix_tran (matrixIn, matrixInCol, matrixInRow,
927 	       matrixInTemp, matrixOutCol, matrixInRow);
928 
929   matrixOutPre = malloc (matrixInRow * matrixOutCol * sizeof (double));
930   for (row = 0; row < matrixInRow; row++)
931     {
932       if (strcmp(Opt,"b") == 0)
933 	wextend_1D_center((matrixInTemp + row * matrixInCol),
934 			  matrixInCol,
935 			  (matrixOutPre + row * matrixOutCol),
936 			  matrixOutCol, extMethod);
937       if (strcmp(Opt,"l") == 0)
938 	wextend_1D_left((matrixInTemp + row * matrixInCol),
939 			matrixInCol,
940 			(matrixOutPre + row * matrixOutCol),
941 			matrixOutCol, extMethod);
942       if (strcmp(Opt,"r") == 0)
943 	wextend_1D_right((matrixInTemp + row * matrixInCol),
944 			 matrixInCol,
945 			 (matrixOutPre + row * matrixOutCol),
946 			 matrixOutCol, extMethod);
947     }
948   free (matrixInTemp);
949   matrix_tran (matrixOutPre, matrixInRow, matrixOutCol,
950 	       matrixOut, matrixInRow, matrixOutCol);
951   free (matrixOutPre);
952   return;
953 }
954 
955 void
wextend_2D_row(double * matrixIn,int matrixInRow,int matrixInCol,double * matrixOut,int matrixOutRow,int matrixOutCol,extend_method extMethod,char * Opt)956 wextend_2D_row (double *matrixIn, int matrixInRow, int matrixInCol,
957 		double *matrixOut, int matrixOutRow, int matrixOutCol,
958 		extend_method extMethod, char *Opt)
959 {
960   int col;
961   //int row, col;
962   for (col = 0; col < matrixInCol; col++)
963     {
964       if (strcmp(Opt,"b") == 0)
965 	wextend_1D_center((matrixIn + col * matrixInRow),
966 			  matrixInRow,
967 			  (matrixOut + col * matrixOutRow),
968 			  matrixOutRow, extMethod);
969       if (strcmp(Opt,"l") == 0)
970 	wextend_1D_left((matrixIn + col * matrixInRow),
971 			matrixInRow,
972 			(matrixOut + col * matrixOutRow),
973 			matrixOutRow, extMethod);
974       if (strcmp(Opt,"r") == 0)
975 	wextend_1D_right((matrixIn + col * matrixInRow),
976 			 matrixInRow,
977 			 (matrixOut + col * matrixOutRow),
978 			 matrixOutRow, extMethod);
979     }
980   return;
981 }
982 
983 
984 /*-------------------------------------------
985  * Signal Extraction
986  *-----------------------------------------*/
987 void
wkeep_1D_center(double * sigIn,int sigInLength,double * sigOut,int sigOutLength)988 wkeep_1D_center (double *sigIn, int sigInLength,
989 		 double *sigOut, int sigOutLength)
990 {
991   int count = 0;
992   for (count = 0; count < sigOutLength; count++)
993 	sigOut[count] = sigIn[(sigInLength -
994 			       sigOutLength) / 2 + count];
995   return;
996 }
997 
998 void
wkeep_1D_left(double * sigIn,int sigInLength,double * sigOut,int sigOutLength)999 wkeep_1D_left (double *sigIn, int sigInLength,
1000 	       double *sigOut, int sigOutLength)
1001 {
1002   int count = 0;
1003   for (count = 0; count < sigOutLength; count++)
1004     sigOut[count] = sigIn[count];
1005   return;
1006 }
1007 
1008 void
wkeep_1D_right(double * sigIn,int sigInLength,double * sigOut,int sigOutLength)1009 wkeep_1D_right (double *sigIn, int sigInLength,
1010 		double *sigOut, int sigOutLength)
1011 {
1012   int count = 0;
1013   for (count = 0; count < sigOutLength; count++)
1014     sigOut[count] = sigIn[sigInLength - sigOutLength + count];
1015   return;
1016 }
1017 
1018 void
wkeep_1D_index(double * sigIn,int sigInLength,double * sigOut,int sigOutLength,int first)1019 wkeep_1D_index (double *sigIn, int sigInLength,
1020 		double *sigOut, int sigOutLength, int first)
1021 {
1022   int count = 0;
1023   for (count = 0; count < sigOutLength; count++)
1024     sigOut[count] = sigIn[count + first - 1];
1025   return;
1026 }
1027 
1028 void
wkeep_2D_center(double * matrixIn,int matrixInRow,int matrixInCol,double * matrixOut,int matrixOutRow,int matrixOutCol)1029 wkeep_2D_center (double *matrixIn, int matrixInRow,
1030 		 int matrixInCol, double *matrixOut,
1031 		 int matrixOutRow, int matrixOutCol)
1032 {
1033   int row, col, startRow, startCol;
1034   double *matrixInTemp, *matrixOutTemp;
1035 
1036   matrixInTemp = malloc (matrixInRow * matrixInCol * sizeof (double));
1037   matrixOutTemp = malloc (matrixOutRow * matrixOutCol * sizeof (double));
1038   matrix_tran (matrixIn, matrixInCol, matrixInRow,
1039 	       matrixInTemp, matrixInCol, matrixInRow);
1040 
1041   startRow = (matrixInRow - matrixOutRow) / 2;
1042   startCol = (matrixInCol - matrixOutCol) / 2;
1043 
1044   for (row = startRow; row < startRow + matrixOutRow; row++)
1045     {
1046       for (col = startCol; col < startCol + matrixOutCol; col++)
1047 	matrixOutTemp[col - startCol + (row - startRow) * matrixOutCol] =
1048 	  matrixInTemp[col + row * matrixInCol];
1049     }
1050   matrix_tran (matrixOutTemp, matrixOutRow, matrixOutCol,
1051 	       matrixOut, matrixOutRow, matrixOutCol);
1052 
1053   free (matrixInTemp);
1054   free (matrixOutTemp);
1055   return;
1056 }
1057 
1058 void
wkeep_2D_index(double * matrixIn,int matrixInRow,int matrixInCol,double * matrixOut,int matrixOutRow,int matrixOutCol,int rowFirst,int colFirst)1059 wkeep_2D_index (double *matrixIn, int matrixInRow,
1060 		int matrixInCol, double *matrixOut,
1061 		int matrixOutRow, int matrixOutCol,
1062 		int rowFirst, int colFirst)
1063 {
1064   int row, col, startRow, startCol;
1065   double *matrixInTemp, *matrixOutTemp;
1066 
1067   matrixInTemp = malloc (matrixInRow * matrixInCol * sizeof (double));
1068   matrixOutTemp = malloc (matrixOutRow * matrixOutCol * sizeof (double));
1069   matrix_tran (matrixIn, matrixInCol, matrixInRow,
1070 	       matrixInTemp, matrixInCol, matrixInRow);
1071 
1072   startRow = rowFirst - 1;
1073   startCol = colFirst - 1;
1074 
1075   for (row = startRow; row < startRow + matrixOutRow; row++)
1076     {
1077       for (col = startCol; col < startCol + matrixOutCol; col++)
1078 	matrixOutTemp[col - startCol + (row - startRow) * matrixOutCol] =
1079 	  matrixInTemp[col + row * matrixInCol];
1080     }
1081   matrix_tran (matrixOutTemp, matrixOutRow, matrixOutCol,
1082 	       matrixOut, matrixOutRow, matrixOutCol);
1083   free (matrixInTemp);
1084   free (matrixOutTemp);
1085   return;
1086 }
1087 
1088 /*-------------------------------------------
1089  * Convolution
1090  *-----------------------------------------*/
1091 void
conv(double * sigIn,int sigInLength,double * sigOut,int sigOutLength,double * filter,int filterLength)1092 conv (double *sigIn, int sigInLength,
1093       double *sigOut, int sigOutLength,
1094       double *filter, int filterLength)
1095 {
1096   int count = 0;
1097   int bufferLength = 0;
1098   int conCount = 0;
1099   double *pBuf;
1100 
1101   bufferLength = sigInLength + 2 * (filterLength - 1);
1102   pBuf = malloc (bufferLength * sizeof (double));
1103 
1104   /* initialize the buffer */
1105   for (count = 0; count < filterLength - 1; count++)
1106      {
1107       pBuf[count] = 0;		/* head */
1108       pBuf[count + sigInLength + filterLength - 1] = 0;	/* tail */
1109     }
1110   for (count = 0; count < sigInLength; count++)
1111     pBuf[count + filterLength - 1] = sigIn[count];
1112 
1113   /* convolution */
1114   for (count = 0; count < sigOutLength; count++)
1115     {
1116       sigOut[count] = 0;
1117       for (conCount = filterLength - 1; conCount >= 0; conCount--)
1118 	   {
1119 	     sigOut[count] += filter[conCount] *
1120 	     pBuf[count + filterLength - conCount - 1];
1121 	   }
1122     }
1123   free (pBuf);
1124   return;
1125 }
1126 
1127 /*-------------------------------------------
1128  * Periodic Convolution
1129  *-----------------------------------------*/
1130 
1131 void
i_conv(double * sigIn,int sigInLength,double * sigOut,int sigOutLength,double * filter,int filterLength)1132 i_conv(double *sigIn, int sigInLength,
1133 	  double *sigOut, int sigOutLength,
1134 	  double *filter, int filterLength)
1135 {
1136   int count = 0;
1137   int bufferLength = 0;
1138   int outLength = 0;
1139   double *pBuf;
1140   double *pOutBuf;
1141 
1142   bufferLength = 2 * sigInLength;
1143   pBuf = malloc(bufferLength*sizeof(double));
1144   for(count=0;count<sigInLength;count++)
1145   {
1146 	  pBuf[count] = sigIn[count];
1147 	  pBuf[count+sigInLength] = sigIn[count];
1148   }
1149 
1150   outLength = filterLength + 2 * sigInLength - 1;
1151   pOutBuf = malloc(outLength*sizeof(double));
1152   conv(pBuf,bufferLength,pOutBuf,outLength,filter,filterLength);
1153 
1154   free(pBuf);
1155 
1156   for(count=0;count<sigOutLength;count++)
1157     sigOut[count] = pOutBuf[filterLength+count];
1158   free(pOutBuf);
1159   return;
1160 }
1161 
1162 /*-------------------------------------------
1163  * EXP2 Convolution
1164  *-----------------------------------------*/
1165 
swt_exp2(int lev,int * outputV)1166  void swt_exp2(int lev, int *outputV)
1167  {
1168 	 int count;
1169 	 *outputV = 1;
1170 	 if (lev>0)
1171 	 {
1172 		 for (count=0;count<lev;count++)
1173 			 *outputV = *outputV * 2;
1174 	 }
1175 	 return;
1176  }
1177 
1178  /*-------------------------------------------
1179  * abs
1180  *-----------------------------------------*/
swt_abs(double sigIn)1181  double swt_abs(double sigIn)
1182  {
1183      if (sigIn>=0)
1184 		 return sigIn;
1185 	 else
1186 		 return (-1*sigIn);
1187  }
1188 
1189 /*-------------------------------------------
1190  * min
1191  *-----------------------------------------*/
1192 
swt_min(double * sigIn,int sigInLength,double * sigMin)1193  void swt_min(double *sigIn, int sigInLength, double *sigMin)
1194  {
1195 	 int count;
1196 
1197 	 *sigMin = sigIn[0];
1198 	 for(count=1;count<sigInLength;count++)
1199 	 {
1200          if (sigIn[count] < *sigMin)
1201 			 *sigMin = sigIn[count];
1202 	 }
1203 	 return;
1204  }
1205 
swt_min_abs(double * sigIn,int sigInLength,double * sigMin)1206  void swt_min_abs(double *sigIn, int sigInLength, double *sigMin)
1207  {
1208 	 int count;
1209 
1210 	 *sigMin = swt_abs(sigIn[0]);
1211 	 for(count=1;count<sigInLength;count++)
1212 	 {
1213          if (swt_abs(sigIn[count]) < *sigMin)
1214 			 *sigMin = swt_abs(sigIn[count]);
1215 	 }
1216 	 return;
1217  }
1218 
1219 
1220  /*-------------------------------------------
1221  * max
1222  *-----------------------------------------*/
1223 
swt_max(double * sigIn,int sigInLength,double * sigMax)1224  void swt_max(double *sigIn, int sigInLength, double *sigMax)
1225  {
1226 	 int count;
1227 
1228 	 *sigMax = sigIn[0];
1229 	 for(count=1;count<sigInLength;count++)
1230 	 {
1231          if (sigIn[count] > *sigMax)
1232 			 *sigMax = sigIn[count];
1233 	 }
1234 	 return;
1235  }
1236 
swt_max_abs(double * sigIn,int sigInLength,double * sigMax)1237  void swt_max_abs(double *sigIn, int sigInLength, double *sigMax)
1238  {
1239 	 int count;
1240 
1241 	 *sigMax = swt_abs(sigIn[0]);
1242 	 for(count=1;count<sigInLength;count++)
1243 	 {
1244          if (swt_abs(sigIn[count]) > *sigMax)
1245 			 *sigMax = swt_abs(sigIn[count]);
1246 	 }
1247 	 return;
1248  }
1249 
1250 
1251 /*-------------------------------------------
1252  * wcodemat
1253  *-----------------------------------------*/
1254 
wcodemat(double * sigIn,int sigInLength,double * sigOut,int sigOutLength,int minv,int maxv)1255  void wcodemat(double *sigIn, int sigInLength, double *sigOut, int sigOutLength, int minv, int maxv)
1256  {
1257      int count;
1258      double sigMin, sigMax;
1259 
1260 	 swt_max(sigIn, sigInLength, &sigMax);
1261 	 swt_min(sigIn, sigInLength, &sigMin);
1262 
1263 	 for(count=0;count<sigInLength;count++)
1264      	 sigOut[count] = ((sigIn[count]-sigMin)/(sigMax-sigMin))*(maxv-minv) + minv;
1265 	 return;
1266  }
1267 
wcodematd(double * sigIn,int sigInLength,double * sigOut,int sigOutLength,double minv,double maxv)1268  void wcodematd(double *sigIn, int sigInLength, double *sigOut, int sigOutLength, double minv, double maxv)
1269  {
1270      int count;
1271      double sigMin, sigMax;
1272 
1273 	 swt_max(sigIn, sigInLength, &sigMax);
1274 	 swt_min(sigIn, sigInLength, &sigMin);
1275 
1276 	 for(count=0;count<sigInLength;count++)
1277      	 sigOut[count] = ((sigIn[count]-sigMin)/(sigMax-sigMin))*(maxv-minv) + minv;
1278 	 return;
1279  }
1280 
1281 
wcodemat_abs(double * sigIn,int sigInLength,double * sigOut,int sigOutLength,int minv,int maxv)1282  void wcodemat_abs(double *sigIn, int sigInLength, double *sigOut, int sigOutLength, int minv, int maxv)
1283  {
1284      int count;
1285      double sigMin, sigMax;
1286 
1287 	 swt_max_abs(sigIn, sigInLength, &sigMax);
1288 	 swt_min_abs(sigIn, sigInLength, &sigMin);
1289 
1290 	 for(count=0;count<sigInLength;count++)
1291      	 sigOut[count] = ((swt_abs(sigIn[count])-sigMin)/(sigMax-sigMin))*(maxv-minv) + minv;
1292 	 return;
1293  }
1294 
wcodemat_matrix_row(double * matrixIn,int matrixInRow,int matrixInCol,double * matrixOut,int matrixOutRow,int matrixOutCol,int minv,int maxv,int abso)1295  void wcodemat_matrix_row (double *matrixIn, int matrixInRow, int matrixInCol,
1296 	                       double *matrixOut, int matrixOutRow, int matrixOutCol,
1297 						   int minv, int maxv, int abso)
1298  {
1299 	 int count;
1300 	 double *matrixOutTemp, *matrixOutPre;
1301 
1302 	 matrixOutTemp = malloc(matrixInRow*matrixInCol*sizeof(double));
1303 	 matrixOutPre = malloc(matrixInRow*matrixInCol*sizeof(double));
1304 
1305 	 matrix_tran(matrixIn,matrixInCol,matrixInRow,
1306 		         matrixOutTemp, matrixInRow, matrixInCol);
1307 	 for(count=0;count<matrixInRow;count++)
1308 	 {
1309 		 if (abso)
1310              wcodemat_abs(matrixOutTemp+count*matrixInCol,matrixInCol,matrixOutPre+count*matrixInCol,matrixInCol,minv,maxv);
1311 		 else
1312 			 wcodemat(matrixOutTemp+count*matrixInCol,matrixInCol,matrixOutPre+count*matrixInCol,matrixInCol,minv,maxv);
1313 	 }
1314 	 free(matrixOutTemp);
1315 	 //matrixOutM = malloc(matrixInRow*matrixInCol*sizeof(double));
1316 	 matrix_tran(matrixOutPre,matrixInRow,matrixInCol,matrixOut,matrixInRow,matrixInCol);
1317 	 free(matrixOutPre);
1318 
1319 	 //for(count=0;count<matrixInRow*matrixInCol;count++)
1320 	//	 matrixOut[count] = (int)(floor(matrixOutM[count]));
1321 	 //free(matrixOutM);
1322 
1323 	 return;
1324  }
1325 
wcodemat_matrix_col(double * matrixIn,int matrixInRow,int matrixInCol,double * matrixOut,int matrixOutRow,int matrixOutCol,int minv,int maxv,int abso)1326  void wcodemat_matrix_col (double *matrixIn, int matrixInRow, int matrixInCol,
1327 	                       double *matrixOut, int matrixOutRow, int matrixOutCol,
1328 						   int minv, int maxv, int abso)
1329  {
1330 	 int count;
1331 	 //double *matrixOutPre;
1332 
1333 	 //matrixOutPre = malloc(matrixInRow*matrixInCol*sizeof(double));
1334 	 for(count=0;count<matrixInCol;count++)
1335 	 {
1336 		 if (abso)
1337              wcodemat_abs(matrixIn+count*matrixInRow,matrixInRow,matrixOut+count*matrixInRow,matrixInRow,minv,maxv);
1338 		 else
1339 			 wcodemat(matrixIn+count*matrixInRow,matrixInRow,matrixOut+count*matrixInRow,matrixInRow,minv,maxv);
1340 	 }
1341      //for(count=0;count<matrixInRow*matrixInCol;count++)
1342 	//	 matrixOut[count] = (int)(floor(matrixOutPre[count]));
1343 	 //free(matrixOutPre);
1344 
1345 	 return;
1346  }
1347 
wcodemat_matrix(double * matrixIn,int matrixInRow,int matrixInCol,double * matrixOut,int matrixOutRow,int matrixOutCol,int minv,int maxv,int abso)1348  void wcodemat_matrix (double *matrixIn, int matrixInRow, int matrixInCol,
1349 	                   double *matrixOut, int matrixOutRow, int matrixOutCol,
1350 					   int minv, int maxv, int abso)
1351  {
1352      //int count;
1353 	 //double *matrixOutPre;
1354 
1355 	 //matrixOutPre = malloc(matrixInRow*matrixInCol*sizeof(double));
1356 	 //for(count=0;count<matrixInRow*matrixInCol;count++)
1357 	 //{
1358 		 if (abso)
1359              wcodemat_abs(matrixIn,matrixInRow*matrixInCol,matrixOut,matrixInRow*matrixInCol,minv,maxv);
1360 		 else
1361 			 wcodemat(matrixIn,matrixInRow*matrixInCol,matrixOut,matrixInRow*matrixInCol,minv,maxv);
1362 	 //}
1363      //for(count=0;count<matrixInRow*matrixInCol;count++)
1364 	//	 matrixOut[count] = (int)(floor(matrixOutPre[count]));
1365 	 //free(matrixOutPre);
1366 	 return;
1367  }
1368 
linspace(double lb,double ub,int n,double * sigOut,int sigOutLength)1369  void linspace(double lb, double ub, int n, double *sigOut, int sigOutLength)
1370  {
1371 	 int count;
1372 	 for(count=0;count<n;count++)
1373 	 	 sigOut[count] = lb + count*(ub-lb)/(n-1);
1374 	 return;
1375  }
1376 
1377 
1378 
1379 /*-------------------------------------------
1380  * ocumsum
1381  *-----------------------------------------*/
1382 /*void ocumsum (double *sigIn, int sigInLength)
1383 {
1384 	int i, j;
1385 	for (i=1;i<sigInLength;i++)
1386 	{
1387 		sigIn[i]+=sigIn[i-1];
1388 	}
1389 	return;
1390 }*/
1391