33 static void four1(
float data[],
unsigned long nn,
int isign)
35 unsigned long n, mmax, m, j, i;
36 double wtemp, wr, wpr, wpi, wi,
52 while (m >= 2 && j > m)
63 unsigned long istep = mmax << 1;
64 theta = isign * (6.28318530717959 /
66 wtemp = sin(0.5 * theta);
67 wpr = -2.0 * wtemp * wtemp;
71 for (m = 1; m < mmax; m += 2)
73 for (i = m; i <=
n; i += istep)
76 tempr = (float)(wr *
data[j] - wi *
data[j + 1]);
77 tempi = (float)(wr *
data[j + 1] + wi *
data[j]);
83 wr = (wtemp = wr) * wpr - wi * wpi +
85 wi = wi * wpr + wtemp * wpi + wi;
104 unsigned long i, i1, i2, i3, i4, np3;
105 float c1 = 0.5, c2, h1r, h1i, h2r, h2i;
106 double wr, wi, wpr, wpi, wtemp,
108 theta = 3.141592653589793 / (double)(
n >> 1);
113 wtemp = sin(0.5 * theta);
114 wpr = -2.0 * wtemp * wtemp;
119 for (i = 2; i <= (
n >> 2); i++)
121 i4 = 1 + (i3 = np3 - (i2 = 1 + (i1 = i + i - 1)));
128 (float)(h1r + wr * h2r - wi * h2i);
131 data[i2] = (float)(h1i + wr * h2i + wi * h2r);
132 data[i3] = (float)(h1r - wr * h2r + wi * h2i);
133 data[i4] = (float)(-h1i + wr * h2i + wi * h2r);
134 wr = (wtemp = wr) * wpr - wi * wpi + wr;
135 wi = wi * wpr + wtemp * wpi + wi;
160 int j, j1, j2, j3, k, k1, ks, l, m;
161 FFT_TYPE wk1r, wk1i, wk2r, wk2i, wk3r, wk3i;
162 FFT_TYPE x0r, x0i, x1r, x1i, x2r, x2i, x3r, x3i;
168 for (j = 0; j <= l - 2; j += 2)
174 x0i =
a[j + 1] +
a[j1 + 1];
176 x1i =
a[j + 1] -
a[j1 + 1];
178 x2i =
a[j2 + 1] +
a[j3 + 1];
180 x3i =
a[j2 + 1] -
a[j3 + 1];
182 a[j + 1] = x0i + x2i;
184 a[j2 + 1] = x0i - x2i;
186 a[j1 + 1] = x1i + x3r;
188 a[j3 + 1] = x1i - x3r;
193 for (j = m; j <= l + m - 2; j += 2)
199 x0i =
a[j + 1] +
a[j1 + 1];
201 x1i =
a[j + 1] -
a[j1 + 1];
203 x2i =
a[j2 + 1] +
a[j3 + 1];
205 x3i =
a[j2 + 1] -
a[j3 + 1];
207 a[j + 1] = x0i + x2i;
209 a[j2 + 1] = x0r - x2r;
212 a[j1] = wk1r * (x0r - x0i);
213 a[j1 + 1] = wk1r * (x0r + x0i);
216 a[j3] = wk1r * (x0i - x0r);
217 a[j3 + 1] = wk1r * (x0i + x0r);
221 for (k = (m << 1); k <=
n - m; k += m)
226 wk1i =
w[(k1 << 1) + 1];
229 wk3r = wk1r - 2 * wk2i * wk1i;
230 wk3i = 2 * wk2i * wk1r - wk1i;
231 for (j = k; j <= l + k - 2; j += 2)
237 x0i =
a[j + 1] +
a[j1 + 1];
239 x1i =
a[j + 1] -
a[j1 + 1];
241 x2i =
a[j2 + 1] +
a[j3 + 1];
243 x3i =
a[j2 + 1] -
a[j3 + 1];
245 a[j + 1] = x0i + x2i;
248 a[j2] = wk2r * x0r - wk2i * x0i;
249 a[j2 + 1] = wk2r * x0i + wk2i * x0r;
252 a[j1] = wk1r * x0r - wk1i * x0i;
253 a[j1 + 1] = wk1r * x0i + wk1i * x0r;
256 a[j3] = wk3r * x0r - wk3i * x0i;
257 a[j3 + 1] = wk3r * x0i + wk3i * x0r;
265 for (j = 0; j <= l - 2; j += 2)
269 x0i =
a[j + 1] -
a[j1 + 1];
271 a[j + 1] +=
a[j1 + 1];
280 int j, j1, j2, j3, k, k1, ks, l, m;
281 FFT_TYPE wk1r, wk1i, wk2r, wk2i, wk3r, wk3i;
282 FFT_TYPE x0r, x0i, x1r, x1i, x2r, x2i, x3r, x3i;
288 for (j = 0; j <= l - 2; j += 2)
294 x0i =
a[j + 1] +
a[j1 + 1];
296 x1i =
a[j + 1] -
a[j1 + 1];
298 x2i =
a[j2 + 1] +
a[j3 + 1];
300 x3i =
a[j2 + 1] -
a[j3 + 1];
302 a[j + 1] = x0i + x2i;
304 a[j2 + 1] = x0i - x2i;
306 a[j1 + 1] = x1i - x3r;
308 a[j3 + 1] = x1i + x3r;
313 for (j = m; j <= l + m - 2; j += 2)
319 x0i =
a[j + 1] +
a[j1 + 1];
321 x1i =
a[j + 1] -
a[j1 + 1];
323 x2i =
a[j2 + 1] +
a[j3 + 1];
325 x3i =
a[j2 + 1] -
a[j3 + 1];
327 a[j + 1] = x0i + x2i;
329 a[j2 + 1] = x2r - x0r;
332 a[j1] = wk1r * (x0i + x0r);
333 a[j1 + 1] = wk1r * (x0i - x0r);
336 a[j3] = wk1r * (x0r + x0i);
337 a[j3 + 1] = wk1r * (x0r - x0i);
341 for (k = (m << 1); k <=
n - m; k += m)
346 wk1i =
w[(k1 << 1) + 1];
349 wk3r = wk1r - 2 * wk2i * wk1i;
350 wk3i = 2 * wk2i * wk1r - wk1i;
351 for (j = k; j <= l + k - 2; j += 2)
357 x0i =
a[j + 1] +
a[j1 + 1];
359 x1i =
a[j + 1] -
a[j1 + 1];
361 x2i =
a[j2 + 1] +
a[j3 + 1];
363 x3i =
a[j2 + 1] -
a[j3 + 1];
365 a[j + 1] = x0i + x2i;
368 a[j2] = wk2r * x0r + wk2i * x0i;
369 a[j2 + 1] = wk2r * x0i - wk2i * x0r;
372 a[j1] = wk1r * x0r + wk1i * x0i;
373 a[j1 + 1] = wk1r * x0i - wk1i * x0r;
376 a[j3] = wk3r * x0r + wk3i * x0i;
377 a[j3 + 1] = wk3r * x0i - wk3i * x0r;
385 for (j = 0; j <= l - 2; j += 2)
389 x0i =
a[j + 1] -
a[j1 + 1];
391 a[j + 1] +=
a[j1 + 1];
409 delta = atan(1.0f) / nwh;
412 w[nwh] = cos(delta * nwh);
414 for (j = 2; j <= nwh - 2; j += 2)
441 delta = atan(1.0f) / nch;
443 c[nch] = 0.5f * cos(delta * nch);
444 for (j = 1; j <= nch - 1; j++)
446 c[j] = 0.5f * cos(delta * j);
447 c[nc - j] = 0.5f * sin(delta * j);
459 int j, j1, k, k1, l, m, m2;
468 for (j = 0; j <= m - 1; j++)
470 ip[m + j] = ip[j] + l;
476 for (k = 1; k <= m - 1; k++)
478 for (j = 0; j <= k - 1; j++)
480 j1 = (j << 1) + ip[k];
481 k1 = (k << 1) + ip[j];
485 a[j1 + 1] =
a[k1 + 1];
494 for (k = 1; k <= m - 1; k++)
496 for (j = 0; j <= k - 1; j++)
498 j1 = (j << 1) + ip[k];
499 k1 = (k << 1) + ip[j];
503 a[j1 + 1] =
a[k1 + 1];
511 a[j1 + 1] =
a[k1 + 1];
526 if (
n > (ip[0] << 2))
551 for (k = (
n >> 1) - 2; k >= 2; k -= 2)
558 xi =
a[k + 1] +
a[j + 1];
559 yr = wkr * xr + wki * xi;
560 yi = wkr * xi - wki * xr;
587 a[1] = 0.5f * (
a[0] -
a[1]);
620 for (k = (
n >> 1) - 2; k >= 2; k -= 2)
627 xi =
a[k + 1] +
a[j + 1];
628 yr = wkr * xr - wki * xi;
629 yi = wkr * xi + wki * xr;
739 int n, nw, nc, n1h, i, j, i2;
762 for (i = 1; i <= n1h - 1; i++)
765 xi =
a[i][0] -
a[j][0];
768 xi =
a[j][1] -
a[i][1];
772 for (j = 0; j <= n2 - 2; j += 2)
774 for (i = 0; i <= n1 - 1; i++)
778 t[i2 + 1] =
a[i][j + 1];
780 cdft(n1 << 1, isgn,
t, ip,
w);
781 for (i = 0; i <= n1 - 1; i++)
785 a[i][j + 1] =
t[i2 + 1];
788 for (i = 0; i <= n1 - 1; i++)
790 rdft(n2, isgn,
a[i], ip,
w);
795 for (i = 0; i <= n1 - 1; i++)
797 rdft(n2, isgn,
a[i], ip,
w);
799 for (j = 0; j <= n2 - 2; j += 2)
801 for (i = 0; i <= n1 - 1; i++)
805 t[i2 + 1] =
a[i][j + 1];
807 cdft(n1 << 1, isgn,
t, ip,
w);
808 for (i = 0; i <= n1 - 1; i++)
812 a[i][j + 1] =
t[i2 + 1];
815 for (i = 1; i <= n1h - 1; i++)
818 a[j][0] = 0.5f * (
a[i][0] -
a[j][0]);
820 a[j][1] = 0.5f * (
a[i][1] +
a[j][1]);
900 if (
n > (ip[0] << 2))
904 for (i = 0; i <= n1 - 1; i++)
906 cdft(n2, isgn,
a[i], ip,
w);
908 for (j = 0; j <= n2 - 2; j += 2)
910 for (i = 0; i <= n1 - 1; i++)
914 t[i2 + 1] =
a[i][j + 1];
916 cdft(n1 << 1, isgn,
t, ip,
w);
917 for (i = 0; i <= n1 - 1; i++)
921 a[i][j + 1] =
t[i2 + 1];
935 unsigned long n = (
unsigned long)in_realData.size();
941 memcpy(&auxVect[1], &in_realData[0],
n *
sizeof(auxVect[0]));
945 unsigned int n_2 = 1 + (
n / 2);
947 out_FFT_Re.resize(n_2);
948 out_FFT_Im.resize(n_2);
949 out_FFT_Mag.resize(n_2);
951 for (
unsigned int i = 0; i < n_2; i++)
954 out_FFT_Re[i] = auxVect[2];
956 out_FFT_Re[i] = auxVect[1 + i * 2];
958 if (i == 0 || i == (n_2 - 1))
961 out_FFT_Im[i] = auxVect[1 + i * 2 + 1];
964 std::sqrt(
square(out_FFT_Re[i]) +
square(out_FFT_Im[i]));
979 size_t dim1 = in_data.rows();
980 size_t dim2 = in_data.cols();
991 a =
new float_ptr[dim1];
992 for (i = 0; i < dim1; i++)
995 for (j = 0; j < dim2; j++)
a[i][j] = in_data.get_unsafe(i, j);
999 ip =
new int[(int)ceil(20 + 2 + sqrt((
FFT_TYPE)max(dim1, dim2 / 2)))];
1001 w =
new FFT_TYPE[max(dim1 / 2, dim2 / 4) + dim2 / 4 + 20];
1005 rdft2d((
int)dim1, (
int)dim2, 1,
a,
t, ip,
w);
1009 out_real.setSize(dim1, dim2);
1010 out_imag.setSize(dim1, dim2);
1015 for (i = 1; i < dim1; i++)
1016 for (j = 1; j < dim2 / 2; j++)
1018 out_real.set_unsafe(i, j, (
float)
a[i][j * 2]);
1019 out_real.set_unsafe(dim1 - i, dim2 - j, (
float)
a[i][j * 2]);
1020 out_imag.set_unsafe(i, j, (
float)-
a[i][j * 2 + 1]);
1021 out_imag.set_unsafe(dim1 - i, dim2 - j, (
float)
a[i][j * 2 + 1]);
1026 for (j = 1; j < dim2 / 2; j++)
1028 out_real.set_unsafe(0, j, (
float)
a[0][j * 2]);
1029 out_real.set_unsafe(0, dim2 - j, (
float)
a[0][j * 2]);
1030 out_imag.set_unsafe(0, j, (
float)-
a[0][j * 2 + 1]);
1031 out_imag.set_unsafe(0, dim2 - j, (
float)
a[0][j * 2 + 1]);
1039 for (i = 1; i < dim1 / 2; i++)
1041 out_real.set_unsafe(i, 0, (
float)
a[i][0]);
1042 out_real.set_unsafe(dim1 - i, 0, (
float)
a[i][0]);
1043 out_imag.set_unsafe(i, 0, (
float)-
a[i][1]);
1044 out_imag.set_unsafe(dim1 - i, 0, (
float)
a[i][1]);
1045 out_real.set_unsafe(i, dim2 / 2, (
float)
a[dim1 - i][1]);
1046 out_real.set_unsafe(dim1 - i, dim2 / 2, (
float)
a[dim1 - i][1]);
1047 out_imag.set_unsafe(i, dim2 / 2, (
float)
a[dim1 - i][0]);
1048 out_imag.set_unsafe(dim1 - i, dim2 / 2, (
float)-
a[dim1 - i][0]);
1055 out_real.set_unsafe(0, 0, (
float)
a[0][0]);
1056 out_real.set_unsafe(0, dim2 / 2, (
float)
a[0][1]);
1057 out_real.set_unsafe(dim1 / 2, 0, (
float)
a[dim1 / 2][0]);
1058 out_real.set_unsafe(dim1 / 2, dim2 / 2, (
float)
a[dim1 / 2][1]);
1061 for (i = 0; i < dim1; i++)
delete[]
a[i];
1079 ASSERT_(in_real.rows() == in_imag.rows());
1080 ASSERT_(in_real.cols() == in_imag.cols());
1083 size_t dim1 = in_real.rows();
1084 size_t dim2 = in_real.cols();
1098 a =
new float_ptr[dim1];
1099 for (i = 0; i < dim1; i++)
a[i] =
new FFT_TYPE[dim2];
1104 for (i = 1; i < dim1; i++)
1105 for (j = 1; j < dim2 / 2; j++)
1107 a[i][2 * j] = in_real.get_unsafe(i, j);
1108 a[i][2 * j + 1] = -in_imag.get_unsafe(i, j);
1114 for (j = 1; j < dim2 / 2; j++)
1116 a[0][2 * j] = in_real.get_unsafe(0, j);
1117 a[0][2 * j + 1] = -in_imag.get_unsafe(0, j);
1125 for (i = 1; i < dim1 / 2; i++)
1127 a[i][0] = in_real.get_unsafe(i, 0);
1128 a[i][1] = -in_imag.get_unsafe(i, 0);
1129 a[dim1 - i][1] = in_real.get_unsafe(i, dim2 / 2);
1130 a[dim1 - i][0] = in_imag.get_unsafe(i, dim2 / 2);
1137 a[0][0] = in_real.get_unsafe(0, 0);
1138 a[0][1] = in_real.get_unsafe(0, dim2 / 2);
1139 a[dim1 / 2][0] = in_real.get_unsafe(dim1 / 2, 0);
1140 a[dim1 / 2][1] = in_real.get_unsafe(dim1 / 2, dim2 / 2);
1143 ip =
new int[(int)ceil(20 + 2 + sqrt((
FFT_TYPE)max(dim1, dim2 / 2)))];
1145 w =
new FFT_TYPE[max(dim1 / 2, dim2 / 4) + dim2 / 4 + 20];
1149 rdft2d((
int)dim1, (
int)dim2, -1,
a,
t, ip,
w);
1153 out_data.setSize(dim1, dim2);
1157 for (i = 0; i < dim1; i++)
1158 for (j = 0; j < dim2; j++)
1159 out_data.set_unsafe(i, j, (
float)(
a[i][j] *
scale));
1162 for (i = 0; i < dim1; i++)
delete[]
a[i];
1180 ASSERT_(in_real.rows() == in_imag.rows());
1181 ASSERT_(in_real.cols() == in_imag.cols());
1184 size_t dim1 = in_real.rows();
1185 size_t dim2 = in_real.cols();
1187 size_t k1, k2, n1, n2;
1189 float ang1 = (float)(
sign *
M_2PI / dim1);
1190 float ang2 = (float)(
sign *
M_2PI / dim2);
1193 float scale =
sign == 1 ? (1.0f / (dim1 * dim2)) : 1;
1195 out_real.setSize(dim1, dim2);
1196 out_imag.setSize(dim1, dim2);
1198 for (k1 = 0; k1 < dim1; k1++)
1200 for (k2 = 0; k2 < dim2; k2++)
1204 for (n1 = 0; n1 < dim1; n1++)
1206 phase = ang1 * n1 * k1;
1207 for (n2 = 0; n2 < dim2; n2++)
1212 R += w_r * in_real.get_unsafe(n1, n2) -
1213 w_i * in_imag.get_unsafe(n1, n2);
1214 I += w_i * in_real.get_unsafe(n1, n2) +
1215 w_r * in_imag.get_unsafe(n1, n2);
1222 out_real.set_unsafe(k1, k2,
R *
scale);
1223 out_imag.set_unsafe(k1, k2, I *
scale);
1238 ASSERT_(in_real.rows() == in_imag.rows());
1239 ASSERT_(in_real.cols() == in_imag.cols());
1242 size_t dim1 = in_real.rows();
1243 size_t dim2 = in_real.cols();
1250 if (dim1IsPower2 && dim2IsPower2)
1261 static int* ip =
nullptr;
1266 static int alreadyInitSize1 = -1, alreadyInitSize2 = -1;
1268 if (alreadyInitSize1 != (
int)dim1 || alreadyInitSize2 != (int)dim2)
1273 for (i = 0; i < dim1; i++)
delete[]
a[i];
1276 if (ip)
delete[] ip;
1280 alreadyInitSize1 = (int)dim1;
1281 alreadyInitSize2 = (int)dim2;
1283 a =
new float_ptr[dim1];
1284 for (i = 0; i < dim1; i++)
a[i] =
new FFT_TYPE[2 * dim2];
1287 ip =
new int[(int)ceil(
1288 20 + 2 + sqrt((
FFT_TYPE)max(dim1, dim2 / 2)))];
1290 w =
new FFT_TYPE[max(dim1 / 2, dim2 / 4) + dim2 / 4 + 20];
1295 for (i = 0; i < dim1; i++)
1296 for (j = 0; j < dim2; j++)
1298 a[i][2 * j + 0] = in_real.get_unsafe(i, j);
1299 a[i][2 * j + 1] = in_imag.get_unsafe(i, j);
1304 cdft2d((
int)dim1, (
int)(2 * dim2), 1,
a,
t, ip,
w);
1308 out_real.setSize(dim1, dim2);
1309 out_imag.setSize(dim1, dim2);
1314 for (i = 0; i < dim1; i++)
1315 for (j = 0; j < dim2; j++)
1317 out_real.set_unsafe(i, j, (
float)
a[i][j * 2 + 0]);
1318 out_imag.set_unsafe(i, j, (
float)
a[i][j * 2 + 1]);
1327 printf(
"Using general DFT...\n");
1328 myGeneralDFT(-1, in_real, in_imag, out_real, out_imag);
1343 ASSERT_(in_real.rows() == in_imag.rows());
1344 ASSERT_(in_real.cols() == in_imag.cols());
1347 size_t dim1 = in_real.rows();
1348 size_t dim2 = in_real.cols();
1355 if (dim1IsPower2 && dim2IsPower2)
1366 static int* ip =
nullptr;
1374 static int alreadyInitSize1 = -1, alreadyInitSize2 = -1;
1376 if (alreadyInitSize1 != (
int)dim1 || alreadyInitSize2 != (int)dim2)
1381 for (i = 0; i < dim1; i++)
delete[]
a[i];
1384 if (ip)
delete[] ip;
1388 alreadyInitSize1 = (int)dim1;
1389 alreadyInitSize2 = (int)dim2;
1391 a =
new float_ptr[dim1];
1392 for (i = 0; i < dim1; i++)
a[i] =
new FFT_TYPE[2 * dim2];
1394 ip =
new int[(int)ceil(
1395 20 + 2 + sqrt((
FFT_TYPE)max(dim1, dim2 / 2)))];
1397 w =
new FFT_TYPE[max(dim1 / 2, dim2 / 4) + dim2 / 4 + 20];
1402 for (i = 0; i < dim1; i++)
1403 for (j = 0; j < dim2; j++)
1405 a[i][2 * j + 0] = in_real.get_unsafe(i, j);
1406 a[i][2 * j + 1] = in_imag.get_unsafe(i, j);
1411 cdft2d((
int)dim1, (
int)(2 * dim2), -1,
a,
t, ip,
w);
1415 out_real.setSize(dim1, dim2);
1416 out_imag.setSize(dim1, dim2);
1423 for (i = 0; i < dim1; i++)
1424 for (j = 0; j < dim2; j++)
1428 out_real.set_unsafe(i, j, (
float)(
a[i][j * 2 + 0]));
1429 out_imag.set_unsafe(i, j, (
float)(
a[i][j * 2 + 1]));
1436 out_imag.set_unsafe(0, 0, 0);
1444 printf(
"Using general DFT...\n");
1456 ASSERT_(A.cols() == B.cols() && A.rows() == B.rows());
1463 const size_t lx = A.cols();
1464 const size_t ly = A.rows();
1475 for (
y = 0;
y < ly;
y++)
1476 for (
x = 0;
x < lx;
x++)
1478 float r1 = I1_R.get_unsafe(
y,
x);
1479 float r2 = I2_R.get_unsafe(
y,
x);
1481 float ii1 = I1_I.get_unsafe(
y,
x);
1482 float ii2 = I2_I.get_unsafe(
y,
x);
1485 I2_R.set_unsafe(
y,
x, (r1 * r2 + ii1 * ii2) / den);
1486 I2_I.set_unsafe(
y,
x, (ii2 * r1 - r2 * ii1) / den);
1493 out_corr.setSize(ly, lx);
1494 for (
y = 0;
y < ly;
y++)
1495 for (
x = 0;
x < lx;
x++)