NFFT  3.3.0
reconstruct_data_inh_3d.c
1 /*
2  * Copyright (c) 2002, 2015 Jens Keiner, Stefan Kunis, Daniel Potts
3  *
4  * This program is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU General Public License as published by the Free Software
6  * Foundation; either version 2 of the License, or (at your option) any later
7  * version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 51
16  * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 /* $Id$ */
20 #include <stdlib.h>
21 #include <math.h>
22 #include <limits.h>
23 #include <complex.h>
24 
25 #include "nfft3.h"
26 
27 #ifndef MAX
28 #define MAX(a,b) (((a)>(b))?(a):(b))
29 #endif
30 
37 static void reconstruct(char* filename,int N,int M,int iteration , int weight)
38 {
39  int j,k,l;
40  double t0, t1;
41  double time,min_time,max_time,min_inh,max_inh;
42  double t,real,imag;
43  double w,epsilon=0.0000003; /* epsilon is a the break criterium for
44  the iteration */;
45  mri_inh_3d_plan my_plan;
46  solver_plan_complex my_iplan;
47  FILE* fp,*fw,*fout_real,*fout_imag,*finh,*ftime;
48  int my_N[3],my_n[3];
49  int flags = PRE_PHI_HUT| PRE_PSI |MALLOC_X| MALLOC_F_HAT|
50  MALLOC_F| FFTW_INIT| FFT_OUT_OF_PLACE;
51  unsigned infft_flags = CGNR | PRECOMPUTE_DAMP;
52 
53  double Ts;
54  double W;
55  int N3;
56  int m=2;
57  double sigma = 1.25;
58 
59  ftime=fopen("readout_time.dat","r");
60  finh=fopen("inh.dat","r");
61 
62  min_time=INT_MAX; max_time=INT_MIN;
63  for(j=0;j<M;j++)
64  {
65  fscanf(ftime,"%le ",&time);
66  if(time<min_time)
67  min_time = time;
68  if(time>max_time)
69  max_time = time;
70  }
71 
72  fclose(ftime);
73 
74  Ts=(min_time+max_time)/2.0;
75 
76 
77  min_inh=INT_MAX; max_inh=INT_MIN;
78  for(j=0;j<N*N;j++)
79  {
80  fscanf(finh,"%le ",&w);
81  if(w<min_inh)
82  min_inh = w;
83  if(w>max_inh)
84  max_inh = w;
85  }
86  fclose(finh);
87 
88  N3=ceil((MAX(fabs(min_inh),fabs(max_inh))*(max_time-min_time)/2.0+m/(2*sigma))*4*sigma);
89  /* N3 has to be even */
90  if(N3%2!=0)
91  N3++;
92 
93  W= MAX(fabs(min_inh),fabs(max_inh))/(0.5-((double) m)/N3);
94 
95  my_N[0]=N;my_n[0]=ceil(N*sigma);
96  my_N[1]=N; my_n[1]=ceil(N*sigma);
97  my_N[2]=N3; my_n[2]=ceil(N3*sigma);
98 
99  /* initialise nfft */
100  mri_inh_3d_init_guru(&my_plan, my_N, M, my_n, m, sigma, flags,
101  FFTW_MEASURE| FFTW_DESTROY_INPUT);
102 
103  if (weight)
104  infft_flags = infft_flags | PRECOMPUTE_WEIGHT;
105 
106  /* initialise my_iplan, advanced */
107  solver_init_advanced_complex(&my_iplan,(nfft_mv_plan_complex*)(&my_plan), infft_flags );
108 
109  /* get the weights */
110  if(my_iplan.flags & PRECOMPUTE_WEIGHT)
111  {
112  fw=fopen("weights.dat","r");
113  for(j=0;j<my_plan.M_total;j++)
114  {
115  fscanf(fw,"%le ",&my_iplan.w[j]);
116  }
117  fclose(fw);
118  }
119 
120  /* get the damping factors */
121  if(my_iplan.flags & PRECOMPUTE_DAMP)
122  {
123  for(j=0;j<N;j++){
124  for(k=0;k<N;k++) {
125  int j2= j-N/2;
126  int k2= k-N/2;
127  double r=sqrt(j2*j2+k2*k2);
128  if(r>(double) N/2)
129  my_iplan.w_hat[j*N+k]=0.0;
130  else
131  my_iplan.w_hat[j*N+k]=1.0;
132  }
133  }
134  }
135 
136  fp=fopen(filename,"r");
137  ftime=fopen("readout_time.dat","r");
138 
139  for(j=0;j<my_plan.M_total;j++)
140  {
141  fscanf(fp,"%le %le %le %le",&my_plan.plan.x[3*j+0],&my_plan.plan.x[3*j+1],&real,&imag);
142  my_iplan.y[j]=real+ _Complex_I*imag;
143  fscanf(ftime,"%le ",&my_plan.plan.x[3*j+2]);
144 
145  my_plan.plan.x[3*j+2] = (my_plan.plan.x[3*j+2]-Ts)*W/N3;
146  }
147  fclose(fp);
148  fclose(ftime);
149 
150 
151  finh=fopen("inh.dat","r");
152  for(j=0;j<N*N;j++)
153  {
154  fscanf(finh,"%le ",&my_plan.w[j]);
155  my_plan.w[j]/=W;
156  }
157  fclose(finh);
158 
159 
160  if(my_plan.plan.flags & PRE_PSI) {
161  nfft_precompute_psi(&my_plan.plan);
162  }
163  if(my_plan.plan.flags & PRE_FULL_PSI) {
164  nfft_precompute_full_psi(&my_plan.plan);
165  }
166 
167  /* init some guess */
168  for(j=0;j<my_plan.N_total;j++)
169  {
170  my_iplan.f_hat_iter[j]=0.0;
171  }
172 
173  t0 = nfft_clock_gettime_seconds();
174 
175  /* inverse trafo */
176  solver_before_loop_complex(&my_iplan);
177  for(l=0;l<iteration;l++)
178  {
179  /* break if dot_r_iter is smaller than epsilon*/
180  if(my_iplan.dot_r_iter<epsilon)
181  break;
182  fprintf(stderr,"%e, %i of %i\n",sqrt(my_iplan.dot_r_iter),
183  l+1,iteration);
184  solver_loop_one_step_complex(&my_iplan);
185  }
186 
187  t1 = nfft_clock_gettime_seconds();
188  t = t1-t0;
189 
190  fout_real=fopen("output_real.dat","w");
191  fout_imag=fopen("output_imag.dat","w");
192 
193  for (j=0;j<N*N;j++) {
194  /* Verschiebung wieder herausrechnen */
195  my_iplan.f_hat_iter[j]*=cexp(-2.0*_Complex_I*M_PI*Ts*my_plan.w[j]*W);
196 
197  fprintf(fout_real,"%le ",creal(my_iplan.f_hat_iter[j]));
198  fprintf(fout_imag,"%le ",cimag(my_iplan.f_hat_iter[j]));
199  }
200 
201  fclose(fout_real);
202  fclose(fout_imag);
203  solver_finalize_complex(&my_iplan);
204  mri_inh_3d_finalize(&my_plan);
205 }
206 
207 
208 int main(int argc, char **argv)
209 {
210  if (argc <= 5) {
211 
212  printf("usage: ./reconstruct_data_inh_3d FILENAME N M ITER WEIGHTS\n");
213  return 1;
214  }
215 
216  reconstruct(argv[1],atoi(argv[2]),atoi(argv[3]),atoi(argv[4]),atoi(argv[5]));
217 
218  return 1;
219 }
220 /* \} */
double * w
weighting factors
Definition: nfft3.h:786
unsigned flags
iteration type
Definition: nfft3.h:786
double dot_r_iter
weighted dotproduct of r_iter
Definition: nfft3.h:786
NFFT_INT N_total
Total number of Fourier coefficients.
Definition: nfft3.h:527
NFFT_INT M_total
Total number of samples.
Definition: nfft3.h:527
fftw_complex * y
right hand side, samples
Definition: nfft3.h:786
double * x
Nodes in time/spatial domain, size is doubles.
Definition: nfft3.h:194
unsigned flags
Flags for precomputation, (de)allocation, and FFTW usage, default setting is PRE_PHI_HUT | PRE_PSI | ...
Definition: nfft3.h:194
data structure for an inverse NFFT plan with double precision
Definition: nfft3.h:786
double * w_hat
damping factors
Definition: nfft3.h:786
static void reconstruct(char *filename, int N, int M, int iteration, int weight)
reconstruct makes an inverse 2d nfft
fftw_complex * f_hat_iter
iterative solution
Definition: nfft3.h:786