1 /*
2  * -------------------------------------------------------------------------
3  * dwt1d.c -- 1-D signal decomposition and reconstruction
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 /*-------------------------------------------
25  * wavelet name parser
26  *-----------------------------------------*/
27 
getdwtMode()28  extend_method getdwtMode(){
29    return dwtMode;
30  }
31 
32 void
filter_clear()33 filter_clear ()
34 {
35   int count;
36   for(count=0;count<80;count++)
37     {
38       LowDecomFilCoef[count] = 0;
39       LowReconFilCoef[count] = 0;
40       HiDecomFilCoef[count] = 0;
41       HiReconFilCoef[count] = 0;
42     }
43   return;
44 }
45 
46 /*-------------------------------------------
47  * Orthfilter Group
48  *-----------------------------------------*/
49 
50 void
orth_filt_group(double * filterIn,int sigInLength,double * filterLowRec,double * filterLowDec,double * filterHiRec,double * filterHiDec)51 orth_filt_group (double *filterIn, int sigInLength,
52 		 double *filterLowRec, double *filterLowDec,
53 		 double *filterHiRec, double *filterHiDec)
54 {
55   int count;
56 
57   for (count = 0; count < sigInLength; count++)
58     filterLowRec[count] = filterIn[count];
59   wrev (filterLowRec, sigInLength, filterLowDec, sigInLength);
60   qmf_even (filterLowRec, sigInLength, filterHiRec, sigInLength);
61   wrev (filterHiRec, sigInLength, filterHiDec, sigInLength);
62   return;
63 }
64 
65 void
bior_filt_group(double * f1,int sigInLength1,double * f2,int sigInLength2,double * lowDecom,int sigOutLength1,double * hiDecom,int sigOutLength2,double * lowRecon,int sigOutLength3,double * hiRecon,int sigOutLength4)66 bior_filt_group (double *f1, int sigInLength1,
67 		 double *f2, int sigInLength2,
68 		 double *lowDecom, int sigOutLength1,
69 		 double *hiDecom, int sigOutLength2,
70 		 double *lowRecon, int sigOutLength3,
71 		 double *hiRecon, int sigOutLength4)
72 {
73   verbatim_copy (f2, sigInLength2, lowRecon, sigOutLength3);
74   wrev (f1, sigInLength1, lowDecom, sigOutLength1);
75   qmf_even (f1, sigInLength1, hiRecon, sigOutLength4);
76   //qmf_odd (f1, sigInLength1, hiRecon, sigOutLength4);
77   qmf_wrev (f2, sigInLength2, hiDecom, sigOutLength2);
78   return;
79 }
80 
81 /*-------------------------------------------
82  * wavelet name parser
83  *-----------------------------------------*/
84 
85 void
wavelet_parser(char * wname,int * family,int * member)86 wavelet_parser (char *wname, int *family, int *member)
87 {
88   int count;
89 
90   *family = NOT_DEFINED;
91   *member = NOT_DEFINED;
92   for(count=0;count<waveletIdentityNum;count++)
93     {
94       if (strcmp(wname,wi[count].wname) == 0)
95 	{
96 	  *family = wi[count].family;
97 	  *member = wi[count].member;
98 	  break;
99 	}
100     }
101   return;
102 }
103 
104 void
wavelet_fun_parser(char * wname,int * ii)105 wavelet_fun_parser (char *wname, int *ii)
106 {
107   int count;
108 
109   *ii = -1;
110   for(count=0;count<waveletIdentityNum;count++)
111     {
112       if (strcmp(wname,wi[count].wname) == 0)
113 	{
114 	  *ii = count;
115 	  break;
116 	}
117     }
118   return;
119 }
120 
121 /*void
122 wave_len_validate (int sigInLen, int waveLength, int *lev, int *val)
123 {
124   int n;
125   int m;
126   int n1;
127   int m1;
128   float di;
129 
130   *val = 0;
131   di = (float) sigInLen / (float) waveLength;
132   if (di < 1)
133     {
134       *lev = 0;
135       *val = 0;
136       return;
137     }
138   else
139     {
140       n = (int) floor (log (di) / log ((float) 2));
141       m = (int) ceil (log (di) / log ((float) 2));
142       if ((((long) 1 << n) * waveLength == sigInLen)
143 	  || (((long) 1 << m) * waveLength == sigInLen))
144 	*lev = m + 1;
145       else
146 	*lev = n + 1;
147       *val = 1;
148       //	  n1 = (int) floor (log (waveLength) / log ((float) 2));
149       //m1 = (int) ceil (log (waveLength) / log ((float) 2));
150       //if (n1 != m1)
151       //    	  *lev = (*lev) - 1;
152       if (di == 2)
153 	*lev -= 1;
154       return;
155     }
156     }*/
157 
158 void
wave_len_validate(int sigInLen,int waveLength,int * lev,int * val)159 wave_len_validate (int sigInLen, int waveLength, int *lev, int *val)
160 {
161   int n;
162   int m;
163 
164   *val = 0;
165   if (sigInLen < 2*waveLength)
166     {
167       *lev = 0;
168       *val = 0;
169       return;
170     }
171   else
172     {
173       *val = 1;
174       *lev = 0;
175       n = sigInLen;
176       do
177 	{
178 	  m = (int)floor((n + waveLength - 1)/2);
179 	  *lev += 1;
180 	  n = m;
181 	}
182       while (m>=2*waveLength);
183       return;
184     }
185 }
186 
187 
188 
189 void
dwt_write(char * mode,int * errCode)190 dwt_write (char *mode, int *errCode)
191 {
192   int count;
193   *errCode = UNKNOWN_INPUT_ERR;
194 
195   for (count=0;count<extensionIdentityNum;count++)
196     {
197       if (strcmp(mode,ei[count].extMethodName) == 0)
198 	{
199 	  dwtMode = ei[count].extMethod;
200 	  *errCode = SUCCESS;
201 	  break;
202 	}
203     }
204   return;
205 }
206 
207 void
dwt_parse(char ** str1)208 dwt_parse(char **str1)
209 {
210   int count;
211   for (count=0;count<extensionIdentityNum;count++)
212     {
213       if (ei[count].extMethod == dwtMode)
214 	{
215 	  strcpy(*str1,ei[count].extMethodName);
216 	  break;
217 	}
218     }
219   return;
220 }
221 
222 void
dwt_nex(double * sigIn,int sigInLength,double * lowDe,double * hiDe,int filterLen,double * approx,double * detail,int sigOutLength)223 dwt_nex (double *sigIn, int sigInLength, double *lowDe,
224 	 double *hiDe, int filterLen, double *approx,
225 	 double *detail, int sigOutLength)
226 {
227   //int sigInLengthTemp,
228   int sigOutLengthTemp, sigOutLengthPre;
229   double *approxTemp, *approxPre;
230   double *detailTemp, *detailPre;
231 
232   //sigInLengthTemp = sigInLength + 2 * (filterLen - 1);
233   //sigInTemp = malloc (sigInLengthTemp * sizeof (double));
234   //wextend_1D_center (sigIn, sigInLength, sigInTemp,
235   //     sigInLengthTemp, extMethod);
236   sigOutLengthTemp = sigInLength + filterLen - 1;
237   approxTemp = malloc (sigOutLengthTemp * sizeof(double));
238   conv (sigIn, sigInLength, approxTemp,
239 	sigOutLengthTemp, lowDe, filterLen);
240   sigOutLengthPre = sigOutLengthTemp/2;
241   approxPre = malloc (sigOutLengthPre * sizeof(double));
242   dyaddown_1D_keep_even (approxTemp, sigOutLengthTemp,
243 			 approxPre, sigOutLengthPre);
244   wkeep_1D_center (approxPre, sigOutLengthPre, approx, sigOutLength);
245 
246   free(approxPre);
247   free(approxTemp);
248   detailTemp = malloc (sigOutLengthTemp * sizeof(double));
249   conv (sigIn, sigInLength, detailTemp,
250 	sigOutLengthTemp, hiDe, filterLen);
251   detailPre = malloc (sigOutLengthPre * sizeof(double));
252   dyaddown_1D_keep_even (detailTemp, sigOutLengthTemp,
253 			 detailPre, sigOutLengthPre);
254   wkeep_1D_center (detailPre, sigOutLengthPre, detail, sigOutLength);
255 
256   free(detailPre);
257   free(detailTemp);
258   //free(sigInTemp);
259   return;
260 }
261 
262 
263 void
dwt(double * sigIn,int sigInLength,double * lowDe,double * hiDe,int filterLen,double * approx,double * detail,int sigOutLength,extend_method extMethod)264 dwt (double *sigIn, int sigInLength, double *lowDe,
265      double *hiDe, int filterLen, double *approx,
266      double *detail, int sigOutLength, extend_method extMethod)
267 {
268   int sigInLengthTemp, sigOutLengthTemp, sigOutLengthPre;
269   double *sigInTemp, *approxTemp, *approxPre;
270   double *detailTemp, *detailPre;
271 
272   sigInLengthTemp = sigInLength + 2 * (filterLen - 1);
273   sigInTemp = malloc (sigInLengthTemp * sizeof (double));
274   wextend_1D_center (sigIn, sigInLength, sigInTemp,
275 		     sigInLengthTemp, extMethod);
276   sigOutLengthTemp = sigInLengthTemp + filterLen - 1;
277   approxTemp = malloc (sigOutLengthTemp * sizeof(double));
278   conv (sigInTemp, sigInLengthTemp, approxTemp,
279 	sigOutLengthTemp, lowDe, filterLen);
280   sigOutLengthPre = sigOutLengthTemp/2;
281   approxPre = malloc (sigOutLengthPre * sizeof(double));
282   dyaddown_1D_keep_even (approxTemp, sigOutLengthTemp,
283 			 approxPre, sigOutLengthPre);
284   wkeep_1D_center (approxPre, sigOutLengthPre, approx, sigOutLength);
285 
286   free(approxPre);
287   free(approxTemp);
288   detailTemp = malloc (sigOutLengthTemp * sizeof(double));
289   conv (sigInTemp, sigInLengthTemp, detailTemp,
290 	sigOutLengthTemp, hiDe, filterLen);
291   detailPre = malloc (sigOutLengthPre * sizeof(double));
292   dyaddown_1D_keep_even (detailTemp, sigOutLengthTemp,
293 			 detailPre, sigOutLengthPre);
294   wkeep_1D_center (detailPre, sigOutLengthPre, detail, sigOutLength);
295 
296   free(detailPre);
297   free(detailTemp);
298   free(sigInTemp);
299   return;
300 }
301 
302 void
dwt_neo(double * sigIn,int sigInLength,double * lowDe,double * hiDe,int filterLen,double * approx,double * detail,int sigOutLength,extend_method extMethod)303 dwt_neo (double *sigIn, int sigInLength, double *lowDe,
304      double *hiDe, int filterLen, double *approx,
305      double *detail, int sigOutLength, extend_method extMethod)
306 {
307   int sigInLengthTemp, sigOutLengthTemp, sigOutLengthPre;
308   double *sigInTemp, *approxTemp, *approxPre;
309   double *detailTemp, *detailPre;
310 
311   sigInLengthTemp = sigInLength + 2 * filterLen;
312   if ((extMethod == PER)&&(sigInLength%2 != 0))
313 	  sigInLengthTemp = sigInLength + 1 + 2 * filterLen;
314   //if ((extMethod == PER)&&(sigInLength%2 == 0))
315 	//  sigInLengthTemp = sigInLength + 2 * filterLen;
316   sigInTemp = malloc (sigInLengthTemp * sizeof (double));
317   wextend_1D_center (sigIn, sigInLength, sigInTemp,
318 		     sigInLengthTemp, extMethod);
319   sigOutLengthTemp = sigInLengthTemp + filterLen - 1;
320   approxTemp = malloc (sigOutLengthTemp * sizeof(double));
321   conv (sigInTemp, sigInLengthTemp, approxTemp,
322 	sigOutLengthTemp, lowDe, filterLen);
323   //sigOutLengthPre = sigOutLengthTemp/2;
324   sigOutLengthPre = sigInLength + filterLen - 1;
325   if ((extMethod==PER)&&(sigInLength%2 == 0))
326 	  sigOutLengthPre = sigInLength;
327   if ((extMethod==PER)&&(sigInLength%2 !=0))
328 	  sigOutLengthPre = sigInLength + 1;
329   approxPre = malloc (sigOutLengthPre * sizeof(double));
330   wkeep_1D_center (approxTemp, sigOutLengthTemp, approxPre, sigOutLengthPre);
331   dyaddown_1D_keep_even (approxPre, sigOutLengthPre,approx,sigOutLength);
332   //dyaddown_1D_keep_even (approxTemp, sigOutLengthTemp,
333 			 //approxPre, sigOutLengthPre);
334   //wkeep_1D_center (approxPre, sigOutLengthPre, approx, sigOutLength);
335 
336   free(approxPre);
337   free(approxTemp);
338   detailTemp = malloc (sigOutLengthTemp * sizeof(double));
339   conv (sigInTemp, sigInLengthTemp, detailTemp,
340 	sigOutLengthTemp, hiDe, filterLen);
341   detailPre = malloc (sigOutLengthPre * sizeof(double));
342   wkeep_1D_center (detailTemp, sigOutLengthTemp, detailPre, sigOutLengthPre);
343   dyaddown_1D_keep_even (detailPre, sigOutLengthPre,detail,sigOutLength);
344   //dyaddown_1D_keep_even (detailTemp, sigOutLengthTemp,
345 			 //detailPre, sigOutLengthPre);
346   //wkeep_1D_center (detailPre, sigOutLengthPre, detail, sigOutLength);
347 
348   free(detailPre);
349   free(detailTemp);
350   free(sigInTemp);
351   return;
352 }
353 
354 
355 void
dwt_conv(double * sigIn,int sigInLength,double * lowDe,double * hiDe,int filterLen,double * approx,double * detail,int sigOutLength)356 dwt_conv (double *sigIn, int sigInLength, double *lowDe,
357      double *hiDe, int filterLen, double *approx,
358      double *detail, int sigOutLength)
359 {
360 
361   //int sigOutLengthTemp;
362   //double *approxTemp;
363   //double *detailTemp;
364 
365 
366   //sigOutLengthTemp = sigInLength + filterLen - 1;
367   //approxTemp = malloc (sigOutLengthTemp * sizeof(double));
368   //conv (sigIn, sigInLength, approxTemp,
369 	//sigOutLengthTemp, lowDe, filterLen);
370   conv (sigIn, sigInLength, approx,
371 	sigOutLength, lowDe, filterLen);
372 
373   //dyaddown_1D_keep_even (approxTemp, sigOutLengthTemp,approx,sigOutLength);
374 
375   //free(approxTemp);
376   //detailTemp = malloc (sigOutLengthTemp * sizeof(double));
377   //conv (sigIn, sigInLength, detailTemp,
378 	//sigOutLengthTemp, hiDe, filterLen);
379   conv (sigIn, sigInLength, detail,
380 	sigOutLength, hiDe, filterLen);
381 
382   //dyaddown_1D_keep_even (detailTemp, sigOutLengthTemp,detail,sigOutLength);
383 
384   //free(detailTemp);
385 
386   return;
387 }
388 
389 void
dwt_no_extension(double * sigIn,int sigInLength,double * lowDe,double * hiDe,int filterLen,double * approx,double * detail,int sigOutLength)390 dwt_no_extension (double *sigIn, int sigInLength, double *lowDe,
391      double *hiDe, int filterLen, double *approx,
392      double *detail, int sigOutLength)
393 {
394   //int sigInLengthTemp,
395   int sigOutLengthTemp;
396   //sigOutLengthPre;
397   //double *sigInTemp,
398   double *approxTemp;//, *approxPre;
399   double *detailTemp;//, *detailPre;
400 
401   //sigInLengthTemp = sigInLength + 2 * filterLen;
402   //if ((extMethod == PER)||(sigInLength%2 != 0))
403 	//  sigInLengthTemp = sigInLength + 1 + 2 * filterLen;
404   //sigInTemp = malloc (sigInLengthTemp * sizeof (double));
405   //wextend_1D_center (sigIn, sigInLength, sigInTemp,
406 	//	     sigInLengthTemp, extMethod);
407   sigOutLengthTemp = sigInLength + filterLen - 1;
408   approxTemp = malloc (sigOutLengthTemp * sizeof(double));
409   conv (sigIn, sigInLength, approxTemp,
410 	sigOutLengthTemp, lowDe, filterLen);
411   //sigOutLengthPre = sigOutLengthTemp/2;
412   //sigOutLengthPre = sigInLength + filterLen - 1;
413   //approxPre = malloc (sigOutLengthPre * sizeof(double));
414   //wkeep_1D_center (approxTemp, sigOutLengthTemp, approxPre, sigOutLengthPre);
415   dyaddown_1D_keep_even (approxTemp, sigOutLengthTemp,approx,sigOutLength);
416   //dyaddown_1D_keep_even (approxTemp, sigOutLengthTemp,
417 			 //approxPre, sigOutLengthPre);
418   //wkeep_1D_center (approxPre, sigOutLengthPre, approx, sigOutLength);
419 
420   //free(approxPre);
421   free(approxTemp);
422   detailTemp = malloc (sigOutLengthTemp * sizeof(double));
423   conv (sigIn, sigInLength, detailTemp,
424 	sigOutLengthTemp, hiDe, filterLen);
425   //detailPre = malloc (sigOutLengthPre * sizeof(double));
426   //wkeep_1D_center (detailTemp, sigOutLengthTemp, detailPre, sigOutLengthPre);
427   dyaddown_1D_keep_even (detailTemp, sigOutLengthTemp,detail,sigOutLength);
428   //dyaddown_1D_keep_even (detailTemp, sigOutLengthTemp,
429 			 //detailPre, sigOutLengthPre);
430   //wkeep_1D_center (detailPre, sigOutLengthPre, detail, sigOutLength);
431 
432   //free(detailPre);
433   free(detailTemp);
434   //free(sigInTemp);
435   return;
436 }
437 
438 
439 void
idwt_complete_ex(double * approx,double * detail,int sigInLength,double * lowRe,double * hiRe,int filterLen,double * sigOut,int sigOutLength,extend_method extMethod)440 idwt_complete_ex (double *approx, double *detail, int sigInLength,
441 		  double *lowRe, double *hiRe, int filterLen,
442 		  double *sigOut, int sigOutLength,
443 		  extend_method extMethod)
444 {
445   int sigInLengthTemp, sigOutLengthTemp, count, ind, sigInLen;
446   double *approxTemp, *detailTemp, *approxPre, *detailPre;
447   double *sigOutPre, *approxEx, *detailEx;
448 
449   sigInLen = sigInLength + 2 * (filterLen - 1);
450   approxEx = malloc(sigInLen*sizeof(double));
451   detailEx = malloc(sigInLen*sizeof(double));
452 
453   wextend_1D_center (approx, sigInLength, approxEx, sigInLen,
454 		     extMethod);
455   wextend_1D_center (detail, sigInLength, detailEx, sigInLen,
456 		     extMethod);
457 
458   sigInLengthTemp = 2 * sigInLen - 1;
459   approxTemp = malloc(sigInLengthTemp * sizeof(double));
460   detailTemp = malloc(sigInLengthTemp * sizeof(double));
461   dyadup_1D_feed_odd (approxEx, sigInLen,
462 		      approxTemp, sigInLengthTemp);
463   dyadup_1D_feed_odd (detailEx, sigInLen,
464 		      detailTemp, sigInLengthTemp);
465   free(approxEx);
466   free(detailEx);
467   //printf("after dyadup!\n");
468   sigOutLengthTemp = sigInLengthTemp + filterLen - 1;
469   approxPre = malloc (sigOutLengthTemp * sizeof(double));
470   detailPre = malloc (sigOutLengthTemp * sizeof(double));
471   //printf("before conv!\n");
472   //if ((!approxPre) || (!detailPre))
473   //printf("Out of memory!\n");
474   conv (approxTemp, sigInLengthTemp, approxPre,
475 	sigOutLengthTemp, lowRe, filterLen);
476   conv (detailTemp, sigInLengthTemp, detailPre,
477 	sigOutLengthTemp, hiRe, filterLen);
478   //printf("conv fin!\n");
479   free(approxTemp);
480   free(detailTemp);
481   //printf("after conv!\n");
482   sigOutPre = malloc(sigOutLengthTemp * sizeof(double));
483   for(count=0;count<sigOutLengthTemp;count++)
484     sigOutPre[count] = approxPre[count] + detailPre[count];
485   free(approxPre);
486   free(detailPre);
487   //printf("before wkeep!\n");
488   //wkeep_1D_center (sigOutPre, sigOutLengthTemp,
489   //   sigOut, sigOutLength);
490   ind = (int)(2 + (sigOutLengthTemp-sigOutLength)/2.0);
491   //ind = 2 + (sigOutLengthTemp-sigOutLength)/2.0;
492   wkeep_1D_index (sigOutPre, sigOutLengthTemp,
493   	  sigOut, sigOutLength, ind);
494   //printf("sigLeng=%d,ind=%d\n",sigOutLengthTemp,ind);
495   free(sigOutPre);
496   //printf("leave idwt!\n");
497   return;
498 }
499 
500 
501 void
idwt_complete(double * approx,double * detail,int sigInLength,double * lowRe,double * hiRe,int filterLen,double * sigOut,int sigOutLength)502 idwt_complete (double *approx, double *detail, int sigInLength,
503 	       double *lowRe, double *hiRe, int filterLen,
504 	       double *sigOut, int sigOutLength)
505 {
506   int sigInLengthTemp, sigOutLengthTemp, count, ind;
507   double *approxTemp, *detailTemp, *approxPre, *detailPre;
508   double *sigOutPre;
509 
510   //printf("enter idwt!\n");
511   sigInLengthTemp = 2 * sigInLength - 1;
512   approxTemp = malloc(sigInLengthTemp * sizeof(double));
513   detailTemp = malloc(sigInLengthTemp * sizeof(double));
514   dyadup_1D_feed_odd (approx, sigInLength,
515 		      approxTemp, sigInLengthTemp);
516   dyadup_1D_feed_odd (detail, sigInLength,
517 		      detailTemp, sigInLengthTemp);
518   //printf("after dyadup!\n");
519   sigOutLengthTemp = sigInLengthTemp + filterLen - 1;
520   approxPre = malloc (sigOutLengthTemp * sizeof(double));
521   detailPre = malloc (sigOutLengthTemp * sizeof(double));
522   //printf("before conv!\n");
523   //if ((!approxPre) || (!detailPre))
524   //printf("Out of memory!\n");
525   conv (approxTemp, sigInLengthTemp, approxPre,
526 	sigOutLengthTemp, lowRe, filterLen);
527   conv (detailTemp, sigInLengthTemp, detailPre,
528 	sigOutLengthTemp, hiRe, filterLen);
529   //printf("conv fin!\n");
530   free(approxTemp);
531   free(detailTemp);
532   //printf("after conv!\n");
533   sigOutPre = malloc(sigOutLengthTemp * sizeof(double));
534   for(count=0;count<sigOutLengthTemp;count++)
535     sigOutPre[count] = approxPre[count] + detailPre[count];
536   free(approxPre);
537   free(detailPre);
538   //printf("before wkeep!\n");
539   //wkeep_1D_center (sigOutPre, sigOutLengthTemp,
540   //   sigOut, sigOutLength);
541   ind = (int)(2 + (sigOutLengthTemp-sigOutLength)/2.0);
542   wkeep_1D_index (sigOutPre, sigOutLengthTemp,
543   	  sigOut, sigOutLength, ind);
544   //printf("sigLeng=%d,ind=%d\n",sigOutLengthTemp,ind);
545   free(sigOutPre);
546   //printf("leave idwt!\n");
547   return;
548 }
549 
550 
551 
552 void
idwt_neo(double * approx,double * detail,int sigInLength,double * lowRe,double * hiRe,int filterLen,double * sigOut,int sigOutLength)553 idwt_neo (double *approx, double *detail, int sigInLength,
554 	       double *lowRe, double *hiRe, int filterLen,
555 	       double *sigOut, int sigOutLength)
556 {
557   int sigInLengthTemp, sigOutLengthTemp, count;//, ind;
558   double *approxTemp, *detailTemp, *approxPre, *detailPre;
559   double *sigOutPre;
560 
561   //printf("enter idwt!\n");
562   sigInLengthTemp = 2 * sigInLength + 1;
563   approxTemp = malloc(sigInLengthTemp * sizeof(double));
564   detailTemp = malloc(sigInLengthTemp * sizeof(double));
565   dyadup_1D_feed_even (approx, sigInLength,
566 		      approxTemp, sigInLengthTemp);
567   dyadup_1D_feed_even (detail, sigInLength,
568 		      detailTemp, sigInLengthTemp);
569   //printf("after dyadup!\n");
570   sigOutLengthTemp = sigInLengthTemp + filterLen - 1;
571   approxPre = malloc (sigOutLengthTemp * sizeof(double));
572   detailPre = malloc (sigOutLengthTemp * sizeof(double));
573   //printf("before conv!\n");
574   //if ((!approxPre) || (!detailPre))
575   //printf("Out of memory!\n");
576   conv (approxTemp, sigInLengthTemp, approxPre,
577 	sigOutLengthTemp, lowRe, filterLen);
578   conv (detailTemp, sigInLengthTemp, detailPre,
579 	sigOutLengthTemp, hiRe, filterLen);
580   //printf("conv fin!\n");
581   free(approxTemp);
582   free(detailTemp);
583   //printf("after conv!\n");
584   sigOutPre = malloc(sigOutLengthTemp * sizeof(double));
585   for(count=0;count<sigOutLengthTemp;count++)
586     sigOutPre[count] = approxPre[count] + detailPre[count];
587   free(approxPre);
588   free(detailPre);
589   //printf("before wkeep!\n");
590   wkeep_1D_center (sigOutPre, sigOutLengthTemp,
591      sigOut, sigOutLength);
592   //ind = 2 + (sigOutLengthTemp-sigOutLength)/2.0;
593   //wkeep_1D_index (sigOutPre, sigOutLengthTemp,
594   	  //sigOut, sigOutLength, ind);
595   //printf("sigLeng=%d,ind=%d\n",sigOutLengthTemp,ind);
596   free(sigOutPre);
597   //printf("leave idwt!\n");
598   return;
599 }
600 
601 
602 void
idwt_approx_ex(double * approx,int sigInLength,double * lowRe,int filterLen,double * sigOut,int sigOutLength,extend_method extMethod)603 idwt_approx_ex (double *approx, int sigInLength,
604 		double *lowRe, int filterLen,
605 		double *sigOut, int sigOutLength,
606 		extend_method extMethod)
607 {
608   int sigInLengthTemp, sigOutLengthTemp, ind, sigInLen;
609   double *approxTemp, *approxPre, *approxEx;
610 
611   sigInLen = sigInLength + 2 * (filterLen - 1);
612   approxEx = malloc(sigInLen * sizeof(double));
613   wextend_1D_center (approx, sigInLength, approxEx, sigInLen,
614 		     extMethod);
615 
616   sigInLengthTemp = 2 * sigInLen - 1;
617   approxTemp = malloc(sigInLengthTemp * sizeof(double));
618   dyadup_1D_feed_odd (approxEx, sigInLen,
619 		      approxTemp, sigInLengthTemp);
620   free(approxEx);
621   sigOutLengthTemp = sigInLengthTemp + filterLen - 1;
622   approxPre = malloc (sigOutLengthTemp * sizeof(double));
623   conv (approxTemp, sigInLengthTemp, approxPre,
624 	sigOutLengthTemp, lowRe, filterLen);
625   free(approxTemp);
626 
627   //wkeep_1D_center (approxPre, sigOutLengthTemp,
628   //   sigOut, sigOutLength);
629   ind = (int)(2 + (sigOutLengthTemp-sigOutLength)/2.0);
630   wkeep_1D_index (approxPre, sigOutLengthTemp,
631   	  sigOut, sigOutLength, ind);
632   free(approxPre);
633   return;
634 }
635 
636 
637 
638 void
idwt_approx(double * approx,int sigInLength,double * lowRe,int filterLen,double * sigOut,int sigOutLength)639 idwt_approx (double *approx, int sigInLength,
640 	     double *lowRe, int filterLen,
641 	     double *sigOut, int sigOutLength)
642 {
643   int sigInLengthTemp, sigOutLengthTemp, ind;
644   double *approxTemp, *approxPre;
645 
646   sigInLengthTemp = 2 * sigInLength - 1;
647   approxTemp = malloc(sigInLengthTemp * sizeof(double));
648   dyadup_1D_feed_odd (approx, sigInLength,
649 		       approxTemp, sigInLengthTemp);
650 
651   sigOutLengthTemp = sigInLengthTemp + filterLen - 1;
652   approxPre = malloc (sigOutLengthTemp * sizeof(double));
653   conv (approxTemp, sigInLengthTemp, approxPre,
654 	sigOutLengthTemp, lowRe, filterLen);
655   free(approxTemp);
656 
657   //wkeep_1D_center (approxPre, sigOutLengthTemp,
658   //   sigOut, sigOutLength);
659   ind = (int)(2 + (sigOutLengthTemp-sigOutLength)/2.0);
660   wkeep_1D_index (approxPre, sigOutLengthTemp,
661   	  sigOut, sigOutLength, ind);
662   free(approxPre);
663   return;
664 }
665 
666 void
idwt_approx_neo(double * approx,int sigInLength,double * lowRe,int filterLen,double * sigOut,int sigOutLength)667 idwt_approx_neo (double *approx, int sigInLength,
668 	     double *lowRe, int filterLen,
669 	     double *sigOut, int sigOutLength)
670 {
671   int sigInLengthTemp, sigOutLengthTemp;//, ind;
672   double *approxTemp, *approxPre;
673 
674   sigInLengthTemp = 2 * sigInLength + 1;
675   approxTemp = (double*)malloc(sigInLengthTemp * sizeof(double));
676   dyadup_1D_feed_even (approx, sigInLength,
677 		       approxTemp, sigInLengthTemp);
678 
679   sigOutLengthTemp = sigInLengthTemp + filterLen - 1;
680   approxPre = (double*)malloc (sigOutLengthTemp * sizeof(double));
681   conv (approxTemp, sigInLengthTemp, approxPre,
682 	sigOutLengthTemp, lowRe, filterLen);
683   free(approxTemp);
684 
685   wkeep_1D_center (approxPre, sigOutLengthTemp,
686      sigOut, sigOutLength);
687   //ind = 2 + (sigOutLengthTemp-sigOutLength)/2.0;
688   //wkeep_1D_index (approxPre, sigOutLengthTemp,
689   	//  sigOut, sigOutLength, ind);
690   free(approxPre);
691   return;
692 }
693 
694 void
idwt_detail(double * detail,int sigInLength,double * hiRe,int filterLen,double * sigOut,int sigOutLength)695 idwt_detail (double *detail, int sigInLength,
696 	     double *hiRe, int filterLen,
697 	     double *sigOut, int sigOutLength)
698 {
699   int sigInLengthTemp, sigOutLengthTemp, ind;
700   double *detailTemp, *detailPre;
701 
702   sigInLengthTemp = 2 * sigInLength - 1;
703   detailTemp = malloc(sigInLengthTemp * sizeof(double));
704   dyadup_1D_feed_odd (detail, sigInLength,
705 		       detailTemp, sigInLengthTemp);
706 
707   sigOutLengthTemp = sigInLengthTemp + filterLen - 1;
708   detailPre = malloc (sigOutLengthTemp * sizeof(double));
709   conv (detailTemp, sigInLengthTemp, detailPre,
710 	sigOutLengthTemp, hiRe, filterLen);
711   free(detailTemp);
712 
713   //wkeep_1D_center (detailPre, sigOutLengthTemp,
714   //   sigOut, sigOutLength);
715   ind = (int)(2 + (sigOutLengthTemp-sigOutLength)/2.0);
716   wkeep_1D_index (detailPre, sigOutLengthTemp,
717 		  sigOut, sigOutLength, ind);
718   free(detailPre);
719   return;
720 }
721 
722 void
idwt_detail_ex(double * detail,int sigInLength,double * hiRe,int filterLen,double * sigOut,int sigOutLength,extend_method extMethod)723 idwt_detail_ex (double *detail, int sigInLength,
724 		double *hiRe, int filterLen,
725 		double *sigOut, int sigOutLength,
726 		extend_method extMethod)
727 {
728   int sigInLengthTemp, sigOutLengthTemp, ind, sigInLen;
729   double *detailTemp, *detailPre, *detailEx;
730 
731   sigInLen = sigInLength + 2 * (filterLen - 1);
732   detailEx = malloc(sigInLen * sizeof(double));
733   wextend_1D_center (detail, sigInLength, detailEx, sigInLen,
734 		     extMethod);
735 
736   sigInLengthTemp = 2 * sigInLen - 1;
737   detailTemp = malloc(sigInLengthTemp * sizeof(double));
738   dyadup_1D_feed_odd (detailEx, sigInLen,
739 		      detailTemp, sigInLengthTemp);
740   free(detailEx);
741   sigOutLengthTemp = sigInLengthTemp + filterLen - 1;
742   detailPre = malloc (sigOutLengthTemp * sizeof(double));
743   conv (detailTemp, sigInLengthTemp, detailPre,
744 	sigOutLengthTemp, hiRe, filterLen);
745   free(detailTemp);
746 
747   //wkeep_1D_center (detailPre, sigOutLengthTemp,
748   //   sigOut, sigOutLength);
749   ind = (int)(2 + (sigOutLengthTemp-sigOutLength)/2.0);
750   wkeep_1D_index (detailPre, sigOutLengthTemp,
751 		  sigOut, sigOutLength, ind);
752   free(detailPre);
753   return;
754 }
755 
756 
757 void
idwt_detail_neo(double * detail,int sigInLength,double * hiRe,int filterLen,double * sigOut,int sigOutLength)758 idwt_detail_neo (double *detail, int sigInLength,
759 	     double *hiRe, int filterLen,
760 	     double *sigOut, int sigOutLength)
761 {
762   int sigInLengthTemp, sigOutLengthTemp;//, ind;
763   double *detailTemp, *detailPre;
764 
765   sigInLengthTemp = 2 * sigInLength + 1;
766   detailTemp = malloc(sigInLengthTemp * sizeof(double));
767   dyadup_1D_feed_even (detail, sigInLength,
768 		       detailTemp, sigInLengthTemp);
769 
770   sigOutLengthTemp = sigInLengthTemp + filterLen - 1;
771   detailPre = malloc (sigOutLengthTemp * sizeof(double));
772   conv (detailTemp, sigInLengthTemp, detailPre,
773 	sigOutLengthTemp, hiRe, filterLen);
774   free(detailTemp);
775 
776   wkeep_1D_center (detailPre, sigOutLengthTemp,
777      sigOut, sigOutLength);
778   //ind = 2 + (sigOutLengthTemp-sigOutLength)/2.0;
779   //wkeep_1D_index (detailPre, sigOutLengthTemp,
780 	//	  sigOut, sigOutLength, ind);
781   free(detailPre);
782   return;
783 }
784 
785 
786 void
wave_dec_len_cal(int filterLen,int sigLength,int stride,int * waveDecLengthArray)787 wave_dec_len_cal (int filterLen, int sigLength,
788 		  int stride, int *waveDecLengthArray)
789 {
790   int count = 0;
791   int calLen;
792   waveDecLengthArray[stride + 1] = sigLength;
793   if (dwtMode!=PER)
794     {
795       calLen = sigLength;
796       for (count = 0; count < stride; count++)
797 	{
798 	  calLen += (filterLen - 1);
799 	  waveDecLengthArray[stride-count]=(int)(floor(calLen/2));
800 	  calLen = *(waveDecLengthArray + stride - count);
801 	}
802       waveDecLengthArray[0] = waveDecLengthArray[1];
803     }
804   else
805     {
806       for (count=stride; count > 0; count--)
807 	waveDecLengthArray[count] =
808 	  (int)ceil(((double)(waveDecLengthArray[count+1]))/2.0);
809      waveDecLengthArray[0] = waveDecLengthArray[1];
810     }
811   return;
812 }
813 
814 void
upcoef_len_cal(int sigInLength,int filterLen,int stride,int * sigOutLength,int * sigOutLengthDefault)815 upcoef_len_cal (int sigInLength, int filterLen, int stride,
816 		int *sigOutLength, int *sigOutLengthDefault)
817 {
818   int count;
819   *sigOutLength = sigInLength;
820   *sigOutLengthDefault = sigInLength;
821 //   if ((2*(*sigOutLength) - filterLen + 2)<0){ //this was implemented for cwt
822       for(count=0;count<stride;count++)
823       {
824 	// original version
825 	*sigOutLengthDefault = 2*(*sigOutLengthDefault) + filterLen - 1;
826 	*sigOutLength = 2*(*sigOutLength) + filterLen - 2;
827 
828       }
829 //     } else { //works with dwt
830 //       for(count=0;count<stride;count++)
831 // 	{
832 //
833 //     //version 1.14 - but does not work with cwt
834 // 	  *sigOutLengthDefault = 2*(*sigOutLengthDefault) + filterLen - 1;
835 // 	  *sigOutLength = 2*(*sigOutLength) - filterLen + 2;
836 //
837 // 	}
838 //      }
839   return;
840 }
841 
842 void
upcoef(double * sigIn,int sigInLength,double * lowRe,double * hiRe,int filterLen,double * sigOut,int sigOutLength,int defaultLength,char * coefType,int step)843 upcoef (double *sigIn, int sigInLength, double *lowRe,double *hiRe,
844 	int filterLen, double *sigOut, int sigOutLength,
845 	int defaultLength, char *coefType, int step)
846 {
847   int count, sigInLengthTemp, leng;
848   double *sigInTemp, *sigOutTemp;
849   //version 1.14 fow dwt - but does not work with cwt
850 //    sigInLengthTemp = 2 * sigInLength - filterLen + 2;
851 
852 //   // works with wavefun, cwt
853       sigInLengthTemp = 2 * sigInLength + filterLen - 2;
854 
855 
856 
857   //sigInLengthTemp = 2 * sigInLength + filterLen - 1;
858   sigInTemp = (double *) malloc(defaultLength*sizeof(double));
859 
860   if (strcmp(coefType,"a")==0)
861   {
862 	  //sciprint("recognized\n");
863 // 	  printf("sigInLength %d, filterLen%d, sigInLengthTemp %d\n",sigInLength,filterLen,sigInLengthTemp);
864 	  idwt_approx_neo (sigIn, sigInLength, lowRe, filterLen,
865 		 sigInTemp, sigInLengthTemp);
866 // 	  sciprint("recognized\n");
867   }
868   else
869     idwt_detail_neo (sigIn, sigInLength, hiRe, filterLen,
870 		 sigInTemp, sigInLengthTemp);
871 
872   if (step > 1)
873     {
874       sigOutTemp = (double *) malloc(defaultLength*sizeof(double));
875       for(count=0;count<defaultLength;count++)
876 	sigOutTemp[count] = 0;
877       leng = sigInLengthTemp;
878       for(count=0;count<(step-1);count++)
879 	{
880 	  //printf("leng %d, filterLen%d, leng*2-filterLen+2 %d\n",leng,filterLen,leng*2-filterLen+2);
881 
882 	  // original version
883 	  idwt_approx_neo (sigInTemp, leng, lowRe, filterLen,
884 	               sigOutTemp, leng*2+filterLen-2);
885 	  leng = leng*2+filterLen-2;
886 
887 // 	  //version 1.14 - but does not work with cwt
888 // 	  idwt_approx_neo (sigInTemp, leng, lowRe, filterLen,
889 // 		       sigOutTemp, leng*2-filterLen+2);
890 // 	  //sciprint("ok\n");
891 // 	  leng = leng*2-filterLen+2;
892 
893 	  verbatim_copy (sigOutTemp, leng, sigInTemp, leng);
894 	}
895       sigInLengthTemp = leng;
896       free(sigOutTemp);
897     }
898 
899 
900   wkeep_1D_center (sigInTemp, sigInLengthTemp, sigOut, sigOutLength);
901   free(sigInTemp);
902   return;
903 }
904 
905 void
wavedec(double * sigIn,int sigInLength,double * sigOut,int sigOutLength,double * lowDe,double * hiDe,int filterLen,int * waveDecLengthArray,int lengthArrayLengh,int stride,extend_method extMethod)906 wavedec (double *sigIn, int sigInLength, double *sigOut,
907 	 int sigOutLength, double *lowDe, double *hiDe,
908 	 int filterLen, int *waveDecLengthArray,
909 	 int lengthArrayLengh, int stride, extend_method extMethod)
910 {
911   int count, pos, countt;
912   int sigInLen;
913   //int filterLen;
914   double *pApprox, *pDetail, *pApproxTemp;
915   double *pSig;
916 
917   pSig = sigIn;
918   sigInLen = sigInLength;
919 
920   pApprox = malloc (sigInLength * sizeof (double));
921   pApproxTemp = malloc (sigInLength * sizeof (double));
922   for (count = 0; count < sigInLength; count++)
923     {
924       pApprox[count] = 0;
925       pApproxTemp[count] = 0;
926     }
927   pos = sigOutLength - waveDecLengthArray[stride];
928   pDetail = sigOut + pos;
929   for (count = 0; count < stride; count++)
930     {
931       dwt_neo (pSig, sigInLen, lowDe, hiDe, filterLen, pApprox,
932 	   pDetail, waveDecLengthArray[stride - count], extMethod);
933       for (countt = 0; countt < waveDecLengthArray[stride - count]; countt++)
934 	pApproxTemp[countt] = pApprox[countt];
935       pSig = pApproxTemp;
936       sigInLen = waveDecLengthArray[stride - count];
937       pos = pos - waveDecLengthArray[stride - count - 1];
938       pDetail = (sigOut + pos);
939     }
940   for (count = 0; count < sigInLen; count++)
941     sigOut[count] = pApprox[count];
942 
943   free (pApprox);
944   free (pApproxTemp);
945   return;
946 }
947 
948 
949 void
waverec(double * sigIn,int sigInLength,double * sigOut,int sigOutLength,double * lowRe,double * hiRe,int filterLen,int * waveDecLengthArray,int lengthArraylength,int stride,extend_method extMethod)950 waverec (double *sigIn, int sigInLength, double *sigOut,
951 	 int sigOutLength, double *lowRe, double *hiRe,
952 	 int filterLen, int *waveDecLengthArray,
953 	 int lengthArraylength, int stride,
954 	 extend_method extMethod)
955 {
956   int count, pos, countt;
957   int sigInLen;
958   double *pApprox, *pDetail, *pApproxTemp;
959 
960   sigInLen = waveDecLengthArray[1];
961 
962   pApprox = malloc (sigOutLength * sizeof (double));
963   pApproxTemp = malloc (sigOutLength * sizeof (double));
964   for (count = 0; count < sigOutLength; count++)
965     {
966       pApprox[count] = 0;
967       pApproxTemp[count] = 0;
968     }
969   pos = waveDecLengthArray[0];
970   pDetail = sigIn + pos;
971   for (count = 0; count < waveDecLengthArray[1]; count++)
972     pApprox[count] = sigIn[count];
973 
974   for (count = 0; count < stride; count++)
975     {
976       idwt_neo (pApprox, pDetail, sigInLen, lowRe, hiRe,
977 		     filterLen, pApproxTemp,
978 		     waveDecLengthArray[count + 2]);
979       for (countt = 0; countt < waveDecLengthArray[count + 2]; countt++)
980 	pApprox[countt] = pApproxTemp[countt];
981       sigInLen = waveDecLengthArray[count + 2];
982       pos += waveDecLengthArray[count + 1];
983       pDetail = sigIn + pos;
984     }
985   for (count = 0; count < sigOutLength; count++)
986     sigOut[count] = pApprox[count];
987 
988   free (pApprox);
989   free (pApproxTemp);
990   return;
991 }
992 
993 void
wenergy(double * coef,int coefLen,int * lenArray,int arrayLen,double * aE,int aELen,double * dE,int dELen)994 wenergy (double *coef, int coefLen, int *lenArray, int arrayLen,
995 	 double *aE, int aELen, double *dE, int dELen)
996 {
997   int count, countt, *pos;
998   double ene;
999 
1000   ene = 0;
1001   for(count=0;count<coefLen;count++)
1002     ene += coef[count]*coef[count];
1003   *aE = 0;
1004   for(count=0;count<lenArray[0];count++)
1005     *aE += coef[count]*coef[count];
1006   *aE = (*aE)*100/ene;
1007 
1008   pos = malloc (dELen*sizeof(int));
1009   for(count=0;count<dELen;count++)
1010     pos[count] = 0;
1011   pos[0] = lenArray[0];
1012   for(count=1;count<dELen;count++)
1013     pos[count] += (lenArray[count] + pos[count-1]);
1014   for(count=0;count<dELen;count++)
1015     {
1016       dE[count] = 0;
1017       for(countt=0;countt<lenArray[count+1];countt++)
1018 	dE[count] += coef[pos[count]+countt]*coef[pos[count]+countt];
1019       dE[count] = dE[count]*100/ene;
1020     }
1021   free(pos);
1022   return;
1023 }
1024 
1025 void
detcoef(double * sigIn,int sigInLength,int * waveDecLengthArray,int arrayLen,double * sigOut,int sigOutLength,int stride,int level)1026 detcoef (double *sigIn, int sigInLength, int *waveDecLengthArray,
1027 	 int arrayLen, double *sigOut, int sigOutLength,
1028 	 int stride, int level)
1029 {
1030    int leng=0;
1031   int startCount=0, count=0;
1032 //  printf ("level %d, leng %d count %d stride %d waveDecLengthArray %d\n",level,leng,count,stride,waveDecLengthArray[stride - level+1]);
1033   if (level != 0)
1034     {
1035 
1036       for (count = 0; count < level; count++){
1037            //sciprint("tmp %d",tmp);
1038 			     leng +=waveDecLengthArray[stride - count];;
1039 			printf("");
1040 
1041 		  }
1042   }
1043     //printf ("sigInLength %d, leng %d\n",sigInLength,leng);
1044   startCount = sigInLength - leng;
1045   //printf ("level %d, leng %d startCount %d sigOutLength %d,sigInLength %d \n",level,leng,startCount,sigOutLength,sigInLength);
1046   for (count = startCount; count <= (startCount + sigOutLength - 1); count++){
1047     sigOut[count - startCount] = sigIn[count];
1048   }
1049 
1050   return;
1051 }
1052 
1053 void
appcoef(double * sigIn,int sigInLength,double * sigOut,int sigOutLength,double * lowRe,double * hiRe,int filterLen,int * waveDecLengthArray,int lengthArraylength,int stride,int level,extend_method extMethod)1054 appcoef (double *sigIn, int sigInLength, double *sigOut,
1055 	 int sigOutLength, double *lowRe, double *hiRe,
1056 	 int filterLen, int *waveDecLengthArray,
1057 	 int lengthArraylength, int stride, int level,
1058 	 extend_method extMethod)
1059 {
1060   int count, pos, countt;
1061   int sigInLen;
1062   double *pApprox, *pDetail, *pApproxTemp;
1063 
1064   if (level == stride)
1065     {
1066       for (count = 0; count < waveDecLengthArray[stride - level + 1]; count++)
1067 	sigOut[count] = sigIn[count];
1068       return;
1069     }
1070 
1071   sigInLen = waveDecLengthArray[1];
1072 
1073   pApprox = malloc (sigOutLength * sizeof (double));
1074   pApproxTemp = malloc (sigOutLength * sizeof (double));
1075   for (count = 0; count < sigOutLength; count++)
1076     {
1077       pApprox[count] = 0;
1078       pApproxTemp[count] = 0;
1079     }
1080   pos = waveDecLengthArray[0];
1081   pDetail = sigIn + pos;
1082   for (count = 0; count < waveDecLengthArray[1]; count++)
1083     pApprox[count] = sigIn[count];
1084 
1085   for (count = 0; count < (stride - level); count++)
1086     {
1087       idwt_neo (pApprox, pDetail, sigInLen, lowRe, hiRe,
1088 		     filterLen, pApproxTemp,
1089 		     waveDecLengthArray[count + 2]);
1090       for (countt = 0; countt < waveDecLengthArray[count + 2]; countt++)
1091 	pApprox[countt] = pApproxTemp[countt];
1092       sigInLen = waveDecLengthArray[count + 2];
1093       pos += waveDecLengthArray[count + 1];
1094       pDetail = sigIn + pos;
1095     }
1096   for (count = 0; count < sigOutLength; count++)
1097     sigOut[count] = pApprox[count];
1098 
1099   free (pApprox);
1100   free (pApproxTemp);
1101   return;
1102 }
1103 
1104 void
wrcoef(double * sigIn,int sigInLength,double * lowRe,double * hiRe,int filterLen,int * waveDecLengthArray,int arrayLen,double * sigOut,int sigOutLength,char * coefType,int stride,int level,extend_method extMethod)1105 wrcoef (double *sigIn, int sigInLength, double *lowRe, double *hiRe,
1106 	int filterLen, int *waveDecLengthArray, int arrayLen,
1107 	double *sigOut, int sigOutLength, char *coefType,
1108 	int stride, int level, extend_method extMethod)
1109 {
1110 
1111   int count = 0;
1112   int startCount, endCount, leng;
1113   double *sigOutTemp;
1114 
1115   sigOutTemp = malloc (sigInLength * sizeof (double));
1116 
1117   if (level != 0)
1118     {
1119       leng = 0;
1120       for (count = 0; count < level; count++)
1121 	leng += waveDecLengthArray[stride - count];
1122     }
1123 
1124   if (strcmp (coefType, "d") == 0)
1125     {
1126       for (count = 0; count < sigInLength; count++)
1127 	sigOutTemp[count] = 0;
1128       if (level != 0)
1129 	{
1130 	  startCount = sigInLength - leng;
1131 	  endCount = startCount + waveDecLengthArray[stride - level + 1] - 1;
1132 	  for (count = startCount; count <= endCount; count++)
1133 	    sigOutTemp[count] = sigIn[count];
1134 	}
1135     }
1136   else
1137     {
1138       for (count = 0; count < sigInLength; count++)
1139 	sigOutTemp[count] = sigIn[count];
1140       if (level != 0)
1141 	{
1142 	  endCount = sigInLength - 1;
1143 	  startCount = endCount - leng + 1;
1144 	  for (count = startCount; count <= endCount; count++)
1145 	    sigOutTemp[count] = 0;
1146 	}
1147     }
1148   waverec (sigOutTemp, sigInLength, sigOut, sigOutLength,
1149 	   lowRe, hiRe, filterLen, waveDecLengthArray, arrayLen,
1150 	   stride, extMethod);
1151   //waverec (sigInLength, sigOutTemp, sigOutLength, sigOut,
1152   //   stride, waveDecLengthArray, waveType, mode);
1153   free (sigOutTemp);
1154   return;
1155 }
1156 
1157 void
upwlev(double * coefArray,int coefLen,int * waveDecLengthArray,int arrayLen,double * lowRe,double * hiRe,int filterLen,double * newCoefArray,int newCoefLen,int * newLenArray,int newArrayLen,double * approx,int approxLen,int stride,extend_method extMethod)1158 upwlev (double *coefArray, int coefLen, int *waveDecLengthArray,
1159 	int arrayLen, double *lowRe, double *hiRe, int filterLen,
1160 	double *newCoefArray, int newCoefLen, int *newLenArray,
1161 	int newArrayLen, double *approx, int approxLen,
1162 	int stride, extend_method extMethod)
1163 {
1164   int count, pos1;
1165   char c='a';
1166   double *app, *det;
1167 
1168   //printf("enter upwlev!\n");
1169   for(count=0;count<approxLen;count++)
1170     approx[count]=coefArray[count];
1171   for(count=arrayLen-1;count>1;count--)
1172     newLenArray[count-1]=waveDecLengthArray[count];
1173   newLenArray[0]=newLenArray[1];
1174 
1175   pos1 = waveDecLengthArray[0] + waveDecLengthArray[1];
1176   for(count=coefLen-1;count>=pos1;count--)
1177     newCoefArray[count-coefLen+newCoefLen]=coefArray[count];
1178 
1179   app = malloc(waveDecLengthArray[1]*sizeof(double));
1180   det = malloc(waveDecLengthArray[1]*sizeof(double));
1181   for(count=0;count<waveDecLengthArray[1];count++)
1182     {
1183       app[count]=coefArray[count];
1184       det[count]=coefArray[count+waveDecLengthArray[1]];
1185     }
1186   idwt_neo (app, det, waveDecLengthArray[1], lowRe, hiRe,
1187 		 filterLen, newCoefArray, waveDecLengthArray[2]);
1188   free(app);
1189   free(det);
1190   return;
1191 }
1192