home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / n / newmat06.zip / FFT.CXX < prev    next >
C/C++ Source or Header  |  1992-11-30  |  4KB  |  135 lines

  1. //$$ fft.cxx                         Fast fourier transform
  2.  
  3. // Copyright (C) 1991,2: 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.