File: wavelets.c

package info (click to toggle)
python-ltfatpy 1.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 41,412 kB
  • sloc: ansic: 8,546; python: 6,470; makefile: 15
file content (762 lines) | stat: -rw-r--r-- 24,306 bytes parent folder | download | duplicates (6)
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
/* NOT PROCESSED DIRECTLY, see ltfat_complexindependent.c */
#ifdef LTFAT_TYPE
#include "ltfat.h"
#include "ltfat_types.h"


LTFAT_EXTERN void
LTFAT_NAME(atrousfilterbank_td)(const LTFAT_TYPE *f, const LTFAT_TYPE *g[],
                                const ltfatInt L, const ltfatInt gl[],
                                const ltfatInt W, const ltfatInt a[],
                                const ltfatInt skip[], const ltfatInt M,
                                LTFAT_TYPE *c, ltfatExtType ext)
{
    for(ltfatInt m=0; m<M; m++)
    {
        for(ltfatInt w=0; w<W; w++)
        {
            LTFAT_NAME(atrousconvsub_td)(f+w*L, g[m], L, gl[m], a[m],
                                   skip[m],c + w*M*L + m*L, ext);
        }
    }
}

LTFAT_EXTERN void
LTFAT_NAME(iatrousfilterbank_td)(const LTFAT_TYPE *c, const LTFAT_TYPE *g[],
                                 const ltfatInt L, const ltfatInt gl[],
                                 const ltfatInt W, const ltfatInt a[],
                                 const ltfatInt skip[], const ltfatInt M,
                                 LTFAT_TYPE *f, ltfatExtType ext)
{
   // Set output array to zeros, since the array is used as an accumulator
    memset(f,0,L*W*sizeof*f);

    for(ltfatInt m=0; m<M; m++)
    {
        for(ltfatInt w=0; w<W; w++)
        {
            LTFAT_NAME(atrousupconv_td)(c + w*M*L + m*L, g[m], L, gl[m], a[m],
                                  skip[m],f+w*L, ext);
        }
    }

}


LTFAT_EXTERN void
LTFAT_NAME(filterbank_td)(const LTFAT_TYPE *f, const LTFAT_TYPE *g[],
                          const ltfatInt L, const ltfatInt gl[],
                          const ltfatInt W, const ltfatInt a[],
                          const ltfatInt skip[], const ltfatInt M,
                          LTFAT_TYPE *c[], ltfatExtType ext)
{
    for(ltfatInt m=0; m<M; m++)
    {
        const ltfatInt N = filterbank_td_size(L,a[m],gl[m],skip[m],ext);
        for(ltfatInt w=0; w<W; w++)
        {

            LTFAT_NAME(convsub_td)(f+w*L, g[m], L, gl[m], a[m],
                                   skip[m],c[m]+w*N, ext);
        }
    }
}


LTFAT_EXTERN void
LTFAT_NAME(ifilterbank_td)(const LTFAT_TYPE *c[], const LTFAT_TYPE *g[],
                           const ltfatInt L, const ltfatInt gl[],
                           const ltfatInt W, const ltfatInt a[],
                           const ltfatInt skip[], const ltfatInt M,
                           LTFAT_TYPE *f, ltfatExtType ext)
{
    memset(f,0,L*W*sizeof*f);

    for(ltfatInt m=0; m<M; m++)
    {
        const ltfatInt N = filterbank_td_size(L,a[m],gl[m],skip[m],ext);
        for(ltfatInt w=0; w<W; w++)
        {

            LTFAT_NAME(upconv_td)(c[m]+w*N, g[m], L, gl[m], a[m],
                                  skip[m],f+w*L, ext);
        }
    }

}


LTFAT_EXTERN void
LTFAT_NAME(atrousconvsub_td)(const LTFAT_TYPE *f, const LTFAT_TYPE *g,
                             const ltfatInt L, const ltfatInt gl, const ltfatInt ga,
                             ltfatInt skip, LTFAT_TYPE *c, ltfatExtType ext)
{
    memset(c,0,L*sizeof*c);
    ltfatInt skipLoc = -skip;
    LTFAT_TYPE *filtRev = ltfat_malloc(gl*sizeof*filtRev);
    LTFAT_NAME(reverse_array)((LTFAT_TYPE*)g,filtRev,gl);

    ltfatInt glUps = ga*gl-(ga-1);

    LTFAT_TYPE *righExtbuff = 0;
    // number of output samples that can be calculated "painlessly"
    ltfatInt Nsafe = imax((L - skipLoc),0);

    // prepare cyclic buf of length of power of two (for effective modulo operations)
    ltfatInt bufgl = nextPow2(glUps);
    // buf index
    ltfatInt buffPtr = 0;

    // allocating and initializing the cyclic buf
    LTFAT_TYPE *buf = ltfat_calloc(bufgl,sizeof*buf);

    // pointer for moving in the input data
    const LTFAT_TYPE *tmpIn = f;
    LTFAT_TYPE *tmpOut = c;
    LTFAT_TYPE *tmpg = filtRev;
    LTFAT_TYPE *tmpBuffPtr = buf;

    // fill buf with the initial values from the input signal according to the boundary treatment
    // last glUps buf samples are filled to keep buffPtr=0
    LTFAT_NAME(extend_left)(f,L,buf,bufgl,glUps,ext,1);

    if(Nsafe<L)
    {
        // right extension is necessary, additional buf from where to copy
        righExtbuff = ltfat_malloc(bufgl*sizeof(LTFAT_TYPE));
        memset(righExtbuff,0,bufgl*sizeof(LTFAT_TYPE));
        // store extension in the buf (must be done now to avoid errors when inplace calculation is done)
        LTFAT_NAME(extend_right)(f,L,righExtbuff,glUps,ext,1);
    }

#define ONEOUTSAMPLE                                                    \
         tmpg = filtRev;                                           \
         ltfatInt revBufPtr = modPow2(buffPtr-glUps,bufgl);             \
         ltfatInt loop1it = gl+1;                                         \
	      while(--loop1it)                                              \
	      {                                                             \
		     tmpBuffPtr = buf + modPow2(revBufPtr,bufgl);          \
		     revBufPtr+=ga;                                         \
           *tmpOut += *(tmpBuffPtr) * *(tmpg++);                  \
         }                                                             \
         tmpOut++;


#define READNEXTDATA(samples,wherePtr)                                              \
	   buffOver = imax(buffPtr+(samples)-bufgl, 0);                               \
   	memcpy(buf + buffPtr, wherePtr, ((samples)-buffOver)*sizeof(LTFAT_TYPE)); \
	   memcpy(buf,wherePtr+(samples)-buffOver,buffOver*sizeof(LTFAT_TYPE));      \
	   buffPtr = modPow2(buffPtr += (samples),bufgl);

#define READNEXTSAMPLE(wherePtr)                               \
   	   *(buf + buffPtr) = *wherePtr;                        \
	   buffPtr = modPow2(++buffPtr,bufgl);


    ltfatInt buffOver = 0;
    /*** initial buf fill ***/
    ltfatInt sampToRead = imin((skipLoc+1),L);
    READNEXTDATA(sampToRead,tmpIn);
    tmpIn += sampToRead;

    /*********** STEP 1: FREE LUNCH ( but also a hot-spot) *******************************/
    // Take the smaller value from "painless" output length and the user defined output length
    ltfatInt iiLoops = imin(Nsafe-1,L-1);

    // loop trough all output samples, omit the very last one.
    for (ltfatInt ii = 0; ii < iiLoops; ii++)
    {
        ONEOUTSAMPLE
        READNEXTSAMPLE(tmpIn)
        tmpIn++;
    }

    /*********** STEP 2: FINALIZE FREE LUNCH ************************************/
    if(Nsafe>0)
    {
        ONEOUTSAMPLE
    }
    /*********** STEP 3: NOW FOR THE TRICKY PART ************************************/
    if(Nsafe<L)
    {
        /************ STEP 3a: DEALING WITH THE REMAINING SAMPLES ******************/
        // CAREFULL NOW! possibly stepping outside of input signal
        // last index in the input signal for which reading next a samples reaches outside of the input signal
        ltfatInt rightExtBuffIdx = 0;
        if(Nsafe>0)
        {
            ltfatInt lastInIdx = ((Nsafe-1)+1+skipLoc);
            rightExtBuffIdx = lastInIdx + 1 - L;
            ltfatInt diff = imax(0,L - lastInIdx);
            READNEXTDATA(diff,(f + lastInIdx))
        }
        else
        {
            rightExtBuffIdx = 1+skipLoc - L;
        }

        // now copying samples that are outside
        READNEXTDATA(rightExtBuffIdx,righExtbuff)

        /************ STEP 3b: ALL OK, proceed reading input values from righExtbuff ******************/
        // loop for the remaining output samples
        for(ltfatInt ii=0; ii<L-Nsafe; ii++)
        {
            ONEOUTSAMPLE
            READNEXTSAMPLE((righExtbuff+rightExtBuffIdx))
            ++rightExtBuffIdx;
            //rightExtBuffIdx = modPow2(++rightExtBuffIdx,bufgl);
        }
    }


#undef READNEXTDATA
#undef READNEXTSAMPLE
#undef ONEOUTSAMPLE
    LTFAT_SAFEFREEALL(buf,filtRev,righExtbuff);
}

LTFAT_EXTERN void
LTFAT_NAME(atrousupconv_td)(const LTFAT_TYPE *c, const LTFAT_TYPE *g,
                            const ltfatInt L, const ltfatInt gl,
                            const ltfatInt ga, const ltfatInt skip,
                            LTFAT_TYPE *f, ltfatExtType ext)
{
    ltfatInt glUps = ga*gl-(ga-1);
    ltfatInt skipLoc = -(1-glUps-skip);

    // Copy, reverse and conjugate the imp resp.
    LTFAT_TYPE *gInv = ltfat_malloc(gl*sizeof*gInv);
    memcpy(gInv,g,gl*sizeof*gInv);
    LTFAT_NAME(reverse_array)(gInv,gInv,gl);
    LTFAT_NAME(conjugate_array)(gInv,gInv,gl);


    // Running output pointer
    LTFAT_TYPE* tmpOut = f;
    // Running input pointer
    LTFAT_TYPE* tmpIn =  (LTFAT_TYPE*) c;

    /** prepare cyclic buf */
    ltfatInt bufgl = nextPow2(glUps);
    LTFAT_TYPE* buf = ltfat_calloc(bufgl,sizeof*buf);
    ltfatInt buffPtr = 0;

    ltfatInt iiLoops = 0;
    ltfatInt remainsOutSamp = L;
    ltfatInt rightBuffPreLoad = 0;

    if(skipLoc >= L)
    {
        rightBuffPreLoad = (skipLoc + 1) - L;
        skipLoc = L;
    }
    else
    {
        iiLoops = imin(L - skipLoc,L); // just in case L < L - inSkip
        remainsOutSamp = L - (iiLoops-1);
    }

    LTFAT_TYPE *rightbuf = ltfat_calloc(bufgl,sizeof*rightbuf);
    LTFAT_TYPE *rightbufTmp = rightbuf;

    if(ext==PER) // if periodic extension
    {
        LTFAT_NAME(extend_left)(c,L,buf,bufgl,glUps,PER,0); // extension as a last (tmpgl-1) samples of the buf -> pointer dont have to be moved
        LTFAT_NAME(extend_right)(c,L,rightbuf,glUps,PER,0);
    }

    ltfatInt iniStoCopy = imin(skipLoc,bufgl);
    ltfatInt tmpInSkip = imax(0,skipLoc-bufgl);
    memcpy(buf,tmpIn+tmpInSkip,iniStoCopy*sizeof*buf);
    tmpIn += (iniStoCopy+tmpInSkip);
    buffPtr = modPow2(buffPtr += iniStoCopy,bufgl);


//LTFAT_TYPE* filtTmp = g;
#define ONEOUTSAMPLE(filtTmp,jjLoops)                                   \
	    for(ltfatInt jj=0;jj<(jjLoops);jj++)                                  \
		    {                                                            \
				ltfatInt idx = modPow2((-jj*ga+buffPtr-1), bufgl);      \
				*tmpOut += *(buf+idx) * *((filtTmp) + jj);            \
		    }                                                            \
	    tmpOut++;

#define READNEXTSAMPLE(wherePtr)                               \
   	   *(buf + buffPtr) = *(wherePtr);                      \
	   buffPtr = modPow2(++buffPtr,bufgl);


    /** STEP 2: MAIN LOOP */
    if(iiLoops>0)
    {
        for(ltfatInt ii=0; ii<iiLoops-1; ii++)
        {
            READNEXTSAMPLE(tmpIn)
            tmpIn++;
            ONEOUTSAMPLE(gInv,gl)
        }
        READNEXTSAMPLE(tmpIn)
        //tmpIn++;
    }


    /** STEP 3b: load samples from right buf */
    while(rightBuffPreLoad--)
    {
        READNEXTSAMPLE((rightbufTmp))
        rightbufTmp++;
    }


    /*
    STEP 3b: calculate remaining output samples,
    Again, there can be shift/up misaligment thne shift>L
    */

    for(ltfatInt ii=0; ii<remainsOutSamp; ii++)
    {
        if(ii!=0)
        {
            READNEXTSAMPLE((rightbufTmp))
            rightbufTmp++;
        }
        ONEOUTSAMPLE((gInv),(gl))
    }

#undef READNEXTDATA
#undef ONEOUTSAMPLE
    LTFAT_SAFEFREEALL(buf,rightbuf,gInv);
}


LTFAT_EXTERN void
LTFAT_NAME(convsub_td)(const LTFAT_TYPE *f, const LTFAT_TYPE *g, const ltfatInt L,
                       const ltfatInt gl, const ltfatInt a, const ltfatInt skip,
                       LTFAT_TYPE *c, ltfatExtType ext)
{
    const ltfatInt N = filterbank_td_size(L,a,gl,skip,ext);
    // Since c is used as an accu
    memset(c,0,N*sizeof*c);
    // Reverse and conjugate the filter
    LTFAT_TYPE *filtRev = ltfat_malloc(gl*sizeof*filtRev);
    LTFAT_NAME(reverse_array)((LTFAT_TYPE*)g, filtRev,gl);

    LTFAT_TYPE *righExtbuff = 0;
    // number of output samples that can be calculated "painlessly"
    ltfatInt Nsafe = imax((L + skip + a -1)/a,0);

    // prepare cyclic buf of length of power of two (for effective modulo operations)
    ltfatInt bufgl = nextPow2(imax(gl,a+1));
    // buf index
    ltfatInt buffPtr = 0;

    // allocating and initializing the cyclic buf
    LTFAT_TYPE *buf = ltfat_calloc(bufgl,sizeof*buf);

    // pointer for moving in the input data
    const LTFAT_TYPE * tmpIn = f;
    LTFAT_TYPE * tmpOut = c;
    LTFAT_TYPE *tmpg = filtRev;
    LTFAT_TYPE *tmpBuffPtr = buf;

    // fill buf with the initial values from the input signal according to the boundary treatment
    // last glUps buf samples are filled to keep buffPtr=0
    LTFAT_NAME(extend_left)(f,L,buf,bufgl,gl,ext,a);

    if(Nsafe<N)
    {
        // right extension is necessary, additional buf from where to copy
        righExtbuff = ltfat_calloc(bufgl,sizeof*righExtbuff);
        // store extension in the buf (must be done now to avoid errors when inplace calculation is done)
        LTFAT_NAME(extend_right)(f,L,righExtbuff,gl,ext,a);
    }

#define ONEOUTSAMPLE                                                    \
          tmpg = filtRev;                                           \
          ltfatInt revBufPtr = modPow2(buffPtr-gl,bufgl);                \
          ltfatInt loop1it = gl+1;                                         \
	      while(--loop1it)                                              \
	      {                                                             \
		     tmpBuffPtr = buf + modPow2(revBufPtr++,bufgl);        \
             *tmpOut += *(tmpBuffPtr) * *(tmpg++);                  \
          }                                                             \
          tmpOut++;



#define READNEXTDATA(samples,wherePtr)                                              \
	   buffOver = imax(buffPtr+(samples)-bufgl, 0);                               \
   	memcpy(buf + buffPtr, wherePtr, ((samples)-buffOver)*sizeof*buf); \
	   memcpy(buf,wherePtr+(samples)-buffOver,buffOver*sizeof*buf);      \
	   buffPtr = modPow2(buffPtr += (samples),bufgl);


    ltfatInt buffOver = 0;
    /*** initial buf fill ***/
    ltfatInt sampToRead = imin((-skip+1),L);
    READNEXTDATA(sampToRead,tmpIn);
    tmpIn += sampToRead;

    /*********** STEP 1: FREE LUNCH ( but also a hot-spot) *******************************/
    // Take the smaller value from "painless" output length and the user defined output length
    ltfatInt iiLoops = imin(Nsafe-1,N-1);

    // loop trough all output samples, omit the very last one.
    for (ltfatInt ii = 0; ii < iiLoops; ii++)
    {
        ONEOUTSAMPLE
        READNEXTDATA(a,tmpIn)
        tmpIn += a;
    }

    /*********** STEP 2: FINALIZE FREE LUNCH ************************************/
    if(Nsafe>0)
    {
        ONEOUTSAMPLE
    }
    /*********** STEP 3: NOW FOR THE TRICKY PART ************************************/
    if(Nsafe<N)
    {
        /************ STEP 3a: DEALING WITH THE REMAINING SAMPLES ******************/
        // CAREFULL NOW! possibly stepping outside of input signal
        // last index in the input signal for which reading next a samples reaches outside of the input signal
        ltfatInt rightExtBuffIdx = 0;
        if(Nsafe>0)
        {
            ltfatInt lastInIdx = (a*(Nsafe-1)+1-skip);
            rightExtBuffIdx = lastInIdx + a - L;
            ltfatInt diff = imax(0,L - lastInIdx);
            READNEXTDATA(diff,(f + lastInIdx))
        }
        else
        {
            rightExtBuffIdx = 1-skip - L;
        }

        // now copying samples that are outside
        READNEXTDATA(rightExtBuffIdx,righExtbuff)

        /************ STEP 3b: ALL OK, proceed reading input values from righExtbuff ******************/
        // loop for the remaining output samples
        for(ltfatInt ii=0; ii<N-Nsafe; ii++)
        {
            ONEOUTSAMPLE
            READNEXTDATA(a,(righExtbuff+rightExtBuffIdx))
            rightExtBuffIdx = modPow2(rightExtBuffIdx += a,bufgl);
        }
    }


#undef READNEXTDATA
#undef ONEOUTSAMPLE
    LTFAT_SAFEFREEALL(buf,filtRev,righExtbuff);
}


LTFAT_EXTERN void
LTFAT_NAME(upconv_td)(const LTFAT_TYPE *c, const LTFAT_TYPE *g, const ltfatInt L,
                      const ltfatInt gl, const ltfatInt a, const ltfatInt skip,
                      LTFAT_TYPE *f, ltfatExtType ext)
{
    const ltfatInt N = filterbank_td_size(L,a,gl,skip,ext);

    // Copy, reverse and conjugate the imp resp.
    LTFAT_TYPE *gInv = ltfat_malloc(gl*sizeof*gInv);
    memcpy(gInv,g,gl*sizeof*gInv);
    LTFAT_NAME(reverse_array)(gInv,gInv,gl);
    LTFAT_NAME(conjugate_array)(gInv,gInv,gl);
    ltfatInt skipRev = -(1 - gl - skip);

    // Running output pointer
    LTFAT_TYPE* tmpOut = f;
    // Running input pointer
    const LTFAT_TYPE* tmpIn =  c;

    /** prepare cyclic buf */
    ltfatInt bufgl = nextPow2(gl);
    LTFAT_TYPE* buf = ltfat_calloc(bufgl,sizeof*buf);
    ltfatInt buffPtr = 0;

    ltfatInt inSkip = (skipRev + a - 1)/a;
    ltfatInt skipModUp = skipRev%a;
    ltfatInt skipToNextUp = 0;
    if(skipModUp!=0)  skipToNextUp = a-skipModUp;
    ltfatInt outAlign = 0;

    ltfatInt iiLoops = 0;
    ltfatInt uuLoops = 0;
    ltfatInt remainsOutSamp = L;
    ltfatInt rightBuffPreLoad = 0;

    if(inSkip >= N)
    {
        inSkip = N;
        outAlign = skipModUp;
        rightBuffPreLoad = (skipRev + 1 + a - 1)/a - N;
    }
    else
    {
        uuLoops = skipToNextUp;
        iiLoops = imin(N - inSkip,(L-skipToNextUp + a -1)/a); // just in case L/a < N - inSkip
        remainsOutSamp = L - (uuLoops + (iiLoops-1)*a);
    }

    LTFAT_TYPE *rightbuf = ltfat_calloc(bufgl,sizeof(LTFAT_TYPE));
    LTFAT_TYPE *rightbufTmp = rightbuf;

    if(ext==PER) // if periodic extension
    {
        LTFAT_NAME(extend_left)(c,N,buf,bufgl,gl,PER,0); // extension as a last (tmpgl-1) samples of the buf -> pointer dont have to be moved
        LTFAT_NAME(extend_right)(c,N,rightbuf,gl,PER,0);
    }

    ltfatInt iniStoCopy = imin(inSkip,bufgl);
    ltfatInt tmpInSkip = imax(0,inSkip-bufgl);
    memcpy(buf,tmpIn+tmpInSkip,iniStoCopy*sizeof*buf);
    tmpIn += (iniStoCopy+tmpInSkip);
    buffPtr = modPow2(buffPtr += iniStoCopy,bufgl);



#define ONEOUTSAMPLE(filtTmp,jjLoops)                                   \
	    for(ltfatInt jj=0;jj<(jjLoops);jj++)                                  \
		    {                                                            \
				ltfatInt idx = modPow2((-jj+buffPtr-1), bufgl);             \
				*tmpOut += *(buf+idx) * *((filtTmp) +(jj*a));        \
		    }                                                            \
	    tmpOut++;

#define READNEXTSAMPLE(wherePtr)                               \
   	   *(buf + buffPtr) = *(wherePtr);                      \
	   buffPtr = modPow2(++buffPtr,bufgl);


    /** STEP 1: Deal with the shift - upsampling misaligment */
    for(ltfatInt uu=0; uu<uuLoops; uu++)
    {
        ONEOUTSAMPLE((gInv + skipModUp+uu),((gl-(skipModUp+uu)+a-1)/a))
    }

    /** STEP 2: MAIN LOOP */
    if(iiLoops>0)
    {
        for(ltfatInt ii=0; ii<iiLoops-1; ii++)
        {
            READNEXTSAMPLE(tmpIn)
            tmpIn++;
            for(ltfatInt uu=0; uu<a; uu++)
            {
                ONEOUTSAMPLE((gInv+uu),((gl-uu+a-1)/a))
            }
        }
        READNEXTSAMPLE(tmpIn)
        tmpIn++;
    }


    /** STEP 3b: load samples from right buf */
    while(rightBuffPreLoad--)
    {
        READNEXTSAMPLE((rightbufTmp))
        rightbufTmp++;
    }


    /*
    STEP 3b: calculate remaining output samples,
    Again, there can be shift/a misaligment thne shift>L
    */

    for(ltfatInt ii=outAlign; ii<remainsOutSamp+outAlign; ii++)
    {
        if(ii!=outAlign&&ii%a==0)
        {
            READNEXTSAMPLE((rightbufTmp))
            rightbufTmp++;
        }
        ONEOUTSAMPLE((gInv+ii%a),((gl-ii%a+a-1)/a))
    }

#undef ONEOUTSAMPLE
#undef READNEXTSAMPLE
    LTFAT_SAFEFREEALL(buf,rightbuf,gInv);
}





// fills last buf samples
LTFAT_EXTERN
void LTFAT_NAME(extend_left)(const LTFAT_TYPE *in, ltfatInt L, LTFAT_TYPE *buf,ltfatInt bufgl, ltfatInt gl, ltfatExtType ext, ltfatInt a)
{
    ltfatInt legalExtLen = (gl-1)%L;
    ltfatInt LTimes = (gl-1)/L;
    LTFAT_TYPE *buffTmp = buf + bufgl - legalExtLen;
    switch (ext)
    {
    case SYM: // half-point symmetry
    case EVEN:
        for(ltfatInt ii=0; ii<legalExtLen; ii++)
            buffTmp[ii] = in[legalExtLen-ii-1];
        break;
    case SYMW: // whole-point symmetry
        legalExtLen = imin(gl-1, L-1);
        buffTmp = buf + bufgl - legalExtLen;
        for(ltfatInt ii=0; ii<legalExtLen; ii++)
            buffTmp[ii] = in[legalExtLen-ii];
        break;
    case ASYM: // half-point antisymmetry
    case ODD:
        for(ltfatInt ii=0; ii<legalExtLen; ii++)
            buffTmp[ii] = -in[legalExtLen-ii-1];
        break;
    case ASYMW: // whole-point antisymmetry
        legalExtLen = imin(gl-1, L-1);
        legalExtLen = imin(gl-1, L-1);
        buffTmp = buf + bufgl - legalExtLen;
        for(ltfatInt ii=0; ii<legalExtLen; ii++)
            buffTmp[ii] = -in[legalExtLen-ii];
        break;
    case PPD: // periodic padding
    case PER:
    {
        LTFAT_TYPE *bufPtr = buf + bufgl - (gl-1);
        for(ltfatInt ii=0; ii<legalExtLen; ii++)
        {
            *(bufPtr) = in[L-1-(legalExtLen-1)+ii];
            bufPtr++;
        }

        for(ltfatInt ii=0; ii<LTimes; ii++)
        {
            for(ltfatInt jj=0; jj<L; jj++)
            {
                *(bufPtr) = in[jj];
                bufPtr++;
            }
        }

    }
    break;
    case SP0: // constant padding
        buffTmp = buf + bufgl - (gl-1);
        for(ltfatInt ii=0; ii<gl-1; ii++)
            buffTmp[ii] = in[0];
        break;
    case PERDEC: // periodic padding with possible last sample repplication
    {
        ltfatInt rem = L%a;
        if(rem==0)
        {
            for(ltfatInt ii=0; ii<legalExtLen; ii++)
                buffTmp[ii] = in[L-1-(legalExtLen-1)+ii];
        }
        else
        {
            ltfatInt remto = a - rem;

            // replicated
            for(ltfatInt ii=0; ii<remto; ii++)
                buffTmp[legalExtLen-1-ii] = in[L-1];

            // periodic extension
            for(ltfatInt ii=0; ii<legalExtLen-remto; ii++)
                buffTmp[ii] = in[L-1-(legalExtLen-1-1)+ii+remto-1];
        }
    }
    break;
    case ZPD: // zero-padding by default
    case ZERO:
    case VALID:
    default:
        break;
    }
}

void LTFAT_NAME(extend_right)(const LTFAT_TYPE *in,ltfatInt L, LTFAT_TYPE *buf, ltfatInt gl, ltfatExtType ext, ltfatInt a)
{
    ltfatInt legalExtLen = (gl-1)%L;
    ltfatInt LTimes = (gl-1)/L;
    switch (ext)
    {
    case SYM: // half-point symmetry
    case EVEN:
        for(ltfatInt ii=0; ii<legalExtLen; ii++)
            buf[ii] = in[legalExtLen-ii];
        break;
    case SYMW: // whole-point symmetry
        legalExtLen = imin(gl-1, L-1);
        for(ltfatInt ii=0; ii<legalExtLen; ii++)
            buf[ii] = in[L-1-1-ii];
        break;
    case ASYM: // half-point antisymmetry
    case ODD:
        for(ltfatInt ii=0; ii<legalExtLen; ii++)
            buf[ii] = -in[L-1-ii];
        break;
    case ASYMW: // whole-point antisymmetry
        legalExtLen = imin(gl-1, L-1);
        for(ltfatInt ii=0; ii<legalExtLen; ii++)
            buf[ii] = -in[L-1-1-ii];
        break;
    case PPD: // periodic padding
    case PER:
    {
        LTFAT_TYPE *bufPtr = buf;
        for(ltfatInt ii=0; ii<LTimes; ii++)
        {
            for(ltfatInt jj=0; jj<L; jj++)
            {
                *(bufPtr) = in[jj];
                bufPtr++;
            }
        }
        for(ltfatInt ii=0; ii<legalExtLen; ii++)
        {
            *(bufPtr) = in[ii];
            bufPtr++;
        }
    }
    break;
    case SP0: // constant padding
        for(ltfatInt ii=0; ii<gl; ii++)
            buf[ii] = in[L-1];
        break;
    case PERDEC: // periodic padding with possible last sample repplication
    {
        ltfatInt rem = L%a;
        if(rem==0)
        {
            for(ltfatInt ii=0; ii<legalExtLen; ii++)
                buf[ii] = in[ii];
        }
        else
        {
            ltfatInt remto = a - rem;
            // replicated
            for(ltfatInt ii=0; ii<remto; ii++)
                buf[ii] = in[L-1];

            // periodized
            for(ltfatInt ii=0; ii<legalExtLen-remto; ii++)
                buf[ii+remto] = in[ii];
        }
        break;
    }
    case ZPD: // zero-padding by default
    case ZERO:
    case VALID:
    default:
        break;
    }



}




#endif // LTFAT_TYPE