NFFT  3.4.1
reconstruct_data_inh_3d.c
1 /*
2  * Copyright (c) 2002, 2017 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 #include <stdlib.h>
19 #include <math.h>
20 #include <limits.h>
21 #include <complex.h>
22 
23 #include "nfft3.h"
24 
25 #ifndef MAX
26 #define MAX(a,b) (((a)>(b))?(a):(b))
27 #endif
28 
35 static void reconstruct(char* filename,int N,int M,int iteration , int weight)
36 {
37  int j,k,l;
38  double t0, t1;
39  double time,min_time,max_time,min_inh,max_inh;
40  double t,real,imag;
41  double w,epsilon=0.0000003; /* epsilon is a the break criterium for
42  the iteration */;
43  mri_inh_3d_plan my_plan;
44  solver_plan_complex my_iplan;
45  FILE* fp,*fw,*fout_real,*fout_imag,*finh,*ftime;
46  int my_N[3],my_n[3];
49  unsigned infft_flags = CGNR | PRECOMPUTE_DAMP;
50 
51  double Ts;
52  double W;
53  int N3;
54  int m=2;
55  double sigma = 1.25;
56 
57  ftime=fopen("readout_time.dat","r");
58  finh=fopen("inh.dat","r");
59 
60  min_time=INT_MAX; max_time=INT_MIN;
61  for(j=0;j<M;j++)
62  {
63  fscanf(ftime,"%le ",&time);
64  if(time<min_time)
65  min_time = time;
66  if(time>max_time)
67  max_time = time;
68  }
69 
70  fclose(ftime);
71 
72  Ts=(min_time+max_time)/2.0;
73 
74 
75  min_inh=INT_MAX; max_inh=INT_MIN;
76  for(j=0;j<N*N;j++)
77  {
78  fscanf(finh,"%le ",&w);
79  if(w<min_inh)
80  min_inh = w;
81  if(w>max_inh)
82  max_inh = w;
83  }
84  fclose(finh);
85 
86  N3=ceil((MAX(fabs(min_inh),fabs(max_inh))*(max_time-min_time)/2.0+m/(2*sigma))*4*sigma);
87  /* N3 has to be even */
88  if(N3%2!=0)
89  N3++;
90 
91  W= MAX(fabs(min_inh),fabs(max_inh))/(0.5-((double) m)/N3);
92 
93  my_N[0]=N;my_n[0]=ceil(N*sigma);
94  my_N[1]=N; my_n[1]=ceil(N*sigma);
95  my_N[2]=N3; my_n[2]=ceil(N3*sigma);
96 
97  /* initialise nfft */
98  mri_inh_3d_init_guru(&my_plan, my_N, M, my_n, m, sigma, flags,
99  FFTW_MEASURE| FFTW_DESTROY_INPUT);
100 
101  if (weight)
102  infft_flags = infft_flags | PRECOMPUTE_WEIGHT;
103 
104  /* initialise my_iplan, advanced */
105  solver_init_advanced_complex(&my_iplan,(nfft_mv_plan_complex*)(&my_plan), infft_flags );
106 
107  /* get the weights */
108  if(my_iplan.flags & PRECOMPUTE_WEIGHT)
109  {
110  fw=fopen("weights.dat","r");
111  for(j=0;j<my_plan.M_total;j++)
112  {
113  fscanf(fw,"%le ",&my_iplan.w[j]);
114  }
115  fclose(fw);
116  }
117 
118  /* get the damping factors */
119  if(my_iplan.flags & PRECOMPUTE_DAMP)
120  {
121  for(j=0;j<N;j++){
122  for(k=0;k<N;k++) {
123  int j2= j-N/2;
124  int k2= k-N/2;
125  double r=sqrt(j2*j2+k2*k2);
126  if(r>(double) N/2)
127  my_iplan.w_hat[j*N+k]=0.0;
128  else
129  my_iplan.w_hat[j*N+k]=1.0;
130  }
131  }
132  }
133 
134  fp=fopen(filename,"r");
135  ftime=fopen("readout_time.dat","r");
136 
137  for(j=0;j<my_plan.M_total;j++)
138  {
139  fscanf(fp,"%le %le %le %le",&my_plan.plan.x[3*j+0],&my_plan.plan.x[3*j+1],&real,&imag);
140  my_iplan.y[j]=real+ _Complex_I*imag;
141  fscanf(ftime,"%le ",&my_plan.plan.x[3*j+2]);
142 
143  my_plan.plan.x[3*j+2] = (my_plan.plan.x[3*j+2]-Ts)*W/N3;
144  }
145  fclose(fp);
146  fclose(ftime);
147 
148 
149  finh=fopen("inh.dat","r");
150  for(j=0;j<N*N;j++)
151  {
152  fscanf(finh,"%le ",&my_plan.w[j]);
153  my_plan.w[j]/=W;
154  }
155  fclose(finh);
156 
157 
158  if(my_plan.plan.flags & PRE_PSI) {
159  nfft_precompute_psi(&my_plan.plan);
160  }
161  if(my_plan.plan.flags & PRE_FULL_PSI) {
162  nfft_precompute_full_psi(&my_plan.plan);
163  }
164 
165  /* init some guess */
166  for(j=0;j<my_plan.N_total;j++)
167  {
168  my_iplan.f_hat_iter[j]=0.0;
169  }
170 
171  t0 = nfft_clock_gettime_seconds();
172 
173  /* inverse trafo */
174  solver_before_loop_complex(&my_iplan);
175  for(l=0;l<iteration;l++)
176  {
177  /* break if dot_r_iter is smaller than epsilon*/
178  if(my_iplan.dot_r_iter<epsilon)
179  break;
180  fprintf(stderr,"%e, %i of %i\n",sqrt(my_iplan.dot_r_iter),
181  l+1,iteration);
182  solver_loop_one_step_complex(&my_iplan);
183  }
184 
185  t1 = nfft_clock_gettime_seconds();
186  t = t1-t0;
187 
188  fout_real=fopen("output_real.dat","w");
189  fout_imag=fopen("output_imag.dat","w");
190 
191  for (j=0;j<N*N;j++) {
192  /* Verschiebung wieder herausrechnen */
193  my_iplan.f_hat_iter[j]*=cexp(-2.0*_Complex_I*M_PI*Ts*my_plan.w[j]*W);
194 
195  fprintf(fout_real,"%le ",creal(my_iplan.f_hat_iter[j]));
196  fprintf(fout_imag,"%le ",cimag(my_iplan.f_hat_iter[j]));
197  }
198 
199  fclose(fout_real);
200  fclose(fout_imag);
201  solver_finalize_complex(&my_iplan);
202  mri_inh_3d_finalize(&my_plan);
203 }
204 
205 
206 int main(int argc, char **argv)
207 {
208  if (argc <= 5) {
209 
210  printf("usage: ./reconstruct_data_inh_3d FILENAME N M ITER WEIGHTS\n");
211  return 1;
212  }
213 
214  reconstruct(argv[1],atoi(argv[2]),atoi(argv[3]),atoi(argv[4]),atoi(argv[5]));
215 
216  return 1;
217 }
218 /* \} */
#define PRECOMPUTE_DAMP
Definition: nfft3.h:792
#define MALLOC_X
Definition: nfft3.h:199
#define MALLOC_F_HAT
Definition: nfft3.h:200
#define PRECOMPUTE_WEIGHT
Definition: nfft3.h:791
double * w
weighting factors
Definition: nfft3.h:785
unsigned flags
iteration type
Definition: nfft3.h:785
double dot_r_iter
weighted dotproduct of r_iter
Definition: nfft3.h:785
void nfft_precompute_full_psi(nfft_plan *ths)
NFFT_INT N_total
Total number of Fourier coefficients.
Definition: nfft3.h:525
void nfft_precompute_psi(nfft_plan *ths)
#define FFTW_INIT
Definition: nfft3.h:203
NFFT_INT M_total
Total number of samples.
Definition: nfft3.h:525
#define MALLOC_F
Definition: nfft3.h:201
#define FFT_OUT_OF_PLACE
Definition: nfft3.h:202
#define PRE_PSI
Definition: nfft3.h:197
#define CGNR
Definition: nfft3.h:788
fftw_complex * y
right hand side, samples
Definition: nfft3.h:785
#define PRE_FULL_PSI
Definition: nfft3.h:198
Header file for the nfft3 library.
double * x
Nodes in time/spatial domain, size is doubles.
Definition: nfft3.h:192
#define PRE_PHI_HUT
Definition: nfft3.h:193
void mri_inh_3d_finalize(mri_inh_3d_plan *ths)
Definition: mri.c:266
unsigned flags
Flags for precomputation, (de)allocation, and FFTW usage, default setting is PRE_PHI_HUT | PRE_PSI | ...
Definition: nfft3.h:192
data structure for an inverse NFFT plan with double precision
Definition: nfft3.h:785
double * w_hat
damping factors
Definition: nfft3.h:785
fftw_complex * f_hat_iter
iterative solution
Definition: nfft3.h:785