home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / sources / misc / 4254 / fft.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-11  |  6.8 KB  |  213 lines

  1. //$$ fft.cxx                         Fast fourier transform
  2.  
  3. // Copyright (C) 1991,2,3: R B Davies
  4.  
  5.  
  6. #define WANT_MATH
  7.  
  8. #include "include.h"
  9.  
  10. #include "newmatap.h"
  11.  
  12.  
  13. static void cossin(int n, int d, Real& c, Real& s)
  14. // calculate cos(twopi*n/d) and sin(twopi*n/d)
  15. // minimise roundoff error
  16. {
  17.    long n4 = n * 4; int sector = (int)floor( (Real)n4 / (Real)d + 0.5 );
  18.    n4 -= sector * d;
  19.    if (sector < 0) sector = 3 - (3 - sector) % 4; else sector %= 4;
  20.    Real ratio = 1.5707963267948966192 * (Real)n4 / (Real)d;
  21.  
  22.    switch (sector)
  23.    {
  24.    case 0: c =  cos(ratio); s =  sin(ratio); break;
  25.    case 1: c = -sin(ratio); s =  cos(ratio); break;
  26.    case 2: c = -cos(ratio); s = -sin(ratio); break;
  27.    case 3: c =  sin(ratio); s = -cos(ratio); break;
  28.    }
  29. }
  30.  
  31. static void fftstep(ColumnVector& A, ColumnVector& B, ColumnVector& X,
  32.    ColumnVector& Y, int after, int now, int before)
  33. {
  34.    Tracer trace("FFT(step)");
  35.    // const Real twopi = 6.2831853071795864769;
  36.    const int gamma = after * before;  const int delta = now * after;
  37.    // const Real angle = twopi / delta;  Real temp;
  38.    // Real r_omega = cos(angle);  Real i_omega = -sin(angle);
  39.    Real r_arg = 1.0;  Real i_arg = 0.0;
  40.    Real* x = X.Store();  Real* y = Y.Store();   // pointers to array storage
  41.    const int m = A.Nrows() - gamma;
  42.  
  43.    for (int j = 0; j < now; j++)
  44.    {
  45.       Real* a = A.Store(); Real* b = B.Store(); // pointers to array storage
  46.       Real* x1 = x; Real* y1 = y; x += after; y += after;
  47.       for (int ia = 0; ia < after; ia++)
  48.       {
  49.      // generate sins & cosines explicitly rather than iteratively
  50.      // for more accuracy; but slower
  51.      cossin(-(j*after+ia), delta, r_arg, i_arg);
  52.  
  53.      Real* a1 = a++; Real* b1 = b++; Real* x2 = x1++; Real* y2 = y1++;
  54.      if (now==2)
  55.      {
  56.         int ib = before; while (ib--)
  57.         {
  58.            Real* a2 = m + a1; Real* b2 = m + b1; a1 += after; b1 += after;
  59.            Real r_value = *a2; Real i_value = *b2;
  60.            *x2 = r_value * r_arg - i_value * i_arg + *(a2-gamma);
  61.            *y2 = r_value * i_arg + i_value * r_arg + *(b2-gamma);
  62.            x2 += delta; y2 += delta;
  63.         }
  64.      }
  65.      else
  66.      {
  67.         int ib = before; while (ib--)
  68.         {
  69.            Real* a2 = m + a1; Real* b2 = m + b1; a1 += after; b1 += after;
  70.            Real r_value = *a2; Real i_value = *b2;
  71.            int in = now-1; while (in--)
  72.            {
  73.           // it should be possible to make this faster
  74.           // hand code for now = 2,3,4,5,8
  75.           // use symmetry to halve number of operations
  76.           a2 -= gamma; b2 -= gamma;  Real temp = r_value;
  77.           r_value = r_value * r_arg - i_value * i_arg + *a2;
  78.           i_value = temp    * i_arg + i_value * r_arg + *b2;
  79.            }
  80.            *x2 = r_value; *y2 = i_value;   x2 += delta; y2 += delta;
  81.         }
  82.      }
  83.  
  84.          // temp = r_arg;
  85.          // r_arg = r_arg * r_omega - i_arg * i_omega;
  86.          // i_arg = temp  * i_omega + i_arg * r_omega;
  87.  
  88.       }
  89.    }
  90. }
  91.  
  92.  
  93. void FFT(const ColumnVector& U, const ColumnVector& V,
  94.    ColumnVector& X, ColumnVector& Y)
  95. {
  96.    // from Carl de Boor (1980), Siam J Sci Stat Comput, 1 173-8
  97.    Tracer trace("FFT");
  98.    const int n = U.Nrows();                     // length of arrays
  99.    if (n != V.Nrows() || n == 0)
  100.       Throw(ProgramException("Vector lengths unequal or zero", U, V));
  101.    ColumnVector A = U; ColumnVector B = V;
  102.    X.ReDimension(n); Y.ReDimension(n);
  103.    const int nextmx = 8;
  104. #ifndef ATandT
  105.    int prime[8] = { 2,3,5,7,11,13,17,19 };
  106. #else
  107.    int prime[8];
  108.    prime[0]=2; prime[1]=3; prime[2]=5; prime[3]=7;
  109.    prime[4]=11; prime[5]=13; prime[6]=17; prime[7]=19;
  110. #endif
  111.    int after = 1; int before = n; int next = 0; Boolean inzee = TRUE;
  112.  
  113.    do
  114.    {
  115.       int now, b1;
  116.       for (;;)
  117.       {
  118.      if (next < nextmx) now = prime[next];
  119.      b1 = before / now;  if (b1 * now == before) break;
  120.      next++; now += 2;
  121.       }
  122.       before = b1;
  123.  
  124.       if (inzee) fftstep(A, B, X, Y, after, now, before);
  125.       else fftstep(X, Y, A, B, after, now, before);
  126.  
  127.       inzee = !inzee; after *= now;
  128.    }
  129.    while (before != 1);
  130.  
  131.    if (inzee) { A.Release(); X = A; B.Release(); Y = B; }
  132. }
  133.  
  134.  
  135. void FFTI(const ColumnVector& U, const ColumnVector& V,
  136.    ColumnVector& X, ColumnVector& Y)
  137. {
  138.    // Inverse transform
  139.    Tracer trace("FFTI");
  140.    FFT(U,-V,X,Y);
  141.    const Real n = X.Nrows(); X = X / n; Y = Y / (-n);
  142. }
  143.  
  144. void RealFFT(const ColumnVector& U, ColumnVector& X, ColumnVector& Y)
  145. {
  146.    // Fourier transform of a real series
  147.    Tracer trace("RealFFT");
  148.    const int n = U.Nrows();                     // length of arrays
  149.    const int n2 = n / 2;
  150.    if (n != 2 * n2)
  151.       Throw(ProgramException("Vector length not multiple of 2", U));
  152.    ColumnVector A(n2), B(n2);
  153.    Real* a = A.Store(); Real* b = B.Store(); Real* u = U.Store(); int i = n2;
  154.    while (i--) { *a++ = *u++; *b++ = *u++; }
  155.    FFT(A,B,A,B);
  156.    int n21 = n2 + 1;
  157.    X.ReDimension(n21); Y.ReDimension(n21);
  158.    i = n2 - 1;
  159.    a = A.Store(); b = B.Store();              // first els of A and B
  160.    Real* an = a + i; Real* bn = b + i;        // last els of A and B
  161.    Real* x = X.Store(); Real* y = Y.Store();  // first els of X and Y
  162.    Real* xn = x + n2; Real* yn = y + n2;      // last els of X and Y
  163.  
  164.    *x++ = *a + *b; *y++ = 0.0;                // first complex element
  165.    *xn-- = *a++ - *b++; *yn-- = 0.0;          // last complex element
  166.  
  167.    int j = -1; i = n2/2;
  168.    while (i--)
  169.    {
  170.       Real c,s; cossin(j--,n,c,s);
  171.       Real am = *a - *an; Real ap = *a++ + *an--;
  172.       Real bm = *b - *bn; Real bp = *b++ + *bn--;
  173.       Real samcbp = s * am + c * bp; Real sbpcam = s * bp - c * am;
  174.       *x++  =  0.5 * ( ap + samcbp); *y++  =  0.5 * ( bm + sbpcam);
  175.       *xn-- =  0.5 * ( ap - samcbp); *yn-- =  0.5 * (-bm + sbpcam);
  176.    }
  177. }
  178.  
  179. void RealFFTI(const ColumnVector& A, const ColumnVector& B, ColumnVector& U)
  180. {
  181.    // inverse of a Fourier transform of a real series
  182.    Tracer trace("RealFFTI");
  183.    const int n21 = A.Nrows();                     // length of arrays
  184.    if (n21 != B.Nrows() || n21 == 0)
  185.       Throw(ProgramException("Vector lengths unequal or zero", A, B));
  186.    const int n2 = n21 - 1;  const int n = 2 * n2;  int i = n2 - 1;
  187.  
  188.    ColumnVector X(n2), Y(n2);
  189.    Real* a = A.Store(); Real* b = B.Store();  // first els of A and B
  190.    Real* an = a + n2;   Real* bn = b + n2;    // last els of A and B
  191.    Real* x = X.Store(); Real* y = Y.Store();  // first els of X and Y
  192.    Real* xn = x + i;    Real* yn = y + i;     // last els of X and Y
  193.  
  194.    Real hn = 0.5 / n2;
  195.    *x++  = hn * (*a + *an);  *y++  = - hn * (*a - *an);
  196.    a++; an--; b++; bn--;
  197.    int j = -1;  i = n2/2;
  198.    while (i--)
  199.    {
  200.       Real c,s; cossin(j--,n,c,s);
  201.       Real am = *a - *an; Real ap = *a++ + *an--;
  202.       Real bm = *b - *bn; Real bp = *b++ + *bn--;
  203.       Real samcbp = s * am - c * bp; Real sbpcam = s * bp + c * am;
  204.       *x++  =  hn * ( ap + samcbp); *y++  =  - hn * ( bm + sbpcam);
  205.       *xn-- =  hn * ( ap - samcbp); *yn-- =  - hn * (-bm + sbpcam);
  206.    }
  207.    FFT(X,Y,X,Y);             // have done inverting elsewhere
  208.    U.ReDimension(n); i = n2;
  209.    x = X.Store(); y = Y.Store(); Real* u = U.Store();
  210.    while (i--) { *u++ = *x++; *u++ = - *y++; }
  211. }
  212.  
  213.