123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365 |
- //$ nobt
- //$ nocpp
-
- /**
- * @file fft4g.h
- *
- * @brief Wrapper class for Takuya OOURA's FFT functions.
- *
- * Functions from the FFT package by: Copyright(C) 1996-2001 Takuya OOURA
- * http://www.kurims.kyoto-u.ac.jp/~ooura/fft.html
- *
- * Modified and used with permission granted by the license.
- *
- * Here, the original "fft4g.c" file was wrapped into the "ooura_fft" class.
- */
-
- #ifndef R8B_FFT4G_INCLUDED
- #define R8B_FFT4G_INCLUDED
-
- /*
- Fast Fourier/Cosine/Sine Transform
- dimension :one
- data length :power of 2
- decimation :frequency
- radix :4, 2
- data :inplace
- table :use
- functions
- cdft: Complex Discrete Fourier Transform
- rdft: Real Discrete Fourier Transform
- ddct: Discrete Cosine Transform
- ddst: Discrete Sine Transform
- dfct: Cosine Transform of RDFT (Real Symmetric DFT)
- dfst: Sine Transform of RDFT (Real Anti-symmetric DFT)
- function prototypes
- void cdft(int, int, FPType *, int *, FPType *);
- void rdft(int, int, FPType *, int *, FPType *);
- void ddct(int, int, FPType *, int *, FPType *);
- void ddst(int, int, FPType *, int *, FPType *);
- void dfct(int, FPType *, FPType *, int *, FPType *);
- void dfst(int, FPType *, FPType *, int *, FPType *);
-
-
- -------- Complex DFT (Discrete Fourier Transform) --------
- [definition]
- <case1>
- X[k] = sum_j=0^n-1 x[j]*exp(2*pi*i*j*k/n), 0<=k<n
- <case2>
- X[k] = sum_j=0^n-1 x[j]*exp(-2*pi*i*j*k/n), 0<=k<n
- (notes: sum_j=0^n-1 is a summation from j=0 to n-1)
- [usage]
- <case1>
- ip[0] = 0; // first time only
- cdft(2*n, 1, a, ip, w);
- <case2>
- ip[0] = 0; // first time only
- cdft(2*n, -1, a, ip, w);
- [parameters]
- 2*n :data length (int)
- n >= 1, n = power of 2
- a[0...2*n-1] :input/output data (FPType *)
- input data
- a[2*j] = Re(x[j]),
- a[2*j+1] = Im(x[j]), 0<=j<n
- output data
- a[2*k] = Re(X[k]),
- a[2*k+1] = Im(X[k]), 0<=k<n
- ip[0...*] :work area for bit reversal (int *)
- length of ip >= 2+sqrt(n)
- strictly,
- length of ip >=
- 2+(1<<int(log(n+0.5)/log(2))/2).
- ip[0],ip[1] are pointers of the cos/sin table.
- w[0...n/2-1] :cos/sin table (FPType *)
- w[],ip[] are initialized if ip[0] == 0.
- [remark]
- Inverse of
- cdft(2*n, -1, a, ip, w);
- is
- cdft(2*n, 1, a, ip, w);
- for (j = 0; j <= 2 * n - 1; ++j) {
- a[j] *= 1.0 / n;
- }
- .
-
-
- -------- Real DFT / Inverse of Real DFT --------
- [definition]
- <case1> RDFT
- R[k] = sum_j=0^n-1 a[j]*cos(2*pi*j*k/n), 0<=k<=n/2
- I[k] = sum_j=0^n-1 a[j]*sin(2*pi*j*k/n), 0<k<n/2
- <case2> IRDFT (excluding scale)
- a[k] = (R[0] + R[n/2]*cos(pi*k))/2 +
- sum_j=1^n/2-1 R[j]*cos(2*pi*j*k/n) +
- sum_j=1^n/2-1 I[j]*sin(2*pi*j*k/n), 0<=k<n
- [usage]
- <case1>
- ip[0] = 0; // first time only
- rdft(n, 1, a, ip, w);
- <case2>
- ip[0] = 0; // first time only
- rdft(n, -1, a, ip, w);
- [parameters]
- n :data length (int)
- n >= 2, n = power of 2
- a[0...n-1] :input/output data (FPType *)
- <case1>
- output data
- a[2*k] = R[k], 0<=k<n/2
- a[2*k+1] = I[k], 0<k<n/2
- a[1] = R[n/2]
- <case2>
- input data
- a[2*j] = R[j], 0<=j<n/2
- a[2*j+1] = I[j], 0<j<n/2
- a[1] = R[n/2]
- ip[0...*] :work area for bit reversal (int *)
- length of ip >= 2+sqrt(n/2)
- strictly,
- length of ip >=
- 2+(1<<int(log(n/2+0.5)/log(2))/2).
- ip[0],ip[1] are pointers of the cos/sin table.
- w[0...n/2-1] :cos/sin table (FPType *)
- w[],ip[] are initialized if ip[0] == 0.
- [remark]
- Inverse of
- rdft(n, 1, a, ip, w);
- is
- rdft(n, -1, a, ip, w);
- for (j = 0; j <= n - 1; ++j) {
- a[j] *= 2.0 / n;
- }
- .
-
-
- -------- DCT (Discrete Cosine Transform) / Inverse of DCT --------
- [definition]
- <case1> IDCT (excluding scale)
- C[k] = sum_j=0^n-1 a[j]*cos(pi*j*(k+1/2)/n), 0<=k<n
- <case2> DCT
- C[k] = sum_j=0^n-1 a[j]*cos(pi*(j+1/2)*k/n), 0<=k<n
- [usage]
- <case1>
- ip[0] = 0; // first time only
- ddct(n, 1, a, ip, w);
- <case2>
- ip[0] = 0; // first time only
- ddct(n, -1, a, ip, w);
- [parameters]
- n :data length (int)
- n >= 2, n = power of 2
- a[0...n-1] :input/output data (FPType *)
- output data
- a[k] = C[k], 0<=k<n
- ip[0...*] :work area for bit reversal (int *)
- length of ip >= 2+sqrt(n/2)
- strictly,
- length of ip >=
- 2+(1<<int(log(n/2+0.5)/log(2))/2).
- ip[0],ip[1] are pointers of the cos/sin table.
- w[0...n*5/4-1] :cos/sin table (FPType *)
- w[],ip[] are initialized if ip[0] == 0.
- [remark]
- Inverse of
- ddct(n, -1, a, ip, w);
- is
- a[0] *= 0.5;
- ddct(n, 1, a, ip, w);
- for (j = 0; j <= n - 1; ++j) {
- a[j] *= 2.0 / n;
- }
- .
-
-
- -------- DST (Discrete Sine Transform) / Inverse of DST --------
- [definition]
- <case1> IDST (excluding scale)
- S[k] = sum_j=1^n A[j]*sin(pi*j*(k+1/2)/n), 0<=k<n
- <case2> DST
- S[k] = sum_j=0^n-1 a[j]*sin(pi*(j+1/2)*k/n), 0<k<=n
- [usage]
- <case1>
- ip[0] = 0; // first time only
- ddst(n, 1, a, ip, w);
- <case2>
- ip[0] = 0; // first time only
- ddst(n, -1, a, ip, w);
- [parameters]
- n :data length (int)
- n >= 2, n = power of 2
- a[0...n-1] :input/output data (FPType *)
- <case1>
- input data
- a[j] = A[j], 0<j<n
- a[0] = A[n]
- output data
- a[k] = S[k], 0<=k<n
- <case2>
- output data
- a[k] = S[k], 0<k<n
- a[0] = S[n]
- ip[0...*] :work area for bit reversal (int *)
- length of ip >= 2+sqrt(n/2)
- strictly,
- length of ip >=
- 2+(1<<int(log(n/2+0.5)/log(2))/2).
- ip[0],ip[1] are pointers of the cos/sin table.
- w[0...n*5/4-1] :cos/sin table (FPType *)
- w[],ip[] are initialized if ip[0] == 0.
- [remark]
- Inverse of
- ddst(n, -1, a, ip, w);
- is
- a[0] *= 0.5;
- ddst(n, 1, a, ip, w);
- for (j = 0; j <= n - 1; ++j) {
- a[j] *= 2.0 / n;
- }
- .
-
-
- -------- Cosine Transform of RDFT (Real Symmetric DFT) --------
- [definition]
- C[k] = sum_j=0^n a[j]*cos(pi*j*k/n), 0<=k<=n
- [usage]
- ip[0] = 0; // first time only
- dfct(n, a, t, ip, w);
- [parameters]
- n :data length - 1 (int)
- n >= 2, n = power of 2
- a[0...n] :input/output data (FPType *)
- output data
- a[k] = C[k], 0<=k<=n
- t[0...n/2] :work area (FPType *)
- ip[0...*] :work area for bit reversal (int *)
- length of ip >= 2+sqrt(n/4)
- strictly,
- length of ip >=
- 2+(1<<int(log(n/4+0.5)/log(2))/2).
- ip[0],ip[1] are pointers of the cos/sin table.
- w[0...n*5/8-1] :cos/sin table (FPType *)
- w[],ip[] are initialized if ip[0] == 0.
- [remark]
- Inverse of
- a[0] *= 0.5;
- a[n] *= 0.5;
- dfct(n, a, t, ip, w);
- is
- a[0] *= 0.5;
- a[n] *= 0.5;
- dfct(n, a, t, ip, w);
- for (j = 0; j <= n; ++j) {
- a[j] *= 2.0 / n;
- }
- .
-
-
- -------- Sine Transform of RDFT (Real Anti-symmetric DFT) --------
- [definition]
- S[k] = sum_j=1^n-1 a[j]*sin(pi*j*k/n), 0<k<n
- [usage]
- ip[0] = 0; // first time only
- dfst(n, a, t, ip, w);
- [parameters]
- n :data length + 1 (int)
- n >= 2, n = power of 2
- a[0...n-1] :input/output data (FPType *)
- output data
- a[k] = S[k], 0<k<n
- (a[0] is used for work area)
- t[0...n/2-1] :work area (FPType *)
- ip[0...*] :work area for bit reversal (int *)
- length of ip >= 2+sqrt(n/4)
- strictly,
- length of ip >=
- 2+(1<<int(log(n/4+0.5)/log(2))/2).
- ip[0],ip[1] are pointers of the cos/sin table.
- w[0...n*5/8-1] :cos/sin table (FPType *)
- w[],ip[] are initialized if ip[0] == 0.
- [remark]
- Inverse of
- dfst(n, a, t, ip, w);
- is
- dfst(n, a, t, ip, w);
- for (j = 1; j <= n - 1; ++j) {
- a[j] *= 2.0 / n;
- }
- .
-
-
- Appendix :
- The cos/sin table is recalculated when the larger table required.
- w[] and ip[] are compatible with all routines.
- */
-
- namespace r8b
- {
-
- /**
- * @brief A wrapper class around Takuya OOURA's FFT functions.
- *
- * A wrapper class around fft4g.c file's FFT functions by Takuya OOURA.
- * Provides static private functions for use by the CDSPRealFFT class.
- */
-
- class ooura_fft
- {
- friend class CDSPRealFFT;
-
- typedef double FPType;
-
- static void cdft(int n, int isgn, FPType* a, int* ip, FPType* w)
- {
- if (n > (ip[0] << 2)) { makewt(n >> 2, ip, w); }
- if (n > 4)
- {
- if (isgn >= 0)
- {
- bitrv2(n, ip + 2, a);
- cftfsub(n, a, w);
- }
- else
- {
- bitrv2conj(n, ip + 2, a);
- cftbsub(n, a, w);
- }
- }
- else if (n == 4) { cftfsub(n, a, w); }
- }
-
- static void rdft(int n, int isgn, FPType* a, int* ip, FPType* w)
- {
- int nw = ip[0];
- if (n > (nw << 2))
- {
- nw = n >> 2;
- makewt(nw, ip, w);
- }
- int nc = ip[1];
- if (n > (nc << 2))
- {
- nc = n >> 2;
- makect(nc, ip, w + nw);
- }
- if (isgn >= 0)
- {
- if (n > 4)
- {
- bitrv2(n, ip + 2, a);
- cftfsub(n, a, w);
- rftfsub(n, a, nc, w + nw);
- }
- else if (n == 4) { cftfsub(n, a, w); }
- double xi = a[0] - a[1];
- a[0] += a[1];
- a[1] = xi;
- }
- else
- {
- a[1] = 0.5 * (a[0] - a[1]);
- a[0] -= a[1];
- if (n > 4)
- {
- rftbsub(n, a, nc, w + nw);
- bitrv2(n, ip + 2, a);
- cftbsub(n, a, w);
- }
- else if (n == 4) { cftfsub(n, a, w); }
- }
- }
-
- static void ddct(int n, int isgn, FPType* a, int* ip, FPType* w)
- {
- int j;
- double xr;
-
- int nw = ip[0];
- if (n > (nw << 2))
- {
- nw = n >> 2;
- makewt(nw, ip, w);
- }
- int nc = ip[1];
- if (n > nc)
- {
- nc = n;
- makect(nc, ip, w + nw);
- }
- if (isgn < 0)
- {
- xr = a[n - 1];
- for (j = n - 2; j >= 2; j -= 2)
- {
- a[j + 1] = a[j] - a[j - 1];
- a[j] += a[j - 1];
- }
- a[1] = a[0] - xr;
- a[0] += xr;
- if (n > 4)
- {
- rftbsub(n, a, nc, w + nw);
- bitrv2(n, ip + 2, a);
- cftbsub(n, a, w);
- }
- else if (n == 4) { cftfsub(n, a, w); }
- }
- dctsub(n, a, nc, w + nw);
- if (isgn >= 0)
- {
- if (n > 4)
- {
- bitrv2(n, ip + 2, a);
- cftfsub(n, a, w);
- rftfsub(n, a, nc, w + nw);
- }
- else if (n == 4) { cftfsub(n, a, w); }
- xr = a[0] - a[1];
- a[0] += a[1];
- for (j = 2; j < n; j += 2)
- {
- a[j - 1] = a[j] - a[j + 1];
- a[j] += a[j + 1];
- }
- a[n - 1] = xr;
- }
- }
-
- static void ddst(int n, int isgn, FPType* a, int* ip, FPType* w)
- {
- int j;
- double xr;
-
- int nw = ip[0];
- if (n > (nw << 2))
- {
- nw = n >> 2;
- makewt(nw, ip, w);
- }
- int nc = ip[1];
- if (n > nc)
- {
- nc = n;
- makect(nc, ip, w + nw);
- }
- if (isgn < 0)
- {
- xr = a[n - 1];
- for (j = n - 2; j >= 2; j -= 2)
- {
- a[j + 1] = -a[j] - a[j - 1];
- a[j] -= a[j - 1];
- }
- a[1] = a[0] + xr;
- a[0] -= xr;
- if (n > 4)
- {
- rftbsub(n, a, nc, w + nw);
- bitrv2(n, ip + 2, a);
- cftbsub(n, a, w);
- }
- else if (n == 4) { cftfsub(n, a, w); }
- }
- dstsub(n, a, nc, w + nw);
- if (isgn >= 0)
- {
- if (n > 4)
- {
- bitrv2(n, ip + 2, a);
- cftfsub(n, a, w);
- rftfsub(n, a, nc, w + nw);
- }
- else if (n == 4) { cftfsub(n, a, w); }
- xr = a[0] - a[1];
- a[0] += a[1];
- for (j = 2; j < n; j += 2)
- {
- a[j - 1] = -a[j] - a[j + 1];
- a[j] -= a[j + 1];
- }
- a[n - 1] = -xr;
- }
- }
-
- static void dfct(int n, FPType* a, FPType* t, int* ip, FPType* w)
- {
- int j, k;
-
- int nw = ip[0];
- if (n > (nw << 3))
- {
- nw = n >> 3;
- makewt(nw, ip, w);
- }
- int nc = ip[1];
- if (n > (nc << 1))
- {
- nc = n >> 1;
- makect(nc, ip, w + nw);
- }
- int m = n >> 1;
- double yi = a[m];
- double xi = a[0] + a[n];
- a[0] -= a[n];
- t[0] = xi - yi;
- t[m] = xi + yi;
- if (n > 2)
- {
- int mh = m >> 1;
- for (j = 1; j < mh; ++j)
- {
- k = m - j;
- double xr = a[j] - a[n - j];
- xi = a[j] + a[n - j];
- double yr = a[k] - a[n - k];
- yi = a[k] + a[n - k];
- a[j] = xr;
- a[k] = yr;
- t[j] = xi - yi;
- t[k] = xi + yi;
- }
- t[mh] = a[mh] + a[n - mh];
- a[mh] -= a[n - mh];
- dctsub(m, a, nc, w + nw);
- if (m > 4)
- {
- bitrv2(m, ip + 2, a);
- cftfsub(m, a, w);
- rftfsub(m, a, nc, w + nw);
- }
- else if (m == 4) { cftfsub(m, a, w); }
- a[n - 1] = a[0] - a[1];
- a[1] = a[0] + a[1];
- for (j = m - 2; j >= 2; j -= 2)
- {
- a[2 * j + 1] = a[j] + a[j + 1];
- a[2 * j - 1] = a[j] - a[j + 1];
- }
- int l = 2;
- m = mh;
- while (m >= 2)
- {
- dctsub(m, t, nc, w + nw);
- if (m > 4)
- {
- bitrv2(m, ip + 2, t);
- cftfsub(m, t, w);
- rftfsub(m, t, nc, w + nw);
- }
- else if (m == 4) { cftfsub(m, t, w); }
- a[n - l] = t[0] - t[1];
- a[l] = t[0] + t[1];
- k = 0;
- for (j = 2; j < m; j += 2)
- {
- k += l << 2;
- a[k - l] = t[j] - t[j + 1];
- a[k + l] = t[j] + t[j + 1];
- }
- l <<= 1;
- mh = m >> 1;
- for (j = 0; j < mh; ++j)
- {
- k = m - j;
- t[j] = t[m + k] - t[m + j];
- t[k] = t[m + k] + t[m + j];
- }
- t[mh] = t[m + mh];
- m = mh;
- }
- a[l] = t[0];
- a[n] = t[2] - t[1];
- a[0] = t[2] + t[1];
- }
- else
- {
- a[1] = a[0];
- a[2] = t[0];
- a[0] = t[1];
- }
- }
-
- static void dfst(int n, FPType* a, FPType* t, int* ip, FPType* w)
- {
- int j, k;
-
- int nw = ip[0];
- if (n > (nw << 3))
- {
- nw = n >> 3;
- makewt(nw, ip, w);
- }
- int nc = ip[1];
- if (n > (nc << 1))
- {
- nc = n >> 1;
- makect(nc, ip, w + nw);
- }
- if (n > 2)
- {
- int m = n >> 1;
- int mh = m >> 1;
- for (j = 1; j < mh; ++j)
- {
- k = m - j;
- double xr = a[j] + a[n - j];
- double xi = a[j] - a[n - j];
- double yr = a[k] + a[n - k];
- double yi = a[k] - a[n - k];
- a[j] = xr;
- a[k] = yr;
- t[j] = xi + yi;
- t[k] = xi - yi;
- }
- t[0] = a[mh] - a[n - mh];
- a[mh] += a[n - mh];
- a[0] = a[m];
- dstsub(m, a, nc, w + nw);
- if (m > 4)
- {
- bitrv2(m, ip + 2, a);
- cftfsub(m, a, w);
- rftfsub(m, a, nc, w + nw);
- }
- else if (m == 4) { cftfsub(m, a, w); }
- a[n - 1] = a[1] - a[0];
- a[1] = a[0] + a[1];
- for (j = m - 2; j >= 2; j -= 2)
- {
- a[2 * j + 1] = a[j] - a[j + 1];
- a[2 * j - 1] = -a[j] - a[j + 1];
- }
- int l = 2;
- m = mh;
- while (m >= 2)
- {
- dstsub(m, t, nc, w + nw);
- if (m > 4)
- {
- bitrv2(m, ip + 2, t);
- cftfsub(m, t, w);
- rftfsub(m, t, nc, w + nw);
- }
- else if (m == 4) { cftfsub(m, t, w); }
- a[n - l] = t[1] - t[0];
- a[l] = t[0] + t[1];
- k = 0;
- for (j = 2; j < m; j += 2)
- {
- k += l << 2;
- a[k - l] = -t[j] - t[j + 1];
- a[k + l] = t[j] - t[j + 1];
- }
- l <<= 1;
- mh = m >> 1;
- for (j = 1; j < mh; ++j)
- {
- k = m - j;
- t[j] = t[m + k] + t[m + j];
- t[k] = t[m + k] - t[m + j];
- }
- t[0] = t[m + mh];
- m = mh;
- }
- a[l] = t[0];
- }
- a[0] = 0;
- }
-
- /* -------- initializing routines -------- */
-
- static void makewt(int nw, int* ip, FPType* w)
- {
- ip[0] = nw;
- ip[1] = 1;
- if (nw > 2)
- {
- int nwh = nw >> 1;
- double delta = atan(1.0) / nwh;
- w[0] = 1;
- w[1] = 0;
- w[nwh] = cos(delta * nwh);
- w[nwh + 1] = w[nwh];
- if (nwh > 2)
- {
- for (int j = 2; j < nwh; j += 2)
- {
- double x = cos(delta * j);
- double y = sin(delta * j);
- w[j] = x;
- w[j + 1] = y;
- w[nw - j] = y;
- w[nw - j + 1] = x;
- }
- bitrv2(nw, ip + 2, w);
- }
- }
- }
-
- static void makect(int nc, int* ip, FPType* c)
- {
- ip[1] = nc;
- if (nc > 1)
- {
- int nch = nc >> 1;
- double delta = atan(1.0) / nch;
- c[0] = cos(delta * nch);
- c[nch] = 0.5 * c[0];
- for (int j = 1; j < nch; ++j)
- {
- c[j] = 0.5 * cos(delta * j);
- c[nc - j] = 0.5 * sin(delta * j);
- }
- }
- }
-
- /* -------- child routines -------- */
-
- static void bitrv2(int n, int* ip, FPType* a)
- {
- int j, j1, k, k1;
- double xr, xi, yr, yi;
-
- ip[0] = 0;
- int l = n;
- int m = 1;
- while ((m << 3) < l)
- {
- l >>= 1;
- for (j = 0; j < m; ++j) { ip[m + j] = ip[j] + l; }
- m <<= 1;
- }
- int m2 = 2 * m;
- if ((m << 3) == l)
- {
- for (k = 0; k < m; ++k)
- {
- for (j = 0; j < k; ++j)
- {
- j1 = 2 * j + ip[k];
- k1 = 2 * k + ip[j];
- xr = a[j1];
- xi = a[j1 + 1];
- yr = a[k1];
- yi = a[k1 + 1];
- a[j1] = yr;
- a[j1 + 1] = yi;
- a[k1] = xr;
- a[k1 + 1] = xi;
- j1 += m2;
- k1 += 2 * m2;
- xr = a[j1];
- xi = a[j1 + 1];
- yr = a[k1];
- yi = a[k1 + 1];
- a[j1] = yr;
- a[j1 + 1] = yi;
- a[k1] = xr;
- a[k1 + 1] = xi;
- j1 += m2;
- k1 -= m2;
- xr = a[j1];
- xi = a[j1 + 1];
- yr = a[k1];
- yi = a[k1 + 1];
- a[j1] = yr;
- a[j1 + 1] = yi;
- a[k1] = xr;
- a[k1 + 1] = xi;
- j1 += m2;
- k1 += 2 * m2;
- xr = a[j1];
- xi = a[j1 + 1];
- yr = a[k1];
- yi = a[k1 + 1];
- a[j1] = yr;
- a[j1 + 1] = yi;
- a[k1] = xr;
- a[k1 + 1] = xi;
- }
- j1 = 2 * k + m2 + ip[k];
- k1 = j1 + m2;
- xr = a[j1];
- xi = a[j1 + 1];
- yr = a[k1];
- yi = a[k1 + 1];
- a[j1] = yr;
- a[j1 + 1] = yi;
- a[k1] = xr;
- a[k1 + 1] = xi;
- }
- }
- else
- {
- for (k = 1; k < m; ++k)
- {
- for (j = 0; j < k; ++j)
- {
- j1 = 2 * j + ip[k];
- k1 = 2 * k + ip[j];
- xr = a[j1];
- xi = a[j1 + 1];
- yr = a[k1];
- yi = a[k1 + 1];
- a[j1] = yr;
- a[j1 + 1] = yi;
- a[k1] = xr;
- a[k1 + 1] = xi;
- j1 += m2;
- k1 += m2;
- xr = a[j1];
- xi = a[j1 + 1];
- yr = a[k1];
- yi = a[k1 + 1];
- a[j1] = yr;
- a[j1 + 1] = yi;
- a[k1] = xr;
- a[k1 + 1] = xi;
- }
- }
- }
- }
-
- static void bitrv2conj(int n, int* ip, FPType* a)
- {
- int j, j1, k, k1;
- double xr, xi, yr, yi;
-
- ip[0] = 0;
- int l = n;
- int m = 1;
- while ((m << 3) < l)
- {
- l >>= 1;
- for (j = 0; j < m; ++j) { ip[m + j] = ip[j] + l; }
- m <<= 1;
- }
- int m2 = 2 * m;
- if ((m << 3) == l)
- {
- for (k = 0; k < m; ++k)
- {
- for (j = 0; j < k; ++j)
- {
- j1 = 2 * j + ip[k];
- k1 = 2 * k + ip[j];
- xr = a[j1];
- xi = -a[j1 + 1];
- yr = a[k1];
- yi = -a[k1 + 1];
- a[j1] = yr;
- a[j1 + 1] = yi;
- a[k1] = xr;
- a[k1 + 1] = xi;
- j1 += m2;
- k1 += 2 * m2;
- xr = a[j1];
- xi = -a[j1 + 1];
- yr = a[k1];
- yi = -a[k1 + 1];
- a[j1] = yr;
- a[j1 + 1] = yi;
- a[k1] = xr;
- a[k1 + 1] = xi;
- j1 += m2;
- k1 -= m2;
- xr = a[j1];
- xi = -a[j1 + 1];
- yr = a[k1];
- yi = -a[k1 + 1];
- a[j1] = yr;
- a[j1 + 1] = yi;
- a[k1] = xr;
- a[k1 + 1] = xi;
- j1 += m2;
- k1 += 2 * m2;
- xr = a[j1];
- xi = -a[j1 + 1];
- yr = a[k1];
- yi = -a[k1 + 1];
- a[j1] = yr;
- a[j1 + 1] = yi;
- a[k1] = xr;
- a[k1 + 1] = xi;
- }
- k1 = 2 * k + ip[k];
- a[k1 + 1] = -a[k1 + 1];
- j1 = k1 + m2;
- k1 = j1 + m2;
- xr = a[j1];
- xi = -a[j1 + 1];
- yr = a[k1];
- yi = -a[k1 + 1];
- a[j1] = yr;
- a[j1 + 1] = yi;
- a[k1] = xr;
- a[k1 + 1] = xi;
- k1 += m2;
- a[k1 + 1] = -a[k1 + 1];
- }
- }
- else
- {
- a[1] = -a[1];
- a[m2 + 1] = -a[m2 + 1];
- for (k = 1; k < m; ++k)
- {
- for (j = 0; j < k; ++j)
- {
- j1 = 2 * j + ip[k];
- k1 = 2 * k + ip[j];
- xr = a[j1];
- xi = -a[j1 + 1];
- yr = a[k1];
- yi = -a[k1 + 1];
- a[j1] = yr;
- a[j1 + 1] = yi;
- a[k1] = xr;
- a[k1 + 1] = xi;
- j1 += m2;
- k1 += m2;
- xr = a[j1];
- xi = -a[j1 + 1];
- yr = a[k1];
- yi = -a[k1 + 1];
- a[j1] = yr;
- a[j1 + 1] = yi;
- a[k1] = xr;
- a[k1 + 1] = xi;
- }
- k1 = 2 * k + ip[k];
- a[k1 + 1] = -a[k1 + 1];
- a[k1 + m2 + 1] = -a[k1 + m2 + 1];
- }
- }
- }
-
- static void cftfsub(int n, FPType* a, const FPType* w)
- {
- int j, j1;
- double x0r, x0i;
-
- int l = 2;
- if (n > 8)
- {
- cft1st(n, a, w);
- l = 8;
- while ((l << 2) < n)
- {
- cftmdl(n, l, a, w);
- l <<= 2;
- }
- }
- if ((l << 2) == n)
- {
- for (j = 0; j < l; j += 2)
- {
- j1 = j + l;
- int j2 = j1 + l;
- int j3 = j2 + l;
- x0r = a[j] + a[j1];
- x0i = a[j + 1] + a[j1 + 1];
- double x1r = a[j] - a[j1];
- double x1i = a[j + 1] - a[j1 + 1];
- double x2r = a[j2] + a[j3];
- double x2i = a[j2 + 1] + a[j3 + 1];
- double x3r = a[j2] - a[j3];
- double x3i = a[j2 + 1] - a[j3 + 1];
- a[j] = x0r + x2r;
- a[j + 1] = x0i + x2i;
- a[j2] = x0r - x2r;
- a[j2 + 1] = x0i - x2i;
- a[j1] = x1r - x3i;
- a[j1 + 1] = x1i + x3r;
- a[j3] = x1r + x3i;
- a[j3 + 1] = x1i - x3r;
- }
- }
- else
- {
- for (j = 0; j < l; j += 2)
- {
- j1 = j + l;
- x0r = a[j] - a[j1];
- x0i = a[j + 1] - a[j1 + 1];
- a[j] += a[j1];
- a[j + 1] += a[j1 + 1];
- a[j1] = x0r;
- a[j1 + 1] = x0i;
- }
- }
- }
-
- static void cftbsub(int n, FPType* a, const FPType* w)
- {
- int j, j1;
- double x0r, x0i;
-
- int l = 2;
- if (n > 8)
- {
- cft1st(n, a, w);
- l = 8;
- while ((l << 2) < n)
- {
- cftmdl(n, l, a, w);
- l <<= 2;
- }
- }
- if ((l << 2) == n)
- {
- for (j = 0; j < l; j += 2)
- {
- j1 = j + l;
- int j2 = j1 + l;
- int j3 = j2 + l;
- x0r = a[j] + a[j1];
- x0i = -a[j + 1] - a[j1 + 1];
- double x1r = a[j] - a[j1];
- double x1i = -a[j + 1] + a[j1 + 1];
- double x2r = a[j2] + a[j3];
- double x2i = a[j2 + 1] + a[j3 + 1];
- double x3r = a[j2] - a[j3];
- double x3i = a[j2 + 1] - a[j3 + 1];
- a[j] = x0r + x2r;
- a[j + 1] = x0i - x2i;
- a[j2] = x0r - x2r;
- a[j2 + 1] = x0i + x2i;
- a[j1] = x1r - x3i;
- a[j1 + 1] = x1i - x3r;
- a[j3] = x1r + x3i;
- a[j3 + 1] = x1i + x3r;
- }
- }
- else
- {
- for (j = 0; j < l; j += 2)
- {
- j1 = j + l;
- x0r = a[j] - a[j1];
- x0i = -a[j + 1] + a[j1 + 1];
- a[j] += a[j1];
- a[j + 1] = -a[j + 1] - a[j1 + 1];
- a[j1] = x0r;
- a[j1 + 1] = x0i;
- }
- }
- }
-
- static void cft1st(int n, FPType* a, const FPType* w)
- {
- double x0r = a[0] + a[2];
- double x0i = a[1] + a[3];
- double x1r = a[0] - a[2];
- double x1i = a[1] - a[3];
- double x2r = a[4] + a[6];
- double x2i = a[5] + a[7];
- double x3r = a[4] - a[6];
- double x3i = a[5] - a[7];
- a[0] = x0r + x2r;
- a[1] = x0i + x2i;
- a[4] = x0r - x2r;
- a[5] = x0i - x2i;
- a[2] = x1r - x3i;
- a[3] = x1i + x3r;
- a[6] = x1r + x3i;
- a[7] = x1i - x3r;
- double wk1r = w[2];
- x0r = a[8] + a[10];
- x0i = a[9] + a[11];
- x1r = a[8] - a[10];
- x1i = a[9] - a[11];
- x2r = a[12] + a[14];
- x2i = a[13] + a[15];
- x3r = a[12] - a[14];
- x3i = a[13] - a[15];
- a[8] = x0r + x2r;
- a[9] = x0i + x2i;
- a[12] = x2i - x0i;
- a[13] = x0r - x2r;
- x0r = x1r - x3i;
- x0i = x1i + x3r;
- a[10] = wk1r * (x0r - x0i);
- a[11] = wk1r * (x0r + x0i);
- x0r = x3i + x1r;
- x0i = x3r - x1i;
- a[14] = wk1r * (x0i - x0r);
- a[15] = wk1r * (x0i + x0r);
- int k1 = 0;
- for (int j = 16; j < n; j += 16)
- {
- k1 += 2;
- int k2 = 2 * k1;
- double wk2r = w[k1];
- double wk2i = w[k1 + 1];
- wk1r = w[k2];
- double wk1i = w[k2 + 1];
- double wk3r = wk1r - 2 * wk2i * wk1i;
- double wk3i = 2 * wk2i * wk1r - wk1i;
- x0r = a[j] + a[j + 2];
- x0i = a[j + 1] + a[j + 3];
- x1r = a[j] - a[j + 2];
- x1i = a[j + 1] - a[j + 3];
- x2r = a[j + 4] + a[j + 6];
- x2i = a[j + 5] + a[j + 7];
- x3r = a[j + 4] - a[j + 6];
- x3i = a[j + 5] - a[j + 7];
- a[j] = x0r + x2r;
- a[j + 1] = x0i + x2i;
- x0r -= x2r;
- x0i -= x2i;
- a[j + 4] = wk2r * x0r - wk2i * x0i;
- a[j + 5] = wk2r * x0i + wk2i * x0r;
- x0r = x1r - x3i;
- x0i = x1i + x3r;
- a[j + 2] = wk1r * x0r - wk1i * x0i;
- a[j + 3] = wk1r * x0i + wk1i * x0r;
- x0r = x1r + x3i;
- x0i = x1i - x3r;
- a[j + 6] = wk3r * x0r - wk3i * x0i;
- a[j + 7] = wk3r * x0i + wk3i * x0r;
- wk1r = w[k2 + 2];
- wk1i = w[k2 + 3];
- wk3r = wk1r - 2 * wk2r * wk1i;
- wk3i = 2 * wk2r * wk1r - wk1i;
- x0r = a[j + 8] + a[j + 10];
- x0i = a[j + 9] + a[j + 11];
- x1r = a[j + 8] - a[j + 10];
- x1i = a[j + 9] - a[j + 11];
- x2r = a[j + 12] + a[j + 14];
- x2i = a[j + 13] + a[j + 15];
- x3r = a[j + 12] - a[j + 14];
- x3i = a[j + 13] - a[j + 15];
- a[j + 8] = x0r + x2r;
- a[j + 9] = x0i + x2i;
- x0r -= x2r;
- x0i -= x2i;
- a[j + 12] = -wk2i * x0r - wk2r * x0i;
- a[j + 13] = -wk2i * x0i + wk2r * x0r;
- x0r = x1r - x3i;
- x0i = x1i + x3r;
- a[j + 10] = wk1r * x0r - wk1i * x0i;
- a[j + 11] = wk1r * x0i + wk1i * x0r;
- x0r = x1r + x3i;
- x0i = x1i - x3r;
- a[j + 14] = wk3r * x0r - wk3i * x0i;
- a[j + 15] = wk3r * x0i + wk3i * x0r;
- }
- }
-
- static void cftmdl(int n, int l, FPType* a, const FPType* w)
- {
- int j, j1, j2, j3, k, k1, k2, m, m2;
- double wk1r, wk1i, wk2r, wk2i, wk3r, wk3i;
- double x0r, x0i, x1r, x1i, x2r, x2i, x3r, x3i;
-
- m = l << 2;
- for (j = 0; j < l; j += 2)
- {
- j1 = j + l;
- j2 = j1 + l;
- j3 = j2 + l;
- x0r = a[j] + a[j1];
- x0i = a[j + 1] + a[j1 + 1];
- x1r = a[j] - a[j1];
- x1i = a[j + 1] - a[j1 + 1];
- x2r = a[j2] + a[j3];
- x2i = a[j2 + 1] + a[j3 + 1];
- x3r = a[j2] - a[j3];
- x3i = a[j2 + 1] - a[j3 + 1];
- a[j] = x0r + x2r;
- a[j + 1] = x0i + x2i;
- a[j2] = x0r - x2r;
- a[j2 + 1] = x0i - x2i;
- a[j1] = x1r - x3i;
- a[j1 + 1] = x1i + x3r;
- a[j3] = x1r + x3i;
- a[j3 + 1] = x1i - x3r;
- }
- wk1r = w[2];
- for (j = m; j < l + m; j += 2)
- {
- j1 = j + l;
- j2 = j1 + l;
- j3 = j2 + l;
- x0r = a[j] + a[j1];
- x0i = a[j + 1] + a[j1 + 1];
- x1r = a[j] - a[j1];
- x1i = a[j + 1] - a[j1 + 1];
- x2r = a[j2] + a[j3];
- x2i = a[j2 + 1] + a[j3 + 1];
- x3r = a[j2] - a[j3];
- x3i = a[j2 + 1] - a[j3 + 1];
- a[j] = x0r + x2r;
- a[j + 1] = x0i + x2i;
- a[j2] = x2i - x0i;
- a[j2 + 1] = x0r - x2r;
- x0r = x1r - x3i;
- x0i = x1i + x3r;
- a[j1] = wk1r * (x0r - x0i);
- a[j1 + 1] = wk1r * (x0r + x0i);
- x0r = x3i + x1r;
- x0i = x3r - x1i;
- a[j3] = wk1r * (x0i - x0r);
- a[j3 + 1] = wk1r * (x0i + x0r);
- }
- k1 = 0;
- m2 = 2 * m;
- for (k = m2; k < n; k += m2)
- {
- k1 += 2;
- k2 = 2 * k1;
- wk2r = w[k1];
- wk2i = w[k1 + 1];
- wk1r = w[k2];
- wk1i = w[k2 + 1];
- wk3r = wk1r - 2 * wk2i * wk1i;
- wk3i = 2 * wk2i * wk1r - wk1i;
- for (j = k; j < l + k; j += 2)
- {
- j1 = j + l;
- j2 = j1 + l;
- j3 = j2 + l;
- x0r = a[j] + a[j1];
- x0i = a[j + 1] + a[j1 + 1];
- x1r = a[j] - a[j1];
- x1i = a[j + 1] - a[j1 + 1];
- x2r = a[j2] + a[j3];
- x2i = a[j2 + 1] + a[j3 + 1];
- x3r = a[j2] - a[j3];
- x3i = a[j2 + 1] - a[j3 + 1];
- a[j] = x0r + x2r;
- a[j + 1] = x0i + x2i;
- x0r -= x2r;
- x0i -= x2i;
- a[j2] = wk2r * x0r - wk2i * x0i;
- a[j2 + 1] = wk2r * x0i + wk2i * x0r;
- x0r = x1r - x3i;
- x0i = x1i + x3r;
- a[j1] = wk1r * x0r - wk1i * x0i;
- a[j1 + 1] = wk1r * x0i + wk1i * x0r;
- x0r = x1r + x3i;
- x0i = x1i - x3r;
- a[j3] = wk3r * x0r - wk3i * x0i;
- a[j3 + 1] = wk3r * x0i + wk3i * x0r;
- }
- wk1r = w[k2 + 2];
- wk1i = w[k2 + 3];
- wk3r = wk1r - 2 * wk2r * wk1i;
- wk3i = 2 * wk2r * wk1r - wk1i;
- for (j = k + m; j < l + (k + m); j += 2)
- {
- j1 = j + l;
- j2 = j1 + l;
- j3 = j2 + l;
- x0r = a[j] + a[j1];
- x0i = a[j + 1] + a[j1 + 1];
- x1r = a[j] - a[j1];
- x1i = a[j + 1] - a[j1 + 1];
- x2r = a[j2] + a[j3];
- x2i = a[j2 + 1] + a[j3 + 1];
- x3r = a[j2] - a[j3];
- x3i = a[j2 + 1] - a[j3 + 1];
- a[j] = x0r + x2r;
- a[j + 1] = x0i + x2i;
- x0r -= x2r;
- x0i -= x2i;
- a[j2] = -wk2i * x0r - wk2r * x0i;
- a[j2 + 1] = -wk2i * x0i + wk2r * x0r;
- x0r = x1r - x3i;
- x0i = x1i + x3r;
- a[j1] = wk1r * x0r - wk1i * x0i;
- a[j1 + 1] = wk1r * x0i + wk1i * x0r;
- x0r = x1r + x3i;
- x0i = x1i - x3r;
- a[j3] = wk3r * x0r - wk3i * x0i;
- a[j3 + 1] = wk3r * x0i + wk3i * x0r;
- }
- }
- }
-
- static void rftfsub(int n, FPType* a, int nc, const FPType* c)
- {
- int m = n >> 1;
- int ks = 2 * nc / m;
- int kk = 0;
- for (int j = 2; j < m; j += 2)
- {
- int k = n - j;
- kk += ks;
- double wkr = 0.5 - c[nc - kk];
- double wki = c[kk];
- double xr = a[j] - a[k];
- double xi = a[j + 1] + a[k + 1];
- double yr = wkr * xr - wki * xi;
- double yi = wkr * xi + wki * xr;
- a[j] -= yr;
- a[j + 1] -= yi;
- a[k] += yr;
- a[k + 1] -= yi;
- }
- }
-
- static void rftbsub(int n, FPType* a, int nc, const FPType* c)
- {
- a[1] = -a[1];
- int m = n >> 1;
- int ks = 2 * nc / m;
- int kk = 0;
- for (int j = 2; j < m; j += 2)
- {
- int k = n - j;
- kk += ks;
- double wkr = 0.5 - c[nc - kk];
- double wki = c[kk];
- double xr = a[j] - a[k];
- double xi = a[j + 1] + a[k + 1];
- double yr = wkr * xr + wki * xi;
- double yi = wkr * xi - wki * xr;
- a[j] -= yr;
- a[j + 1] = yi - a[j + 1];
- a[k] += yr;
- a[k + 1] = yi - a[k + 1];
- }
- a[m + 1] = -a[m + 1];
- }
-
- static void dctsub(int n, FPType* a, int nc, const FPType* c)
- {
- int m = n >> 1;
- int ks = nc / n;
- int kk = 0;
- for (int j = 1; j < m; ++j)
- {
- int k = n - j;
- kk += ks;
- double wkr = c[kk] - c[nc - kk];
- double wki = c[kk] + c[nc - kk];
- double xr = wki * a[j] - wkr * a[k];
- a[j] = wkr * a[j] + wki * a[k];
- a[k] = xr;
- }
- a[m] *= c[0];
- }
-
- static void dstsub(int n, FPType* a, int nc, const FPType* c)
- {
- int m = n >> 1;
- int ks = nc / n;
- int kk = 0;
- for (int j = 1; j < m; ++j)
- {
- int k = n - j;
- kk += ks;
- double wkr = c[kk] - c[nc - kk];
- double wki = c[kk] + c[nc - kk];
- double xr = wki * a[k] - wkr * a[j];
- a[k] = wkr * a[k] + wki * a[j];
- a[j] = xr;
- }
- a[m] *= c[0];
- }
- };
- } // namespace r8b
-
- #endif // R8B_FFT4G_INCLUDED
|