NFFT  3.3.2
reconstruct_data_inh_2d1d.c
00001 /*
00002  * Copyright (c) 2002, 2016 Jens Keiner, Stefan Kunis, Daniel Potts
00003  *
00004  * This program is free software; you can redistribute it and/or modify it under
00005  * the terms of the GNU General Public License as published by the Free Software
00006  * Foundation; either version 2 of the License, or (at your option) any later
00007  * version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but WITHOUT
00010  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00011  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00012  * details.
00013  *
00014  * You should have received a copy of the GNU General Public License along with
00015  * this program; if not, write to the Free Software Foundation, Inc., 51
00016  * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  */
00018 #include <stdlib.h>
00019 #include <math.h>
00020 #include <limits.h>
00021 #include <complex.h>
00022 
00023 #include "nfft3.h"
00024 
00025 #ifndef MAX
00026 #define MAX(a,b) (((a)>(b))?(a):(b))
00027 #endif
00028 
00035 static void reconstruct(char* filename,int N,int M,int iteration , int weight)
00036 {
00037   int j,k,l;
00038   double time,min_time,max_time,min_inh,max_inh;
00039   double t0, t1;
00040   double t,real,imag;
00041   double w,epsilon=0.0000003;     /* epsilon is a the break criterium for
00042                                    the iteration */;
00043   mri_inh_2d1d_plan my_plan;
00044   solver_plan_complex my_iplan;
00045   FILE* fp,*fw,*fout_real,*fout_imag,*finh,*ftime;
00046   int my_N[3],my_n[3];
00047   int flags = PRE_PHI_HUT| PRE_PSI |MALLOC_X| MALLOC_F_HAT|
00048                       MALLOC_F| FFTW_INIT| FFT_OUT_OF_PLACE;
00049   unsigned infft_flags = CGNR | PRECOMPUTE_DAMP;
00050 
00051   double Ts;
00052   double W,T;
00053   int N3;
00054   int m=2;
00055   double sigma = 1.25;
00056 
00057   ftime=fopen("readout_time.dat","r");
00058   finh=fopen("inh.dat","r");
00059 
00060   min_time=INT_MAX; max_time=INT_MIN;
00061   for(j=0;j<M;j++)
00062   {
00063     fscanf(ftime,"%le ",&time);
00064     if(time<min_time)
00065       min_time = time;
00066     if(time>max_time)
00067       max_time = time;
00068   }
00069 
00070   fclose(ftime);
00071 
00072   Ts=(min_time+max_time)/2.0;
00073 
00074 
00075   min_inh=INT_MAX; max_inh=INT_MIN;
00076   for(j=0;j<N*N;j++)
00077   {
00078     fscanf(finh,"%le ",&w);
00079     if(w<min_inh)
00080       min_inh = w;
00081     if(w>max_inh)
00082       max_inh = w;
00083   }
00084   fclose(finh);
00085 
00086   N3=ceil((MAX(fabs(min_inh),fabs(max_inh))*(max_time-min_time)/2.0+(m)/(2*sigma))*4*sigma);
00087   /* N3 has to be even */
00088   if(N3%2!=0)
00089     N3++;
00090 
00091   T=((max_time-min_time)/2.0)/(0.5-((double) (m))/N3);
00092   W=N3/T;
00093 
00094   my_N[0]=N; my_n[0]=ceil(N*sigma);
00095   my_N[1]=N; my_n[1]=ceil(N*sigma);
00096   my_N[2]=N3; my_n[2]=N3;
00097 
00098   /* initialise nfft */
00099   mri_inh_2d1d_init_guru(&my_plan, my_N, M, my_n, m, sigma, flags,
00100                       FFTW_MEASURE| FFTW_DESTROY_INPUT);
00101 
00102 
00103   /* precompute lin psi if set */
00104   if(my_plan.plan.flags & PRE_LIN_PSI)
00105     nfft_precompute_lin_psi(&my_plan.plan);
00106 
00107   if (weight)
00108     infft_flags = infft_flags | PRECOMPUTE_WEIGHT;
00109 
00110   /* initialise my_iplan, advanced */
00111   solver_init_advanced_complex(&my_iplan,(nfft_mv_plan_complex*)(&my_plan), infft_flags );
00112 
00113   /* get the weights */
00114   if(my_iplan.flags & PRECOMPUTE_WEIGHT)
00115   {
00116     fw=fopen("weights.dat","r");
00117     for(j=0;j<my_plan.M_total;j++)
00118     {
00119         fscanf(fw,"%le ",&my_iplan.w[j]);
00120     }
00121     fclose(fw);
00122   }
00123 
00124   /* get the damping factors */
00125   if(my_iplan.flags & PRECOMPUTE_DAMP)
00126   {
00127     for(j=0;j<N;j++){
00128       for(k=0;k<N;k++) {
00129         int j2= j-N/2;
00130         int k2= k-N/2;
00131         double r=sqrt(j2*j2+k2*k2);
00132         if(r>(double) N/2)
00133           my_iplan.w_hat[j*N+k]=0.0;
00134         else
00135           my_iplan.w_hat[j*N+k]=1.0;
00136       }
00137     }
00138   }
00139 
00140   fp=fopen(filename,"r");
00141   ftime=fopen("readout_time.dat","r");
00142 
00143   for(j=0;j<my_plan.M_total;j++)
00144   {
00145     fscanf(fp,"%le %le %le %le",&my_plan.plan.x[2*j+0],&my_plan.plan.x[2*j+1],&real,&imag);
00146     my_iplan.y[j]=real+ _Complex_I*imag;
00147     fscanf(ftime,"%le ",&my_plan.t[j]);
00148 
00149     my_plan.t[j] = (my_plan.t[j]-Ts)/T;
00150   }
00151   fclose(fp);
00152   fclose(ftime);
00153 
00154 
00155   finh=fopen("inh.dat","r");
00156   for(j=0;j<N*N;j++)
00157   {
00158     fscanf(finh,"%le ",&my_plan.w[j]);
00159     my_plan.w[j]/=W;
00160   }
00161   fclose(finh);
00162 
00163 
00164   if(my_plan.plan.flags & PRE_PSI) {
00165     nfft_precompute_psi(&my_plan.plan);
00166   }
00167   if(my_plan.plan.flags & PRE_FULL_PSI) {
00168       nfft_precompute_full_psi(&my_plan.plan);
00169   }
00170 
00171   /* init some guess */
00172   for(j=0;j<my_plan.N_total;j++)
00173   {
00174     my_iplan.f_hat_iter[j]=0.0;
00175   }
00176 
00177   t0 = nfft_clock_gettime_seconds();
00178 
00179   /* inverse trafo */
00180   solver_before_loop_complex(&my_iplan);
00181   for(l=0;l<iteration;l++)
00182   {
00183     /* break if dot_r_iter is smaller than epsilon*/
00184     if(my_iplan.dot_r_iter<epsilon)
00185     break;
00186     fprintf(stderr,"%e,  %i of %i\n",sqrt(my_iplan.dot_r_iter),
00187     l+1,iteration);
00188     solver_loop_one_step_complex(&my_iplan);
00189   }
00190 
00191   t1 = nfft_clock_gettime_seconds();
00192   t = t1-t0;
00193 
00194   fout_real=fopen("output_real.dat","w");
00195   fout_imag=fopen("output_imag.dat","w");
00196 
00197   for (j=0;j<N*N;j++) {
00198     /* Verschiebung wieder herausrechnen */
00199     my_iplan.f_hat_iter[j]*=cexp(-2.0*_Complex_I*M_PI*Ts*my_plan.w[j]*W);
00200 
00201     fprintf(fout_real,"%le ",creal(my_iplan.f_hat_iter[j]));
00202     fprintf(fout_imag,"%le ",cimag(my_iplan.f_hat_iter[j]));
00203   }
00204 
00205   fclose(fout_real);
00206   fclose(fout_imag);
00207   solver_finalize_complex(&my_iplan);
00208   mri_inh_2d1d_finalize(&my_plan);
00209 }
00210 
00211 
00212 int main(int argc, char **argv)
00213 {
00214   if (argc <= 5) {
00215 
00216     printf("usage: ./reconstruct_data_inh_2d1d FILENAME N M ITER WEIGHTS\n");
00217     return 1;
00218   }
00219 
00220   reconstruct(argv[1],atoi(argv[2]),atoi(argv[3]),atoi(argv[4]),atoi(argv[5]));
00221 
00222   return 1;
00223 }
00224 /* \} */