-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathyao_fast.c
1705 lines (1438 loc) · 54.8 KB
/
yao_fast.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* yao_fast.c
*
* yao wavefront sensors engines
*
* This file is part of the yao package, an adaptive optics simulation tool.
*
* Copyright (c) 2002-2017, Francois Rigaut
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details (to receive a copy of the GNU
* General Public License, write to the Free Software Foundation, Inc., 675
* Mass Ave, Cambridge, MA 02139, USA).
*
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <complex.h>
#include <fftw3.h>
#include <time.h>
#include "ydata.h"
#include "yapi.h"
#include "play.h" // for p_wall_secs() and p_cpu_secs()
//#define FFTWOPTMODE FFTW_ESTIMATE
#define FFTWOPTMODE FFTW_MEASURE
// use FFTW_PATIENT for thread optimization (see below):
//#define FFTWOPTMODE FFTW_PATIENT
// static int n_threads = 1;
int use_sincos_approx_flag = 0;
void _eclat_long(long *ar, int nx, int ny);
void _eclat_float(float *ar, int nx, int ny);
void _eclat_double(double *ar, int nx, int ny);
void _poidev(float *xmv, long n);
void _gaussdev(float *xmv, long n);
void ran1init();
float ran1();
// void _ran1init(long n)
// {
// srandom(n);
// }
// float ran1()
// {
// float norm;
// norm = 2147483647.f;
// return random()/norm;
// }
// int Y__fftw_init_threads(void)
// {
// return fftwf_init_threads();
// }
//
//
// void Y_fftw_set_n_threads(int nargs)
// {
// n_threads = ygets_i(0);
// If you plan with FFTW_PATIENT, it will automatically
// disable threads for sizes that don't benefit from parallelization.
// fftwf_plan_with_nthreads(n_threads);
// }
//
// void Y_fftw_get_n_threads(int nargs)
// {
// ypush_int(n_threads);
// }
void _set_sincos_approx(int flag)
{
use_sincos_approx_flag = flag;
}
void Y__get_sincos_approx(int nargs)
{
ypush_int(use_sincos_approx_flag);
}
float sine(float x)
{
const float pi = 3.141592653589793f;
const float B = 4.0f/pi;
const float C = -4.0f/(pi*pi);
const float D = 2*pi;
const float P = 0.225;
x = x - roundf(x/D)*D;
float y = B * x + C * x * fabsf(x);
y = P * (y * fabsf(y) - y) + y;
return y;
}
float cosine(float x)
{
const float hpi = 3.141592653589793f/2.0f;
float y = sine(x+hpi);
return y;
}
void _sinecosinef(float x, float *s, float *c)
{
float sc;
*s = sine(x);
*c = cosine(x);
sc = (*c)*(*c) + (*s)*(*s);
sc = 1.0f/sqrtf(sc);
*s = *s * sc;
*c = *c * sc;
}
#ifdef __APPLE__
extern float sinf(float x);
extern float cosf(float x);
void
sincosf(float x, float *s, float *c)
{
*s = sinf(x);
*c = cosf(x);
}
#endif
int _import_wisdom(char *wisdom_file)
{
FILE *fp;
int status;
if((fp = fopen(wisdom_file, "r"))==NULL) return (1);
status = 1-fftwf_import_wisdom_from_file(fp);
fclose(fp);
return status;
}
int _export_wisdom(char *wisdom_file)
{
FILE *fp;
if((fp = fopen(wisdom_file, "w"))==NULL) return (1);
fftwf_export_wisdom_to_file(fp);
fclose(fp);
return (0);
}
int _init_fftw_plans(int nlimit)
{
int n, size;
fftwf_complex *inf,*outf;
fftwf_plan p1, p2;
size=1;
for (n=0;n<nlimit+1;n++) {
printf("Optimizing 2D FFT - size = %d\n",size);
inf = fftwf_malloc(size*size*sizeof(fftwf_complex));
outf = fftwf_malloc(size*size*sizeof(fftwf_complex));
p1 = fftwf_plan_dft_2d(size, size, inf, outf, FFTW_FORWARD, FFTWOPTMODE);
p2 = fftwf_plan_dft_2d(size, size, inf, outf, FFTW_BACKWARD, FFTWOPTMODE);
fftwf_destroy_plan(p1);
fftwf_destroy_plan(p2);
fftwf_free(inf);
fftwf_free(outf);
size*=2;
}
size = 1;
for (n=0;n<nlimit+1;n++) {
printf("Optimizing 1D FFT - size = %d\n",size);
inf = fftwf_malloc(size*sizeof(fftwf_complex));
outf = fftwf_malloc(size*sizeof(fftwf_complex));
p1 = fftwf_plan_dft_1d(size, inf, outf, FFTW_FORWARD, FFTWOPTMODE);
p2 = fftwf_plan_dft_1d(size, inf, outf, FFTW_BACKWARD, FFTWOPTMODE);
fftwf_destroy_plan(p1);
fftwf_destroy_plan(p2);
fftwf_free(inf);
fftwf_free(outf);
size*=2;
}
return(0);
}
int _init_fftw_plan(int size)
{
fftwf_complex *inf;
fftwf_complex *outf;
fftwf_plan p1,p2;
float *ptr;
int i;
printf("Optimizing 2D FFT - size = %d\n",size);
inf = fftwf_malloc(size*size*sizeof(fftwf_complex));
outf = fftwf_malloc(size*size*sizeof(fftwf_complex));
ptr = (void *)inf;
for (i=0;i<2*size*size;i++) *(ptr++) = 0.0f;
p1 = fftwf_plan_dft_2d(size, size, inf, outf, FFTW_FORWARD, FFTWOPTMODE);
p2 = fftwf_plan_dft_2d(size, size, inf, outf, FFTW_BACKWARD, FFTWOPTMODE);
fftwf_destroy_plan(p1);
fftwf_destroy_plan(p2);
fftwf_free(inf);
fftwf_free(outf);
return(0);
}
/**************************************************************
* The following function computes the PSF, given a pupil and *
* a phase, both float. phase can be a 3 dimensional array, *
* in which case the routine compute and returns an image for *
* each plan. This routine uses the FFTW routines. *
* Called by calc_psf_fast(pupil,phase,scale=) in yorickVE.i *
* Author: F.Rigaut *
* Date: Dec 12 2003. *
* modified 2004apr3 to adapt FFTW from apple veclib *
**************************************************************/
int _calc_psf_fast(float *pupil, /* pupil image, dim [ n , n ] */
float *phase, /* phase cube, dim [ n , n , nplans ] */
float *image, /* output images, dim [ n , n , nplans ] */
int n, /* side linear dimension */
int nplans, /* number of plans (min 1, mostly to spped up calculations */
/* by avoiding multiple setups and mallocs */
float scal, /* phase scaling factor */
int swap)
{
/* Declarations */
fftwf_complex *in, *out;
float *ptr;
long i,k,koff;
fftwf_plan p;
float ppsin,ppcos;
// fftwf_plan_with_nthreads(n_threads);
/* Allocate memory for the input operands and check its availability. */
in = fftwf_malloc(sizeof(fftwf_complex) * n * n);
out = fftwf_malloc(sizeof(fftwf_complex) * n * n );
if ( in == NULL || out == NULL ) { return (-1); }
/* Set up the required memory for the FFT routines */
p = fftwf_plan_dft_2d(n, n, in, out, FFTW_FORWARD, FFTWOPTMODE);
/* Main loop on plan #, in case several phases are input to the routine */
for ( k=0; k<nplans; k++ ) {
koff = k*n*n;
// ptr = &(in[0]);
ptr = (void *)in;
for ( i=0; i<n*n; i++ ) {
if ( pupil[i] != 0.f ) {
//~ *(ptr) = pupil[i] * cosf( phase[koff+i] * scal );
//~ *(ptr+1) = pupil[i] * sinf( phase[koff+i] * scal );
//~ *(ptr) = pupil[i] * cosine( phase[koff+i] * scal );
//~ *(ptr+1) = pupil[i] * sine( phase[koff+i] * scal );
if (use_sincos_approx_flag) _sinecosinef(phase[koff+i] * scal, &ppsin, &ppcos);
else sincosf(phase[koff+i] * scal, &ppsin, &ppcos);
*(ptr) = pupil[i] * ppcos;
*(ptr+1) = pupil[i] * ppsin;
} else {
*(ptr) = 0.0f;
*(ptr+1) = 0.0f;
}
ptr +=2;
}
/* Carry out a Forward 2d FFT transform, check for errors. */
fftwf_execute(p); /* repeat as needed */
// ptr = &(out[0]);
ptr = (void *)out;
for ( i = 0; i < n*n; i++ ) {
image[koff+i] = ( *(ptr) * *(ptr) +
*(ptr+1) * *(ptr+1) );
ptr +=2;
}
/* swap quadrants. */
if (swap) _eclat_float(&(image[koff]),n,n);
}
/* Free allocated memory. */
fftwf_destroy_plan(p);
fftwf_free(in);
fftwf_free(out);
return (0);
}
int _fftVE(float *rp,
float *ip,
int n,
int dir) /* forward (1) or reverse (-1) */
{
/* Declarations */
fftwf_complex *in, *out;
float *ptr;
long i;
fftwf_plan p;
/* Allocate memory for the input operands and check its availability. */
in = fftwf_malloc(sizeof(fftwf_complex) * n * n);
out = fftwf_malloc(sizeof(fftwf_complex) * n * n );
if ( in == NULL || out == NULL ) { return (-1); }
/* Set up the required memory for the FFT routines */
if (dir == 1) {
p = fftwf_plan_dft_2d(n, n, in, out, FFTW_FORWARD, FFTWOPTMODE);
} else {
p = fftwf_plan_dft_2d(n, n, in, out, FFTW_BACKWARD, FFTWOPTMODE);
}
/* fill input */
ptr = (void *)in;
for ( i=0; i<n*n; i++ ) {
*(ptr) = rp[i];
*(ptr+1) = ip[i];
ptr +=2;
}
/* Carry out a Forward 2d FFT transform, check for errors. */
fftwf_execute(p); /* repeat as needed */
ptr = (void *)out;
for ( i = 0; i < n*n; i++ ) {
rp[i] = *(ptr);
ip[i] = *(ptr+1);
ptr +=2;
}
/* Free allocated memory. */
fftwf_destroy_plan(p);
fftwf_free(in);
fftwf_free(out);
return (0);
}
void _fftVE2(fftwf_complex *in,
fftwf_complex *out,
int n, /* log2 of side linear dimension (e.g. 6 for 64x64 arrays) */
int dir) /* forward (1) or reverse (-1) */
/* do not use this one, it was an experiment, but _fftVE is actually faster */
{
/* Declarations */
fftwf_plan p;
/* Set up the required memory for the FFT routines */
if (dir == 1) {
p = fftwf_plan_dft_2d(n, n, in, out, FFTW_FORWARD, FFTWOPTMODE);
} else {
p = fftwf_plan_dft_2d(n, n, in, out, FFTW_BACKWARD, FFTWOPTMODE);
}
/* Carry out a Forward 2d FFT transform, check for errors. */
fftwf_execute(p); /* repeat as needed */
/* Free allocated memory. */
fftwf_destroy_plan(p);
}
int embed_image(float *inim, // Input (origin) image
int indx, // X dim of origin image
int indy, // Y dim of origin image
float *outim, // Output (destination) image
int outdx, // X dim of destination image
int outdy, // Y dim of destination image
int destx, // 0-based X indice of origin image pixel (0,0) in dest. image
int desty, // 0-based Y indice of origin image pixel (0,0) in dest. image
int roll) // roll the input image before embedding?
{
int i,j,ioff,joff;
if (roll==0) {
for ( j=0 ; j<indy ; j++ ) {
joff = j+desty;
if (joff<0) continue;
if (joff>=outdy) break;
joff *= outdx;
for ( i=0 ; i<indx ; i++ ) {
ioff = i+destx;
if (ioff<0) continue;
if (ioff>=outdx) break;
outim[ioff+joff] += inim[i+j*indx];
}
}
return 0;
}
int hx,hy;
hx = indx/2;
hy = indy/2;
for ( j=0 ; j<hy ; j++ ) {
joff = j+desty;
if (joff<0) continue;
if (joff>=outdy) break;
joff *= outdx;
for ( i=0 ; i<hx ; i++ ) {
ioff = i+destx;
if (ioff<0) continue;
if (ioff>=outdx) break;
outim[ioff+joff] += inim[i+hx+(j+hy)*indx];
}
}
for ( j=hy ; j<indy ; j++ ) {
joff = j+desty;
if (joff<0) continue;
if (joff>=outdy) break;
joff *= outdx;
for ( i=0 ; i<hx ; i++ ) {
ioff = i+destx;
if (ioff<0) continue;
if (ioff>=outdx) break;
outim[ioff+joff] += inim[i+hx+(j-hy)*indx];
}
}
for ( j=0 ; j<hy ; j++ ) {
joff = j+desty;
if (joff<0) continue;
if (joff>=outdy) break;
joff *= outdx;
for ( i=hx ; i<indx ; i++ ) {
ioff = i+destx;
if (ioff<0) continue;
if (ioff>=outdx) break;
outim[ioff+joff] += inim[i-hx+(j+hy)*indx];
}
}
for ( j=hy ; j<indy ; j++ ) {
joff = j+desty;
if (joff<0) continue;
if (joff>=outdy) break;
joff *= outdx;
for ( i=hx ; i<indx ; i++ ) {
ioff = i+destx;
if (ioff<0) continue;
if (ioff>=outdx) break;
outim[ioff+joff] += inim[i-hx+(j-hy)*indx];
}
}
return 0;
}
/* Shack- Hartmann coded in C
pass one phase array and a set of indices (start and end of each subapertures)
then this routine puts the phase sections in one larger phase and does a serie
of FFTs to compute the images. Then each image is binned according to the map
"binindices". It then put these images back into a large image
that contains all subaperture image, suitable for visualization.
flow:
- set up of a couple of constants and memory allocation
- loop on subaperture number:
- put pupil and phase in complex A[ns,ns]
- do FFT
- compute image as square(FFT result complex) (no eclat)
- compute binned/resampled subaperture image using binindices (has
to made up for the missing eclat at previous step).
- compute X and Y centroids
- optionaly stuff this image in a larger image (fimage) for display
- free memory
The array used in here are:
pupil (dim*dim): pupil image, float
phase (dim*dim): phase image, float
virtual(nx*ny) : wavefront-let, i.e. part of the pupil/phase contained
within the subaperture.
A (ns*ns) : Complex array into which the amplitude and phase
corresponding to one subaperture wavefront-let are placed.
Dimension n2*n2 e.g. 16x16 if 5x5 pixels/subaperture
simage (ns*ns) : Focal plane image obtained after FFT of A.
ximage (nx*nx) : Extended focal plane array, into which simage is embedded
(+ shifted) to allow extended field of view.
bimage (nb*nb) : Image obtained after binning of ximage to take into account
bigger pixels than the one delivered after the FFT.
fimage (nf*nf) : Final image into which all the subaperture images have been
placed at pre-defined positions (see imistart)
*/
int _shwfs_phase2spots(
float *pupil, // input pupil
float *phase, // input phase, in microns
float phasescale, // phase scaling factor: microns -> radians @ wfs.lambda
float *phaseoffset, // input phase offset
int dim, // X or Y dim of phase. Used as stride for extraction
int *istart, // vector of i starts of each subaperture
int *jstart, // vector of j starts of each subaperture
int nsx, // subaperture i size
int nsy, // subaperture j size
int nsubs, // # of subapertures
int sdimpow2, // dimension of small array for FFTs, in power of 2
long domask, // go through amplitude mask loop (0/1).
float *submask, // subaperture mask. Corresponds/applied to simage.
float *kernel, // to convolve the (s)image with. dim nx*nx*nkernels
// compute dynamically at each call, i.e. can change
int nkernels, // number of plans in *kernel
float *kernels, // to convolve the (s)image with, one per subaperture
// dimension: 2^sdimpow2 * 2 * nsubs. FFTs precomputed
// at init call.
float *kerfftr, // real part of kernels FFT. dim: same as kernels
float *kerffti, // imaginary part of kernels FFT. same dim as kerfftr
int initkernels, // init kernels: pre-compute FFTs
int kernconv, // convolve with kernel?
int *binindices, // int 2d array containing the indices of the binned
// subaperture image, i.e. to which pixel in the binned
// image should one pixel in the FFT'd image be added.
int nb, // dimension of binned subaperture image (bimage)
int rebinfactor, // rebin factor from small to big pixels
int nx, // dimension of extended subaperture image
float *unittip, // nsx*nsy array with unit tip (1,2,3..)
float *unittilt, // nsx*nsy array with unit tilt
float *lgs_prof_amp, // vector of lgs profile amplitudes
float *lgs_defocuses,// vector of lgs profile altitudes
int n_in_profile, // dimension of lgs_prof_amp and lgs_prof_alt
float *unit_defocus, // as it says, same dimsof as phase
float *fimage, // final image with spots
int *svipc_subok, // to skip (0) subap for svipc partial spot comput.
int *imistart, // vector of i starts of each image
int *imjstart, // vector of j starts of each image
int fimnx, // final image X dimension
int fimny, // final image Y dimension
float *flux, // vector of flux (input), dim nsubs
float *rayleighflux, // vector of flux for rayleigh (input), dim nsubs
float *skyflux, // vector of flux for sky (input), dim nsubs
float darkcurrent, // dark current, e-/pix/frame
int rayleighflag, // enable rayleigh processing
float *rayleigh, // rayleigh background, nx*nx*nsubs
// here I separated background and rayleigh. the background includes
// not only the rayleigh, but also the sky and the dark current.
int bckgrdinit, // init background processing. fill bckgrdcalib
int counter, // current counter (in number of cycles)
int niter) // total # of cycles over which to integrate
{
/* Declarations */
fftwf_complex *A, *result, *Ax, *Kx, *Ker, *resultx;
fftwf_plan fftps,fftpx,fftpxi;
float *ptr,*ptr1,*ptr2;
float *phase_scaled;
float *simage;
float *bimage;
float *ximage; // extended image for one subap, un-rebinned
float *bsubmask;
float *brayleigh;
float tot, totrayleigh, krp, kip, sky;
float corfact;
float dx,dxp,dy,dyp;
float lgsdef,lgsamp,pp,ppsin,ppcos;
long log2nr, log2nc, n, ns;
int i,j,k,l,koff,kk,nalt;
int idxp,idyp,ndx,ndy,dynrange;
int debug=0;
int nxdiff;
double sys,cpu0,cpu1,cpu2,cpu3,cpu4,cpu5,cpu6;
double cpu10,cpu21,cpu32,cpu43,cpu54,cpu65;
const float pi = 3.141592653589793f;
const float twopi = 2*pi;
cpu10=0.0;cpu21=0.0;cpu32=0.0;cpu43=0.0;cpu54=0.0;cpu65=0.0;
cpu0=0.0;cpu1=0.0;cpu2=0.0;cpu3=0.0;cpu4=0.0;cpu5=0.0;cpu6=0.0;
//======================
// Global setup for FFTs:
//======================
// fftwf_plan_with_nthreads((int)1);
// Set the size of 2d FFT.
log2nr = sdimpow2;
log2nc = sdimpow2;
n = 1 << ( log2nr + log2nc ); // total number of pixels in small array
ns = 1 << log2nr; // side of small array, pixels, n=ns*ns
// enlarge dynamical range?
if (nx==ns) dynrange=0; else dynrange=1;
// integrate = 1; // force to pass by the end (subap overlap upgrade)
// if (niter > 1) {integrate = 1;} // we are in "integrating mode"
if (debug>1) printf("here1 nx=%d ns=%d dynrange=%d\n",(int)nx,(int)ns,(int)dynrange);
// Allocate memory for the input operands and check its availability.
A = fftwf_malloc ( n * sizeof ( fftwf_complex ) );
result = fftwf_malloc ( n * sizeof ( fftwf_complex ) );
Ax = fftwf_malloc ( nx *nx * sizeof ( fftwf_complex ) );
Kx = fftwf_malloc ( nx *nx * sizeof ( fftwf_complex ) );
Ker = fftwf_malloc ( nx *nx * sizeof ( fftwf_complex ) );
resultx = fftwf_malloc ( nx *nx * sizeof ( fftwf_complex ) );
phase_scaled = ( float* ) malloc ( dim * dim * sizeof ( float ) );
simage = ( float* ) malloc ( ns * ns * sizeof ( float ) );
ximage = ( float* ) malloc ( nx * nx * sizeof ( float ) );
bimage = ( float* ) malloc ( nb * nb * sizeof ( float ) );
brayleigh = ( float* ) malloc ( nb * nb * sizeof ( float ) );
bsubmask = ( float* ) malloc ( nb * nb * sizeof ( float ) );
if ( A == NULL || result == NULL || Ax == NULL || \
Kx == NULL || Ker == NULL || resultx == NULL || phase_scaled == NULL ||
bimage == NULL || simage == NULL || ximage == NULL ||
brayleigh == NULL || bsubmask == NULL ) { return (1); }
//Zero out final image if first iteration
if (counter == 1) {
// in fact, let's put the dark current value in there:
//~ for (i=0;i<(fimnx*fimny);i++) { fimage[i] = 0.0f; }
// these 2 lines temporarily disabled for svipc shm_var of ffimage:
// FIXME (remove comments):FIXME FIXME FIXME
//~ totdark = (float) niter * darkcurrent;
//~ for (i=0;i<(fimnx*fimny);i++) { fimage[i] = totdark; }
// 2012sep17: the problem above it seems is that the loop is over the
// entire array. If several forks are accessing it, -> problem.
// the simplest might be to add it at the yorick level before
// calling _shwfs_spots2slopes().
}
// compute scaled phase ( we do this operation several times below):
for ( i=0 ; i<dim*dim ; i++ ) \
phase_scaled[i] = (phase[i]+ phaseoffset[i]) * phasescale;
// compute bsubmask from submask and binindices:
for ( i=0 ; i<nb*nb ; i++ ) { bsubmask[i] = (float)(1-domask); }
if (domask) {
for ( i=0 ; i<nx*nx ; i++ ) {
if (binindices[i] >= 0) {
bsubmask[binindices[i]] += (float)submask[i];
}
}
corfact = 1.0f / (float)rebinfactor / (float)rebinfactor;
for ( i=0 ; i<nb*nb ; i++ ) { bsubmask[i] *= corfact; }
}
// in the following, we'll need to shift slightly where we embed simage
// into ximage.
// this is linked to the number of -1 pixels at the end of binindices.
nxdiff = 0;
// do that for first row only:
for (i=0;i<nx;i++) if (binindices[i]==-1) nxdiff++;
// Set up the required memory for the FFT routines and
// check its availability.
fftpx = fftwf_plan_dft_2d(nx, nx, Ax, Kx, FFTW_FORWARD, FFTWOPTMODE);
if (initkernels == 1) {
// Transform kernels, store and return for future use
for ( l=0 ; l<nsubs ; l++ ) {
ptr = (void *)Ax;
for ( i=0 ; i<nx*nx ; i++ ) {
*(ptr) = kernels[i+l*nx*nx];
*(ptr+1) = 0.0f;
ptr += 2;
}
fftwf_execute(fftpx);
ptr = (void *)Kx;
for ( i=0 ; i<nx*nx ; i++ ) {
kerfftr[i+l*nx*nx] = *(ptr);
kerffti[i+l*nx*nx] = *(ptr+1);
ptr += 2;
}
}
}
if (debug>1) printf("here2\n");
if (kernconv == 1) {
// init resultx to (1,0)
// we're starting from a non-filter.
ptr = (void *)resultx;
for ( i=0; i<nx*nx ; i++ ) {
*(ptr) = 1.0f;
*(ptr+1) = 0.0f;
ptr += 2;
}
// nkernels is in case we have several effects
// that we want to cumulate, e.g. laser beam,
// uplink seeing, etc...
for ( k=0 ; k<nkernels ; k++ ) {
koff = k*nx*nx;
// Transform kernel
ptr = (void *)Ax;
for ( i=koff; i<koff+nx*nx; i++ ) {
*(ptr) = kernel[i];
*(ptr+1) = 0.0f;
ptr +=2;
}
fftwf_execute(fftpx); // Ax -> Kx
// result in in Kx
// Kx * resultx -> Ker:
ptr1 = (void *)Kx;
ptr2 = (void *)resultx;
ptr = (void *)Ker;
for ( i=0; i<nx*nx; i++ ) {
*(ptr) = *(ptr1) * *(ptr2) - *(ptr1+1) * *(ptr2+1);
*(ptr+1) = *(ptr1) * *(ptr2+1) + *(ptr1+1) * *(ptr2);
ptr +=2; ptr1 +=2; ptr2 +=2;
}
if (k==(nkernels-1)) break;
// copy in resultx for next one:
ptr1 = (void *)Ker;
ptr2 = (void *)resultx;
for ( i=0; i<2*nx*nx; i++ ) *(ptr2++) = *(ptr1++);
}
}
fftwf_destroy_plan(fftpx);
// at this point, Ker contains the Fourier transform of
// all kernels comulated. Just one however for all subapertures.
fftps = fftwf_plan_dft_2d(ns, ns, A, result, FFTW_FORWARD, FFTWOPTMODE);
fftpx = fftwf_plan_dft_2d(nx, nx, Ax, resultx, FFTW_FORWARD, FFTWOPTMODE);
fftpxi = fftwf_plan_dft_2d(nx, nx, Ax, resultx, FFTW_BACKWARD, FFTWOPTMODE);
//=====================
// LOOP ON SUBAPERTURES
//=====================
for ( l=0 ; l<nsubs ; l++ ) {
// zero out ximage:
for ( i=0 ; i<nx*nx ; i++ ) ximage[i] = 0.0f;
//====================
// LOOP ON LGS PROFILE
//====================
for ( nalt=0; nalt<n_in_profile ; nalt++ ) {
lgsdef = lgs_defocuses[nalt];
lgsamp = lgs_prof_amp[nalt];
if ( n_in_profile==1 ) lgsamp = 1.0f;
if ( svipc_subok[l]==0 ) continue;
cpu0 = p_cpu_secs(&sys);
// reset A and result
ptr = (void *)A;
for ( i=0; i<n ; i++ ) { *(ptr) = 0.0f; *(ptr+1) = 0.0f; ptr +=2; }
ptr = (void *)result;
for ( i=0; i<n ; i++ ) { *(ptr) = 0.0f; *(ptr+1) = 0.0f; ptr +=2; }
// indice offset of phaselet in phase/pupil array
koff = istart[l] + jstart[l]*dim;
// START section to allow larger dynamical range by
// subtracting a tilt to the phase and moving later on
// the image in the big image
// (declarations on top of function)
if (dynrange) {
// compute approximate average slope over the subaperture
// it doesn't matter if this is not the exact value
// as it only serves to determine if we should offset,
// but the end result should be the same. Because of edge subapertures,
// we'll have to do the whole average gradient calculation
dx = 0.0f; dy = 0.0f;
ndx = 0; ndy = 0;
for ( j=0; j<(nsy-1); j++ ) {
for ( i=0; i<(nsx-1) ; i++ ) {
k = koff + i + j*dim;
if ( pupil[k] && pupil[k+1] ) {
dx += phase_scaled[k+1] - phase_scaled[k] + \
lgsdef*( unit_defocus[k+1] - unit_defocus[k] );
ndx++;
}
if ( pupil[k] && pupil[k+dim] ) {
dy += phase_scaled[k+dim] - phase_scaled[k] + \
lgsdef*( unit_defocus[k+dim] - unit_defocus[k] );
ndy++;
}
}
}
if (ndx) dx = dx / (float)(ndx); // in radian/pixel
if (ndy) dy = dy / (float)(ndy);
// now we need to transform this average phase gradient into
// pixel motion.
// So how many small pixels we expect the spot to move?
// if dx = 2*pi, the spot will wrap all the way back to center,
// i.e. will move by ns pixels:
dxp = dx / twopi * (float)(ns);
dyp = dy / twopi * (float)(ns);
// round to the nearest (small) pixel:
idxp = lroundf(dxp);
idyp = lroundf(dyp);
if (idxp!=0) dx = dx * (float)(idxp) / dxp; else dx = 0.0f;
if (idyp!=0) dy = dy * (float)(idyp) / dyp; else dy = 0.0f;
if ((debug>1)&&((idxp!=0)||(idyp!=0))) printf("idxp = %d, idyp = %d, dx=%f, dy=%f\n",idxp,idyp,dx,dy);
} else {
dx = 0.0f; dy = 0.0f;
idxp = 0; idyp = 0;
}
// END section to allow larger dynamical range
// (more below to add to phase and to shift imagelets)
cpu1 = p_cpu_secs(&sys);
cpu10 += cpu1-cpu0;
// fill in the complex wavefront array for this subaperture:
// cos & sin are very costly, so we use an aproximation:
ptr = (void *)A;
if (dynrange) {
if (debug>1) printf("here, dynrange enabled\n");
for ( j=0; j<nsy ; j++ ) {
for ( i=0; i<nsx ; i++ ) {
k = koff + i + j*dim;
kk = i + j*nsx;
pp = phase_scaled[k] + lgsdef * unit_defocus[k] \
- dx * unittip[kk] - dy * unittilt[kk];
if (use_sincos_approx_flag) _sinecosinef(pp,&ppsin,&ppcos);
else sincosf(pp,&ppsin,&ppcos);
*(ptr + 2*(i+j*ns)) = pupil[k] * ppcos;
*(ptr + 2*(i+j*ns)+1) = pupil[k] * ppsin;
}
}
} else {
if (debug>1) printf("here, dynrange disabled\n");
for ( j=0; j<nsy ; j++ ) {
for ( i=0; i<nsx ; i++ ) {
k = koff + i + j*dim;
pp = phase_scaled[k];
if (use_sincos_approx_flag) _sinecosinef(pp,&ppsin,&ppcos);
else sincosf(pp,&ppsin,&ppcos);
*(ptr + 2*(i+j*ns)) = pupil[k] * ppcos;
*(ptr + 2*(i+j*ns)+1) = pupil[k] * ppsin;
}
}
}
if (debug>1) printf("here3\n");
cpu2 = p_cpu_secs(&sys);
cpu21 += cpu2-cpu1;
// Carry out a Forward 2d FFT transform, check for errors.
fftwf_execute(fftps); // A -> result
// at this point result should contain the diffraction
// of the subaperture + turbulence, but not kernel yet
// compute image from complex image object:
ptr = (void *)result;
for ( i=0; i<n; i++ ) {
simage[i] = (*(ptr) * *(ptr) + *(ptr+1) * *(ptr+1) );
simage[i] *= lgsamp;
if ( (debug>20) && ( l==10 ) ) printf("%f ",simage[i]);
ptr +=2;
}
if ( (debug>1) && ( l==10 ) ) printf(" ");
cpu3 = p_cpu_secs(&sys);
cpu32 += cpu3-cpu2;
// Embed (and add to) this simage into ximage, the extended field
// of view image for this subaperture (with shifts computed above):
if ( (debug>1) && (l==10) ) printf("\nns=%d nx=%d\n",(int)ns,(int)nx);
embed_image(simage,ns,ns,ximage,nx,nx,(nx-ns)/2+idxp-nxdiff,(nx-ns)/2+idyp-nxdiff,1);
} // END LOOP ON LGS PROFILE
if ( (debug>1) && (l==10) ) printf("here4\n");
if ((debug>10)&&(l==10)) {
FILE *fp;
fp=fopen("xim-pre.dat", "w");
fprintf(fp, "%d\n",(int)nx);
for ( i=0 ; i<nx*nx ; i++ ) fprintf(fp, "%f\n",ximage[i]);
fclose(fp);
}
// Carry out convolution by kernel if required
if (kernconv == 1) {
// Transform ximage
ptr = (void *)Ax;
for ( i=0 ; i<nx*nx ; i++ ) {
*(ptr) = ximage[i];
*(ptr+1) = 0.0f;
ptr +=2;
}
fftwf_execute(fftpx);
// multiply by kernel transform:
ptr = (void *)Ker;
ptr1 = (void *)Ax;
ptr2 = (void *)resultx;
for ( i=0 ; i<nx*nx ; i++ ) {
// this is FFT(kernel) * FFT(kernelS)
krp = *(ptr)*kerfftr[i+l*nx*nx] - *(ptr+1)*kerffti[i+l*nx*nx];
kip = *(ptr)*kerffti[i+l*nx*nx] + *(ptr+1)*kerfftr[i+l*nx*nx];
// and next we multiply by FFT(image):
*(ptr1) = *(ptr2)*krp - *(ptr2+1)*kip;
*(ptr1+1) = *(ptr2+1)*krp + *(ptr2)*kip;
ptr +=2; ptr1 +=2; ptr2 +=2;
}
// Transform back:
fftwf_execute(fftpxi);
ptr = (void *)resultx;
for ( i=0 ; i<nx*nx ; i++ ) {
ximage[i] = sqrt ( *(ptr) * *(ptr) + *(ptr+1) * *(ptr+1) );
ptr +=2;
}
}
cpu4 = p_cpu_secs(&sys);
cpu43 += cpu4-cpu3;
// FLUX NORMALIZATION FOR STAR. Has to be done *before* applying fieldstop
// will be used a bit below
// LGS FIXME FIXME FIXME: flux totally screwed up w/ new lgs_prof_amp!
tot = 0.0f;
for ( i=0 ; i<nx*nx ; i++ ) tot += ximage[i];
// APPLY FIELD STOP / AMPLITUDE MASK
// For instance to take into account the central dark spot of STRAP,
// or more generally a field stop
if (domask == 1) {
for ( i=0 ; i<nx*nx ; i++ ) {
ximage[i] = ximage[i] * submask[i];
}
}
// IF BACKGROUND CALIBRATION, NULL STAR SIGNAL
if (bckgrdinit) {
for ( i=0 ; i<nx*nx ; i++ ) ximage[i] = 0.0f;
}
// PUT THIS SUBAPERTURE'S XIMAGE INTO BIMAGE (binned image)
for ( i=0 ; i<nb*nb ; i++ ) { bimage[i] = 0.0f; }
for ( i=0 ; i<nx*nx ; i++ ) {
if (binindices[i]<0) continue;
//~ if (binindices[i]>nb*nb) {
//~ printf("binindices[%d] = %d > nb*nb (%d). It shouldn't be so! (ns=%d, nx=%d, nb=%d)\n",i,binindices[i],nb*nb,ns,nx,nb);
//~ return 1;
//~ }