fft.cc
Go to the documentation of this file.
00001 //$$ fft.cpp                         Fast fourier transform
00002 
00003 // Copyright (C) 1991,2,3,4,8: R B Davies
00004 
00005 
00006 #define WANT_MATH
00007 // #define WANT_STREAM
00008 
00009 #include "include.h"
00010 
00011 #include "newmatap.h"
00012 
00013 // #include "newmatio.h"
00014 
00015 #ifdef use_namespace
00016 namespace NEWMAT {
00017 #endif
00018 
00019 #ifdef DO_REPORT
00020 #define REPORT { static ExeCounter ExeCount(__LINE__,19); ++ExeCount; }
00021 #else
00022 #define REPORT {}
00023 #endif
00024 
00025 static void cossin(int n, int d, Real& c, Real& s)
00026 // calculate cos(twopi*n/d) and sin(twopi*n/d)
00027 // minimise roundoff error
00028 {
00029    REPORT
00030    long n4 = n * 4; int sector = (int)floor( (Real)n4 / (Real)d + 0.5 );
00031    n4 -= sector * d;
00032    if (sector < 0) { REPORT sector = 3 - (3 - sector) % 4; }
00033    else  { REPORT sector %= 4; }
00034    Real ratio = 1.5707963267948966192 * (Real)n4 / (Real)d;
00035 
00036    switch (sector)
00037    {
00038    case 0: REPORT c =  cos(ratio); s =  sin(ratio); break;
00039    case 1: REPORT c = -sin(ratio); s =  cos(ratio); break;
00040    case 2: REPORT c = -cos(ratio); s = -sin(ratio); break;
00041    case 3: REPORT c =  sin(ratio); s = -cos(ratio); break;
00042    }
00043 }
00044 
00045 static void fftstep(ColumnVector& A, ColumnVector& B, ColumnVector& X,
00046    ColumnVector& Y, int after, int now, int before)
00047 {
00048    REPORT
00049    Tracer trace("FFT(step)");
00050    // const Real twopi = 6.2831853071795864769;
00051    const int gamma = after * before;  const int delta = now * after;
00052    // const Real angle = twopi / delta;  Real temp;
00053    // Real r_omega = cos(angle);  Real i_omega = -sin(angle);
00054    Real r_arg = 1.0;  Real i_arg = 0.0;
00055    Real* x = X.Store();  Real* y = Y.Store();   // pointers to array storage
00056    const int m = A.Nrows() - gamma;
00057 
00058    for (int j = 0; j < now; j++)
00059    {
00060       Real* a = A.Store(); Real* b = B.Store(); // pointers to array storage
00061       Real* x1 = x; Real* y1 = y; x += after; y += after;
00062       for (int ia = 0; ia < after; ia++)
00063       {
00064          // generate sins & cosines explicitly rather than iteratively
00065          // for more accuracy; but slower
00066          cossin(-(j*after+ia), delta, r_arg, i_arg);
00067 
00068          Real* a1 = a++; Real* b1 = b++; Real* x2 = x1++; Real* y2 = y1++;
00069          if (now==2)
00070          {
00071             REPORT int ib = before;
00072             if (ib) for (;;)
00073             {
00074                REPORT
00075                Real* a2 = m + a1; Real* b2 = m + b1; a1 += after; b1 += after;
00076                Real r_value = *a2; Real i_value = *b2;
00077                *x2 = r_value * r_arg - i_value * i_arg + *(a2-gamma);
00078                *y2 = r_value * i_arg + i_value * r_arg + *(b2-gamma);
00079                if (!(--ib)) break;
00080                x2 += delta; y2 += delta;
00081             }
00082          }
00083          else
00084          {
00085             REPORT int ib = before;
00086             if (ib) for (;;)
00087             {
00088                REPORT
00089                Real* a2 = m + a1; Real* b2 = m + b1; a1 += after; b1 += after;
00090                Real r_value = *a2; Real i_value = *b2;
00091                int in = now-1; while (in--)
00092                {
00093                   // it should be possible to make this faster
00094                   // hand code for now = 2,3,4,5,8
00095                   // use symmetry to halve number of operations
00096                   a2 -= gamma; b2 -= gamma;  Real temp = r_value;
00097                   r_value = r_value * r_arg - i_value * i_arg + *a2;
00098                   i_value = temp    * i_arg + i_value * r_arg + *b2;
00099                }
00100                *x2 = r_value; *y2 = i_value;
00101                if (!(--ib)) break;
00102                x2 += delta; y2 += delta;
00103             }
00104          }
00105 
00106          // temp = r_arg;
00107          // r_arg = r_arg * r_omega - i_arg * i_omega;
00108          // i_arg = temp  * i_omega + i_arg * r_omega;
00109 
00110       }
00111    }
00112 }
00113 
00114 
00115 void FFTI(const ColumnVector& U, const ColumnVector& V,
00116    ColumnVector& X, ColumnVector& Y)
00117 {
00118    // Inverse transform
00119    Tracer trace("FFTI");
00120    REPORT
00121    FFT(U,-V,X,Y);
00122    const Real n = X.Nrows(); X /= n; Y /= (-n);
00123 }
00124 
00125 void RealFFT(const ColumnVector& U, ColumnVector& X, ColumnVector& Y)
00126 {
00127    // Fourier transform of a real series
00128    Tracer trace("RealFFT");
00129    REPORT
00130    const int n = U.Nrows();                     // length of arrays
00131    const int n2 = n / 2;
00132    if (n != 2 * n2)
00133       Throw(ProgramException("Vector length not multiple of 2", U));
00134    ColumnVector A(n2), B(n2);
00135    Real* a = A.Store(); Real* b = B.Store(); Real* u = U.Store(); int i = n2;
00136    while (i--) { *a++ = *u++; *b++ = *u++; }
00137    FFT(A,B,A,B);
00138    int n21 = n2 + 1;
00139    X.ReSize(n21); Y.ReSize(n21);
00140    i = n2 - 1;
00141    a = A.Store(); b = B.Store();              // first els of A and B
00142    Real* an = a + i; Real* bn = b + i;        // last els of A and B
00143    Real* x = X.Store(); Real* y = Y.Store();  // first els of X and Y
00144    Real* xn = x + n2; Real* yn = y + n2;      // last els of X and Y
00145 
00146    *x++ = *a + *b; *y++ = 0.0;                // first complex element
00147    *xn-- = *a++ - *b++; *yn-- = 0.0;          // last complex element
00148 
00149    int j = -1; i = n2/2;
00150    while (i--)
00151    {
00152       Real c,s; cossin(j--,n,c,s);
00153       Real am = *a - *an; Real ap = *a++ + *an--;
00154       Real bm = *b - *bn; Real bp = *b++ + *bn--;
00155       Real samcbp = s * am + c * bp; Real sbpcam = s * bp - c * am;
00156       *x++  =  0.5 * ( ap + samcbp); *y++  =  0.5 * ( bm + sbpcam);
00157       *xn-- =  0.5 * ( ap - samcbp); *yn-- =  0.5 * (-bm + sbpcam);
00158    }
00159 }
00160 
00161 void RealFFTI(const ColumnVector& A, const ColumnVector& B, ColumnVector& U)
00162 {
00163    // inverse of a Fourier transform of a real series
00164    Tracer trace("RealFFTI");
00165    REPORT
00166    const int n21 = A.Nrows();                     // length of arrays
00167    if (n21 != B.Nrows() || n21 == 0)
00168       Throw(ProgramException("Vector lengths unequal or zero", A, B));
00169    const int n2 = n21 - 1;  const int n = 2 * n2;  int i = n2 - 1;
00170 
00171    ColumnVector X(n2), Y(n2);
00172    Real* a = A.Store(); Real* b = B.Store();  // first els of A and B
00173    Real* an = a + n2;   Real* bn = b + n2;    // last els of A and B
00174    Real* x = X.Store(); Real* y = Y.Store();  // first els of X and Y
00175    Real* xn = x + i;    Real* yn = y + i;     // last els of X and Y
00176 
00177    Real hn = 0.5 / n2;
00178    *x++  = hn * (*a + *an);  *y++  = - hn * (*a - *an);
00179    a++; an--; b++; bn--;
00180    int j = -1;  i = n2/2;
00181    while (i--)
00182    {
00183       Real c,s; cossin(j--,n,c,s);
00184       Real am = *a - *an; Real ap = *a++ + *an--;
00185       Real bm = *b - *bn; Real bp = *b++ + *bn--;
00186       Real samcbp = s * am - c * bp; Real sbpcam = s * bp + c * am;
00187       *x++  =  hn * ( ap + samcbp); *y++  =  - hn * ( bm + sbpcam);
00188       *xn-- =  hn * ( ap - samcbp); *yn-- =  - hn * (-bm + sbpcam);
00189    }
00190    FFT(X,Y,X,Y);             // have done inverting elsewhere
00191    U.ReSize(n); i = n2;
00192    x = X.Store(); y = Y.Store(); Real* u = U.Store();
00193    while (i--) { *u++ = *x++; *u++ = - *y++; }
00194 }
00195 
00196 void FFT(const ColumnVector& U, const ColumnVector& V,
00197    ColumnVector& X, ColumnVector& Y)
00198 {
00199    // from Carl de Boor (1980), Siam J Sci Stat Comput, 1 173-8
00200    // but first try Sande and Gentleman
00201    Tracer trace("FFT");
00202    REPORT
00203    const int n = U.Nrows();                     // length of arrays
00204    if (n != V.Nrows() || n == 0)
00205       Throw(ProgramException("Vector lengths unequal or zero", U, V));
00206    if (n == 1) { REPORT X = U; Y = V; return; }
00207 
00208    // see if we can use the newfft routine
00209    if (!FFT_Controller::OnlyOldFFT && FFT_Controller::CanFactor(n))
00210    {
00211       REPORT
00212       X = U; Y = V;
00213       if ( FFT_Controller::ar_1d_ft(n,X.Store(),Y.Store()) ) return;
00214    }
00215 
00216    ColumnVector B = V;
00217    ColumnVector A = U;
00218    X.ReSize(n); Y.ReSize(n);
00219    const int nextmx = 8;
00220 #ifndef ATandT
00221    int prime[8] = { 2,3,5,7,11,13,17,19 };
00222 #else
00223    int prime[8];
00224    prime[0]=2; prime[1]=3; prime[2]=5; prime[3]=7;
00225    prime[4]=11; prime[5]=13; prime[6]=17; prime[7]=19;
00226 #endif
00227    int after = 1; int before = n; int next = 0; bool inzee = true;
00228    int now = 0; int b1;             // initialised to keep gnu happy
00229 
00230    do
00231    {
00232       for (;;)
00233       {
00234          if (next < nextmx) { REPORT now = prime[next]; }
00235          b1 = before / now;  if (b1 * now == before) { REPORT break; }
00236          next++; now += 2;
00237       }
00238       before = b1;
00239 
00240       if (inzee) { REPORT fftstep(A, B, X, Y, after, now, before); }
00241       else { REPORT fftstep(X, Y, A, B, after, now, before); }
00242 
00243       inzee = !inzee; after *= now;
00244    }
00245    while (before != 1);
00246 
00247    if (inzee) { REPORT A.Release(); X = A; B.Release(); Y = B; }
00248 }
00249 
00250 // Trigonometric transforms
00251 // see Charles Van Loan (1992) "Computational frameworks for the fast
00252 // Fourier transform" published by SIAM; section 4.4.
00253 
00254 void DCT_II(const ColumnVector& U, ColumnVector& V)
00255 {
00256    // Discrete cosine transform, type II, of a real series
00257    Tracer trace("DCT_II");
00258    REPORT
00259    const int n = U.Nrows();                     // length of arrays
00260    const int n2 = n / 2; const int n4 = n * 4;
00261    if (n != 2 * n2)
00262       Throw(ProgramException("Vector length not multiple of 2", U));
00263    ColumnVector A(n);
00264    Real* a = A.Store(); Real* b = a + n; Real* u = U.Store();
00265    int i = n2;
00266    while (i--) { *a++ = *u++; *(--b) = *u++; }
00267    ColumnVector X, Y;
00268    RealFFT(A, X, Y); A.CleanUp();
00269    V.ReSize(n);
00270    Real* x = X.Store(); Real* y = Y.Store();
00271    Real* v = V.Store(); Real* w = v + n;
00272    *v = *x;
00273    int k = 0; i = n2;
00274    while (i--)
00275    {
00276       Real c, s; cossin(++k, n4, c, s);
00277       Real xi = *(++x); Real yi = *(++y);
00278       *(++v) = xi * c + yi * s; *(--w) = xi * s - yi * c;
00279    }
00280 }
00281 
00282 void DCT_II_inverse(const ColumnVector& V, ColumnVector& U)
00283 {
00284    // Inverse of discrete cosine transform, type II
00285    Tracer trace("DCT_II_inverse");
00286    REPORT
00287    const int n = V.Nrows();                     // length of array
00288    const int n2 = n / 2; const int n4 = n * 4; const int n21 = n2 + 1;
00289    if (n != 2 * n2)
00290       Throw(ProgramException("Vector length not multiple of 2", V));
00291    ColumnVector X(n21), Y(n21);
00292    Real* x = X.Store(); Real* y = Y.Store();
00293    Real* v = V.Store(); Real* w = v + n;
00294    *x = *v; *y = 0.0;
00295    int i = n2; int k = 0;
00296    while (i--)
00297    {
00298       Real c, s; cossin(++k, n4, c, s);
00299       Real vi = *(++v); Real wi = *(--w);
00300       *(++x) = vi * c + wi * s; *(++y) = vi * s - wi * c;
00301    }
00302    ColumnVector A; RealFFTI(X, Y, A);
00303    X.CleanUp(); Y.CleanUp(); U.ReSize(n);
00304    Real* a = A.Store(); Real* b = a + n; Real* u = U.Store();
00305    i = n2;
00306    while (i--) { *u++ = *a++; *u++ = *(--b); }
00307 }
00308 
00309 void DST_II(const ColumnVector& U, ColumnVector& V)
00310 {
00311    // Discrete sine transform, type II, of a real series
00312    Tracer trace("DST_II");
00313    REPORT
00314    const int n = U.Nrows();                     // length of arrays
00315    const int n2 = n / 2; const int n4 = n * 4;
00316    if (n != 2 * n2)
00317       Throw(ProgramException("Vector length not multiple of 2", U));
00318    ColumnVector A(n);
00319    Real* a = A.Store(); Real* b = a + n; Real* u = U.Store();
00320    int i = n2;
00321    while (i--) { *a++ = *u++; *(--b) = -(*u++); }
00322    ColumnVector X, Y;
00323    RealFFT(A, X, Y); A.CleanUp();
00324    V.ReSize(n);
00325    Real* x = X.Store(); Real* y = Y.Store();
00326    Real* v = V.Store(); Real* w = v + n;
00327    *(--w) = *x;
00328    int k = 0; i = n2;
00329    while (i--)
00330    {
00331       Real c, s; cossin(++k, n4, c, s);
00332       Real xi = *(++x); Real yi = *(++y);
00333       *v++ = xi * s - yi * c; *(--w) = xi * c + yi * s;
00334    }
00335 }
00336 
00337 void DST_II_inverse(const ColumnVector& V, ColumnVector& U)
00338 {
00339    // Inverse of discrete sine transform, type II
00340    Tracer trace("DST_II_inverse");
00341    REPORT
00342    const int n = V.Nrows();                     // length of array
00343    const int n2 = n / 2; const int n4 = n * 4; const int n21 = n2 + 1;
00344    if (n != 2 * n2)
00345       Throw(ProgramException("Vector length not multiple of 2", V));
00346    ColumnVector X(n21), Y(n21);
00347    Real* x = X.Store(); Real* y = Y.Store();
00348    Real* v = V.Store(); Real* w = v + n;
00349    *x = *(--w); *y = 0.0;
00350    int i = n2; int k = 0;
00351    while (i--)
00352    {
00353       Real c, s; cossin(++k, n4, c, s);
00354       Real vi = *v++; Real wi = *(--w);
00355       *(++x) = vi * s + wi * c; *(++y) = - vi * c + wi * s;
00356    }
00357    ColumnVector A; RealFFTI(X, Y, A);
00358    X.CleanUp(); Y.CleanUp(); U.ReSize(n);
00359    Real* a = A.Store(); Real* b = a + n; Real* u = U.Store();
00360    i = n2;
00361    while (i--) { *u++ = *a++; *u++ = -(*(--b)); }
00362 }
00363 
00364 void DCT_inverse(const ColumnVector& V, ColumnVector& U)
00365 {
00366    // Inverse of discrete cosine transform, type I
00367    Tracer trace("DCT_inverse");
00368    REPORT
00369    const int n = V.Nrows()-1;                     // length of transform
00370    const int n2 = n / 2; const int n21 = n2 + 1;
00371    if (n != 2 * n2)
00372       Throw(ProgramException("Vector length not multiple of 2", V));
00373    ColumnVector X(n21), Y(n21);
00374    Real* x = X.Store(); Real* y = Y.Store(); Real* v = V.Store();
00375    Real vi = *v++; *x++ = vi; *y++ = 0.0;
00376    Real sum1 = vi / 2.0; Real sum2 = sum1; vi = *v++;
00377    int i = n2-1;
00378    while (i--)
00379    {
00380       Real vi2 = *v++; sum1 += vi2 + vi; sum2 += vi2 - vi;
00381       *x++ = vi2; vi2 = *v++; *y++ = vi - vi2; vi = vi2;
00382    }
00383    sum1 += vi; sum2 -= vi;
00384    vi = *v; *x = vi; *y = 0.0; vi /= 2.0; sum1 += vi; sum2 += vi;
00385    ColumnVector A; RealFFTI(X, Y, A);
00386    X.CleanUp(); Y.CleanUp(); U.ReSize(n+1);
00387    Real* a = A.Store(); Real* b = a + n; Real* u = U.Store(); v = u + n;
00388    i = n2; int k = 0; *u++ = sum1 / n2; *v-- = sum2 / n2;
00389    while (i--)
00390    {
00391       Real s = sin(1.5707963267948966192 * (++k) / n2);
00392       Real ai = *(++a); Real bi = *(--b);
00393       Real bz = (ai - bi) / 4 / s; Real az = (ai + bi) / 2;
00394       *u++ = az - bz; *v-- = az + bz;
00395    }
00396 }
00397 
00398 void DCT(const ColumnVector& U, ColumnVector& V)
00399 {
00400    // Discrete cosine transform, type I
00401    Tracer trace("DCT");
00402    REPORT
00403    DCT_inverse(U, V);
00404    V *= (V.Nrows()-1)/2;
00405 }
00406 
00407 void DST_inverse(const ColumnVector& V, ColumnVector& U)
00408 {
00409    // Inverse of discrete sine transform, type I
00410    Tracer trace("DST_inverse");
00411    REPORT
00412    const int n = V.Nrows()-1;                     // length of transform
00413    const int n2 = n / 2; const int n21 = n2 + 1;
00414    if (n != 2 * n2)
00415       Throw(ProgramException("Vector length not multiple of 2", V));
00416    ColumnVector X(n21), Y(n21);
00417    Real* x = X.Store(); Real* y = Y.Store(); Real* v = V.Store();
00418    Real vi = *(++v); *x++ = 2 * vi; *y++ = 0.0;
00419    int i = n2-1;
00420    while (i--) { *y++ = *(++v); Real vi2 = *(++v); *x++ = vi2 - vi; vi = vi2; }
00421    *x = -2 * vi; *y = 0.0;
00422    ColumnVector A; RealFFTI(X, Y, A);
00423    X.CleanUp(); Y.CleanUp(); U.ReSize(n+1);
00424    Real* a = A.Store(); Real* b = a + n; Real* u = U.Store(); v = u + n;
00425    i = n2; int k = 0; *u++ = 0.0; *v-- = 0.0;
00426    while (i--)
00427    {
00428       Real s = sin(1.5707963267948966192 * (++k) / n2);
00429       Real ai = *(++a); Real bi = *(--b);
00430       Real az = (ai + bi) / 4 / s; Real bz = (ai - bi) / 2;
00431       *u++ = az - bz; *v-- = az + bz;
00432    }
00433 }
00434 
00435 void DST(const ColumnVector& U, ColumnVector& V)
00436 {
00437    // Discrete sine transform, type I
00438    Tracer trace("DST");
00439    REPORT
00440    DST_inverse(U, V);
00441    V *= (V.Nrows()-1)/2;
00442 }
00443 
00444 
00445 
00446 #ifdef use_namespace
00447 }
00448 #endif
00449 
00450 


rl_agent
Author(s): Todd Hester
autogenerated on Thu Jun 6 2019 22:00:13