Debian Codegraph @

src/dr/math/FastFourierTransform.java

raw symbols SCIP
1 /*
2 * FastFourierTransform.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.math;
27
28 /**
29 * Performs FFT on vectors with lengths equaling powers-of-2
30 *
31 * @author Marc A. Suchard
32 */
33 public class FastFourierTransform {
34
35 /**
36 * Computes the fast fourier transform
37 *
38 * @param data an interleaved array of (real,complex) values
39 * @param nn data length
40 * @param inverse true is performing inverse FFT
41 */
42 public static void fft(double[] data, int nn, boolean inverse) {
43 int n, mmax, m, j, istep, i;
44 double wtemp, wr, wpr, wpi, wi, theta;
45 double tempr, tempi;
46
47 final double radians;
48 if (inverse) {
49 radians = 2.0 * Math.PI;
50 } else {
51 radians = -2.0 * Math.PI;
52 }
53
54 // reverse-binary reindexing
55 n = nn << 1;
56 j = 1;
57 for (i = 1; i < n; i += 2) {
58 if (j > i) {
59 swap(data, j - 1, i - 1);
60 swap(data, j, i);
61 }
62 m = nn;
63 while (m >= 2 && j > m) {
64 j -= m;
65 m >>= 1;
66 }
67 j += m;
68 }
69
70 // here begins the Danielson-Lanczos section
71 mmax = 2;
72 while (n > mmax) {
73 istep = mmax << 1;
74 theta = radians / mmax;
75 wtemp = Math.sin(0.5 * theta);
76 wpr = -2.0 * wtemp * wtemp;
77 wpi = Math.sin(theta);
78 wr = 1.0;
79 wi = 0.0;
80 for (m = 1; m < mmax; m += 2) {
81 for (i = m; i <= n; i += istep) {
82 j = i + mmax;
83 tempr = wr * data[j - 1] - wi * data[j];
84 tempi = wr * data[j] + wi * data[j - 1];
85
86 data[j - 1] = data[i - 1] - tempr;
87 data[j] = data[i] - tempi;
88 data[i - 1] += tempr;
89 data[i] += tempi;
90 }
91 wtemp = wr;
92 wr += wr * wpr - wi * wpi;
93 wi += wi * wpr + wtemp * wpi;
94 }
95 mmax = istep;
96 }
97 }
98
99
100 /**
101 * Computes the fast fourier transform
102 *
103 * @param ca an array of complex values
104 * @param inverse true if performing inverse FFT
105 */
106 public static void fft(ComplexArray ca, boolean inverse) {
107
108 final double[] real = ca.real;
109 final double[] complex = ca.complex;
110
111 int n, mmax, m, j, istep, i;
112 double wtemp, wr, wpr, wpi, wi, theta;
113 double tempr, tempi;
114
115 final double radians;
116 if (inverse) {
117 radians = 2.0 * Math.PI;
118 } else {
119 radians = -2.0 * Math.PI;
120 }
121
122 // reverse-binary reindexing
123 n = ca.length << 1;
124 j = 1;
125 for (i = 1; i < n; i += 2) {
126 if (j > i) {
127 final int halfI = i >> 1;
128 final int halfJ = j >> 1;
129 swap(real, halfJ, halfI);
130 swap(complex, halfJ, halfI);
131 }
132 m = ca.length;
133 while (m >= 2 && j > m) {
134 j -= m;
135 m >>= 1;
136 }
137 j += m;
138 }
139
140 // here begins the Danielson-Lanczos section
141 mmax = 2;
142 while (n > mmax) {
143 istep = mmax << 1;
144 theta = (radians / mmax);
145 wtemp = Math.sin(0.5 * theta);
146 wpr = -2.0 * wtemp * wtemp;
147 wpi = Math.sin(theta);
148 wr = 1.0;
149 wi = 0.0;
150 for (m = 1; m < mmax; m += 2) {
151 for (i = m; i <= n; i += istep) {
152 j = i + mmax;
153 final int halfI = i >> 1;
154 final int halfJ = j >> 1;
155 tempr = wr * real[halfJ] - wi * complex[halfJ];
156 tempi = wr * complex[halfJ] + wi * real[halfJ];
157
158 real[halfJ] = real[halfI] - tempr;
159 complex[halfJ] = complex[halfI] - tempi;
160 real[halfI] += tempr;
161 complex[halfI] += tempi;
162 }
163 wtemp = wr;
164 wr += wr * wpr - wi * wpi;
165 wi += wi * wpr + wtemp * wpi;
166 }
167 mmax = istep;
168 }
169 }
170
171 private static void swap(double[] x, int i, int j) {
172 double tmp = x[i];
173 x[i] = x[j];
174 x[j] = tmp;
175 }
176 }