7#include "imageautoguiding.h"
18#define FREE_ARG char *
20#define TWOPI 6.28318530717959
25float ***f3tensorSP(
long nrl,
long nrh,
long ncl,
long nch,
long ndl,
long ndh);
26void free_f3tensorSP(
float ***t,
long nrl,
long nrh,
long ncl,
long nch,
long ndl,
long ndh);
28float **matrixSP(
long nrl,
long nrh,
long ncl,
long nch);
30void free_matrixSP(
float **m,
long nrl,
long nrh,
long ncl,
long nch);
33void fournNR(
float data[],
unsigned long nn[],
long ndim,
long isign);
35void rlft3NR(
float ***data,
float **speq,
unsigned long nn1,
unsigned long nn2,
unsigned long nn3,
long isign);
37void ShiftEST(
float ***testimage,
float ***refimage,
int n,
float *xshift,
float *yshift,
int k);
39namespace ImageAutoGuiding
41void ImageAutoGuiding1(
float *ref,
float *im,
int n,
float *xshift,
float *yshift)
43 float ***RefImage, ***TestImage;
49 RefImage = f3tensorSP(1, 1, 1, n, 1, n);
50 TestImage = f3tensorSP(1, 1, 1, n, 1, n);
56 for (j = 1; j <= n; ++j)
58 for (i = 1; i <= n; ++i)
60 RefImage[1][j][i] = ref[k];
61 TestImage[1][j][i] = im[k];
69 ShiftEST(TestImage, RefImage, n, &x, &y, 1);
76 free_f3tensorSP(RefImage, 1, 1, 1, n, 1, n);
77 free_f3tensorSP(TestImage, 1, 1, 1, n, 1, n);
83void ShiftEST(
float ***testimage,
float ***refimage,
int n,
float *xshift,
float *yshift,
int k)
85 int ix, iy, nh, nhplusone;
86 double deltax, deltay, fx2sum, fy2sum, phifxsum, phifysum, fxfysum;
87 double fx, fy, ff, fn, re, im, testre, testim, rev, imv, phi;
88 double power, dem, f2, f2limit;
91 f2limit = FFITMAX * FFITMAX;
93 speq = matrixSP(1, 1, 1, 2 * n);
103 for (ix = 1; ix <= 2 * n; ++ix)
108 rlft3NR(refimage, speq, 1, n, n, 1);
112 for (ix = 1; ix <= 2 * n; ++ix)
117 rlft3NR(testimage, speq, 1, n, n, 1);
129 for (ix = 1; ix <= n; ++ix)
133 fx = ff * ((float)(ix - 1));
137 fx = -ff * ((float)(n + 1 - ix));
140 for (iy = 1; iy <= nh; ++iy)
142 fy = ff * ((float)(iy - 1));
144 f2 = fx * fx + fy * fy;
152 re = refimage[k][ix][2 * iy - 1];
156 im = refimage[k][ix][2 * iy];
158 power = re * re + im * im;
162 testre = testimage[k][ix][2 * iy - 1];
166 testim = testimage[k][ix][2 * iy];
168 rev = re * testre + im * testim;
169 imv = re * testim - im * testre;
173 phi = atan2(imv, rev);
175 fx2sum += power * fx * fx;
176 fy2sum += power * fy * fy;
178 phifxsum += power * fx * phi;
179 phifysum += power * fy * phi;
181 fxfysum += power * fx * fy;
188 dem = fx2sum * fy2sum - fxfysum * fxfysum;
190 deltax = (phifxsum * fy2sum - fxfysum * phifysum) / (dem * TWOPI);
191 deltay = (phifysum * fx2sum - fxfysum * phifxsum) / (dem * TWOPI);
193 free_matrixSP(speq, 1, 1, 1, 2 * n);
206void rlft3NR(
float ***data,
float **speq,
unsigned long nn1,
unsigned long nn2,
unsigned long nn3,
long isign)
208 void fournNR(
float data[],
unsigned long nn[],
long ndim,
long isign);
210 unsigned long i1, i2, i3, j1, j2, j3, nn[4], ii3;
211 double theta, wpi, wpr, wtemp;
212 float c1, c2, h1r, h1i, h2r, h2i;
215 if ((
unsigned long)(1 + &data[nn1][nn2][nn3] - &data[1][1][1]) != nn1 * nn2 * nn3)
219 theta = isign * (6.28318530717959 / nn3);
220 wtemp = sin(0.5 * theta);
221 wpr = -2.0 * wtemp * wtemp;
228 fournNR(&data[1][1][1] - 1, nn, 3, isign);
229 for (i1 = 1; i1 <= nn1; i1++)
230 for (i2 = 1, j2 = 0; i2 <= nn2; i2++)
232 speq[i1][++j2] = data[i1][i2][1];
233 speq[i1][++j2] = data[i1][i2][2];
236 for (i1 = 1; i1 <= nn1; i1++)
238 j1 = (i1 != 1 ? nn1 - i1 + 2 : 1);
241 for (ii3 = 1, i3 = 1; i3 <= (nn3 >> 2) + 1; i3++, ii3 += 2)
243 for (i2 = 1; i2 <= nn2; i2++)
247 j2 = (i2 != 1 ? ((nn2 - i2) << 1) + 3 : 1);
248 h1r = c1 * (data[i1][i2][1] + speq[j1][j2]);
249 h1i = c1 * (data[i1][i2][2] - speq[j1][j2 + 1]);
250 h2i = c2 * (data[i1][i2][1] - speq[j1][j2]);
251 h2r = -c2 * (data[i1][i2][2] + speq[j1][j2 + 1]);
252 data[i1][i2][1] = h1r + h2r;
253 data[i1][i2][2] = h1i + h2i;
254 speq[j1][j2] = h1r - h2r;
255 speq[j1][j2 + 1] = h2i - h1i;
259 j2 = (i2 != 1 ? nn2 - i2 + 2 : 1);
260 j3 = nn3 + 3 - (i3 << 1);
261 h1r = c1 * (data[i1][i2][ii3] + data[j1][j2][j3]);
262 h1i = c1 * (data[i1][i2][ii3 + 1] - data[j1][j2][j3 + 1]);
263 h2i = c2 * (data[i1][i2][ii3] - data[j1][j2][j3]);
264 h2r = -c2 * (data[i1][i2][ii3 + 1] + data[j1][j2][j3 + 1]);
265 data[i1][i2][ii3] = h1r + wr * h2r - wi * h2i;
266 data[i1][i2][ii3 + 1] = h1i + wr * h2i + wi * h2r;
267 data[j1][j2][j3] = h1r - wr * h2r + wi * h2i;
268 data[j1][j2][j3 + 1] = -h1i + wr * h2i + wi * h2r;
271 wr = (wtemp = wr) * wpr - wi * wpi + wr;
272 wi = wi * wpr + wtemp * wpi + wi;
276 fournNR(&data[1][1][1] - 1, nn, 3, isign);
285void fournNR(
float data[],
unsigned long nn[],
long ndim,
long isign)
288 unsigned long i1, i2rev, i3rev, ip1, ip2, ip3, ifp1, ifp2;
289 unsigned long i2, i3;
290 unsigned long ibit, k1 = 0, k2, n, nprev, nrem, ntot;
291 float wi, wr, tempi, tempr;
292 double theta, wpi, wpr, wtemp;
294 for (ntot = 1, idim = 1; idim <= ndim; idim++)
297 for (idim = ndim; idim >= 1; idim--)
300 nrem = ntot / (n * nprev);
305 for (i2 = 1; i2 <= ip2; i2 += ip1)
309 for (i1 = i2; i1 <= i2 + ip1 - 2; i1 += 2)
311 for (i3 = i1; i3 <= ip3; i3 += ip2)
313 i3rev = i2rev + i3 - i2;
314 SWAP(data[i3], data[i3rev]);
315 SWAP(data[i3 + 1], data[i3rev + 1]);
320 while (ibit >= ip1 && i2rev > ibit)
331 theta = isign * 6.28318530717959 / (ifp2 / ip1);
332 wtemp = sin(0.5 * theta);
333 wpr = -2.0 * wtemp * wtemp;
337 for (i3 = 1; i3 <= ifp1; i3 += ip1)
339 for (i1 = i3; i1 <= i3 + ip1 - 2; i1 += 2)
341 for (i2 = i1; i2 <= ip3; i2 += ifp2)
345 tempr = (double)wr * data[k2] - (
double)wi * data[k2 + 1];
346 tempi = (double)wr * data[k2 + 1] + (
double)wi * data[k2];
347 data[k2] = data[k1] - tempr;
348 data[k2 + 1] = data[k1 + 1] - tempi;
350 data[k1 + 1] += tempi;
353 wr = (wtemp = wr) * wpr - wi * wpi + wr;
354 wi = wi * wpr + wtemp * wpi + wi;
369float **matrixSP(
long nrl,
long nrh,
long ncl,
long nch)
372 long i, nrow = nrh - nrl + 1, ncol = nch - ncl + 1;
376 m = (
float **)malloc((
size_t)((nrow + NR_END) *
sizeof(
float *)));
383 m[nrl] = (
float *)malloc((
size_t)((nrow * ncol + NR_END) *
sizeof(
float)));
389 for (i = nrl + 1; i <= nrh; i++)
390 m[i] = m[i - 1] + ncol;
396float ***f3tensorSP(
long nrl,
long nrh,
long ncl,
long nch,
long ndl,
long ndh)
399 long i, j, nrow = nrh - nrl + 1, ncol = nch - ncl + 1, ndep = ndh - ndl + 1;
403 t = (
float ***)malloc((
size_t)((nrow + NR_END) *
sizeof(
float **)));
410 t[nrl] = (
float **)malloc((
size_t)((nrow * ncol + NR_END) *
sizeof(
float *)));
417 t[nrl][ncl] = (
float *)malloc((
size_t)((nrow * ncol * ndep + NR_END) *
sizeof(
float)));
420 t[nrl][ncl] += NR_END;
423 for (j = ncl + 1; j <= nch; j++)
424 t[nrl][j] = t[nrl][j - 1] + ndep;
425 for (i = nrl + 1; i <= nrh; i++)
427 t[i] = t[i - 1] + ncol;
428 t[i][ncl] = t[i - 1][ncl] + ncol * ndep;
429 for (j = ncl + 1; j <= nch; j++)
430 t[i][j] = t[i][j - 1] + ndep;
437void free_matrixSP(
float **m,
long nrl,
long nrh,
long ncl,
long nch)
442 free((FREE_ARG)(m[nrl] + ncl - NR_END));
443 free((FREE_ARG)(m + nrl - NR_END));
446void free_f3tensorSP(
float ***t,
long nrl,
long nrh,
long ncl,
long nch,
long ndl,
long ndh)
452 free((FREE_ARG)(t[nrl][ncl] + ndl - NR_END));
453 free((FREE_ARG)(t[nrl] + ncl - NR_END));
454 free((FREE_ARG)(t + nrl - NR_END));