-
Notifications
You must be signed in to change notification settings - Fork 0
/
xynWave.c
1987 lines (1608 loc) · 63.9 KB
/
xynWave.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* xynWave.c
*
* Created on: Feb 5, 2011
* Author: mousomer
*
*
* symmetry types for symmetric biorthogonal transforms:
* half-symmetric: 32100123
* whole-symmetric: 3210123
* data lowpass highpass
* length head start end start end
* even lowpass whole half half whole
* even highpass half whole whole half
* odd lowpass whole whole half half
* odd highpass half half whole whole
*
* via lifting:
* whole symmetric means a_0+=c*(2*a_1)
* half symmetric means a_0+=c*(a_1+a_0)
*
*
* algorithmic convection:
* inPlane non-inPlace
* ---------------------------------------------------------------------------------------
* lift inp+=2, out+=2, transform: a 1 a not implemented
*
* normal inp++, out[2]+=2, change out-index (oddity) inp++, out[2]+=1, change oddity
*
*
*
* ToDo list:
* Align highpass / lowpass filter positions using filt->pos during fwd/inv filter transform.
*
*/
#include "libxyn.h"
#define LOW_LEN(len, oddity) len/2 + (len%2 * (1-oddity))
static int FixWaveShift = 10;
static int FixWaveScale = 1000;
/** declarations of static functions **/
/* filter generators (filter are global variables) */
static void wavInitHaar();
static void wavInitCDF53();
static void wavInitCDF97();
static void wavInitTest();
static void wavInitLiftCDF53();
static void wavInitLiftCDF97();
/*static void wavInitPloyHaar();
static void wavInitPolyCDF53();
static void wavInitPolyCDF97();*/
static void genPolyFilterFromWave ( waveFilterPtr wfp, wavePolyPtr pfp );
/** QMF symmetries and filter operations **/
static void wavCompleteWavFilterData ( waveFilterPtr wfp );
/** QMF auxiliaries: **/
static int wavFilterQMF_f ( float* out, float* inp, int len, int zeroPos );
static int wavFilterFold_f ( float (*out)[FILTER_LEN], int* outLen, float* inp, int len, int zeroPos );
static int wavFilterQMF_i ( int* out, int* inp, int len, int zeroPos );
static int wavFilterSym_f ( float* data, int len, int zeroPos );
static int wavCoeffsIntFromFloat ( int* int_coeffs, float* float_coeffs, int n );
/* lift stage in-place */
static int wavLiftingStageInPlace_f ( float* data, long len, int start, register float a );
static int wavLiftingStageInPlace_i ( int* data, long len, int start, register int a );
static int wavLiftingStageInPlace_d ( double* data, long len, int start, register double a );
static int wavLiftingStageInPlace_fVect ( float* data, long vLen, int vSize, int start, register float a );
static int wavLiftingStageInPlace_iVect ( int* data, long vLen, int vSize, int start, register int a );
static int wavLiftingStageInPlace_dVect ( double* data, long vLen, int vSize, int start, register double a );
/* filter transform - main loops */
static int wavTransformHiLow_f ( float* dataInp[2], int ji, float* dataOut[2], int jo, long len, bool oddity, waveFilterPtr filt );
static int wavInvTransformHiLow_f ( float* dataInp[2], int ji, float* dataOut, int jo, long len, bool oddity, waveFilterPtr filt );
/* lift stage not in-place
static int wavLiftingStage_f ( float* inpData, float* outData, long len, int start, register float a );
static int wavLiftingStage_i ( int* inpData, int* outData, long len, int start, register int a );
static int wavLiftingStage_fVect ( float* inpData, float* outData, long vLen, int vSize, int start, register float a );
static int wavLiftingStage_iVect ( int* inpData, int* outData, long vLen, int vSize, int start, register int a );
*/
/* packing / unpacking (reordering after/before lifting) */
static int wavPack1d_f ( float* data, long len, int oddity );
static int wavPack1d_i ( int* data, long len, int oddity );
static int wavPack1d_d ( double* data, long len, int oddity );
static int wavUnpack1d_f ( float* data, long len, int oddity );
static int wavUnpack1d_i ( int* data, long len, int oddity );
/* calculate last lowpass position */
#define SCALE_END(len, oddity) ( (len%2)&&oddity ) || ( (1-len%2)&&(1-oddity) )
/* global wavelet transform filters */
struct waveletFilter wavHaar, wavCDF97, wavCDF53, wavTest;
struct wavePolyFilter wavPolyHaar, wavPolyCDF97, wavPolyCDF53;
struct waveLiftFilter wavLiftHaar, wavLiftCDF97, wavLiftCDF53;
/** filter print **/
int wavPrint ( waveFilterPtr filt, FILE* fp )
{
fprintf (fp, "wavelet filter %s\n", filt->name);
fprintf( fp, "lowPass analysis (start at %d): ", filt->pos_anal[0]);
displayArray_f ( filt->f_anal[0], filt->len_anal[0], fp );
fprintf( fp, "highPass analysis (start at %d): ", filt->pos_anal[1]);
displayArray_f ( filt->f_anal[1], filt->len_anal[1], fp );
fprintf( fp, "lowPass synthesis (start at %d): ", filt->pos_synt[0]);
displayArray_f ( filt->f_synt[0], filt->len_synt[0], fp );
fprintf( fp, "highPass synthesis (start at %d): ", filt->pos_synt[1]);
displayArray_f ( filt->f_synt[1], filt->len_synt[1], fp );
fprintf( fp, "\nfolded elements: lowpass analysis\t");
displayArray_i ( filt->len_anal_folded[0], filt->len_anal[0], fp ); NL;
for (int i=0; i<filt->len_anal[0]; i++)
displayArray_f ( filt->f_anal_folded[0][i], filt->len_anal_folded[0][i], fp );
fprintf( fp, "\nfolded elements: highpass analysis\t");
displayArray_i ( filt->len_anal_folded[1], filt->len_anal[1], fp ); NL;
for (int i=0; i<filt->len_anal[1]; i++)
displayArray_f ( filt->f_anal_folded[1][i], filt->len_anal_folded[1][i], fp );
fprintf( fp, "\nfolded elements: lowpass synthesis\t");
displayArray_i ( filt->len_synt_folded[0], filt->len_synt[0], fp ); NL;
for (int i=0; i<filt->len_synt[0]; i++)
displayArray_f ( filt->f_synt_folded[0][i], filt->len_synt_folded[0][i], fp );
fprintf( fp, "\nfolded elements: highpass synthesis\t");
displayArray_i ( filt->len_synt_folded[1], filt->len_synt[1], fp ); NL;
for (int i=0; i<filt->len_synt[1]; i++)
displayArray_f ( filt->f_synt_folded[1][i], filt->len_synt_folded[1][i], fp );
NL;
fprintf( fp, "\nfolded elements: lowpass analysis\t");
displayArray_i ( filt->len_anal_folded[0], filt->len_anal[0], fp ); NL;
for (int i=0; i<filt->len_anal[0]; i++)
displayArray_i ( filt->i_anal_folded[0][i], filt->len_anal_folded[0][i], fp );
fprintf( fp, "\nfolded elements: highpass analysis\t");
displayArray_i ( filt->len_anal_folded[1], filt->len_anal[1], fp ); NL;
for (int i=0; i<filt->len_anal[1]; i++)
displayArray_i ( filt->i_anal_folded[1][i], filt->len_anal_folded[1][i], fp );
fprintf( fp, "\nfolded elements: lowpass synthesis\t");
displayArray_i ( filt->len_synt_folded[0], filt->len_synt[0], fp ); NL;
for (int i=0; i<filt->len_synt[0]; i++)
displayArray_i ( filt->i_synt_folded[0][i], filt->len_synt_folded[0][i], fp );
fprintf( fp, "\nfolded elements: highpass synthesis\t");
displayArray_i ( filt->len_synt_folded[1], filt->len_synt[1], fp ); NL;
for (int i=0; i<filt->len_synt[1]; i++)
displayArray_i ( filt->i_synt_folded[1][i], filt->len_synt_folded[1][i], fp );
NL;
return OK;
}
int wavPolyPrint ( wavePolyPtr filt, FILE* fp )
{
fprintf (fp, "wavelet filter %s\n", filt->name);
fprintf( fp, "lowPass analysis (start at %d/%d): ", filt->pos_anal[0][0], filt->pos_anal[0][1]);
displayArray_f ( filt->f_anal[0][0], filt->len_anal[0][0], fp );
displayArray_f ( filt->f_anal[0][1], filt->len_anal[0][1], fp );
fprintf( fp, "highPass analysis (start at %d/%d): ", filt->pos_anal[1][0], filt->pos_anal[1][1]);
displayArray_f ( filt->f_anal[1][0], filt->len_anal[1][0], fp );
displayArray_f ( filt->f_anal[1][1], filt->len_anal[1][1], fp );
fprintf( fp, "lowPass synthesis (start at %d/%d): ", filt->pos_synt[0][0], filt->pos_synt[0][1]);
displayArray_f ( filt->f_synt[0][0], filt->len_synt[0][0], fp );
displayArray_f ( filt->f_synt[0][1], filt->len_synt[0][1], fp );
fprintf( fp, "highPass synthesis (start at %d/%d): ", filt->pos_synt[1][0], filt->pos_synt[1][1]);
displayArray_f ( filt->f_synt[1][0], filt->len_synt[1][0], fp );
displayArray_f ( filt->f_synt[1][1], filt->len_synt[1][1], fp );
fprintf( fp, "\nfolded elements:\n");
for (int p=0; p<=1; p++)
for (int o=0; o<=1; o++)
{
fprintf( fp, "\n analysis %d/%d lengths ", p, o);
displayArray_i ( filt->len_anal_folded[p][o], filt->len_anal[p][o], fp ); NL;
for (int i=0; i<filt->len_anal[p][o]; i++)
displayArray_f ( filt->f_anal_folded[p][o][i], filt->len_anal_folded[p][o][i], fp );
fprintf( fp, "\n synthesis %d/%d lengths ", p, o);
displayArray_i ( filt->len_synt_folded[p][o], filt->len_synt[p][o], fp ); NL;
for (int i=0; i<filt->len_synt[p][o]; i++)
displayArray_f ( filt->f_synt_folded[p][o][i], filt->len_synt_folded[p][o][i], fp );
}
for (int p=0; p<=1; p++)
for (int o=0; o<=1; o++)
{
fprintf( fp, "\n analysis %d/%d lengths ", p, o);
displayArray_i ( filt->len_anal_folded[p][o], filt->len_anal[p][o], fp ); NL;
for (int i=0; i<filt->len_anal[p][o]; i++)
displayArray_i ( filt->i_anal_folded[p][o][i], filt->len_anal_folded[p][o][i], fp );
fprintf( fp, "\n synthesis %d/%d lengths ", p, o);
displayArray_i ( filt->len_synt_folded[p][o], filt->len_synt[p][o], fp ); NL;
for (int i=0; i<filt->len_synt[p][o]; i++)
displayArray_i ( filt->i_synt_folded[p][o][i], filt->len_synt_folded[p][o][i], fp );
}
return OK;
}
int wavLiftPrint ( waveLiftPtr filt, FILE* fp )
{
fprintf (fp, "wavelet lifting filter %s, number of predict/update stages: %d\n", filt->name, filt->nPredict);
fprintf (fp, "wavelet lifting filter %s\n", filt->name);
fprintf( fp, "floating point update filter: " );
displayArray_f ( filt->coeff_f[0], filt->nPredict, fp );
fprintf( fp, "floating point predict filter: " );
displayArray_f ( filt->coeff_f[1], filt->nPredict, fp );
fprintf( fp, "floating point lowpass scaler: %f\n", filt->scaler_f[0] );
fprintf( fp, "floating point highpass scaler: %f\n", filt->scaler_f[1] );
fprintf( fp, "integer lowpass scaler: %d\n", filt->scaler_i[0] );
fprintf( fp, "integer highpass scaler: %d\n", filt->scaler_i[1] );
fprintf( fp, "integer update filter: " );
displayArray_i ( filt->coeff_i[0], filt->nPredict, fp );
fprintf( fp, "integer predict filter: " );
displayArray_i ( filt->coeff_i[1], filt->nPredict, fp );
return OK;
}
/** filter generation by wavelet name **/
waveFilterPtr wavGetWaveletName( const char* name )
{
waveFilterPtr wp=NULL;
//printf ("function %s. %s\n", __FUNCTION__, name );
if ( strncmp(name, "CDF-5-3", 7) == 0 )
{
wp = &wavCDF53;
wavInitCDF53();
//printf("wavelet: CDF-5-3\n");
}
else if ( strncmp(name,"Haar", 4) == 0 )
{
wp = &wavHaar;
wavInitHaar();
//printf("wavelet: Haar\n");
}
else if ( strncmp(name, "CDF-9-7", 7) ==0 )
{
wp = &wavCDF97;
wavInitCDF97();
//printf("wavelet: CDF-9-7\n");
}
else if ( strncmp(name, "test", 4) ==0 )
{
wp = &wavTest;
wavInitTest();
//printf("wavelet: test\n");
}
else
perror ("no such wavelet filter implemented here.\n");
return wp;
}
waveLiftPtr wavGetLiftWaveName( const char* name )
{
waveLiftPtr wp = NULL;
//printf ("function %s. %s\n", __FUNCTION__, name );
if ( strncmp(name, "CDF-5-3", 7) == 0 )
{
wp = &wavLiftCDF53;
wavInitLiftCDF53();
}
else if ( strncmp(name,"Haar", 4) == 0 )
{
ErrPrintMsg("not a valid lifting filter/n");
//wp = &wavLiftHaar;
//wavInitHaar();
}
else if ( strncmp(name, "CDF-9-7", 7) ==0 )
{
wp = &wavLiftCDF97;
wavInitLiftCDF97();
}
return wp;
}
wavePolyPtr wavGetPolyWaveName( const char* name )
{
waveFilterPtr fp = NULL;
wavePolyPtr pfp = NULL;
//printf ("function %s. %s\n", __FUNCTION__, name );
if ( strncmp(name, "CDF-5-3", 7) == 0 )
{
fp = &wavCDF53;
pfp = &wavPolyCDF53;
wavInitCDF53();
}
else if ( strncmp(name,"Haar", 4) == 0 )
{
fp = &wavHaar;
pfp = &wavPolyHaar;
wavInitHaar();
}
else if ( strncmp(name, "CDF-9-7", 7) ==0 )
{
fp = &wavCDF97;
pfp = &wavPolyCDF97;
wavInitCDF97();
}
genPolyFilterFromWave ( fp, pfp );
return pfp;
}
dwtInfPtr wavGenDWTInfo ( const char* wavName, enum waveType wtype, bool oddity, bool inPlace )
{
assert ( wavName );
dwtInfPtr ip = (dwtInfPtr) malloc (sizeof(struct dwtInfo));
assert ( ip );
ip->oddity = (oddity>0) ? 1 : 0;
ip->inPlace = (inPlace>0) ? 1 : 0;
ip->wType = wtype;
ip->fwp = NULL;
ip->lwp = NULL;
if ( ip->wType == xynWaveleType )
ip->fwp = wavGetWaveletName ( wavName );
else if ( ip->wType == xynLiftingType )
ip->lwp = wavGetLiftWaveName ( wavName );
else
perror ("wavelet type not implemented yet.\n");
return ip;
}
/**** DWT transforms types:
* wavTransform - normal filter
* invTransform - inverse
* liftTransform - lifting implementation
* inPlace - leave data in place (data -> low, high, low, high.... )
* HiLow - fwd transform divided into two output (fwd) / input (inv) arrays
* ..._i - integer transform
* ..._f - floating point transform
* IO:
* int/float *dataInp (sometimes *inpLow, *inpHigh) - input array[s]
* int/float *dataOut (sometimes *outLow, *outHigh) - output array[s]
* long len (sometimes lenLow, lenHigh) - array[s] length
* boolean oddity - 0 start with lowpass, 1 start with highpass.
* boolean inPlave - tells lifting implementation whether to pack/unpack data
* boolean rescale - tells lifting implementation whether to rescale (0) or not (1) the data
* filt - DWT filter (waveFilter for normal transform, waveLift for lifting)
* ****/
/* floating point transform - get 1 output data set, divide it into high half and low half */
int wavTransform_f( float* dataInp, float* dataOut, long len, bool oddity, bool inPlace, waveFilterPtr filt )
{
assert(dataInp!=NULL); assert(dataOut!=NULL); assert(filt!=NULL);
float *cpyOut[2] = { dataOut, dataOut }; // work with IO pointer copies
float *cpyInp[2] = { dataInp, dataInp };
int jumpOut = 2, jumpInp = 2;
if (inPlace)
{
cpyOut[0] += oddity;
cpyOut[1] += 1-oddity;
}
else
{
cpyOut[1] += LOW_LEN(len, oddity);
jumpOut = 1;
}
XYN_DPRINTF(DEBUG_HEADER, "%s %s: len %d; oddity %d. jumps: in %d out %d\n", __FUNCTION__, filt->name, len, oddity, jumpInp, jumpOut);
wavTransformHiLow_f ( cpyInp, jumpInp, cpyOut, jumpOut, len, oddity, filt );
return OK;
}
int wavInvTransform_f( float* dataInp, float* dataOut, long len, bool oddity, bool inPlace, waveFilterPtr filt )
{
assert(dataInp!=NULL); assert(dataOut!=NULL); assert(filt!=NULL);
float *cpyInp[2] = { dataInp, dataInp };
int jumpOut = 1, jumpInp = 2;
if (inPlace)
{
cpyInp[0] += oddity;
cpyInp[1] += 1-oddity;
}
else
{
cpyInp[1] += LOW_LEN(len, oddity);
jumpInp = 1;
jumpOut = 1;
}
XYN_DPRINTF(DEBUG_HEADER, "%s %s: len %d; oddity %d. jumps: in %d out %d\t lowLen %ld (%f)\n",
__FUNCTION__, filt->name, len, oddity, jumpInp, jumpOut, len/2 + (len%2 * (1-oddity)), *cpyInp[1] );
wavInvTransformHiLow_f ( cpyInp, jumpInp, dataOut, jumpOut, len, oddity, filt );
return OK;
}
/* floating point transform - normal filter, input (1) -> output (2 arrays)
* set starting positions, boundary lengths, etc.
* run transform on lower boundary - use folded filters
* (set fold position)
* run transform normally - keep changing oddity (from start-> high to low to high...)
* output[low/high] = filter[low/high] * data
* run transform on upper
*
* Assumptions:
* - filter length (and hence, folded boundaries of the filter) should be shorter than any data buffer
* - filter is symmetric (in form, if not in content) around filt->zeroPos
*
* jumpInp should be 2
* jumpOut is 2 for inPlace (l h l h...)
* is 1 for packed transform (lllll... hhhhhh...)
* */
static int wavTransformHiLow_f( float* dataInp[2], int ji, float* dataOut[2], int jo,
long len, bool oddity, waveFilterPtr filt )
{
/* check input integrity */
assert(dataInp[0]!=NULL); assert(dataInp[1]!=NULL); assert(dataOut[0]!=NULL); assert(dataOut[1]!=NULL); assert(filt!=NULL);
if ( len<XYN_MAX(filt->len_anal[0], filt->len_anal[1] ) )
ErrMsg("data no long enough for filter\n");
int fold[2]={0,0}; fold[1-oddity]=1; // fold[0/1] is lowpass/highpass folded filter index
XYN_DPRINTF(DEBUG_HEADER,"%s: (len %d). oddity: %d. Folded filters %d/%d\n", __FUNCTION__, len, oddity, fold[0], fold[1]);
for (long i=0; i<len; i++, oddity = 1-oddity) // i from 1 - this is second pass type
{
if (i < filt->pos_anal[oddity]) // inside lower boundary - call folded filter
{
*(dataOut[oddity]) = dotProd_f ( filt->f_anal_folded[oddity][fold[oddity]], dataInp[oddity],
filt->len_anal_folded[oddity][fold[oddity]] );
XYN_DPRINTF(DEBUG_DETAIL, "i=%6ld; fold %4d=%7.2f(%d)\t", i, fold[oddity], *dataOut[oddity], oddity);
//displayArray_f(filt->f_anal_folded[oddity][fold[oddity]], filt->len_anal_folded[oddity][fold[oddity]], stdout);
fold[oddity]+=2;
if ( fold[oddity] > filt->pos_anal[oddity] )
dataInp[oddity] ++;
}
else if ( i >= len-filt->pos_anal[oddity] ) // inside upper boundary - call upper filter
{
int cFold = filt->len_anal[oddity] - (len-i); /* calculate current fold (oddity may cancel dependence on lower fold) */
*(dataOut[oddity]) = dotProd_f ( filt->f_anal_folded[oddity][cFold], dataInp[oddity],
filt->len_anal_folded[oddity][cFold] );
XYN_DPRINTF(DEBUG_DETAIL, "\ni=%6ld; fold %4d=%7.2f(%d)\t", i, cFold, *dataOut[oddity], oddity);
//displayArray_f(filt->f_anal_folded[oddity][cFold], filt->len_anal_folded[oddity][cFold], stdout);
dataInp[oddity] += ji;
}
else
{
*(dataOut[oddity]) = dotProd_f ( filt->f_anal[oddity], dataInp[oddity], filt->len_anal[oddity] );
XYN_DPRINTF(DEBUG_DETAIL, "%4ld=%5.2f(%d) ", i, *dataOut[oddity], oddity);
if (i%10==9) XYN_DPRINTF(DEBUG_DETAIL, "\n");
dataInp[oddity] += ji;
}
dataOut[oddity] += jo; // output pointer advance
}
return OK;
}
/* floating point transform - normal filter, input (1) -> output (2 arrays)
* set starting positions, boundary lengths, etc.
* run transform on lower boundary - use folded filters
* (set fold position)
* run transform normally - keep changing oddity (from start-> high to low to high...)
* output[low/high] = filter[low/high] * data
* run transform on upper
*
* Assumptions: filter length (and hence, folded boundaries of the filter) should be shorter than any data buffer
*
* jumpInp (ji) is the input jump:
* - for packed transform (llll...hhhh... -> d d d d d...)
* - for inPlace transform (lhlhlhlh... -> d d d d d...)
* jumpOut (jo) is the output jump - should be 1.
*
* lower boundary depends on oddity (0 oddity for starting lowpass)
* upper boundary depends on end_oddity
* 0 for ending lowpss {oddity=0 len odd, or oddity=1 len even}
* 1 for ending highpass {oddity=1 len odd, or oddity=0 len even}
* i.e. end_oddity = (oddity & len%2 ) | (1-oddity)&(1-len%2)
* algorithm:
* calc lowpass (with movement by oddity)
* add highpass
* */
static int wavInvTransformHiLow_f( float* dataInp[2], int ji, float* dataOut, int jo,
long len, bool oddity, waveFilterPtr filt )
{
/* check input integrity */
assert(dataInp[0]!=NULL); assert(dataInp[1]!=NULL); assert(dataOut!=NULL); assert(filt!=NULL);
if ( len<XYN_MAX(filt->len_synt[0], filt->len_synt[1] ) )
ErrMsg("data no long enough for filter\n");
bool od0=oddity, od1=1-oddity;
// bool end_oddity = (od0 * len%2 ) | ( od1*(1 - len%2) );
/* start points, lengths. low/high * even(zero)/odd
* e.g. 9-7 inverse is 7-9 (l-h) ->
* positions are ordinary +-3/+-4 ->
* polyphase [-2 0 2 / -3 -1 1 3]/[-4 -2 0 2 4 / -3 -1 1 3]
* into [-1 0 1 / -2 -1 0 1 +od] / [-2 -1 0 1 2 / -2 -1 0 1 +od]
* lengths: (3/4) (low e/o) / (5/4) (high e/o)
* positions: (1/-2 (+oddity) ) / (2/-1 (-oddity) ) */
int strt[2][2] = { { filt->pos_synt[0]%2, (filt->pos_synt[0]-1)%2 } , { (filt->pos_synt[1])%2, (filt->pos_synt[1]+1)%2 } };
int lens[2][2] = { { filt->len_synt[0]/2, (filt->len_synt[0]+1)/2 } , { (filt->len_synt[1]+1)/2, (filt->len_synt[1]+1)/2 } };
XYN_DPRINTF(DEBUG_HEADER,"%s: (len %d). oddity %d. inp/out Jumps %d/%d\ndata input: %p/%p, output: %p/%p\n",
__FUNCTION__, len, oddity, ji, jo, dataInp[0], dataInp[1], dataOut[0], dataOut[1] );
XYN_DPRINTF(DEBUG_HEADER,"lengths (l_even/l_odd %d/%d) (h_even/h_odd %d/%d)\npositions (l_even/l_odd %d/%d) (h_even/h_odd %d/%d)\n",
lens[0][0], lens[0][1], lens[1][0], lens[1][1], strt[0][0], strt[0][1], strt[1][0], strt[1][1] );
for (long i=0; i<len; i++, od0=od1, od1=1-od1) // i from 1 - this is second pass type
{
/* start lowpass */
if (i < filt->pos_synt[0]) // inside lower boundary - call folded filter
{
XYN_DPRINTF(DEBUG_DETAIL, "fold %d (%d): \t", i, od0);
//displayArrayJump_f ( filt->f_synt_folded[0][i]+oddity, 2, (filt->len_synt_folded[0][i]+1)/2, stdout);
//displayArrayJump_f ( dataInp[0], 1, (filt->len_synt_folded[0][i]+1)/2, stdout);
*dataOut = dotProdJump_f ( filt->f_synt_folded[0][i]+oddity, 2, dataInp[0], 1, (filt->len_synt_folded[0][i]+1)/2 );
}
else if ( i >= len-filt->pos_synt[0] ) // inside upper boundary - call upper filter
{
int cFold = filt->len_synt[0] - (len-i); // calculate current fold (od0 may cancel dependence on lower fold)
bool end_oddity = ( ((i-filt->pos_synt[0])%2) != oddity );
float* lowFold = filt->f_synt_folded[0][cFold] ;
XYN_DPRINTF(DEBUG_DETAIL, "low fold %d (i=%d, end-oddity %d): \n", cFold, i, end_oddity);
*dataOut = dotProdJump_f ( lowFold+end_oddity, 2, dataInp[0], 1, (filt->len_synt_folded[0][cFold]+1)/2 );
if (od0==1) dataInp[0] += ji;
}
else
{
*dataOut = dotProdJump_f ( filt->f_synt[0]+strt[0][od0], 2, dataInp[0], 1, lens[0][od0] );
//DPRINTF(DEBUG_DETAIL, "%4ld=%7.2f(%d+%d)", i, *dataOut, od0, strt[0][od0]);
if (od0==1) dataInp[0] += ji;
}
//DPRINTF(DEBUG_DETAIL, "%4ld=%7.2f(%d) ", i, *dataOut[od1], od1);
/* add highpass */
if (i < filt->pos_synt[1]) // inside lower boundary - call folded filter
{
XYN_DPRINTF(DEBUG_DETAIL, "fold %d (%d): \t", i, od1);
//displayArrayJump_f ( filt->f_synt_folded[1][i]+1-oddity, 2, (filt->len_synt_folded[1][i]+1)/2, stdout);
//displayArrayJump_f ( dataInp[1], 1, (filt->len_synt_folded[1][i]+1)/2, stdout);
*dataOut += dotProdJump_f ( filt->f_synt_folded[1][i]+1-oddity, 2, dataInp[1], 1, (filt->len_synt_folded[1][i]+1)/2 );
}
else if ( i >= len-filt->pos_synt[1] ) // inside upper boundary - call upper filter
{
int cFold = filt->len_synt[1] - (len-i); // calculate current fold (od1 may cancel dependence on lower fold)
bool end_oddity = ( ( (i-filt->pos_synt[1])%2) == oddity );
float* highFold = filt->f_synt_folded[1][cFold];
XYN_DPRINTF(DEBUG_DETAIL, "\nhigh fold %d (i=%d, end-oddity %d): \n", cFold, i, 1-end_oddity);
displayArrayJump_f ( highFold+end_oddity, 2, (filt->len_synt_folded[1][cFold]+1)/2, stdout);
displayArrayJump_f ( dataInp[1], 1, (filt->len_synt_folded[1][cFold]+1)/2, stdout);
*dataOut += dotProdJump_f ( highFold+end_oddity, 2, dataInp[1], 1, (filt->len_synt_folded[1][cFold]+1)/2 );
if (od1==0) dataInp[1] += ji;
}
else
{
*dataOut += dotProdJump_f ( filt->f_synt[1]+strt[1][od1], 2, dataInp[1], 1, lens[1][od1] );
//DPRINTF(DEBUG_DETAIL, "+(%d+%d)=%7.2f ", od1, strt[1][od1], *dataOut); if (i%5==4) DPRINTF(DEBUG_DETAIL,"\n");
if (od1==0) dataInp[1] += ji;
}
dataOut++; // output pointer advance
}
return OK;
}
/* normal DWT filter transforms and inverse transforms - integer versions */
int wavTransform_i( int* dataInp, int* dataOut, long len, bool oddity, waveFilterPtr filt )
{
assert(dataInp!=NULL); assert(filt!=NULL); assert(dataOut!=NULL); assert(len>=0);
int start[2] = { filt->pos_anal[0]+oddity, filt->pos_anal[1]+1-oddity };
long startInp = XYN_MIN(start[0], start[1]);
long endInp = len - startInp;
int* cpyInp = dataInp;
int* cpyOut[2] = { dataOut, dataOut + len/2 + oddity*(len%2) };
XYN_DPRINTF(DEBUG_HEADER, "%s %s: len %d, startLow %d startHigh %d oddity %d\n",
__FUNCTION__, filt->name, len, start[0], start[1], oddity);
cpyOut[0] += start[0];
cpyOut[1] += start[1];
for ( int i=startInp; i<endInp; i++)
{
*cpyOut[oddity]++ = dotProd_i ( filt->i_anal[oddity], cpyInp++, filt->len_anal[oddity] );
oddity = 1-oddity;
}
return OK;
}
int wavInvTransform_i( int* dataInp, int* dataOut, long len, bool oddity, waveFilterPtr filt )
{
int start = XYN_MAX( filt->pos_synt[0]+oddity, filt->pos_synt[1]+1-oddity);
int end = len - start;
int* cpyInp = dataInp;
int* cpyOut = dataOut;
XYN_DPRINTF(DEBUG_HEADER, "%s %s: len %d, start %d oddity %d\n",
__FUNCTION__, filt->name, len, start, oddity);
cpyOut += start;
for ( int i=start; i<end; i++)
{
*cpyOut++ = dotProd_i ( filt->i_synt[oddity], cpyInp++, filt->len_synt[oddity] );
oddity = 1-oddity;
}
return OK;
}
/** DWT inplace **/
/* in place transform */
int wavTransformInPlace_i( int* data, long len, bool oddity, waveFilterPtr filt )
{
int start[2] = { filt->pos_anal[0]+oddity, filt->pos_anal[1]+1-oddity };
long startInp = XYN_MIN(start[0], start[1]);
long endInp = len - startInp;
int* cpyInp = data;
int* cpyOut[2] = { data, data+ len/2 + oddity*(len%2) };
cpyOut[0] += start[0];
cpyOut[1] += start[1];
for ( int i=startInp; i<endInp; i++)
{
*cpyOut[oddity]++ = dotProd_i ( filt->i_anal[oddity], cpyInp++, filt->len_anal[oddity] );
oddity = 1-oddity;
}
return OK;
}
int wavInvTransformInPlace_i( int* data, long len, bool oddity, waveFilterPtr filt )
{
int start = XYN_MAX( filt->pos_synt[0]+oddity, filt->pos_synt[1]+1-oddity);
int end = len - start;
int* cpyInp = data;
int* cpyOut = data;
cpyOut += start;
for ( int i=start; i<end; i++, oddity = 1-oddity)
*cpyOut++ = dotProd_i ( filt->i_synt[oddity], cpyInp++, filt->len_synt[oddity] );
return OK;
}
/*** lifting implementation -
* (predict high from low, update low from high) X num_of_predictions
* scale (optional)
* pack (optional)
* */
int liftTransform_d( double* data, long len, bool oddity, bool inPlace, bool noRescale, waveLiftPtr filt )
{
int sclEnd = SCALE_END(len, oddity);
int wavEnd = 1-sclEnd;
XYN_DPRINTF(DEBUG_HEADER, "%s (%p, len %ld, oddity %d, %s) (end: %d %d)\n", __FUNCTION__, data, len, oddity, filt->name, sclEnd, wavEnd);
for (int k=0; k<filt->nPredict; k++)
{
// predict (high from low)
XYN_DPRINTF(DEBUG_COEFF,"predict: \t");
wavLiftingStageInPlace_d ( data, len, oddity, filt->coeff_f[0][k] );
// update (low from high)
XYN_DPRINTF(DEBUG_COEFF,"update: \t");
wavLiftingStageInPlace_d ( data, len, 1-oddity, filt->coeff_f[1][k] );
}
if ( !noRescale )
{
XYN_DPRINTF(DEBUG_PROCESS, "%s rescaling low by %f high by %f\n", __FUNCTION__, filt->scaler_f[1], filt->scaler_f[0] );
mulVectScalJump_d ( data+oddity, filt->scaler_f[0], len-oddity , 2 );
mulVectScalJump_d ( data+1-oddity, filt->scaler_f[1], len-1+oddity , 2 );
}
// packing
if ( !inPlace )
{
XYN_DPRINTF(DEBUG_PROCESS, "%s packing\n", __FUNCTION__ );
wavPack1d_d ( data, len, oddity );
}
return OK;
}
int liftTransform_f( float* data, long len, bool oddity, bool inPlace, bool noRescale, waveLiftPtr filt )
{
int sclEnd = SCALE_END(len, oddity);
int wavEnd = 1-sclEnd;
XYN_DPRINTF(DEBUG_HEADER, "%s (%p, len %ld, oddity %d, %s) (end: %d %d)\n",
__FUNCTION__, data, len, oddity, filt->name, sclEnd, wavEnd);
for (int k=0; k<filt->nPredict; k++)
{
// predict (high from low)
XYN_DPRINTF(DEBUG_COEFF,"predict: \t");
wavLiftingStageInPlace_f ( data, len, oddity, filt->coeff_f[0][k] );
// update (low from high)
XYN_DPRINTF(DEBUG_COEFF,"update: \t");
wavLiftingStageInPlace_f ( data, len, 1-oddity, filt->coeff_f[1][k] );
}
if ( !noRescale )
{
XYN_DPRINTF(DEBUG_PROCESS, "%s rescaling low by %f high by %f\n", __FUNCTION__, filt->scaler_f[1], filt->scaler_f[0] );
mulVectScalJump_f ( data+oddity, filt->scaler_f[0], len-oddity , 2 );
mulVectScalJump_f ( data+1-oddity, filt->scaler_f[1], len-1+oddity , 2 );
}
// packing
if ( !inPlace )
{
XYN_DPRINTF(DEBUG_PROCESS, "%s packing\n", __FUNCTION__ );
wavPack1d_f ( data, len, oddity );
}
return OK;
}
int liftInvTransform_f( float* data, long len, bool oddity, bool inPlace, bool noRescale, waveLiftPtr filt )
{
int sclEnd = SCALE_END(len, oddity);
int wavEnd = 1-sclEnd;
XYN_DPRINTF(DEBUG_HEADER, "%s (%p, len %ld, oddity %d, %s) end: (%d %d)\n",
__FUNCTION__, data, len, oddity, filt->name, sclEnd, wavEnd);
// unPack
if ( !inPlace )
{
XYN_DPRINTF(DEBUG_PROCESS, "%s unPacking n", __FUNCTION__ );
wavUnpack1d_f ( data, len, oddity );
}
if ( !noRescale )
{
XYN_DPRINTF(DEBUG_PROCESS, "%s rescaling low by %f high by %f\n", __FUNCTION__, filt->scaler_f[1], filt->scaler_f[0] );
mulVectScalJump_f ( data+oddity, filt->scaler_f[1], len , 2 ); // scalers are inverse!!!
mulVectScalJump_f ( data+1-oddity, filt->scaler_f[0], len , 2 );
}
for (int k=filt->nPredict-1; k>=0; k--)
{
XYN_DPRINTF(DEBUG_PROCESS, "%s inverse lifting run %d\n", __FUNCTION__, k );
// de-update (low from high)
XYN_DPRINTF(DEBUG_COEFF,"predict: \t");
wavLiftingStageInPlace_f ( data, len, 1-oddity, -1 * filt->coeff_f[1][k] );
// de-predict (high from low)
XYN_DPRINTF(DEBUG_COEFF,"predict: \t");
wavLiftingStageInPlace_f ( data, len, oddity, -1 * filt->coeff_f[0][k] );
}
return OK;
}
/* single lifting stage -
* if start, first element uses symmetric boundary. */
static int wavLiftingStageInPlace_d ( double* data, long len, int start, register double a )
{
XYN_DPRINTF(DEBUG_HEADER, "%s (%p, %ld, start? %d) coefficient %f\n",
__FUNCTION__, data, len, start, a );
if (start) // do first position d[0] is relevant pass-band, d[0]+= a(d[-1]+d[1])
{
*data += a*2*data[1];
XYN_DPRINTF(DEBUG_DETAIL,"%p: %8.4f - start\n", data, *data);
data+= 2;
len-=2;
}
else // first position is lowpass, jump over it.
{
data++;
len--;
}
while( len > 1 )
{
*data += a*(data[-1]+data[1]);
XYN_DPRINTF(DEBUG_DETAIL,"%p: %8.4f (%ld)\n", data, *data, len);
data += 2;
len -=2;
}
if ( len ) // last position - len was decremented by 2, so if it is ledt 1, do last dot.
{
*data += a*2*data[-1];
XYN_DPRINTF(DEBUG_DETAIL,"%p: %8.4f - end\n", data, *data);
}
return OK;
}
static int wavLiftingStageInPlace_f ( float* data, long len, int start, register float a )
{
XYN_DPRINTF(DEBUG_HEADER, "%s (%p, %ld, start? %d) coefficient %f\n",
__FUNCTION__, data, len, start, a );
if (start) // do first position d[0] is relevant pass-band, d[0]+= a(d[-1]+d[1])
{
*data += a*2*data[1];
XYN_DPRINTF(DEBUG_DETAIL,"%p: %8.4f - start\n", data, *data);
data+= 2;
len-=2;
}
else // first position is lowpass, jump over it.
{
data++;
len--;
}
while( len > 1 )
{
*data += a*(data[-1]+data[1]);
XYN_DPRINTF(DEBUG_DETAIL,"%p: %8.4f (%ld)\n", data, *data, len);
data += 2;
len -=2;
}
if ( len ) // last position - len was decremented by 2, so if it is ledt 1, do last dot.
{
*data += a*2*data[-1];
XYN_DPRINTF(DEBUG_DETAIL,"%p: %8.4f - end\n", data, *data);
}
return OK;
}
/* integer lifting transforms */
int liftTransform_i( int* data, long len, bool oddity, bool inPlace, bool noRescale, waveLiftPtr filt )
{
int sclEnd = SCALE_END(len, oddity);
int wavEnd = 1-sclEnd;
XYN_DPRINTF(DEBUG_HEADER, "%s (%p, len %ld, oddity %d, %s) end: (%d %d)\n",
__FUNCTION__, data, len, oddity, filt->name, sclEnd, wavEnd);
for (int k=0; k<filt->nPredict; k++)
{
// predict (high from low)
XYN_DPRINTF(DEBUG_COEFF,"predict: \t");
wavLiftingStageInPlace_i ( data, len, oddity, filt->coeff_i[0][k] );
// update (low from high)
XYN_DPRINTF(DEBUG_COEFF,"predict: \t");
wavLiftingStageInPlace_i ( data, len, 1-oddity, filt->coeff_i[1][k] );
}
// packing
if ( !inPlace )
wavPack1d_i ( data, len, oddity );
return OK;
}
int liftInvTransform_i( int* data, long len, bool oddity, bool inPlace, bool noRescale, waveLiftPtr filt )
{
int sclEnd = SCALE_END(len, oddity);
int wavEnd = 1-sclEnd;
XYN_DPRINTF(DEBUG_HEADER, "%s (%p, len %ld, oddity %d, %s) end: (%d %d)\n",
__FUNCTION__, data, len, oddity, filt->name, sclEnd, wavEnd);
// unPack
if ( !inPlace )
wavUnpack1d_i ( data, len, oddity );
for (int k=filt->nPredict-1; k>=0; k--)
{
XYN_DPRINTF(DEBUG_PROCESS, "%s inverse lifting run %d\n", __FUNCTION__, k );
// de-update (low from high)
XYN_DPRINTF(DEBUG_COEFF,"predict: \t");
wavLiftingStageInPlace_i ( data, len, 1-oddity, -1 * filt->coeff_i[1][k] );
// de-predict (high from low)
XYN_DPRINTF(DEBUG_COEFF,"predict: \t");
wavLiftingStageInPlace_i ( data, len, oddity, -1 * filt->coeff_i[0][k] );
}
return OK;
}
static int wavLiftingStageInPlace_i ( int* data, long len, int start, register int a )
{
XYN_DPRINTF(DEBUG_HEADER, "%s (%p, %ld, start? %d) coefficient %d\n",
__FUNCTION__, data, len, start, a );
if (start) // first position d[0] is highpass, d[0]+= a(d[-1]+d[1])
{
*data += a*2*data[1] / FixWaveScale;
XYN_DPRINTF(DEBUG_DETAIL,"%p: %8.4d - start\n", data, *data);
data+= 2;
len-=2;
}
else // first position is lowpass, jump over it.
{
data++;
len--;
}
while( len > 1 )
{
*data += a*(data[-1]+data[1]) / FixWaveScale;
XYN_DPRINTF(DEBUG_DETAIL,"%p: %8d (%ld)\n", data, *data, len);
data += 2;
len -=2;
}
if ( len ) // last position - len was decremented by 2, so if it is ledt 1, do last dot.
{
*data += a*2*data[-1] / FixWaveScale;
XYN_DPRINTF(DEBUG_DETAIL,"%p: %8d - end\n", data, *data);
}
return OK;
}
/* rescale */
int rescaleVect_f( float* data, long len )
{
while (len-- > 0)
*data++ /= FixWaveScale;
return OK;
}
int upScaleVect_i( int* data, long len )
{
while (len-- > 0)
{
*data = (*data) << FixWaveShift;
data ++;
}
return OK;
}
int downScaleVect_i( int* data, long len )
{
while (len-- > 0)
{
*data = (*data) >> FixWaveShift;
data ++;
}
return OK;