| Bsoft 2.0.5 Bernard's software package

fft_tool.cpp
Go to the documentation of this file.
1
11 #include "fft_tool.h"
12 #include "Bimage.h"
13
14 #include "utilities.h"
15
16 int
17 fft_1D_forward (Bimage * p)
18 {
19 // cout << "forward 1D fft" << endl;
20
21 scramrig (p);
22 farfarig (p, 1);
23 mixrig (p, 1);
24
25 return 0;
26 }
27
28 int
29 fft_1D_backward (Bimage * p)
30 {
31 // cout << "backward 1D fft" << endl;
32
33 mixrig (p, -1);
34 scramrig (p);
35 farfarig (p, -1);
36
37 return 0;
38 }
39
40 int
41 fft_2D_forward (Bimage * p)
42 {
43 char order[12] = "yxz";
44
45 // cout << "forward 2D fft" << endl;
46
47 fft_1D_forward (p);
48 p->reslice(order);
49 fft_1D_forward (p);
50
51 return 0;
52 }
53
54 int
55 fft_2D_backward (Bimage * p)
56 {
57 char order[12] = "yxz";
58
59 // cout << "backward 2D fft" << endl;
60
61 fft_1D_backward (p);
62 p->reslice(order);
63 fft_1D_backward (p);
64
65 return 0;
66 }
67
74 int
75 farfarig (Bimage * p, int ifl)
76 {
77 double a, b, c, e, dp = 1., q, uno = 1.;
78 int j, i, r, u = 2, rifl;
79 float *ppr, *ppi, *pqr, *pqi, *plim;
80 int rncol, rnrow, ripw, ipwy;
81
82 float *pr = (float *) p->data_pointer();
83
84 for (i = 2, ipwy = 0; i < p->sizeY(); ipwy++, i *= 2);
85
86 rncol = p->sizeX();
87 rnrow = p->sizeY();
88 ripw = ipwy;
89 rifl = ifl;
90
91 for (i = 0; i < ripw; i++)
92 {
93 c = 1;
94 e = 0;
95 q = rifl * sqrt ((uno - dp) * 0.5);
96 dp = sqrt ((uno + dp) * 0.5);
97 if (i == 0)
98 dp = (-dp);
99 for (r = 0; r < u; r += 2)
100 {
101 for (j = r; j < rnrow; j += u * 2)
102 {
103 ppr = pr + j * rncol;
104 ppi = ppr + rncol;
105 plim = ppr + u * rncol;
106 pqi = plim + rncol;
107 for (pqr = plim; pqr < plim + rncol; pqr++)
108 {
109 a = c ** pqr + e ** pqi;
110 b = e ** pqr - c ** pqi;
111 *pqr = *ppr - a;
112 *ppr += a;
113 *pqi = *ppi + b;
114 *ppi -= b;
115 ppr++;
116 ppi++;
117 pqi++;
118 }
119 }
120 a = e * dp + c * q;
121 c = c * dp - e * q;
122 e = a;
123 }
124 u += u;
125 }
126 return 0;
127 }
128
135 int
136 mixrig (Bimage * p, int ifl)
137 {
138 double a, b, u, v, fat;
139 int j;
140 float *prj, *pij, *prk, *pik, *plim;
141 double dp, q, c, e, ad1, ad, ad2;
142 int rncol, rnr2, rnrow;
143
144 float *pr = (float *) p->data_pointer();
145
146 rncol = p->sizeX();
147 rnrow = p->sizeY();
148 rnr2 = p->sizeY() / 2;
149 ad2 = rnrow;
150 ad = rnrow;
151 if (ifl == -1)
152 {
153 ad = 0.5;
154 ad2 = 1;
155 }
156 ad1 = 1 / (ad * 2);
157 ad = 1 / ad;
158 ad2 = 1 / ad2;
159 dp = cos (M_PI / rnr2);
160 q = sin (M_PI / rnr2) * ifl;
161
162 fat = ad2;
163 prj = pr;
164 pij = pr + rncol;
165 plim = pij;
166 while (prj < plim)
167 {
168 a = *prj;
169 b = *pij;
170 *prj++ = (a + b) * fat;
171 *pij++ = (a - b) * fat;
172 }
173
174 c = ifl;
175 e = 0;
176 fat = ad1;
177 for (j = 2; j < rnr2; j += 2)
178 {
179 a = e * dp + c * q;
180 c = c * dp - e * q;
181 e = a;
182 prk = pr + (rnrow - j) * rncol;
183 pik = prk + rncol;
184 prj = pr + j * rncol;
185 pij = prj + rncol;
186 plim = pik;
187 while (prk < plim)
188 {
189 a = *prj + *prk;
190 b = (*pij + *pik) * c - (*prj - *prk) * e;
191
192 u = *pij - *pik;
193 v = (*pij + *pik) * e + (*prj - *prk) * c;
194
195 *prj++ = (a + b) * fat;
196 *pij++ = (u - v) * fat;
197 *prk++ = (a - b) * fat;
198 *pik++ = -(u + v) * fat;
199 }
200 }
201
202 fat = ad;
203 prk = pr + rnr2 * rncol;
204 pik = prk + rncol;
205 plim = pik;
206 while (prk < plim)
207 {
208 *pik *= -fat;
209 *prk *= fat;
210 pik++;
211 prk++;
212 }
213 return 0;
214 }
215
224 int
225 scramrig (Bimage * p)
226 {
227 int j, i, k = 0, a, ncol2 = 2 * p->sizeX();
228 int *prj, *prk, *pij, *pik, *plim;
229 int rncol = p->sizeX(), nr2 = p->sizeY() / 2;
230
231 float *pr = (float *) p->data_pointer();
232
233 for (j = 1; j < nr2; j++)
234 {
235 i = 2;
236 while (k >= nr2 / i)
237 {
238 k -= nr2 / i;
239 i += i;
240 }
241 k += nr2 / i;
242 if (k > j)
243 {
244 prj = (int *) pr + j * ncol2;
245 pij = prj + rncol;
246 prk = (int *) pr + k * ncol2;
247 pik = prk + rncol;
248 plim = pij;
249 while (prj < plim)
250 {
251 a = *prj;
252 *prj++ = *prk;
253 *prk++ = a;
254 a = *pij;
255 *pij++ = *pik;
256 *pik++ = a;
257 }
258 }
259 }
260 return 0;
261 }
262
279 int
280 abtori (Bimage * p)
281 {
282 float *p1, *pp1, *pp2, *pp3, *pp4, *ptot;
283 float a, c;
284 int ncol2;
285
286 float *pr = (float *) p->data_pointer();
287
288 ptot = pr + p->sizeX() * p->sizeY();
289 ncol2 = 2 * p->sizeX();
290 pp2 = pr + p->sizeX();
291 pp3 = pr + 1;
292 pp4 = pr + 1 + p->sizeX();
293 for (p1 = pr; p1 < ptot; p1 += ncol2)
294 {
295 for (pp1 = p1; pp1 < p1 + p->sizeX(); pp1 += 2)
296 {
297 a = *pp1;
298 *pp1 = *pp1 - *pp4;
299 *pp4 += a;
300 c = *pp2 - *pp3;
301 *pp2 += *pp3;
302 *pp3 = c;
303 pp2 += 2;
304 pp3 += 2;
305 pp4 += 2;
306 }
307 pp2 += p->sizeX();
308 pp3 += p->sizeX();
309 pp4 += p->sizeX();
310 }
311 return 0;
312 }
313
331 int
332 ritoab (Bimage * p)
333 {
334 float *p1, *pp1, *pp2, *pp3, *pp4;
335 float rp, im;
336
337 float *pr = (float *) p->data_pointer();
338
339 pp2 = pr + p->sizeX();
340 pp3 = pr + 1;
341 pp4 = pr + 1 + p->sizeX();
342 for (p1 = pr; p1 < pr + p->sizeX() * p->sizeY(); p1 += 2 * p->sizeX())
343 {
344 for (pp1 = p1; pp1 < p1 + p->sizeX(); pp1 += 2)
345 {
346 rp = *pp1;
347 *pp1 += *pp4;
348 *pp1 /= 2;
349 *pp4 -= rp;
350 *pp4 /= 2;
351 im = *pp3;
352 *pp3 = *pp2 - im;
353 *pp3 /= 2;
354 *pp2 += im;
355 *pp2 /= 2;
356 pp2 += 2;
357 pp3 += 2;
358 pp4 += 2;
359 }
360 pp2 += p->sizeX();
361 pp3 += p->sizeX();
362 pp4 += p->sizeX();
363 }
364
365 return 0;
366 }
367
372 int
373 zeroes (Bimage * p)
374 {
375 float *pb1, *pc1, *pd1;
376
377 float *pr = (float *) p->data_pointer();
378
379 pd1 = pr + p->sizeX();
380 *pd1 = 0.; // origin Tri=Tir=Tii=0 //
381 for (pd1 = pr + p->sizeX(); pd1 < pr + 2 * p->sizeX(); pd1 += 2) // axis Ky Tri=0 Tii=0 //
382 {
383 pb1 = pd1 + 1;
384 *pd1 = 0.;
385 *pb1 = 0.;
386 }
387 for (pc1 = pr + 1; pc1 < pr + p->sizeX() * p->sizeY(); pc1 += 2 * p->sizeX()) // axis Kx Tir=Tii=0 //
388 {
389 pb1 = pc1 + p->sizeX();
390 *pc1 = 0.;
391 *pb1 = 0.;
392 }
393 return 0;
394 }
395
406 int
407 rephase_orig (Bimage * p)
408 {
409 float *p1, *pp1, *pp2, *pp3, *pp4;
410 float cs, csx, csy, meno;
411
412 float *pr = (float *) p->data_pointer();
413
414 meno = -1.;
415 csx = -1.;
416 pp2 = pr + p->sizeX();
417 pp3 = pr + 1;
418 pp4 = pr + 1 + p->sizeX();
419 for (p1 = pr; p1 < pr + p->sizeX() * p->sizeY(); p1 += 2 * p->sizeX())
420 {
421 csy = -1.;
422 csx *= meno;
423 for (pp1 = p1; pp1 < p1 + p->sizeX(); pp1 += 2)
424 {
425 csy *= meno;
426 cs = csy * csx;
427 *pp1 *= cs;
428 *pp3 *= cs;
429 *pp4 *= cs;
430 *pp2 *= cs;
431 pp2 += 2;
432 pp3 += 2;
433 pp4 += 2;
434 }
435 pp2 += p->sizeX();
436 pp3 += p->sizeX();
437 pp4 += p->sizeX();
438 }
439 return 0;
440 }
fft_tool.h
Bimage.h
Header file for image class.
utilities.h
Header file for general utilities.
zeroes
int zeroes(Bimage *p)
Definition: fft_tool.cpp:373
abtori
int abtori(Bimage *p)
Changes direct Fourier transform representation.
Definition: fft_tool.cpp:280
fft_2D_backward
int fft_2D_backward(Bimage *p)
Definition: fft_tool.cpp:55
mixrig
int mixrig(Bimage *p, int ifl)
With FARFARIG and SCRAMRIG fast Fourier transform a 2D image.
Definition: fft_tool.cpp:136
Bimage
General image parameter class.
Definition: Bimage.h:163
M_PI
#define M_PI
Definition: utilities.h:59
fft_2D_forward
int fft_2D_forward(Bimage *p)
Definition: fft_tool.cpp:41
farfarig
int farfarig(Bimage *p, int ifl)
With MIXRIG and SCRAMRIG fast Fourier transform a 2D image.
Definition: fft_tool.cpp:75
Bimage::reslice
void reslice(const char *order)
Definition: Bimage.h:709
Bimage::data_pointer
unsigned char * data_pointer()
Definition: Bimage.h:261
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:377
fft_1D_forward
int fft_1D_forward(Bimage *p)
Definition: fft_tool.cpp:17
fft_1D_backward
int fft_1D_backward(Bimage *p)
Definition: fft_tool.cpp:29
ritoab
int ritoab(Bimage *p)
Changes direct Fourier transform representation.
Definition: fft_tool.cpp:332
scramrig
int scramrig(Bimage *p)
With FARFARIG and MIXRIG fast Fourier transform a 2D image.
Definition: fft_tool.cpp:225
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:378
rephase_orig
int rephase_orig(Bimage *p)
Shifting the origin by half the complex image size.
Definition: fft_tool.cpp:407