1 | //$$ fft.cpp Fast fourier transform |
---|
2 | |
---|
3 | // Copyright (C) 1991,2,3,4,8: R B Davies |
---|
4 | |
---|
5 | |
---|
6 | #define WANT_MATH |
---|
7 | // #define WANT_STREAM |
---|
8 | |
---|
9 | #include "include.h" |
---|
10 | |
---|
11 | #include "newmatap.h" |
---|
12 | |
---|
13 | // #include "newmatio.h" |
---|
14 | |
---|
15 | #ifdef use_namespace |
---|
16 | namespace NEWMAT { |
---|
17 | #endif |
---|
18 | |
---|
19 | #ifdef DO_REPORT |
---|
20 | #define REPORT { static ExeCounter ExeCount(__LINE__,19); ++ExeCount; } |
---|
21 | #else |
---|
22 | #define REPORT {} |
---|
23 | #endif |
---|
24 | |
---|
25 | static void cossin(int n, int d, Real& c, Real& s) |
---|
26 | // calculate cos(twopi*n/d) and sin(twopi*n/d) |
---|
27 | // minimise roundoff error |
---|
28 | { |
---|
29 | REPORT |
---|
30 | long n4 = n * 4; int sector = (int)floor( (Real)n4 / (Real)d + 0.5 ); |
---|
31 | n4 -= sector * d; |
---|
32 | if (sector < 0) { REPORT sector = 3 - (3 - sector) % 4; } |
---|
33 | else { REPORT sector %= 4; } |
---|
34 | Real ratio = 1.5707963267948966192 * (Real)n4 / (Real)d; |
---|
35 | |
---|
36 | switch (sector) |
---|
37 | { |
---|
38 | case 0: REPORT c = cos(ratio); s = sin(ratio); break; |
---|
39 | case 1: REPORT c = -sin(ratio); s = cos(ratio); break; |
---|
40 | case 2: REPORT c = -cos(ratio); s = -sin(ratio); break; |
---|
41 | case 3: REPORT c = sin(ratio); s = -cos(ratio); break; |
---|
42 | } |
---|
43 | } |
---|
44 | |
---|
45 | static void fftstep(ColumnVector& A, ColumnVector& B, ColumnVector& X, |
---|
46 | ColumnVector& Y, int after, int now, int before) |
---|
47 | { |
---|
48 | REPORT |
---|
49 | Tracer trace("FFT(step)"); |
---|
50 | // const Real twopi = 6.2831853071795864769; |
---|
51 | const int gamma = after * before; const int delta = now * after; |
---|
52 | // const Real angle = twopi / delta; Real temp; |
---|
53 | // Real r_omega = cos(angle); Real i_omega = -sin(angle); |
---|
54 | Real r_arg = 1.0; Real i_arg = 0.0; |
---|
55 | Real* x = X.Store(); Real* y = Y.Store(); // pointers to array storage |
---|
56 | const int m = A.Nrows() - gamma; |
---|
57 | |
---|
58 | for (int j = 0; j < now; j++) |
---|
59 | { |
---|
60 | Real* a = A.Store(); Real* b = B.Store(); // pointers to array storage |
---|
61 | Real* x1 = x; Real* y1 = y; x += after; y += after; |
---|
62 | for (int ia = 0; ia < after; ia++) |
---|
63 | { |
---|
64 | // generate sins & cosines explicitly rather than iteratively |
---|
65 | // for more accuracy; but slower |
---|
66 | cossin(-(j*after+ia), delta, r_arg, i_arg); |
---|
67 | |
---|
68 | Real* a1 = a++; Real* b1 = b++; Real* x2 = x1++; Real* y2 = y1++; |
---|
69 | if (now==2) |
---|
70 | { |
---|
71 | REPORT int ib = before; |
---|
72 | if (ib) for (;;) |
---|
73 | { |
---|
74 | REPORT |
---|
75 | Real* a2 = m + a1; Real* b2 = m + b1; a1 += after; b1 += after; |
---|
76 | Real r_value = *a2; Real i_value = *b2; |
---|
77 | *x2 = r_value * r_arg - i_value * i_arg + *(a2-gamma); |
---|
78 | *y2 = r_value * i_arg + i_value * r_arg + *(b2-gamma); |
---|
79 | if (!(--ib)) break; |
---|
80 | x2 += delta; y2 += delta; |
---|
81 | } |
---|
82 | } |
---|
83 | else |
---|
84 | { |
---|
85 | REPORT int ib = before; |
---|
86 | if (ib) for (;;) |
---|
87 | { |
---|
88 | REPORT |
---|
89 | Real* a2 = m + a1; Real* b2 = m + b1; a1 += after; b1 += after; |
---|
90 | Real r_value = *a2; Real i_value = *b2; |
---|
91 | int in = now-1; while (in--) |
---|
92 | { |
---|
93 | // it should be possible to make this faster |
---|
94 | // hand code for now = 2,3,4,5,8 |
---|
95 | // use symmetry to halve number of operations |
---|
96 | a2 -= gamma; b2 -= gamma; Real temp = r_value; |
---|
97 | r_value = r_value * r_arg - i_value * i_arg + *a2; |
---|
98 | i_value = temp * i_arg + i_value * r_arg + *b2; |
---|
99 | } |
---|
100 | *x2 = r_value; *y2 = i_value; |
---|
101 | if (!(--ib)) break; |
---|
102 | x2 += delta; y2 += delta; |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | // temp = r_arg; |
---|
107 | // r_arg = r_arg * r_omega - i_arg * i_omega; |
---|
108 | // i_arg = temp * i_omega + i_arg * r_omega; |
---|
109 | |
---|
110 | } |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | void FFTI(const ColumnVector& U, const ColumnVector& V, |
---|
116 | ColumnVector& X, ColumnVector& Y) |
---|
117 | { |
---|
118 | // Inverse transform |
---|
119 | Tracer trace("FFTI"); |
---|
120 | REPORT |
---|
121 | FFT(U,-V,X,Y); |
---|
122 | const Real n = X.Nrows(); X /= n; Y /= (-n); |
---|
123 | } |
---|
124 | |
---|
125 | void RealFFT(const ColumnVector& U, ColumnVector& X, ColumnVector& Y) |
---|
126 | { |
---|
127 | // Fourier transform of a real series |
---|
128 | Tracer trace("RealFFT"); |
---|
129 | REPORT |
---|
130 | const int n = U.Nrows(); // length of arrays |
---|
131 | const int n2 = n / 2; |
---|
132 | if (n != 2 * n2) |
---|
133 | Throw(ProgramException("Vector length not multiple of 2", U)); |
---|
134 | ColumnVector A(n2), B(n2); |
---|
135 | Real* a = A.Store(); Real* b = B.Store(); Real* u = U.Store(); int i = n2; |
---|
136 | while (i--) { *a++ = *u++; *b++ = *u++; } |
---|
137 | FFT(A,B,A,B); |
---|
138 | int n21 = n2 + 1; |
---|
139 | X.ReSize(n21); Y.ReSize(n21); |
---|
140 | i = n2 - 1; |
---|
141 | a = A.Store(); b = B.Store(); // first els of A and B |
---|
142 | Real* an = a + i; Real* bn = b + i; // last els of A and B |
---|
143 | Real* x = X.Store(); Real* y = Y.Store(); // first els of X and Y |
---|
144 | Real* xn = x + n2; Real* yn = y + n2; // last els of X and Y |
---|
145 | |
---|
146 | *x++ = *a + *b; *y++ = 0.0; // first complex element |
---|
147 | *xn-- = *a++ - *b++; *yn-- = 0.0; // last complex element |
---|
148 | |
---|
149 | int j = -1; i = n2/2; |
---|
150 | while (i--) |
---|
151 | { |
---|
152 | Real c,s; cossin(j--,n,c,s); |
---|
153 | Real am = *a - *an; Real ap = *a++ + *an--; |
---|
154 | Real bm = *b - *bn; Real bp = *b++ + *bn--; |
---|
155 | Real samcbp = s * am + c * bp; Real sbpcam = s * bp - c * am; |
---|
156 | *x++ = 0.5 * ( ap + samcbp); *y++ = 0.5 * ( bm + sbpcam); |
---|
157 | *xn-- = 0.5 * ( ap - samcbp); *yn-- = 0.5 * (-bm + sbpcam); |
---|
158 | } |
---|
159 | } |
---|
160 | |
---|
161 | void RealFFTI(const ColumnVector& A, const ColumnVector& B, ColumnVector& U) |
---|
162 | { |
---|
163 | // inverse of a Fourier transform of a real series |
---|
164 | Tracer trace("RealFFTI"); |
---|
165 | REPORT |
---|
166 | const int n21 = A.Nrows(); // length of arrays |
---|
167 | if (n21 != B.Nrows() || n21 == 0) |
---|
168 | Throw(ProgramException("Vector lengths unequal or zero", A, B)); |
---|
169 | const int n2 = n21 - 1; const int n = 2 * n2; int i = n2 - 1; |
---|
170 | |
---|
171 | ColumnVector X(n2), Y(n2); |
---|
172 | Real* a = A.Store(); Real* b = B.Store(); // first els of A and B |
---|
173 | Real* an = a + n2; Real* bn = b + n2; // last els of A and B |
---|
174 | Real* x = X.Store(); Real* y = Y.Store(); // first els of X and Y |
---|
175 | Real* xn = x + i; Real* yn = y + i; // last els of X and Y |
---|
176 | |
---|
177 | Real hn = 0.5 / n2; |
---|
178 | *x++ = hn * (*a + *an); *y++ = - hn * (*a - *an); |
---|
179 | a++; an--; b++; bn--; |
---|
180 | int j = -1; i = n2/2; |
---|
181 | while (i--) |
---|
182 | { |
---|
183 | Real c,s; cossin(j--,n,c,s); |
---|
184 | Real am = *a - *an; Real ap = *a++ + *an--; |
---|
185 | Real bm = *b - *bn; Real bp = *b++ + *bn--; |
---|
186 | Real samcbp = s * am - c * bp; Real sbpcam = s * bp + c * am; |
---|
187 | *x++ = hn * ( ap + samcbp); *y++ = - hn * ( bm + sbpcam); |
---|
188 | *xn-- = hn * ( ap - samcbp); *yn-- = - hn * (-bm + sbpcam); |
---|
189 | } |
---|
190 | FFT(X,Y,X,Y); // have done inverting elsewhere |
---|
191 | U.ReSize(n); i = n2; |
---|
192 | x = X.Store(); y = Y.Store(); Real* u = U.Store(); |
---|
193 | while (i--) { *u++ = *x++; *u++ = - *y++; } |
---|
194 | } |
---|
195 | |
---|
196 | void FFT(const ColumnVector& U, const ColumnVector& V, |
---|
197 | ColumnVector& X, ColumnVector& Y) |
---|
198 | { |
---|
199 | // from Carl de Boor (1980), Siam J Sci Stat Comput, 1 173-8 |
---|
200 | // but first try Sande and Gentleman |
---|
201 | Tracer trace("FFT"); |
---|
202 | REPORT |
---|
203 | const int n = U.Nrows(); // length of arrays |
---|
204 | if (n != V.Nrows() || n == 0) |
---|
205 | Throw(ProgramException("Vector lengths unequal or zero", U, V)); |
---|
206 | if (n == 1) { REPORT X = U; Y = V; return; } |
---|
207 | |
---|
208 | // see if we can use the newfft routine |
---|
209 | if (!FFT_Controller::OnlyOldFFT && FFT_Controller::CanFactor(n)) |
---|
210 | { |
---|
211 | REPORT |
---|
212 | X = U; Y = V; |
---|
213 | if ( FFT_Controller::ar_1d_ft(n,X.Store(),Y.Store()) ) return; |
---|
214 | } |
---|
215 | |
---|
216 | ColumnVector B = V; |
---|
217 | ColumnVector A = U; |
---|
218 | X.ReSize(n); Y.ReSize(n); |
---|
219 | const int nextmx = 8; |
---|
220 | #ifndef ATandT |
---|
221 | int prime[8] = { 2,3,5,7,11,13,17,19 }; |
---|
222 | #else |
---|
223 | int prime[8]; |
---|
224 | prime[0]=2; prime[1]=3; prime[2]=5; prime[3]=7; |
---|
225 | prime[4]=11; prime[5]=13; prime[6]=17; prime[7]=19; |
---|
226 | #endif |
---|
227 | int after = 1; int before = n; int next = 0; bool inzee = true; |
---|
228 | int now = 0; int b1; // initialised to keep gnu happy |
---|
229 | |
---|
230 | do |
---|
231 | { |
---|
232 | for (;;) |
---|
233 | { |
---|
234 | if (next < nextmx) { REPORT now = prime[next]; } |
---|
235 | b1 = before / now; if (b1 * now == before) { REPORT break; } |
---|
236 | next++; now += 2; |
---|
237 | } |
---|
238 | before = b1; |
---|
239 | |
---|
240 | if (inzee) { REPORT fftstep(A, B, X, Y, after, now, before); } |
---|
241 | else { REPORT fftstep(X, Y, A, B, after, now, before); } |
---|
242 | |
---|
243 | inzee = !inzee; after *= now; |
---|
244 | } |
---|
245 | while (before != 1); |
---|
246 | |
---|
247 | if (inzee) { REPORT A.Release(); X = A; B.Release(); Y = B; } |
---|
248 | } |
---|
249 | |
---|
250 | // Trigonometric transforms |
---|
251 | // see Charles Van Loan (1992) "Computational frameworks for the fast |
---|
252 | // Fourier transform" published by SIAM; section 4.4. |
---|
253 | |
---|
254 | void DCT_II(const ColumnVector& U, ColumnVector& V) |
---|
255 | { |
---|
256 | // Discrete cosine transform, type II, of a real series |
---|
257 | Tracer trace("DCT_II"); |
---|
258 | REPORT |
---|
259 | const int n = U.Nrows(); // length of arrays |
---|
260 | const int n2 = n / 2; const int n4 = n * 4; |
---|
261 | if (n != 2 * n2) |
---|
262 | Throw(ProgramException("Vector length not multiple of 2", U)); |
---|
263 | ColumnVector A(n); |
---|
264 | Real* a = A.Store(); Real* b = a + n; Real* u = U.Store(); |
---|
265 | int i = n2; |
---|
266 | while (i--) { *a++ = *u++; *(--b) = *u++; } |
---|
267 | ColumnVector X, Y; |
---|
268 | RealFFT(A, X, Y); A.CleanUp(); |
---|
269 | V.ReSize(n); |
---|
270 | Real* x = X.Store(); Real* y = Y.Store(); |
---|
271 | Real* v = V.Store(); Real* w = v + n; |
---|
272 | *v = *x; |
---|
273 | int k = 0; i = n2; |
---|
274 | while (i--) |
---|
275 | { |
---|
276 | Real c, s; cossin(++k, n4, c, s); |
---|
277 | Real xi = *(++x); Real yi = *(++y); |
---|
278 | *(++v) = xi * c + yi * s; *(--w) = xi * s - yi * c; |
---|
279 | } |
---|
280 | } |
---|
281 | |
---|
282 | void DCT_II_inverse(const ColumnVector& V, ColumnVector& U) |
---|
283 | { |
---|
284 | // Inverse of discrete cosine transform, type II |
---|
285 | Tracer trace("DCT_II_inverse"); |
---|
286 | REPORT |
---|
287 | const int n = V.Nrows(); // length of array |
---|
288 | const int n2 = n / 2; const int n4 = n * 4; const int n21 = n2 + 1; |
---|
289 | if (n != 2 * n2) |
---|
290 | Throw(ProgramException("Vector length not multiple of 2", V)); |
---|
291 | ColumnVector X(n21), Y(n21); |
---|
292 | Real* x = X.Store(); Real* y = Y.Store(); |
---|
293 | Real* v = V.Store(); Real* w = v + n; |
---|
294 | *x = *v; *y = 0.0; |
---|
295 | int i = n2; int k = 0; |
---|
296 | while (i--) |
---|
297 | { |
---|
298 | Real c, s; cossin(++k, n4, c, s); |
---|
299 | Real vi = *(++v); Real wi = *(--w); |
---|
300 | *(++x) = vi * c + wi * s; *(++y) = vi * s - wi * c; |
---|
301 | } |
---|
302 | ColumnVector A; RealFFTI(X, Y, A); |
---|
303 | X.CleanUp(); Y.CleanUp(); U.ReSize(n); |
---|
304 | Real* a = A.Store(); Real* b = a + n; Real* u = U.Store(); |
---|
305 | i = n2; |
---|
306 | while (i--) { *u++ = *a++; *u++ = *(--b); } |
---|
307 | } |
---|
308 | |
---|
309 | void DST_II(const ColumnVector& U, ColumnVector& V) |
---|
310 | { |
---|
311 | // Discrete sine transform, type II, of a real series |
---|
312 | Tracer trace("DST_II"); |
---|
313 | REPORT |
---|
314 | const int n = U.Nrows(); // length of arrays |
---|
315 | const int n2 = n / 2; const int n4 = n * 4; |
---|
316 | if (n != 2 * n2) |
---|
317 | Throw(ProgramException("Vector length not multiple of 2", U)); |
---|
318 | ColumnVector A(n); |
---|
319 | Real* a = A.Store(); Real* b = a + n; Real* u = U.Store(); |
---|
320 | int i = n2; |
---|
321 | while (i--) { *a++ = *u++; *(--b) = -(*u++); } |
---|
322 | ColumnVector X, Y; |
---|
323 | RealFFT(A, X, Y); A.CleanUp(); |
---|
324 | V.ReSize(n); |
---|
325 | Real* x = X.Store(); Real* y = Y.Store(); |
---|
326 | Real* v = V.Store(); Real* w = v + n; |
---|
327 | *(--w) = *x; |
---|
328 | int k = 0; i = n2; |
---|
329 | while (i--) |
---|
330 | { |
---|
331 | Real c, s; cossin(++k, n4, c, s); |
---|
332 | Real xi = *(++x); Real yi = *(++y); |
---|
333 | *v++ = xi * s - yi * c; *(--w) = xi * c + yi * s; |
---|
334 | } |
---|
335 | } |
---|
336 | |
---|
337 | void DST_II_inverse(const ColumnVector& V, ColumnVector& U) |
---|
338 | { |
---|
339 | // Inverse of discrete sine transform, type II |
---|
340 | Tracer trace("DST_II_inverse"); |
---|
341 | REPORT |
---|
342 | const int n = V.Nrows(); // length of array |
---|
343 | const int n2 = n / 2; const int n4 = n * 4; const int n21 = n2 + 1; |
---|
344 | if (n != 2 * n2) |
---|
345 | Throw(ProgramException("Vector length not multiple of 2", V)); |
---|
346 | ColumnVector X(n21), Y(n21); |
---|
347 | Real* x = X.Store(); Real* y = Y.Store(); |
---|
348 | Real* v = V.Store(); Real* w = v + n; |
---|
349 | *x = *(--w); *y = 0.0; |
---|
350 | int i = n2; int k = 0; |
---|
351 | while (i--) |
---|
352 | { |
---|
353 | Real c, s; cossin(++k, n4, c, s); |
---|
354 | Real vi = *v++; Real wi = *(--w); |
---|
355 | *(++x) = vi * s + wi * c; *(++y) = - vi * c + wi * s; |
---|
356 | } |
---|
357 | ColumnVector A; RealFFTI(X, Y, A); |
---|
358 | X.CleanUp(); Y.CleanUp(); U.ReSize(n); |
---|
359 | Real* a = A.Store(); Real* b = a + n; Real* u = U.Store(); |
---|
360 | i = n2; |
---|
361 | while (i--) { *u++ = *a++; *u++ = -(*(--b)); } |
---|
362 | } |
---|
363 | |
---|
364 | void DCT_inverse(const ColumnVector& V, ColumnVector& U) |
---|
365 | { |
---|
366 | // Inverse of discrete cosine transform, type I |
---|
367 | Tracer trace("DCT_inverse"); |
---|
368 | REPORT |
---|
369 | const int n = V.Nrows()-1; // length of transform |
---|
370 | const int n2 = n / 2; const int n21 = n2 + 1; |
---|
371 | if (n != 2 * n2) |
---|
372 | Throw(ProgramException("Vector length not multiple of 2", V)); |
---|
373 | ColumnVector X(n21), Y(n21); |
---|
374 | Real* x = X.Store(); Real* y = Y.Store(); Real* v = V.Store(); |
---|
375 | Real vi = *v++; *x++ = vi; *y++ = 0.0; |
---|
376 | Real sum1 = vi / 2.0; Real sum2 = sum1; vi = *v++; |
---|
377 | int i = n2-1; |
---|
378 | while (i--) |
---|
379 | { |
---|
380 | Real vi2 = *v++; sum1 += vi2 + vi; sum2 += vi2 - vi; |
---|
381 | *x++ = vi2; vi2 = *v++; *y++ = vi - vi2; vi = vi2; |
---|
382 | } |
---|
383 | sum1 += vi; sum2 -= vi; |
---|
384 | vi = *v; *x = vi; *y = 0.0; vi /= 2.0; sum1 += vi; sum2 += vi; |
---|
385 | ColumnVector A; RealFFTI(X, Y, A); |
---|
386 | X.CleanUp(); Y.CleanUp(); U.ReSize(n+1); |
---|
387 | Real* a = A.Store(); Real* b = a + n; Real* u = U.Store(); v = u + n; |
---|
388 | i = n2; int k = 0; *u++ = sum1 / n2; *v-- = sum2 / n2; |
---|
389 | while (i--) |
---|
390 | { |
---|
391 | Real s = sin(1.5707963267948966192 * (++k) / n2); |
---|
392 | Real ai = *(++a); Real bi = *(--b); |
---|
393 | Real bz = (ai - bi) / 4 / s; Real az = (ai + bi) / 2; |
---|
394 | *u++ = az - bz; *v-- = az + bz; |
---|
395 | } |
---|
396 | } |
---|
397 | |
---|
398 | void DCT(const ColumnVector& U, ColumnVector& V) |
---|
399 | { |
---|
400 | // Discrete cosine transform, type I |
---|
401 | Tracer trace("DCT"); |
---|
402 | REPORT |
---|
403 | DCT_inverse(U, V); |
---|
404 | V *= (V.Nrows()-1)/2; |
---|
405 | } |
---|
406 | |
---|
407 | void DST_inverse(const ColumnVector& V, ColumnVector& U) |
---|
408 | { |
---|
409 | // Inverse of discrete sine transform, type I |
---|
410 | Tracer trace("DST_inverse"); |
---|
411 | REPORT |
---|
412 | const int n = V.Nrows()-1; // length of transform |
---|
413 | const int n2 = n / 2; const int n21 = n2 + 1; |
---|
414 | if (n != 2 * n2) |
---|
415 | Throw(ProgramException("Vector length not multiple of 2", V)); |
---|
416 | ColumnVector X(n21), Y(n21); |
---|
417 | Real* x = X.Store(); Real* y = Y.Store(); Real* v = V.Store(); |
---|
418 | Real vi = *(++v); *x++ = 2 * vi; *y++ = 0.0; |
---|
419 | int i = n2-1; |
---|
420 | while (i--) { *y++ = *(++v); Real vi2 = *(++v); *x++ = vi2 - vi; vi = vi2; } |
---|
421 | *x = -2 * vi; *y = 0.0; |
---|
422 | ColumnVector A; RealFFTI(X, Y, A); |
---|
423 | X.CleanUp(); Y.CleanUp(); U.ReSize(n+1); |
---|
424 | Real* a = A.Store(); Real* b = a + n; Real* u = U.Store(); v = u + n; |
---|
425 | i = n2; int k = 0; *u++ = 0.0; *v-- = 0.0; |
---|
426 | while (i--) |
---|
427 | { |
---|
428 | Real s = sin(1.5707963267948966192 * (++k) / n2); |
---|
429 | Real ai = *(++a); Real bi = *(--b); |
---|
430 | Real az = (ai + bi) / 4 / s; Real bz = (ai - bi) / 2; |
---|
431 | *u++ = az - bz; *v-- = az + bz; |
---|
432 | } |
---|
433 | } |
---|
434 | |
---|
435 | void DST(const ColumnVector& U, ColumnVector& V) |
---|
436 | { |
---|
437 | // Discrete sine transform, type I |
---|
438 | Tracer trace("DST"); |
---|
439 | REPORT |
---|
440 | DST_inverse(U, V); |
---|
441 | V *= (V.Nrows()-1)/2; |
---|
442 | } |
---|
443 | |
---|
444 | |
---|
445 | |
---|
446 | #ifdef use_namespace |
---|
447 | } |
---|
448 | #endif |
---|
449 | |
---|
450 | |
---|