/***/
/* 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. PeerJ 1:e30 DOI:10.7717/peerj.30 (2013)\n");
   (void) printf("    McCabe & Wassall Solid State NMR 10 53-61 (1997)\n");

 sw  = getParm( fdata, NDSW, CUR_XDIM );
 dw  = 1.0/sw;
 dnu = sw/size;
 sph = sqrt(1.5/PI);
 size2 = integer(0.6 + size/2.0);

 (void) setParm( fdata, 1003, 2.0*sw, CUR_XDIM );
 (void) setParm( fdata, 1013,    0.0, CUR_XDIM );
 (void) setParm( fdata, 1006,      1, CUR_XDIM );
 (void) setParm( fdata, 1004,    -sw, CUR_XDIM );
};

if (sliceCode > 0) {
 if (!argTest( "-all" )) {
  (void) printf( "dePakeFT.M requires `-all'.\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 );

 freq  = sw/2.0; 
		
 for( i=0; i<size2; i++ ) {
  s1 = sph * sqrt( abs(freq)  );   
  s2 = sph * sqrt( abs(dnu-freq) );

  rtmp = s2 * rdata[size-i-1];
  itmp = s2 * idata[size-i-1];
  rdata[size-i-1] = s1 * -idata[i];
  idata[size-i-1] = s1 *  rdata[i];
  rdata[i] = rtmp;
  idata[i] = itmp;

  freq  -= dnu;
 }

 (void) phase(rdata, idata, size, -45.0, 0.0) 
};
