33 #define NSFTT_DISABLE_TEST
46 plan_1d->
x[j] = ths->
x[ths->
d * j + 1];
49 for(k0=0;k0<ths->
N[0];k0++)
57 omega = ((double)(k0 - ths->
N[0]/2)) * ths->
x[ths->
d * j + 0];
58 ths->
f[j] += plan_1d->
f[j] * cexp( - I*2*
PI*omega);
69 plan_1d->
x[j] = ths->
x[ths->
d * j + 1];
71 for(k0=0;k0<ths->N[0];k0++)
75 omega = ((double)(k0 - ths->
N[0]/2)) * ths->
x[ths->
d * j + 0];
76 plan_1d->
f[j] = ths->
f[j] * cexp( + _Complex_I*2*
PI*omega);
81 nfft_adjoint(plan_1d);
96 plan_1d->
x[j] = ths->
x[ths->
d * j + 2];
99 for(k0=0;k0<ths->
N[0];k0++)
100 for(k1=0;k1<ths->
N[1];k1++)
102 plan_1d->
f_hat = ths->
f_hat + (k0*ths->
N[1]+k1)*ths->
N[2];
108 omega = ((double)(k0 - ths->
N[0]/2)) * ths->
x[ths->
d * j + 0]
109 + ((
double)(k1 - ths->
N[1]/2)) * ths->
x[ths->
d * j + 1];
110 ths->
f[j] += plan_1d->
f[j] * cexp( - I*2*
PI*omega);
121 plan_1d->
x[j] = ths->
x[ths->
d * j + 2];
123 for(k0=0;k0<ths->N[0];k0++)
124 for(k1=0;k1<ths->
N[1];k1++)
128 omega = ((double)(k0 - ths->
N[0]/2)) * ths->
x[ths->
d * j + 0]
129 + ((
double)(k1 - ths->
N[1]/2)) * ths->
x[ths->
d * j + 1];
130 plan_1d->
f[j] = ths->
f[j] * cexp( + _Complex_I*2*
PI*omega);
133 plan_1d->
f_hat = ths->
f_hat + (k0*ths->
N[1]+k1)*ths->
N[2];
135 nfft_adjoint(plan_1d);
150 plan_2d->
x[2*j+0] = ths->
x[ths->
d * j + 1];
151 plan_2d->
x[2*j+1] = ths->
x[ths->
d * j + 2];
154 for(k0=0;k0<ths->
N[0];k0++)
162 omega = ((double)(k0 - ths->
N[0]/2)) * ths->
x[ths->
d * j + 0];
163 ths->
f[j] += plan_2d->
f[j] * cexp( - I*2*
PI*omega);
175 plan_2d->
x[2*j+0] = ths->
x[ths->
d * j + 1];
176 plan_2d->
x[2*j+1] = ths->
x[ths->
d * j + 2];
179 for(k0=0;k0<ths->
N[0];k0++)
183 omega = ((double)(k0 - ths->
N[0]/2)) * ths->
x[ths->
d * j + 0];
184 plan_2d->
f[j] = ths->
f[j] * cexp( + _Complex_I*2*
PI*omega);
189 nfft_adjoint(plan_2d);
196 static int index_sparse_to_full_direct_2d(
int J,
int k)
204 int i, o, a, b,s,l,m1,m2;
207 if (k>=(J+4)*X(exp2i)(J+1))
216 i=k-4*((J+1)/2+1)*N_B;
317 static inline int index_sparse_to_full_2d(
nsfft_plan *ths,
int k)
320 if( k < ths->N_total)
326 #ifndef NSFTT_DISABLE_TEST
327 static int index_full_to_sparse_2d(
int J,
int k)
339 if ( (k1>=-(a/2)) && (k1<a/2) && (k2>=(-a/2)) && (k2<a/2) )
341 return(4*((J+1)/2+1)*N_B+(k1+a/2)*a+(k2+a/2));
344 for (r=0; r<=(J+1)/2; r++)
348 if ( (k1>=-(b/2)) && (k1<(b+1)/2) && (k2>=a) && (k2<2*a) )
351 return((4*r+0)*N_B+(k1+b/2)*a+(k2-a));
353 return((4*r+0)*N_B+(k2-a)*b+(k1+b/2));
355 else if ( (k1>=a) && (k1<2*a) && (k2>=-(b/2)) && (k2<(b+1)/2) )
358 return((4*r+1)*N_B+(k2+b/2)*a+(k1-a));
360 return((4*r+1)*N_B+(k1-a)*b+(k2+b/2));
362 else if ( (k1>=-(b/2)) && (k1<(b+1)/2) && (k2>=-2*a) && (k2<-a) )
365 return((4*r+2)*N_B+(k1+b/2)*a+(k2+2*a));
367 return((4*r+2)*N_B+(k2+2*a)*b+(k1+b/2));
369 else if ( (k1>=-2*a) && (k1<-a) && (k2>=-(b/2)) && (k2<(b+1)/2) )
372 return((4*r+3)*N_B+(k2+b/2)*a+(k1+2*a));
374 return((4*r+3)*N_B+(k1+2*a)*b+(k2+b/2));
383 static void init_index_sparse_to_full_2d(
nsfft_plan *ths)
387 for (k_S=0; k_S<ths->
N_total; k_S++)
393 static inline int index_sparse_to_full_3d(
nsfft_plan *ths,
int k)
396 if( k < ths->N_total)
403 #ifndef NSFTT_DISABLE_TEST
404 static int index_full_to_sparse_3d(
int J,
int k)
413 int k2=((k/N)%N)-N/2;
418 if((k1>=-(a/2)) && (k1<a/2) && (k2>=(-a/2)) && (k2<a/2) && (k3>=(-a/2)) &&
421 return(6*X(exp2i)(J)*(X(exp2i)((J+1)/2+1)-1)+((k1+a/2)*a+(k2+a/2))*a+
426 for (r=0; r<=(J+1)/2; r++)
434 if ((k1>=a) && (k1<2*a) && (k2>=-(b/2)) && (k2<(b+1)/2) &&
435 (k3>=-(b/2)) && (k3<(b+1)/2))
438 return sum_N_B_less_r+N_B_r*0 + ((k2+b/2)*b+k3+b/2)*a + (k1-a);
440 return sum_N_B_less_r+N_B_r*0 + ((k1-a)*b+(k2+b/2))*b + (k3+b/2);
442 else if ((k2>=a) && (k2<2*a) && (k1>=-(b/2)) && (k1<(b+1)/2) &&
443 (k3>=-(b/2)) && (k3<(b+1)/2))
446 return sum_N_B_less_r+N_B_r*1 + ((k1+b/2)*b+k3+b/2)*a + (k2-a);
448 return sum_N_B_less_r+N_B_r*1 + ((k1+b/2)*b+(k2-a))*a + (k3+b/2);
450 return sum_N_B_less_r+N_B_r*1 + ((k2-a)*b+(k1+b/2))*b + (k3+b/2);
452 else if ((k3>=a) && (k3<2*a) && (k1>=-(b/2)) && (k1<(b+1)/2) &&
453 (k2>=-(b/2)) && (k2<(b+1)/2))
456 return sum_N_B_less_r+N_B_r*2 + ((k1+b/2)*b+k2+b/2)*a + (k3-a);
458 return sum_N_B_less_r+N_B_r*2 + ((k3-a)*b+(k1+b/2))*b + (k2+b/2);
461 else if ((k1>=-2*a) && (k1<-a) && (k2>=-(b/2)) && (k2<(b+1)/2) &&
462 (k3>=-(b/2)) && (k3<(b+1)/2))
465 return sum_N_B_less_r+N_B_r*3 + ((k2+b/2)*b+k3+b/2)*a + (k1+2*a);
467 return sum_N_B_less_r+N_B_r*3 + ((k1+2*a)*b+(k2+b/2))*b + (k3+b/2);
469 else if ((k2>=-2*a) && (k2<-a) && (k1>=-(b/2)) && (k1<(b+1)/2) &&
470 (k3>=-(b/2)) && (k3<(b+1)/2))
473 return sum_N_B_less_r+N_B_r*4 + ((k1+b/2)*b+k3+b/2)*a + (k2+2*a);
475 return sum_N_B_less_r+N_B_r*4 + ((k1+b/2)*b+(k2+2*a))*a + (k3+b/2);
477 return sum_N_B_less_r+N_B_r*4 + ((k2+2*a)*b+(k1+b/2))*b + (k3+b/2);
479 else if ((k3>=-2*a) && (k3<-a) && (k1>=-(b/2)) && (k1<(b+1)/2) &&
480 (k2>=-(b/2)) && (k2<(b+1)/2))
483 return sum_N_B_less_r+N_B_r*5 + ((k1+b/2)*b+k2+b/2)*a + (k3+2*a);
485 return sum_N_B_less_r+N_B_r*5 + ((k3+2*a)*b+(k1+b/2))*b + (k2+b/2);
488 sum_N_B_less_r+=6*N_B_r;
496 static void init_index_sparse_to_full_3d(
nsfft_plan *ths)
500 int N=X(exp2i)(ths->
J+2);
503 for (k_s=0, r=0; r<=(ths->
J+1)/2; r++)
505 a=X(exp2i)(ths->
J-r);
512 for(k2=-b/2;k2<(b+1)/2;k2++)
513 for(k3=-b/2;k3<(b+1)/2;k3++)
514 for(k1=a; k1<2*a; k1++,k_s++)
517 for(k1=a; k1<2*a; k1++)
518 for(k2=-b/2;k2<(b+1)/2;k2++)
519 for(k3=-b/2;k3<(b+1)/2;k3++,k_s++)
524 for(k1=-b/2;k1<(b+1)/2;k1++)
525 for(k3=-b/2;k3<(b+1)/2;k3++)
526 for(k2=a; k2<2*a; k2++,k_s++)
529 for(k1=-b/2;k1<(b+1)/2;k1++)
530 for(k2=a; k2<2*a; k2++)
531 for(k3=-b/2;k3<(b+1)/2;k3++,k_s++)
534 for(k2=a; k2<2*a; k2++)
535 for(k1=-b/2;k1<(b+1)/2;k1++)
536 for(k3=-b/2;k3<(b+1)/2;k3++,k_s++)
541 for(k1=-b/2;k1<(b+1)/2;k1++)
542 for(k2=-b/2;k2<(b+1)/2;k2++)
543 for(k3=a; k3<2*a; k3++,k_s++)
546 for(k3=a; k3<2*a; k3++)
547 for(k1=-b/2;k1<(b+1)/2;k1++)
548 for(k2=-b/2;k2<(b+1)/2;k2++,k_s++)
553 for(k2=-b/2;k2<(b+1)/2;k2++)
554 for(k3=-b/2;k3<(b+1)/2;k3++)
555 for(k1=-2*a; k1<-a; k1++,k_s++)
558 for(k1=-2*a; k1<-a; k1++)
559 for(k2=-b/2;k2<(b+1)/2;k2++)
560 for(k3=-b/2;k3<(b+1)/2;k3++,k_s++)
565 for(k1=-b/2;k1<(b+1)/2;k1++)
566 for(k3=-b/2;k3<(b+1)/2;k3++)
567 for(k2=-2*a; k2<-a; k2++,k_s++)
570 for(k1=-b/2;k1<(b+1)/2;k1++)
571 for(k2=-2*a; k2<-a; k2++)
572 for(k3=-b/2;k3<(b+1)/2;k3++,k_s++)
575 for(k2=-2*a; k2<-a; k2++)
576 for(k1=-b/2;k1<(b+1)/2;k1++)
577 for(k3=-b/2;k3<(b+1)/2;k3++,k_s++)
582 for(k1=-b/2;k1<(b+1)/2;k1++)
583 for(k2=-b/2;k2<(b+1)/2;k2++)
584 for(k3=-2*a; k3<-a; k3++,k_s++)
587 for(k3=-2*a; k3<-a; k3++)
588 for(k1=-b/2;k1<(b+1)/2;k1++)
589 for(k2=-b/2;k2<(b+1)/2;k2++,k_s++)
594 for(k1=-Nc/2;k1<Nc/2;k1++)
595 for(k2=-Nc/2;k2<Nc/2;k2++)
596 for(k3=-Nc/2; k3<Nc/2; k3++,k_s++)
607 memset(ths_full_plan->
f_hat, 0, ths_full_plan->
N_total*
sizeof(
double _Complex));
617 #ifndef NSFTT_DISABLE_TEST
625 const int N=ths_full_plan->
N[0];
626 const int N_B=X(exp2i)(J);
629 nsfft_cp(ths, ths_full_plan);
632 printf(
"f_hat blockwise\n");
633 for (r=0; r<=(J+1)/2; r++){
634 a=X(exp2i)(J-r); b=X(exp2i)(r);
637 for (k1=0; k1<a; k1++){
638 for (k2=0; k2<b; k2++){
639 printf(
"(%1.1f,%1.1f) ", creal(ths->
f_hat[(4*r+1)*N_B+ k1*b+k2]),
640 cimag(ths->
f_hat[(4*r+1)*N_B+ k1*b+k2]));
646 for (k1=0; k1<a; k1++){
647 for (k2=0; k2<b; k2++){
648 printf(
"(%1.1f,%1.1f) ", creal(ths->
f_hat[(4*r+3)*N_B+ k1*b+k2]),
649 cimag(ths->
f_hat[(4*r+3)*N_B+ k1*b+k2]));
655 for (k2=0; k2<b; k2++){
656 for (k1=0; k1<a; k1++){
657 printf(
"(%1.1f,%1.1f) ", creal(ths->
f_hat[(4*r+0)*N_B+ k2*a+k1]),
658 cimag(ths->
f_hat[(4*r+0)*N_B+ k2*a+k1]));
664 for (k2=0; k2<b; k2++){
665 for (k1=0; k1<a; k1++){
666 printf(
"(%1.1f,%1.1f) ", creal(ths->
f_hat[(4*r+2)*N_B+ k2*a+k1]),
667 cimag(ths->
f_hat[(4*r+2)*N_B+ k2*a+k1]));
675 printf(
"full f_hat\n");
676 for (k1=0;k1<N;k1++){
677 for (k2=0;k2<N;k2++){
678 printf(
"(%1.1f,%1.1f) ", creal(ths_full_plan->
f_hat[k1*N+k2]),
679 cimag(ths_full_plan->
f_hat[k1*N+k2]));
686 #ifndef NSFTT_DISABLE_TEST
687 static void test_sparse_to_full_2d(
nsfft_plan* ths)
690 int N=X(exp2i)(ths->
J+2);
692 printf(
"N=%d\n\n",N);
697 k_S=index_full_to_sparse_2d(ths->
J, k1*N+k2);
699 printf(
"(%+d, %+d)\t= %+d \t= %+d = %+d \n",k1-N/2,k2-N/2,
705 #ifndef NSFTT_DISABLE_TEST
706 static void test_sparse_to_full_3d(
nsfft_plan* ths)
709 int N=X(exp2i)(ths->
J+2);
711 printf(
"N=%d\n\n",N);
717 k_S=index_full_to_sparse_3d(ths->
J, (k1*N+k2)*N+k3);
719 printf(
"(%d, %d, %d)\t= %d \t= %d = %d \n",k1-N/2,k2-N/2,k3-N/2,
726 void nsfft_init_random_nodes_coeffs(
nsfft_plan *ths)
767 int N=X(exp2i)(ths->
J+2);
769 memset(ths->
f,0,ths->
M_total*
sizeof(
double _Complex));
771 for(k_S=0;k_S<ths->
N_total;k_S++)
782 ths->
f[j] += ths->
f_hat[k_S] * cexp( - I*2*
PI*omega);
791 int N=X(exp2i)(ths->
J+2);
794 memset(ths->
f,0,ths->
M_total*
sizeof(
double _Complex));
796 for(k_S=0;k_S<ths->
N_total;k_S++)
810 ths->
f[j] += ths->
f_hat[k_S] * cexp( - I*2*
PI*omega);
827 int N=X(exp2i)(ths->
J+2);
829 memset(ths->
f_hat,0,ths->
N_total*
sizeof(
double _Complex));
831 for(k_S=0;k_S<ths->
N_total;k_S++)
842 ths->
f_hat[k_S] += ths->
f[j] * cexp( + _Complex_I*2*
PI*omega);
851 int N=X(exp2i)(ths->
J+2);
854 memset(ths->
f_hat,0,ths->
N_total*
sizeof(
double _Complex));
856 for(k_S=0;k_S<ths->
N_total;k_S++)
870 ths->
f_hat[k_S] += ths->
f[j] * cexp( + _Complex_I*2*
PI*omega);
878 nsdft_adjoint_2d(ths);
880 nsdft_adjoint_3d(ths);
902 for(rr=0;rr<=(J+1)/2;rr++)
911 temp=-3.0*
PI*X(exp2i)(J-rr);
937 if((r==rr)&&(J-rr!=rr))
948 if((r==rr)&&(J-rr!=rr))
979 if((r==rr)&&(J-rr!=rr))
990 if((r==rr)&&(J-rr!=rr))
1018 for(rr=0;rr<=(J+1)/2;rr++)
1027 temp=-3.0*
PI*X(exp2i)(J-rr);
1057 if((r==rr)&&(J-rr!=rr))
1068 if((r==rr)&&(J-rr!=rr))
1099 if((r==rr)&&(J-rr!=rr))
1110 if((r==rr)&&(J-rr!=rr))
1119 int sum_N_B_less_r,N_B_r,a,b;
1136 for(rr=0;rr<=(J+1)/2;rr++)
1144 ths->
act_nfft_plan->my_fftw_plan1 = ths->set_fftw_plan1[rr];
1162 if((J==0)||((J==1)&&(rr==1)))
1165 temp=-3.0*
PI*X(exp2i)(J-rr);
1244 if((J==0)||((J==1)&&(rr==1)))
1247 temp=-3.0*
PI*X(exp2i)(J-rr);
1325 sum_N_B_less_r+=6*N_B_r;
1329 static void nsfft_adjoint_3d(
nsfft_plan *ths)
1333 int sum_N_B_less_r,N_B_r,a,b;
1350 for(rr=0;rr<=(J+1)/2;rr++)
1358 ths->
act_nfft_plan->my_fftw_plan1 = ths->set_fftw_plan1[rr];
1359 ths->
act_nfft_plan->my_fftw_plan2 = ths->set_fftw_plan2[rr];
1377 if((J==0)||((J==1)&&(rr==1)))
1380 temp=-3.0*
PI*X(exp2i)(J-rr);
1459 if((J==0)||((J==1)&&(rr==1)))
1462 temp=-3.0*
PI*X(exp2i)(J-rr);
1540 sum_N_B_less_r+=6*N_B_r;
1547 nsfft_trafo_2d(ths);
1549 nsfft_trafo_3d(ths);
1555 nsfft_adjoint_2d(ths);
1557 nsfft_adjoint_3d(ths);
1564 static void nsfft_init_2d(
nsfft_plan *ths,
int J,
int M,
int m,
unsigned snfft_flags)
1570 ths->
flags=snfft_flags;
1574 ths->
N_total=(J+4)*X(exp2i)(J+1);
1577 ths->
f = (
double _Complex *)
nfft_malloc(M*
sizeof(
double _Complex));
1584 ths->set_fftw_plan1=(fftw_plan*)
nfft_malloc((J/2+1)*
sizeof(fftw_plan));
1585 ths->set_fftw_plan2=(fftw_plan*)
nfft_malloc((J/2+1)*
sizeof(fftw_plan));
1591 N[0]=1; n[0]=ths->
sigma*N[0];
1592 N[1]=X(exp2i)(J); n[1]=ths->
sigma*N[1];
1594 nfft_init_guru(ths->
act_nfft_plan,2,N,M,n,m, FG_PSI| MALLOC_X| MALLOC_F| FFTW_INIT, FFTW_MEASURE);
1604 N[0]=X(exp2i)(r); n[0]=ths->
sigma*N[0];
1605 N[1]=X(exp2i)(J-r); n[1]=ths->
sigma*N[1];
1606 ths->set_fftw_plan1[r] =
1610 ths->set_fftw_plan2[r] =
1616 for(r=0;r<=X(log2i)(m);r++)
1618 N[0]=X(exp2i)(J-r); n[0]=ths->
sigma*N[0];
1620 nfft_init_guru(&(ths->
set_nfft_plan_1d[r]),1,N,M,n,m, MALLOC_X| MALLOC_F| FFTW_INIT, FFTW_MEASURE);
1628 N[0]=X(exp2i)(J/2+1); n[0]=ths->
sigma*N[0];
1629 N[1]=X(exp2i)(J/2+1); n[1]=ths->
sigma*N[1];
1638 if(ths->
flags & NSDFT)
1641 init_index_sparse_to_full_2d(ths);
1649 static void nsfft_init_3d(
nsfft_plan *ths,
int J,
int M,
int m,
unsigned snfft_flags)
1655 ths->
flags=snfft_flags;
1659 ths->
N_total=6*X(exp2i)(J)*(X(exp2i)((J+1)/2+1)-1)+X(exp2i)(3*(J/2+1));
1662 ths->
f = (
double _Complex *)
nfft_malloc(M*
sizeof(
double _Complex));
1665 ths->x_102= (
double*)
nfft_malloc(3*M*
sizeof(
double));
1666 ths->x_201= (
double*)
nfft_malloc(3*M*
sizeof(
double));
1667 ths->x_120= (
double*)
nfft_malloc(3*M*
sizeof(
double));
1673 ths->set_fftw_plan1=(fftw_plan*)
nfft_malloc(((J+1)/2+1)*
sizeof(fftw_plan));
1674 ths->set_fftw_plan2=(fftw_plan*)
nfft_malloc(((J+1)/2+1)*
sizeof(fftw_plan));
1681 N[0]=1; n[0]=ths->
sigma*N[0];
1682 N[1]=1; n[1]=ths->
sigma*N[1];
1683 N[2]=X(exp2i)(J); n[2]=ths->
sigma*N[2];
1685 nfft_init_guru(ths->
act_nfft_plan,3,N,M,n,m, FG_PSI| MALLOC_X| MALLOC_F, FFTW_MEASURE);
1704 for(rr=1;rr<=(J+1)/2;rr++)
1711 n[0]=ths->
sigma*X(exp2i)(r);
1713 n[1]=ths->
sigma*X(exp2i)(J-r);
1715 n[1]=ths->
sigma*X(exp2i)(r);
1716 n[2]=ths->
sigma*X(exp2i)(J-r);
1718 ths->set_fftw_plan1[rr] =
1721 ths->set_fftw_plan2[rr] =
1727 for(r=0;r<=X(log2i)(m);r++)
1729 N[0]=X(exp2i)(J-r); n[0]=ths->
sigma*N[0];
1730 N[1]=X(exp2i)(J-r); n[1]=ths->
sigma*N[1];
1734 nfft_init_guru(&(ths->
set_nfft_plan_1d[r]),1,N,M,n,m, MALLOC_X| MALLOC_F| FFTW_INIT, FFTW_MEASURE);
1738 nfft_init_guru(&(ths->
set_nfft_plan_2d[r]),2,N,M,n,m, MALLOC_X| MALLOC_F| FFTW_INIT, FFTW_MEASURE);
1747 N[0]=X(exp2i)(J/2+1); n[0]=ths->
sigma*N[0];
1748 N[1]=X(exp2i)(J/2+1); n[1]=ths->
sigma*N[1];
1749 N[2]=X(exp2i)(J/2+1); n[2]=ths->
sigma*N[2];
1758 if(ths->
flags & NSDFT)
1761 init_index_sparse_to_full_3d(ths);
1767 void nsfft_init(
nsfft_plan *ths,
int d,
int J,
int M,
int m,
unsigned flags)
1772 nsfft_init_2d(ths, J, M, m, flags);
1774 nsfft_init_3d(ths, J, M, m, flags);
1777 ths->
mv_trafo = (void (*) (
void* ))nsfft_trafo;
1778 ths->
mv_adjoint = (void (*) (
void* ))nsfft_adjoint;
1781 void nsfft_init(
nsfft_plan *ths,
int d,
int J,
int M,
int m,
unsigned flags)
1790 "\nError in kernel/nsfft_init: require GAUSSIAN window function\n");
1794 static void nsfft_finalize_2d(
nsfft_plan *ths)
1798 if(ths->
flags & NSDFT)
1817 for(r=1;r<=ths->
J/2;r++)
1819 fftw_destroy_plan(ths->set_fftw_plan2[r]);
1820 fftw_destroy_plan(ths->set_fftw_plan1[r]);
1837 static void nsfft_finalize_3d(
nsfft_plan *ths)
1841 if(ths->
flags & NSDFT)
1864 for(r=1;r<=(ths->
J+1)/2;r++)
1866 fftw_destroy_plan(ths->set_fftw_plan2[r]);
1867 fftw_destroy_plan(ths->set_fftw_plan1[r]);
1891 nsfft_finalize_2d(ths);
1893 nsfft_finalize_3d(ths);