/***/
/* dePakeFT.M: dePake via wieghted FT. Use in place of FT. Usage:
/*       | nmrPipe -fn MAC -macro dePakeFT.M -all \
/***/

if (sliceCode == CODE_INIT) {
   (void) printf("*** dePakeFT.M:\n");
   (void) printf("    Sani et al J. Biomol. NMR, Submitted (2011)\n");
   (void) printf("    McCabe & Wassall Solid State NMR 10 53-61 (1997)\n");
   (void) printf("    http://www.chemistry.unimelb.edu.au/staff/jgehman/research/\n");

        sw  = getParm( fdata, NDSW, CUR_XDIM );
        dw  = 1.0/sw;
        dnu = sw/(size-1);
        sph = sqrt(1.5/PI);
        size2  = integer(size/2);
        size2s = integer(0.5 + size/2);
        orig   = -sw*(size-size2-1)/(size/2);

        (void) setParm( fdata, NDSW,   2.0*sw, CUR_XDIM );
        (void) setParm( fdata, NDCAR,     0.0, CUR_XDIM );
        (void) setParm( fdata, NDFTFLAG,    1, CUR_XDIM );
        (void) setParm( fdata, NDORIG,   orig, CUR_XDIM );
};

if (sliceCode > 0) {
        if (!argTest( "-all" )) {
           (void) printf( "dePakeFT.M Error: this macro requires the -all option.\n");
           exit( 0 );
        }

        t  = 0.0;

        for( i=0; i<size; i++ ) {
                rdata[i] *= sqrt( t );
                idata[i] *= sqrt( t );
                t += dw;
        }

        (void) fft( rdata, idata, size );

        float rtmp[size]; (void) vsSet(rtmp,size,0.0);
        float itmp[size]; (void) vsSet(itmp,size,0.0);

        freq  = sw/2.0; 
        freq0 = freq/(size-1);

        for( i=0; i<size2; i++ ) {
                rtmp[i+size2s] = rdata[i]*sph*sqrt( abs(freq) );
                itmp[i+size2s] = idata[i]*sph*sqrt( abs(freq) );
                rtmp[i] = rdata[i+size2s]*sph*sqrt( abs(freq0) );
                itmp[i] = idata[i+size2s]*sph*sqrt( abs(freq0) );
                freq  -= dnu;
                freq0 += dnu;
        }

        (void) phase( rtmp, itmp, size, -45, 90 );

        for( i=0; i<size2; i++ ) {
                rdata[i+size2s] = rtmp[size-i-1];
                idata[i+size2s] = itmp[size-i-1];
                rdata[i] = rtmp[size2-i-1];
                idata[i] = itmp[size2-i-1];
        }
};

